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
115       if (!removeSet.isEmpty()) {
116         // // remove relations of locationElement -> cycle
117         connectedSet.removeAll(removeSet);
118         // add a new relation of location Element -> shared loc
119         connectedSet.add(newLoc);
120         getTable().put(keyElement, connectedSet);
121       }
122     }
123
124     Set<T> newConnectedSet = new HashSet<T>();
125     for (Iterator iterator = cycleSet.iterator(); iterator.hasNext();) {
126       T cycleElement = (T) iterator.next();
127       Set<T> connectedSet = get(cycleElement);
128       if (connectedSet != null) {
129         newConnectedSet.addAll(connectedSet);
130       }
131       getTable().remove(cycleElement);
132     }
133     newConnectedSet.removeAll(cycleSet);
134     newConnectedSet.remove(newLoc);
135
136     Set<T> set = getTable().get(newLoc);
137     set.addAll(newConnectedSet);
138
139     // clean up lattice
140     for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
141       T keyElement = (T) iterator.next();
142       get(keyElement).removeAll(cycleSet);
143     }
144
145     for (Iterator iterator = cycleSet.iterator(); iterator.hasNext();) {
146       T cycleElement = (T) iterator.next();
147       getTable().remove(cycleElement);
148     }
149
150   }
151
152   public void substituteLocation(T oldLoc, T newLoc) {
153     // the new location is going to take all relations of the old location
154     if (!getKeySet().contains(newLoc)) {
155       put(newLoc);
156     }
157
158     // consider the set of location s.t. LOC is greater than oldLoc
159     Set<T> keySet = getKeySet();
160     Set<T> directedConnctedHigherLocSet = new HashSet<T>();
161
162     for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
163       T key = (T) iterator.next();
164       Set<T> connectedSet = getTable().get(key);
165       if (connectedSet.contains(oldLoc)) {
166         directedConnctedHigherLocSet.add(key);
167       }
168     }
169
170     Set<T> connctedLowerSet = getTable().get(oldLoc);
171     Set<T> directedConnctedLowerLocSet = new HashSet<T>();
172     if (connctedLowerSet != null) {
173       directedConnctedLowerLocSet.addAll(connctedLowerSet);
174     }
175
176     for (Iterator iterator = directedConnctedHigherLocSet.iterator(); iterator.hasNext();) {
177       T higher = (T) iterator.next();
178       if (!higher.equals(newLoc)) {
179         addRelationHigherToLower(higher, newLoc);
180       }
181     }
182
183     for (Iterator iterator = directedConnctedLowerLocSet.iterator(); iterator.hasNext();) {
184       T lower = (T) iterator.next();
185       if (!lower.equals(newLoc)) {
186         addRelationHigherToLower(newLoc, lower);
187       }
188     }
189
190     getTable().remove(oldLoc);
191
192     for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
193       T key = (T) iterator.next();
194       getTable().get(key).remove(oldLoc);
195     }
196
197   }
198
199   public void removeRedundantEdges() {
200     boolean isUpdated;
201     do {
202       isUpdated = recurRemoveRedundant();
203     } while (isUpdated);
204   }
205
206   public boolean recurRemoveRedundant() {
207
208     Set<T> keySet = getKeySet();
209     Set<T> visited = new HashSet<T>();
210
211     for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
212       T key = (T) iterator.next();
213       Set<T> connectedSet = getTable().get(key);
214       if (connectedSet != null) {
215         Set<T> toberemovedSet = new HashSet<T>();
216         for (Iterator iterator2 = connectedSet.iterator(); iterator2.hasNext();) {
217           T dst = (T) iterator2.next();
218           Set<T> otherNeighborSet = new HashSet<T>();
219           otherNeighborSet.addAll(connectedSet);
220           otherNeighborSet.remove(dst);
221           for (Iterator iterator3 = otherNeighborSet.iterator(); iterator3.hasNext();) {
222             T neighbor = (T) iterator3.next();
223             if (isReachable(neighbor, visited, dst)) {
224               toberemovedSet.add(dst);
225             }
226           }
227         }
228         if (toberemovedSet.size() > 0) {
229           connectedSet.removeAll(toberemovedSet);
230           return true;
231         }
232       }
233     }
234
235     return false;
236
237   }
238
239   private boolean isReachable(T neighbor, Set<T> visited, T dst) {
240     Set<T> connectedSet = getTable().get(neighbor);
241     if (connectedSet != null) {
242       for (Iterator<T> iterator = connectedSet.iterator(); iterator.hasNext();) {
243         T n = iterator.next();
244         if (n.equals(dst)) {
245           return true;
246         }
247         if (!visited.contains(n)) {
248           visited.add(n);
249           if (isReachable(n, visited, dst)) {
250             return true;
251           }
252         }
253       }
254     }
255     return false;
256   }
257 }