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);
140 public void substituteLocation(T oldLoc, T newLoc) {
141 // the new location is going to take all relations of the old location
143 // consider the set of location s.t. LOC is greater than oldLoc
144 Set<T> keySet = getKeySet();
145 Set<T> directedConnctedHigherLocSet = new HashSet<T>();
147 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
148 T key = (T) iterator.next();
149 Set<T> connectedSet = getTable().get(key);
150 if (connectedSet.contains(oldLoc)) {
151 directedConnctedHigherLocSet.add(key);
155 Set<T> connctedLowerSet = getTable().get(oldLoc);
156 Set<T> directedConnctedLowerLocSet = new HashSet<T>();
157 if (connctedLowerSet != null) {
158 directedConnctedLowerLocSet.addAll(connctedLowerSet);
161 for (Iterator iterator = directedConnctedHigherLocSet.iterator(); iterator.hasNext();) {
162 T higher = (T) iterator.next();
163 if (!higher.equals(newLoc)) {
164 addRelationHigherToLower(higher, newLoc);
168 for (Iterator iterator = directedConnctedLowerLocSet.iterator(); iterator.hasNext();) {
169 T lower = (T) iterator.next();
170 if (!lower.equals(newLoc)) {
171 addRelationHigherToLower(newLoc, lower);