package Analysis.SSJava;
import java.io.BufferedWriter;
-import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
-import java.util.Map.Entry;
-import Analysis.OoOJava.ConflictEdge;
-import Analysis.OoOJava.ConflictNode;
+import IR.ClassDescriptor;
import IR.Descriptor;
import IR.FieldDescriptor;
import IR.MethodDescriptor;
return nodeSet;
}
+ public MethodDescriptor getMethodDescriptor() {
+ return md;
+ }
+
public Set<FlowNode> getParameterNodeSet() {
Set<FlowNode> paramNodeSet = new HashSet<FlowNode>();
for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
return returnNodeSet;
}
+ public Set<FlowNode> getReachableFlowNodeSet(FlowNode fn) {
+ Set<FlowNode> set = new HashSet<FlowNode>();
+ getReachableFlowNodeSet(fn, set);
+ return set;
+ }
+
+ private void getReachableFlowNodeSet(FlowNode fn, Set<FlowNode> visited) {
+
+ for (Iterator iterator = fn.getOutEdgeSet().iterator(); iterator.hasNext();) {
+ FlowEdge edge = (FlowEdge) iterator.next();
+
+ if (fn.equals(getFlowNode(edge.getInitTuple()))) {
+
+ FlowNode dstNode = getFlowNode(edge.getEndTuple());
+
+ if (!visited.contains(dstNode)) {
+ visited.add(dstNode);
+ getReachableFlowNodeSet(dstNode, visited);
+ }
+ }
+ }
+
+ }
+
+ public Set<NTuple<Location>> getReachableFlowTupleSet(Set<NTuple<Location>> visited, FlowNode fn) {
+ for (Iterator iterator = fn.getOutEdgeSet().iterator(); iterator.hasNext();) {
+ FlowEdge edge = (FlowEdge) iterator.next();
+
+ if (fn.getDescTuple().equals(edge.getInitTuple())) {
+ FlowNode dstNode = getFlowNode(edge.getEndTuple());
+ NTuple<Location> dstTuple = getLocationTuple(dstNode);
+
+ if (!visited.contains(dstTuple)) {
+ visited.add(dstTuple);
+ visited.addAll(getReachableFlowTupleSet(visited, dstNode));
+ }
+
+ }
+ }
+ return visited;
+ }
+
+ public NTuple<Location> getLocationTuple(FlowNode fn) {
+
+ NTuple<Descriptor> descTuple = fn.getDescTuple();
+
+ NTuple<Location> locTuple = new NTuple<Location>();
+
+ ClassDescriptor cd = null;
+
+ for (int i = 0; i < descTuple.size(); i++) {
+ Descriptor d = descTuple.get(i);
+ Location loc;
+ if (i == 0) {
+ loc = new Location(md, d.getSymbol());
+ cd = ((VarDescriptor) d).getType().getClassDesc();
+ } else {
+ loc = new Location(cd, d.getSymbol());
+ cd = ((FieldDescriptor) d).getType().getClassDesc();
+ }
+ locTuple.add(loc);
+ }
+
+ return locTuple;
+ }
+
+ public Set<FlowNode> getIncomingFlowNodeSet(FlowNode node) {
+ Set<FlowNode> set = new HashSet<FlowNode>();
+ getIncomingFlowNodeSet(node, set);
+ return set;
+ }
+
+ public void getIncomingFlowNodeSet(FlowNode node, Set<FlowNode> visited) {
+
+ for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
+ FlowNode curNode = (FlowNode) iterator.next();
+ Set<FlowEdge> edgeSet = curNode.getOutEdgeSet();
+
+ for (Iterator iterator2 = edgeSet.iterator(); iterator2.hasNext();) {
+ FlowEdge flowEdge = (FlowEdge) iterator2.next();
+
+ if (node.equals(getFlowNode(flowEdge.getEndTuple()))) {
+ FlowNode incomingNode = getFlowNode(flowEdge.getInitTuple());
+
+ if (!visited.contains(incomingNode)) {
+ visited.add(incomingNode);
+ getIncomingFlowNodeSet(incomingNode, visited);
+ }
+ }
+ }
+ }
+
+ }
+
+ public Set<NTuple<Location>> getIncomingFlowTupleSet(FlowNode fn) {
+
+ NTuple<Descriptor> dstTuple = fn.getDescTuple();
+
+ Set<NTuple<Location>> set = new HashSet<NTuple<Location>>();
+
+ ClassDescriptor cd = null;
+
+ for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
+ FlowNode node = (FlowNode) iterator.next();
+ Set<FlowEdge> edgeSet = node.getOutEdgeSet();
+ for (Iterator iterator2 = edgeSet.iterator(); iterator2.hasNext();) {
+ FlowEdge flowEdge = (FlowEdge) iterator2.next();
+ if (dstTuple.equals(flowEdge.getEndTuple())) {
+ NTuple<Descriptor> initTuple = flowEdge.getInitTuple();
+ NTuple<Location> locTuple = new NTuple<Location>();
+ for (int i = 0; i < initTuple.size(); i++) {
+ Descriptor d = initTuple.get(i);
+ Location loc;
+ if (i == 0) {
+ loc = new Location(md, d.getSymbol());
+ cd = ((VarDescriptor) d).getType().getClassDesc();
+ } else {
+ loc = new Location(cd, d.getSymbol());
+ cd = ((FieldDescriptor) d).getType().getClassDesc();
+ }
+ locTuple.add(loc);
+ }
+ set.add(locTuple);
+ }
+ }
+ }
+ return set;
+ }
+
public boolean isParamter(NTuple<Descriptor> tuple) {
// return true if a descriptor tuple is started with a parameter descriptor
Descriptor firstIdxDesc = tuple.get(0);
import java.util.Set;
import java.util.Stack;
-import Analysis.SSJava.FlowDownCheck.ComparisonResult;
-import Analysis.SSJava.FlowDownCheck.CompositeLattice;
import IR.ClassDescriptor;
import IR.Descriptor;
import IR.FieldDescriptor;
private Map<MethodInvokeNode, Map<Integer, NTuple<Descriptor>>> mapMethodInvokeNodeToArgIdxMap;
- private Map<MethodDescriptor, MethodLocationInfo> mapLatticeToMethodLocationInfo;
+ private Map<MethodDescriptor, MethodLocationInfo> mapMethodDescToMethodLocationInfo;
+
+ private Map<ClassDescriptor, LocationInfo> mapClassToLocationInfo;
private Map<MethodDescriptor, Set<MethodDescriptor>> mapMethodDescToPossibleMethodDescSet;
new HashMap<MethodDescriptor, Set<MethodInvokeNode>>();
this.mapMethodInvokeNodeToArgIdxMap =
new HashMap<MethodInvokeNode, Map<Integer, NTuple<Descriptor>>>();
- this.mapLatticeToMethodLocationInfo = new HashMap<MethodDescriptor, MethodLocationInfo>();
+ this.mapMethodDescToMethodLocationInfo = new HashMap<MethodDescriptor, MethodLocationInfo>();
this.mapMethodDescToPossibleMethodDescSet =
new HashMap<MethodDescriptor, Set<MethodDescriptor>>();
+ this.mapClassToLocationInfo = new HashMap<ClassDescriptor, LocationInfo>();
}
public void setupToAnalyze() {
return desc.getSymbol();
}
+ private Descriptor getDescriptor(int idx, FlowNode node) {
+ Descriptor desc = node.getDescTuple().get(idx);
+ return desc;
+ }
+
private void analyzeMethodLattice(MethodDescriptor md, SSJavaLattice<String> methodLattice) {
MethodLocationInfo methodInfo = getMethodLocationInfo(md);
extractRelationFromFieldFlows(varClassDesc, srcNode, dstNode, 1);
} else {
- // in this case, take a look at connected nodes at the local level
- addRelationToLattice(md, methodLattice, srcNode, dstNode);
+ // for the method lattice, we need to look at the first element of
+ // NTuple<Descriptor>
+ if (srcNodeTuple.size() == 1 || dstNodeTuple.size() == 1) {
+ // in this case, take a look at connected nodes at the local level
+ addRelationToLattice(md, methodLattice, srcNode, dstNode);
+ }
}
}
-
}
-
}
// grab the this location if the method use the 'this' reference
}
+ private LocationInfo getLocationInfo(Descriptor d) {
+ if (d instanceof MethodDescriptor) {
+ return getMethodLocationInfo((MethodDescriptor) d);
+ } else {
+ return getFieldLocationInfo((ClassDescriptor) d);
+ }
+ }
+
private MethodLocationInfo getMethodLocationInfo(MethodDescriptor md) {
- if (!mapLatticeToMethodLocationInfo.containsKey(md)) {
- mapLatticeToMethodLocationInfo.put(md, new MethodLocationInfo(md));
+ if (!mapMethodDescToMethodLocationInfo.containsKey(md)) {
+ mapMethodDescToMethodLocationInfo.put(md, new MethodLocationInfo(md));
+ }
+
+ return mapMethodDescToMethodLocationInfo.get(md);
+
+ }
+
+ private LocationInfo getFieldLocationInfo(ClassDescriptor cd) {
+
+ if (!mapClassToLocationInfo.containsKey(cd)) {
+ mapClassToLocationInfo.put(cd, new LocationInfo(cd));
}
- return mapLatticeToMethodLocationInfo.get(md);
+ return mapClassToLocationInfo.get(cd);
}
private void addRelationToLattice(MethodDescriptor md, SSJavaLattice<String> methodLattice,
FlowNode srcNode, FlowNode dstNode) {
- // add a new binary relation of dstNode < srcNode
- String srcSymbol = getSymbol(0, srcNode);
- String dstSymbol = getSymbol(0, dstNode);
+ System.out.println("### addRelationToLattice src=" + srcNode + " dst=" + dstNode);
+ // add a new binary relation of dstNode < srcNode
FlowGraph flowGraph = getFlowGraph(md);
MethodLocationInfo methodInfo = getMethodLocationInfo(md);
+ String srcOriginSymbol = getSymbol(0, srcNode);
+ String dstOriginSymbol = getSymbol(0, dstNode);
+
+ Descriptor srcDesc = getDescriptor(0, srcNode);
+ Descriptor dstDesc = getDescriptor(0, dstNode);
+
+ String srcSymbol = methodInfo.getLocName(srcOriginSymbol);
+ String dstSymbol = methodInfo.getLocName(dstOriginSymbol);
+
if (srcNode.isParameter()) {
int paramIdx = flowGraph.getParamIdx(srcNode.getDescTuple());
- methodInfo.addParameter(srcSymbol, srcNode, paramIdx);
+ methodInfo.addParameter(srcSymbol, srcDesc, paramIdx);
+ } else {
+ methodInfo.addMappingOfLocNameToDescriptor(srcSymbol, srcDesc);
}
+
if (dstNode.isParameter()) {
int paramIdx = flowGraph.getParamIdx(dstNode.getDescTuple());
- methodInfo.addParameter(dstSymbol, dstNode, paramIdx);
+ methodInfo.addParameter(dstSymbol, dstDesc, paramIdx);
+ } else {
+ methodInfo.addMappingOfLocNameToDescriptor(dstSymbol, dstDesc);
+ }
+
+ // consider a composite location case
+
+ boolean isSrcLocalVar = false;
+ boolean isDstLocalVar = false;
+ if (srcNode.getDescTuple().size() == 1) {
+ isSrcLocalVar = true;
+ }
+
+ if (dstNode.getDescTuple().size() == 1) {
+ isDstLocalVar = true;
+ }
+
+ boolean isAssignedCompositeLocation = false;
+ if (isSrcLocalVar && isDstLocalVar) {
+ // both src and dst are local var
+
+ // CompositeLocation inferSrc=methodInfo.getInferLocation(srcNode);
+ // CompositeLocation inferDst=methodInfo.getInferLocation(dstNode);
+ // if( (inferSrc!=null && inferSrc.getSize()>1) || (inferDst!=null &&
+ // inferDst.getSize()>1)){
+ // isAssignedCompositeLocation = true;
+ // }
+
+ // TODO: need to fix
+ isAssignedCompositeLocation =
+ calculateCompositeLocationForLocalVar(flowGraph, methodLattice, methodInfo, srcNode);
+ calculateCompositeLocationForLocalVar(flowGraph, methodLattice, methodInfo, dstNode);
+
+ } else if (isSrcLocalVar) {
+ // src is local var
+ isAssignedCompositeLocation =
+ calculateCompositeLocationForLocalVar(flowGraph, methodLattice, methodInfo, srcNode);
+ } else if (isDstLocalVar) {
+ // dst is local var
+ isAssignedCompositeLocation =
+ calculateCompositeLocationForLocalVar(flowGraph, methodLattice, methodInfo, dstNode);
+ }
+
+ if (!isAssignedCompositeLocation) {
+ if (!methodLattice.isGreaterThan(srcSymbol, dstSymbol)) {
+ // if the lattice does not have this relation, add it
+ methodLattice.addRelationHigherToLower(srcSymbol, dstSymbol);
+ }
+ }
+
+ // Set<String> cycleElementSet =
+ // methodLattice.getPossibleCycleElements(srcSymbol, dstSymbol);
+ //
+ // System.out.println("### POSSIBLE CYCLE ELEMENT SET=" + cycleElementSet);
+ // boolean hasNonPrimitiveElement = false;
+ // for (Iterator iterator = cycleElementSet.iterator(); iterator.hasNext();)
+ // {
+ // String cycleElementSymbol = (String) iterator.next();
+ // Set<Descriptor> flowNodeSet =
+ // methodInfo.getFlowNodeSet(cycleElementSymbol);
+ // for (Iterator iterator2 = flowNodeSet.iterator(); iterator2.hasNext();) {
+ // Descriptor desc = (Descriptor) iterator2.next();
+ // if (!isPrimitiveTypeDescriptor(desc)) {
+ // hasNonPrimitiveElement = true;
+ // }
+ // System.out
+ // .println("flowNode=" + desc + " is primitive?=" +
+ // isPrimitiveTypeDescriptor(desc));
+ // }
+ // }
+ //
+ // if (hasNonPrimitiveElement) {
+ // // if there is non-primitive element in the cycle, no way to merge cyclic
+ // // elements into the shared location
+ // System.out.println("Failed to merge cyclic value flows into a shared location.");
+ //
+ // // try to assign more fine-grind composite location to the source or the
+ // // destination
+ // System.out.println("SRC=" + srcSymbol + " DST=" + dstSymbol);
+ // System.out.println("### SRCNODE=" + srcNode + " DSTNODE=" + dstNode);
+ //
+ // FlowNode targetNode;
+ // if (isPrimitiveLocalVariable(srcNode)) {
+ // targetNode = srcNode;
+ // } else {
+ // targetNode = dstNode;
+ // }
+ //
+ // calculateMoreFineGrainedLocation(md, methodLattice, targetNode);
+ //
+ // return;
+ // }
+ //
+ // if (cycleElementSet.size() > 0) {
+ // String newSharedLoc = "SharedLoc" + (SSJavaLattice.seed++);
+ // methodLattice.mergeIntoSharedLocation(cycleElementSet, newSharedLoc);
+ //
+ // for (Iterator iterator = cycleElementSet.iterator(); iterator.hasNext();)
+ // {
+ // String locName = (String) iterator.next();
+ // methodInfo.mapDescSymbolToLocName(locName, newSharedLoc);
+ // }
+ //
+ // } else if (!methodLattice.isGreaterThan(srcSymbol, dstSymbol)) {
+ // // if the lattice does not have this relation, add it
+ // methodLattice.addRelationHigherToLower(srcSymbol, dstSymbol);
+ // }
+ //
+ // System.out.println("methodLattice=" + methodLattice.getKeySet());
+
+ }
+
+ private void addPrefixMapping(Map<NTuple<Location>, Set<NTuple<Location>>> map,
+ NTuple<Location> prefix, NTuple<Location> element) {
+
+ if (!map.containsKey(prefix)) {
+ map.put(prefix, new HashSet<NTuple<Location>>());
}
+ map.get(prefix).add(element);
+
+ }
- if (!methodLattice.isGreaterThan(srcSymbol, dstSymbol)) {
- // if the lattice does not have this relation, add it
- methodLattice.addRelationHigherToLower(srcSymbol, dstSymbol);
+ private NTuple<Location> getLocationTuple(MethodLocationInfo methodInfo, FlowGraph flowGraph,
+ FlowNode flowNode) {
+
+ NTuple<Location> locTuple;
+ CompositeLocation inferLoc = methodInfo.getInferLocation(flowNode);
+ if (inferLoc != null) {
+ // the flow node has already been assigned to the location
+ locTuple = new NTuple<Location>();
+ NTuple<Location> inferLocTuple = inferLoc.getTuple();
+ for (int i = 0; i < inferLocTuple.size(); i++) {
+ locTuple.add(inferLocTuple.get(i));
+ }
+ } else {
+ locTuple = flowGraph.getLocationTuple(flowNode);
}
+ return locTuple;
+ }
+
+ private boolean calculateCompositeLocationForLocalVar(FlowGraph flowGraph,
+ SSJavaLattice<String> methodLattice, MethodLocationInfo methodInfo, FlowNode flowNode) {
+
+ Set<FlowNode> inNodeSet = flowGraph.getIncomingFlowNodeSet(flowNode);
+ Set<FlowNode> reachableNodeSet = flowGraph.getReachableFlowNodeSet(flowNode);
+
+ Map<NTuple<Location>, Set<NTuple<Location>>> mapPrefixToIncomingLocTupleSet =
+ new HashMap<NTuple<Location>, Set<NTuple<Location>>>();
+
+ List<NTuple<Location>> prefixList = new ArrayList<NTuple<Location>>();
+
+ for (Iterator iterator = inNodeSet.iterator(); iterator.hasNext();) {
+ FlowNode inNode = (FlowNode) iterator.next();
+ NTuple<Location> inTuple = getLocationTuple(methodInfo, flowGraph, inNode);
+
+ if (inTuple.size() > 1) {
+ for (int i = 1; i < inTuple.size(); i++) {
+ NTuple<Location> prefix = inTuple.subList(0, i);
+ if (!prefixList.contains(prefix)) {
+ prefixList.add(prefix);
+ }
+ addPrefixMapping(mapPrefixToIncomingLocTupleSet, prefix, inTuple);
+ }
+ }
+ }
+
+ Collections.sort(prefixList, new Comparator<NTuple<Location>>() {
+ public int compare(NTuple<Location> arg0, NTuple<Location> arg1) {
+ int s0 = arg0.size();
+ int s1 = arg1.size();
+ if (s0 > s1) {
+ return -1;
+ } else if (s0 == s1) {
+ return 0;
+ } else {
+ return 1;
+ }
+ }
+ });
+
+ // find out reachable nodes that have the longest common prefix
+ for (int i = 0; i < prefixList.size(); i++) {
+ NTuple<Location> curPrefix = prefixList.get(i);
+ Set<NTuple<Location>> reachableCommonPrefixSet = new HashSet<NTuple<Location>>();
+
+ for (Iterator iterator2 = reachableNodeSet.iterator(); iterator2.hasNext();) {
+ FlowNode reachableNode = (FlowNode) iterator2.next();
+ NTuple<Location> reachLocTuple = flowGraph.getLocationTuple(reachableNode);
+ if (reachLocTuple.startsWith(curPrefix)) {
+ reachableCommonPrefixSet.add(reachLocTuple);
+ }
+ }
+
+ if (!reachableCommonPrefixSet.isEmpty()) {
+ Set<NTuple<Location>> incomingCommonPrefixSet =
+ mapPrefixToIncomingLocTupleSet.get(curPrefix);
+
+ int idx = curPrefix.size();
+ NTuple<Location> element = incomingCommonPrefixSet.iterator().next();
+ Descriptor desc = element.get(idx).getDescriptor();
+
+ SSJavaLattice<String> lattice = getLattice(desc);
+ LocationInfo locInfo = getLocationInfo(desc);
+
+ CompositeLocation inferNode = methodInfo.getInferLocation(flowNode);
+ String nodeSymbol;
+ if (inferNode != null) {
+
+ } else {
+ String newLocSymbol = "Loc" + (SSJavaLattice.seed++);
+ inferNode = new CompositeLocation();
+ for (int locIdx = 0; locIdx < curPrefix.size(); locIdx++) {
+ inferNode.addLocation(curPrefix.get(locIdx));
+ }
+ inferNode.addLocation(new Location(desc, newLocSymbol));
+ methodInfo.mapFlowNodeToInferLocation(flowNode, inferNode);
+ if (flowNode.getDescTuple().size() == 1) {
+ // local variable case
+ modifyLocalLatticeAccrodingToNewlyAddedCompositeLocation(methodLattice, methodInfo,
+ inferNode, flowNode);
+ }
+ }
+
+ nodeSymbol = inferNode.get(inferNode.getSize() - 1).getLocIdentifier();
+
+ for (Iterator iterator = incomingCommonPrefixSet.iterator(); iterator.hasNext();) {
+ NTuple<Location> tuple = (NTuple<Location>) iterator.next();
+ Location loc = tuple.get(idx);
+ String higher = locInfo.getLocName(loc.getLocIdentifier());
+ lattice.addRelationHigherToLower(higher, nodeSymbol);
+ }
+
+ for (Iterator iterator = reachableCommonPrefixSet.iterator(); iterator.hasNext();) {
+ NTuple<Location> tuple = (NTuple<Location>) iterator.next();
+ Location loc = tuple.get(idx);
+ String lower = locInfo.getLocName(loc.getLocIdentifier());
+ lattice.addRelationHigherToLower(nodeSymbol, lower);
+ }
+
+ return true;
+ }
+
+ }
+
+ return false;
+
+ }
+
+ private void modifyLocalLatticeAccrodingToNewlyAddedCompositeLocation(
+ SSJavaLattice<String> methodLattice, MethodLocationInfo methodInfo,
+ CompositeLocation inferNode, FlowNode flowNode) {
+
+ Location localLocation = inferNode.get(0);
+ String newLocName = methodInfo.getLocName(localLocation.getLocIdentifier());
+ String oldLocName = methodInfo.getLocName(flowNode.getDescTuple().get(0).getSymbol());
+
+ methodInfo.mapDescSymbolToLocName(oldLocName, newLocName);
+ methodLattice.substituteLocation(oldLocName, newLocName);
+
+ }
+
+ public boolean isPrimitiveLocalVariable(FlowNode node) {
+ VarDescriptor varDesc = (VarDescriptor) node.getDescTuple().get(0);
+ return varDesc.getType().isPrimitive();
+ }
+
+ private SSJavaLattice<String> getLattice(Descriptor d) {
+ if (d instanceof MethodDescriptor) {
+ return getMethodLattice((MethodDescriptor) d);
+ } else {
+ return getFieldLattice((ClassDescriptor) d);
+ }
}
private SSJavaLattice<String> getMethodLattice(MethodDescriptor md) {
// add a new binary relation of dstNode < srcNode
SSJavaLattice<String> fieldLattice = getFieldLattice(cd);
- String srcSymbol = srcFieldDesc.getSymbol();
- String dstSymbol = dstFieldDesc.getSymbol();
+ String srcOriginalSymbol = srcFieldDesc.getSymbol();
+ String dstOriginalSymbol = dstFieldDesc.getSymbol();
+
+ LocationInfo fieldInfo = getFieldLocationInfo(cd);
+
+ String srcSymbol = fieldInfo.getLocName(srcOriginalSymbol);
+ String dstSymbol = fieldInfo.getLocName(dstOriginalSymbol);
+
+ Set<String> cycleElementSet = fieldLattice.getPossibleCycleElements(srcSymbol, dstSymbol);
+
+ if (cycleElementSet.size() > 0) {
+ String newSharedLoc = "SharedLoc" + (SSJavaLattice.seed++);
+ fieldLattice.mergeIntoSharedLocation(cycleElementSet, newSharedLoc);
+
+ for (Iterator iterator = cycleElementSet.iterator(); iterator.hasNext();) {
+ String locName = (String) iterator.next();
+ fieldInfo.mapDescSymbolToLocName(locName, newSharedLoc);
+ }
- if (!fieldLattice.isGreaterThan(srcSymbol, dstSymbol)) {
+ } else if (!fieldLattice.isGreaterThan(srcSymbol, dstSymbol)) {
+ // if the lattice does not have this relation, add it
fieldLattice.addRelationHigherToLower(srcSymbol, dstSymbol);
}
}
}
+
+ public Set<T> getPossibleCycleElements(T higherLoc, T lowerLoc) {
+ // if a relation of higherloc & lowerloc introduces a new cycle flow,
+ // return the set of elements consisting of the cycle
+ Set<T> cycleElemetns = new HashSet<T>();
+
+ // if lowerLoc has already been higher than higherLoc, the new relation
+ // introduces a cycle to the lattice
+ if (lowerLoc.equals(higherLoc)) {
+ cycleElemetns.add(lowerLoc);
+ cycleElemetns.add(higherLoc);
+ } else if (isGreaterThan(lowerLoc, higherLoc)) {
+ cycleElemetns.add(lowerLoc);
+ cycleElemetns.add(higherLoc);
+ getInBetweenElements(lowerLoc, higherLoc, cycleElemetns);
+ }
+ return cycleElemetns;
+ }
+
+ private void getInBetweenElements(T start, T end, Set<T> elementSet) {
+ Set<T> connectedSet = get(start);
+ for (Iterator iterator = connectedSet.iterator(); iterator.hasNext();) {
+ T cur = (T) iterator.next();
+ if ((!start.equals(cur)) && (!cur.equals(end)) && isGreaterThan(cur, end)) {
+ elementSet.add(cur);
+ getInBetweenElements(cur, end, elementSet);
+ }
+ }
+ }
+
+ public void mergeIntoSharedLocation(Set<T> cycleSet, T newLoc) {
+
+ // add a new shared loc
+ put(newLoc);
+ addSharedLoc(newLoc);
+
+ Set<T> keySet = getKeySet();
+
+ for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
+ T keyElement = (T) iterator.next();
+ Set<T> connectedSet = get(keyElement);
+ Set<T> removeSet = new HashSet<T>();
+ for (Iterator iterator2 = connectedSet.iterator(); iterator2.hasNext();) {
+ T cur = (T) iterator2.next();
+ if (cycleSet.contains(cur)) {
+ removeSet.add(cur);
+ }
+ }
+ if (!removeSet.isEmpty()) {
+ // remove relations of locationElement -> cycle
+ connectedSet.removeAll(removeSet);
+ // add a new relation of location Element -> shared loc
+ connectedSet.add(newLoc);
+ getTable().put(keyElement, connectedSet);
+ }
+ }
+
+ Set<T> newConnectedSet = new HashSet<T>();
+ for (Iterator iterator = cycleSet.iterator(); iterator.hasNext();) {
+ T cycleElement = (T) iterator.next();
+ Set<T> connectedSet = get(cycleElement);
+ if (connectedSet != null) {
+ newConnectedSet.addAll(connectedSet);
+ }
+ getTable().remove(cycleElement);
+ }
+ newConnectedSet.removeAll(cycleSet);
+ newConnectedSet.remove(newLoc);
+
+ Set<T> set = getTable().get(newLoc);
+ set.addAll(newConnectedSet);
+
+ }
+
+ public void substituteLocation(T oldLoc, T newLoc) {
+ // the new location is going to take all relations of the old location
+
+ // consider the set of location s.t. LOC is greater than oldLoc
+ Set<T> keySet = getKeySet();
+ Set<T> directedConnctedHigherLocSet = new HashSet<T>();
+
+ for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
+ T key = (T) iterator.next();
+ Set<T> connectedSet = getTable().get(key);
+ if (connectedSet.contains(oldLoc)) {
+ directedConnctedHigherLocSet.add(key);
+ }
+ }
+
+ Set<T> connctedLowerSet = getTable().get(oldLoc);
+ Set<T> directedConnctedLowerLocSet = new HashSet<T>();
+ if (connctedLowerSet != null) {
+ directedConnctedLowerLocSet.addAll(connctedLowerSet);
+ }
+
+ for (Iterator iterator = directedConnctedHigherLocSet.iterator(); iterator.hasNext();) {
+ T higher = (T) iterator.next();
+ if (!higher.equals(newLoc)) {
+ addRelationHigherToLower(higher, newLoc);
+ }
+ }
+
+ for (Iterator iterator = directedConnctedLowerLocSet.iterator(); iterator.hasNext();) {
+ T lower = (T) iterator.next();
+ if (!lower.equals(newLoc)) {
+ addRelationHigherToLower(newLoc, lower);
+ }
+ }
+
+ }
+
}