changes.
[IRC.git] / Robust / src / Analysis / SSJava / SSJavaLattice.java
1 package Analysis.SSJava;
2
3 import java.util.HashSet;
4 import java.util.Iterator;
5 import java.util.Set;
6
7 import Util.Lattice;
8
9 public class SSJavaLattice<T> extends Lattice<T> {
10
11   Set<T> sharedLocSet;
12   public static int seed = 0;
13
14   public SSJavaLattice(T top, T bottom) {
15     super(top, bottom);
16     sharedLocSet = new HashSet<T>();
17   }
18
19   public Set<T> getSharedLocSet() {
20     return sharedLocSet;
21   }
22
23   public void addSharedLoc(T loc) {
24     sharedLocSet.add(loc);
25   }
26
27   public boolean isSharedLoc(T loc) {
28     return sharedLocSet.contains(loc);
29   }
30
31   public boolean addRelationHigherToLower(T higher, T lower) {
32
33     System.out.println("add a relation: " + lower + "<" + higher);
34
35     return put(higher, lower);
36   }
37
38   public void insertNewLocationAtOneLevelHigher(T lowerLoc, T newLoc) {
39     // first identifying which location is connected to the input loc
40     Set<T> keySet = getKeySet();
41     Set<T> oneLevelHigherLocSet = new HashSet<T>();
42
43     for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
44       T locKey = (T) iterator.next();
45       Set<T> conntectedSet = get(locKey);
46       for (Iterator iterator2 = conntectedSet.iterator(); iterator2.hasNext();) {
47         T connectedLoc = (T) iterator2.next();
48         if (connectedLoc.equals(lowerLoc)) {
49           oneLevelHigherLocSet.add(locKey);
50         }
51       }
52     }
53
54     put(newLoc);
55     addRelationHigherToLower(newLoc, lowerLoc);
56
57     for (Iterator iterator = oneLevelHigherLocSet.iterator(); iterator.hasNext();) {
58       T higherLoc = (T) iterator.next();
59       // remove an existing edge between the higher loc and the input loc
60       get(higherLoc).remove(lowerLoc);
61       // add a new edge from the higher loc to the new location
62       put(higherLoc, newLoc);
63     }
64
65   }
66
67   public Set<T> getPossibleCycleElements(T higherLoc, T lowerLoc) {
68     // if a relation of higherloc & lowerloc introduces a new cycle flow,
69     // return the set of elements consisting of the cycle
70     Set<T> cycleElemetns = new HashSet<T>();
71
72     // if lowerLoc has already been higher than higherLoc, the new relation
73     // introduces a cycle to the lattice
74     if (lowerLoc.equals(higherLoc)) {
75       cycleElemetns.add(lowerLoc);
76       cycleElemetns.add(higherLoc);
77     } else if (isGreaterThan(lowerLoc, higherLoc)) {
78       cycleElemetns.add(lowerLoc);
79       cycleElemetns.add(higherLoc);
80       getInBetweenElements(lowerLoc, higherLoc, cycleElemetns);
81     }
82     return cycleElemetns;
83   }
84
85   private void getInBetweenElements(T start, T end, Set<T> elementSet) {
86     Set<T> connectedSet = get(start);
87     for (Iterator iterator = connectedSet.iterator(); iterator.hasNext();) {
88       T cur = (T) iterator.next();
89       if ((!start.equals(cur)) && (!cur.equals(end)) && isGreaterThan(cur, end)) {
90         elementSet.add(cur);
91         getInBetweenElements(cur, end, elementSet);
92       }
93     }
94   }
95
96   public void mergeIntoSharedLocation(Set<T> cycleSet, T newLoc) {
97
98     // add a new shared loc
99     put(newLoc);
100     addSharedLoc(newLoc);
101
102     Set<T> keySet = getKeySet();
103
104     for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
105       T keyElement = (T) iterator.next();
106       Set<T> connectedSet = get(keyElement);
107       Set<T> removeSet = new HashSet<T>();
108       for (Iterator iterator2 = connectedSet.iterator(); iterator2.hasNext();) {
109         T cur = (T) iterator2.next();
110         if (cycleSet.contains(cur)) {
111           removeSet.add(cur);
112         }
113       }
114       if (!removeSet.isEmpty()) {
115         // remove relations of locationElement -> cycle
116         connectedSet.removeAll(removeSet);
117         // add a new relation of location Element -> shared loc
118         connectedSet.add(newLoc);
119         getTable().put(keyElement, connectedSet);
120       }
121     }
122
123     Set<T> newConnectedSet = new HashSet<T>();
124     for (Iterator iterator = cycleSet.iterator(); iterator.hasNext();) {
125       T cycleElement = (T) iterator.next();
126       Set<T> connectedSet = get(cycleElement);
127       if (connectedSet != null) {
128         newConnectedSet.addAll(connectedSet);
129       }
130       getTable().remove(cycleElement);
131     }
132     newConnectedSet.removeAll(cycleSet);
133     newConnectedSet.remove(newLoc);
134
135     Set<T> set = getTable().get(newLoc);
136     set.addAll(newConnectedSet);
137
138     // clean up lattice
139     for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
140       T keyElement = (T) iterator.next();
141       get(keyElement).removeAll(cycleSet);
142     }
143
144     for (Iterator iterator = cycleSet.iterator(); iterator.hasNext();) {
145       T cycleElement = (T) iterator.next();
146       getTable().remove(cycleElement);
147     }
148
149   }
150
151   public void substituteLocation(T oldLoc, T newLoc) {
152     // the new location is going to take all relations of the old location
153     if (!getKeySet().contains(newLoc)) {
154       put(newLoc);
155     }
156
157     // consider the set of location s.t. LOC is greater than oldLoc
158     Set<T> keySet = getKeySet();
159     Set<T> directedConnctedHigherLocSet = new HashSet<T>();
160
161     for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
162       T key = (T) iterator.next();
163       Set<T> connectedSet = getTable().get(key);
164       if (connectedSet.contains(oldLoc)) {
165         directedConnctedHigherLocSet.add(key);
166       }
167     }
168
169     Set<T> connctedLowerSet = getTable().get(oldLoc);
170     Set<T> directedConnctedLowerLocSet = new HashSet<T>();
171     if (connctedLowerSet != null) {
172       directedConnctedLowerLocSet.addAll(connctedLowerSet);
173     }
174
175     for (Iterator iterator = directedConnctedHigherLocSet.iterator(); iterator.hasNext();) {
176       T higher = (T) iterator.next();
177       if (!higher.equals(newLoc)) {
178         addRelationHigherToLower(higher, newLoc);
179       }
180     }
181
182     for (Iterator iterator = directedConnctedLowerLocSet.iterator(); iterator.hasNext();) {
183       T lower = (T) iterator.next();
184       if (!lower.equals(newLoc)) {
185         addRelationHigherToLower(newLoc, lower);
186       }
187     }
188
189     getTable().remove(oldLoc);
190
191     for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
192       T key = (T) iterator.next();
193       getTable().get(key).remove(oldLoc);
194     }
195
196   }
197
198   public void removeRedundantEdges() {
199     boolean isUpdated;
200     do {
201       isUpdated = recurRemoveRedundant();
202     } while (isUpdated);
203   }
204
205   public boolean recurRemoveRedundant() {
206
207     Set<T> keySet = getKeySet();
208     Set<T> visited = new HashSet<T>();
209
210     for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
211       T key = (T) iterator.next();
212       Set<T> connectedSet = getTable().get(key);
213       if (connectedSet != null) {
214         Set<T> toberemovedSet = new HashSet<T>();
215         for (Iterator iterator2 = connectedSet.iterator(); iterator2.hasNext();) {
216           T dst = (T) iterator2.next();
217           Set<T> otherNeighborSet = new HashSet<T>();
218           otherNeighborSet.addAll(connectedSet);
219           otherNeighborSet.remove(dst);
220           for (Iterator iterator3 = otherNeighborSet.iterator(); iterator3.hasNext();) {
221             T neighbor = (T) iterator3.next();
222             if (isReachable(neighbor, visited, dst)) {
223               toberemovedSet.add(dst);
224             }
225           }
226         }
227         if (toberemovedSet.size() > 0) {
228           connectedSet.removeAll(toberemovedSet);
229           return true;
230         }
231       }
232     }
233
234     return false;
235
236   }
237
238   private boolean isReachable(T neighbor, Set<T> visited, T dst) {
239     Set<T> connectedSet = getTable().get(neighbor);
240     if (connectedSet != null) {
241       for (Iterator<T> iterator = connectedSet.iterator(); iterator.hasNext();) {
242         T n = iterator.next();
243         if (n.equals(dst)) {
244           return true;
245         }
246         if (!visited.contains(n)) {
247           visited.add(n);
248           if (isReachable(n, visited, dst)) {
249             return true;
250           }
251         }
252       }
253     }
254     return false;
255   }
256 }