1 package Analysis.SSJava;
3 import java.util.HashSet;
4 import java.util.Iterator;
9 public class SSJavaLattice<T> extends Lattice<T> {
12 public static int seed = 0;
14 public SSJavaLattice(T top, T bottom) {
16 sharedLocSet = new HashSet<T>();
19 public Set<T> getSharedLocSet() {
23 public void addSharedLoc(T loc) {
24 sharedLocSet.add(loc);
27 public boolean isSharedLoc(T loc) {
28 return sharedLocSet.contains(loc);
31 public boolean addRelationHigherToLower(T higher, T lower) {
33 System.out.println("add a relation: " + lower + "<" + higher);
35 return put(higher, lower);
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>();
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);
55 addRelationHigherToLower(newLoc, lowerLoc);
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);
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>();
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);
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)) {
91 getInBetweenElements(cur, end, elementSet);
96 public void mergeIntoSharedLocation(Set<T> cycleSet, T newLoc) {
98 // add a new shared loc
100 addSharedLoc(newLoc);
102 Set<T> keySet = getKeySet();
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)) {
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);
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);
130 getTable().remove(cycleElement);
132 newConnectedSet.removeAll(cycleSet);
133 newConnectedSet.remove(newLoc);
135 Set<T> set = getTable().get(newLoc);
136 set.addAll(newConnectedSet);
139 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
140 T keyElement = (T) iterator.next();
141 get(keyElement).removeAll(cycleSet);
144 for (Iterator iterator = cycleSet.iterator(); iterator.hasNext();) {
145 T cycleElement = (T) iterator.next();
146 getTable().remove(cycleElement);
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)) {
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>();
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);
169 Set<T> connctedLowerSet = getTable().get(oldLoc);
170 Set<T> directedConnctedLowerLocSet = new HashSet<T>();
171 if (connctedLowerSet != null) {
172 directedConnctedLowerLocSet.addAll(connctedLowerSet);
175 for (Iterator iterator = directedConnctedHigherLocSet.iterator(); iterator.hasNext();) {
176 T higher = (T) iterator.next();
177 if (!higher.equals(newLoc)) {
178 addRelationHigherToLower(higher, newLoc);
182 for (Iterator iterator = directedConnctedLowerLocSet.iterator(); iterator.hasNext();) {
183 T lower = (T) iterator.next();
184 if (!lower.equals(newLoc)) {
185 addRelationHigherToLower(newLoc, lower);
189 getTable().remove(oldLoc);
191 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
192 T key = (T) iterator.next();
193 getTable().get(key).remove(oldLoc);
198 public void removeRedundantEdges() {
201 isUpdated = recurRemoveRedundant();
205 public boolean recurRemoveRedundant() {
207 Set<T> keySet = getKeySet();
208 Set<T> visited = new HashSet<T>();
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);
227 if (toberemovedSet.size() > 0) {
228 connectedSet.removeAll(toberemovedSet);
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();
246 if (!visited.contains(n)) {
248 if (isReachable(n, visited, dst)) {