d30aa3998cd1775afd96c6b08393fd01d2c2de49
[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   }
139
140   public void substituteLocation(T oldLoc, T newLoc) {
141     // the new location is going to take all relations of the old location
142
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>();
146
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);
152       }
153     }
154
155     Set<T> connctedLowerSet = getTable().get(oldLoc);
156     Set<T> directedConnctedLowerLocSet = new HashSet<T>();
157     if (connctedLowerSet != null) {
158       directedConnctedLowerLocSet.addAll(connctedLowerSet);
159     }
160
161     for (Iterator iterator = directedConnctedHigherLocSet.iterator(); iterator.hasNext();) {
162       T higher = (T) iterator.next();
163       if (!higher.equals(newLoc)) {
164         addRelationHigherToLower(higher, newLoc);
165       }
166     }
167
168     for (Iterator iterator = directedConnctedLowerLocSet.iterator(); iterator.hasNext();) {
169       T lower = (T) iterator.next();
170       if (!lower.equals(newLoc)) {
171         addRelationHigherToLower(newLoc, lower);
172       }
173     }
174
175   }
176
177 }