More pieces for new version of analysis
[IRC.git] / Robust / src / Analysis / MLP / SESELock.java
1 package Analysis.MLP;
2
3 import java.util.HashMap;
4 import java.util.HashSet;
5 import java.util.Iterator;
6 import java.util.Set;
7
8 public class SESELock {
9         
10         private HashSet<ConflictNode> conflictNodeSet;
11         private HashSet<ConflictEdge> conflictEdgeSet;
12         private HashMap<ConflictNode, Integer> nodeTypeMap;
13         private int id;
14         
15         public SESELock(){
16                 conflictNodeSet=new HashSet<ConflictNode>();
17                 conflictEdgeSet=new HashSet<ConflictEdge>();
18                 nodeTypeMap=new HashMap<ConflictNode, Integer>();
19         }
20         
21         public void addConflictNode(ConflictNode node, int type){
22                 conflictNodeSet.add(node);
23                 setNodeType(node, type);
24         }
25         
26         public void setNodeType(ConflictNode node, int type){
27                 nodeTypeMap.put(node, new Integer(type));
28         }
29         
30         public int getNodeType(ConflictNode node){
31                 return nodeTypeMap.get(node).intValue();
32         }
33         
34         public void addConflictEdge(ConflictEdge e){
35                 conflictEdgeSet.add(e);
36         }
37         
38         public boolean containsConflictEdge(ConflictEdge e){
39                 return conflictEdgeSet.contains(e);
40         }
41         
42         public HashSet<ConflictNode> getConflictNodeSet(){
43                 return conflictNodeSet;
44         }
45         
46         public boolean isWriteNode(ConflictNode node){
47                 if (node instanceof StallSiteNode) {
48                         StallSiteNode stallSiteNode = (StallSiteNode) node;
49                         HashSet<Effect> effectSet = stallSiteNode.getStallSite()
50                                         .getEffectSet();
51                         for (Iterator iterator = effectSet.iterator(); iterator.hasNext();) {
52                                 Effect effect = (Effect) iterator.next();
53                                 if (effect.getEffectType().equals(StallSite.WRITE_EFFECT)) {
54                                         return true;
55                                 }
56                         }
57                 } else {
58                         LiveInNode liveInNode = (LiveInNode) node;
59                         Set<SESEEffectsKey> writeEffectSet = liveInNode
60                                         .getWriteEffectsSet();
61                         if (writeEffectSet != null && writeEffectSet.size() > 0) {
62                                 return true;
63                         }
64                 }
65
66                 return false;
67                 
68         }
69         
70         
71         public boolean hasSelfCoarseEdge(ConflictNode node){
72                 
73                 HashSet<ConflictEdge> set=node.getEdgeSet();
74                 for (Iterator iterator = set.iterator(); iterator.hasNext();) {
75                         ConflictEdge conflictEdge = (ConflictEdge) iterator.next();
76                         if(conflictEdge.getType()!=ConflictEdge.FINE_GRAIN_EDGE&&conflictEdge.getVertexU()==conflictEdge.getVertexV()){
77                                 return true;
78                         }
79                 }
80                 return false;
81         }
82         
83         private boolean isFineNode(ConflictNode node){
84                 
85                 if(node.getType()<4){
86                         return true;
87                 }
88                 
89                 return false;
90                 
91         }
92         
93         public ConflictNode getNewNodeCoarseConnectedWithGroup(ConflictEdge newEdge) {
94
95                 // check whether or not the new node has a fine-grained edges to all
96                 // current nodes.
97
98                 ConflictNode newNode;
99                 if (conflictNodeSet.contains(newEdge.getVertexU())) {
100                         newNode = newEdge.getVertexV();
101                 } else if(conflictNodeSet.contains(newEdge.getVertexV())) {
102                         newNode = newEdge.getVertexU();
103                 }else{
104                         return null;
105                 }
106
107                 int count = 0;
108                 HashSet<ConflictEdge> edgeSet = newNode.getEdgeSet();
109                 for (Iterator iterator = edgeSet.iterator(); iterator.hasNext();) {
110                         ConflictEdge conflictEdge = (ConflictEdge) iterator.next();
111                         if (!conflictEdge.getVertexU().equals(newNode)
112                                         && conflictNodeSet.contains(conflictEdge.getVertexU()) && isFineNode(conflictEdge.getVertexU()) ) {
113                                 count++;
114                         } else if (!conflictEdge.getVertexV().equals(newNode)
115                                         && conflictNodeSet.contains(conflictEdge.getVertexV()) && isFineNode(conflictEdge.getVertexU()) ) {
116                                 count++;
117                         }
118                 }
119
120                 if (count == conflictNodeSet.size()) {
121                         // connected to all current nodes in group
122                         return newNode;
123                 }
124
125                 return null;
126
127         }
128         
129         
130         public ConflictNode getNewNodeConnectedWithGroup(ConflictEdge newEdge) {
131
132                 // check whether or not the new node has a fine-grained edges to all
133                 // current nodes.
134
135                 ConflictNode newNode;
136                 if (conflictNodeSet.contains(newEdge.getVertexU())) {
137                         newNode = newEdge.getVertexV();
138                 } else if(conflictNodeSet.contains(newEdge.getVertexV())){
139                         newNode = newEdge.getVertexU();
140                 }else{
141                         return null;
142                 }
143
144                 int count = 0;
145                 HashSet<ConflictEdge> edgeSet = newNode.getEdgeSet();
146                 for (Iterator iterator = edgeSet.iterator(); iterator.hasNext();) {
147                         ConflictEdge conflictEdge = (ConflictEdge) iterator.next();
148                         if (!conflictEdge.getVertexU().equals(newNode)
149                                         && conflictNodeSet.contains(conflictEdge.getVertexU())) {
150                                 count++;
151                         } else if (!conflictEdge.getVertexV().equals(newNode)
152                                         && conflictNodeSet.contains(conflictEdge.getVertexV())) {
153                                 count++;
154                         }
155                 }
156
157                 if (count == conflictNodeSet.size()) {
158                         // connected to all current nodes in group
159                         return newNode;
160                 }
161
162                 return null;
163
164         }
165         
166         public void addEdge(ConflictEdge edge){
167                 conflictNodeSet.add(edge.getVertexU());
168                 conflictNodeSet.add(edge.getVertexV());
169         }
170         
171         public int getID(){
172                 return id;
173         }
174         
175         public void setID(int id){
176                 this.id=id;
177         }
178         
179         public boolean containsConflictNode(ConflictNode node){
180                 
181                 return conflictNodeSet.contains(node);          
182                 
183         }
184         
185         
186         public boolean testEdge(ConflictEdge newEdge){
187                 
188                 
189                 if( !conflictNodeSet.contains(newEdge.getVertexU()) && !conflictNodeSet.contains(newEdge.getVertexV()) ){
190                         return false;
191                 }
192                 
193                 ConflictNode nodeToAdd=conflictNodeSet.contains(newEdge.getVertexU())?newEdge.getVertexV():newEdge.getVertexU();
194                 
195                 HashSet<ConflictNode> nodeSet=new HashSet<ConflictNode>(conflictNodeSet);
196
197                 for(Iterator edgeIter=nodeToAdd.getEdgeSet().iterator();edgeIter.hasNext();){
198                         ConflictEdge edge=(ConflictEdge)edgeIter.next();
199                         if(nodeSet.contains(edge.getVertexU())){
200                                 nodeSet.remove(edge.getVertexU());
201                         }else if(nodeSet.contains(edge.getVertexV())){
202                                 nodeSet.remove(edge.getVertexV());
203                         }
204                 }
205                 
206                 return nodeSet.isEmpty();
207                 
208         }
209         
210         public String toString(){
211                 String rtr="";
212                 
213                 for (Iterator<ConflictNode> iterator = conflictNodeSet.iterator(); iterator.hasNext();) {
214                         ConflictNode node = (ConflictNode) iterator.next();
215                         rtr+=" "+node+"::"+getNodeType(node);
216                 }
217                 
218                 return rtr;
219         }
220
221 }