implementing inheritance check + missing features.
[IRC.git] / Robust / src / Analysis / SSJava / BuildLattice.java
index d1c631fd32b74c3548c515af81c36cdbb281a684..b4f2e37d23fdef7eb30a4b6c727166713fb09695 100644 (file)
@@ -17,10 +17,14 @@ public class BuildLattice {
   private Map<HNode, TripleItem> mapSharedNodeToTripleItem;
   private Map<HNode, Integer> mapHNodeToHighestIndex;
 
+  private Map<Pair<HNode, HNode>, Integer> mapItemToHighestIndex;
+
   public BuildLattice(LocationInference infer) {
     this.infer = infer;
     this.mapSharedNodeToTripleItem = new HashMap<HNode, TripleItem>();
     this.mapHNodeToHighestIndex = new HashMap<HNode, Integer>();
+    this.mapItemToHighestIndex = new HashMap<Pair<HNode, HNode>, Integer>();
+
   }
 
   public SSJavaLattice<String> buildLattice(Descriptor desc) {
@@ -66,8 +70,6 @@ public class BuildLattice {
       HierarchyGraph inputGraph, LocationSummary locSummary,
       Map<Set<Integer>, Set<Set<Integer>>> mapImSucc) {
 
-    HierarchyGraph simpleHierarchyGraph = infer.getSimpleHierarchyGraph(desc);
-
     SSJavaLattice<String> lattice =
         new SSJavaLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM);
 
@@ -140,6 +142,7 @@ public class BuildLattice {
 
     }
 
+    inputGraph.removeRedundantEdges();
     return lattice;
   }
 
@@ -219,16 +222,18 @@ public class BuildLattice {
               // startNode = node;
               // }
 
-              Set<HNode> endNodeSetFromSimpleGraph =
-                  simpleGraph.getDirectlyReachableSkeletonCombinationNodeFrom(outNode, null);
+              // TODO
+              // Set<HNode> endNodeSetFromSimpleGraph =
+              // simpleGraph.getDirectlyReachableSkeletonCombinationNodeFrom(outNode, null);
+              // Set<HNode> endCombNodeSet = new HashSet<HNode>();
+              // for (Iterator iterator3 = endNodeSetFromSimpleGraph.iterator();
+              // iterator3.hasNext();) {
+              // HNode endNode = (HNode) iterator3.next();
+              // endCombNodeSet.add(getCombinationNodeInSCGraph(desc, endNode));
+              // }
+
+              Set<HNode> endCombNodeSet = scGraph.getOutgoingNodeSet(startNode);
 
-              // System.out.println("endNodeSetFromSimpleGraph=" + endNodeSetFromSimpleGraph
-              // + "   from=" + outNode);
-              Set<HNode> endCombNodeSet = new HashSet<HNode>();
-              for (Iterator iterator3 = endNodeSetFromSimpleGraph.iterator(); iterator3.hasNext();) {
-                HNode endNode = (HNode) iterator3.next();
-                endCombNodeSet.add(getCombinationNodeInSCGraph(desc, endNode));
-              }
               // System.out.println("endCombNodeSet=" + endCombNodeSet);
               visited.add(outNode);
               if (endCombNodeSet.size() > 0) {
@@ -288,7 +293,8 @@ public class BuildLattice {
       HNode sharedNode = (HNode) iterator.next();
       TripleItem item = mapSharedNodeToTripleItem.get(sharedNode);
       String nonSharedLocName = mapIntermediateLoc.get(item);
-      // System.out.println("sharedNode=" + sharedNode + "    locName=" + nonSharedLocName);
+
+      System.out.println("sharedNode=" + sharedNode + "    locName=" + nonSharedLocName);
 
       String newLocName;
       if (locSummary.getHNodeNameSetByLatticeLoationName(nonSharedLocName) != null
@@ -301,8 +307,8 @@ public class BuildLattice {
         Set<String> belowElementSet = new HashSet<String>();
         belowElementSet.addAll(lattice.get(nonSharedLocName));
 
-        // System.out.println("nonSharedLocName=" + nonSharedLocName + "   belowElementSet="
-        // + belowElementSet + "  newLocName=" + newLocName);
+        System.out.println("nonSharedLocName=" + nonSharedLocName + "   belowElementSet="
+            + belowElementSet + "  newLocName=" + newLocName);
 
         lattice.insertNewLocationBetween(nonSharedLocName, belowElementSet, newLocName);
       } else {
@@ -379,6 +385,7 @@ public class BuildLattice {
     }
 
     HierarchyGraph simpleGraph = infer.getSimpleHierarchyGraph(desc);
+    HierarchyGraph scGraph = infer.getSkeletonCombinationHierarchyGraph(desc);
 
     Set<HNode> combineSkeletonNodeSet = simpleGraph.getCombineSetByCombinationNode(cnode);
 
@@ -389,14 +396,17 @@ public class BuildLattice {
 
     // System.out.println("combinationNodeSet=" + combinationNodeSet);
 
-    Set<HNode> endNodeSetFromSimpleGraph =
-        simpleGraph.getDirectlyReachableSkeletonCombinationNodeFrom(cnode, combinationNodeSet);
+    // TODO
+    // Set<HNode> endNodeSetFromSimpleGraph =
+    // simpleGraph.getDirectlyReachableSkeletonCombinationNodeFrom(cnode, combinationNodeSet);
     // System.out.println("-endNodeSetFromSimpleGraph=" + endNodeSetFromSimpleGraph);
-    Set<HNode> endCombNodeSet = new HashSet<HNode>();
-    for (Iterator iterator3 = endNodeSetFromSimpleGraph.iterator(); iterator3.hasNext();) {
-      HNode endNode = (HNode) iterator3.next();
-      endCombNodeSet.add(getCombinationNodeInSCGraph(desc, endNode));
-    }
+    // Set<HNode> endCombNodeSet = new HashSet<HNode>();
+    // for (Iterator iterator3 = endNodeSetFromSimpleGraph.iterator(); iterator3.hasNext();) {
+    // HNode endNode = (HNode) iterator3.next();
+    // endCombNodeSet.add(getCombinationNodeInSCGraph(desc, endNode));
+    // }
+
+    Set<HNode> endCombNodeSet = scGraph.getOutgoingNodeSet(combinationNodeInSCGraph);
     visited.add(cnode);
 
     // follows the straight line up to another skeleton/combination node
@@ -529,7 +539,6 @@ public class BuildLattice {
       int idx, LocationSummary locSummary, HNode curNode) {
 
     TripleItem item = new TripleItem(startNode, endNodeSet, idx);
-    // System.out.println("item=" + item);
     if (!mapIntermediateLoc.containsKey(item)) {
       // need to create a new intermediate location in the lattice
       String newLocName = "ILOC" + (LocationInference.locSeed++);
@@ -563,6 +572,7 @@ public class BuildLattice {
 
     if (curNode.isSharedNode()) {
       // if the current node is shared location, add a shared location to the lattice later
+      System.out.println("###SHARED ITEM=" + item);
       mapSharedNodeToTripleItem.put(curNode, item);
     } else {
       Set<Descriptor> descSet = simpleHierarchyGraph.getDescSetOfNode(curNode);
@@ -586,15 +596,18 @@ public class BuildLattice {
       System.out.println("---incomingHNodeSetToOutNode=" + incomingHNodeSetToOutNode);
 
       if (!outNode.isSkeleton() && !outNode.isCombinationNode() && !visited.contains(outNode)) {
+        Pair<HNode, HNode> pair = new Pair(startNode, outNode);
         if (visited.containsAll(simpleHierarchyGraph.getIncomingNodeSet(outNode))) {
           visited.add(outNode);
-          int newidx = getCurrentHighestIndex(outNode, idx + 1);
+          int newidx = getCurrentHighestIndex(pair, idx + 1);
+          // int newidx = getCurrentHighestIndex(outNode, idx + 1);
           recurDFSNormalNode(desc, lattice, startNode, endNodeSet, visited, mapIntermediateLoc,
               newidx, locSummary, outNode);
           // recurDFSNormalNode(desc, lattice, startNode, endNodeSet, visited, mapIntermediateLoc,
           // idx + 1, locSummary, outNode);
         } else {
-          updateHighestIndex(outNode, idx + 1);
+          updateHighestIndex(pair, idx + 1);
+          // updateHighestIndex(outNode, idx + 1);
           System.out.println("NOT RECUR");
         }
       } else if (!outNode.isSkeleton() && outNode.isCombinationNode() && !visited.contains(outNode)) {
@@ -652,6 +665,7 @@ public class BuildLattice {
     String locName = mapIntermediateLoc.get(item);
     if (curNode.isSharedNode()) {
       // if the current node is shared location, add a shared location to the lattice later
+      System.out.println("###SHARED ITEM=" + item);
       mapSharedNodeToTripleItem.put(curNode, item);
     } else {
       Set<Descriptor> descSet = simpleHierarchyGraph.getDescSetOfNode(curNode);
@@ -692,17 +706,21 @@ public class BuildLattice {
 
           // check whether the next combination node is different from the current node
           if (combinationNodeInSCGraph.equals(getCombinationNodeInSCGraph(desc, outNode))) {
+            Pair<HNode, HNode> pair = new Pair(combinationNodeInSCGraph, outNode);
             if (visited.containsAll(incomingCombinedHNodeSet)) {
               visited.add(outNode);
               System.out.println("-------curIdx=" + (idx + 1));
-              int newIdx = getCurrentHighestIndex(outNode, idx + 1);
+
+              int newIdx = getCurrentHighestIndex(pair, idx + 1);
+              // int newIdx = getCurrentHighestIndex(outNode, idx + 1);
               System.out.println("-------newIdx=" + newIdx);
               recurDFS(desc, lattice, combinationNodeInSCGraph, endNodeSet, visited,
                   mapIntermediateLoc, newIdx, locSummary, outNode);
               // recurDFS(desc, lattice, combinationNodeInSCGraph, endNodeSet, visited,
               // mapIntermediateLoc, idx + 1, locSummary, outNode);
             } else {
-              updateHighestIndex(outNode, idx + 1);
+              updateHighestIndex(pair, idx + 1);
+              // updateHighestIndex(outNode, idx + 1);
               System.out.println("-----NOT RECUR!");
             }
           } else {
@@ -722,6 +740,15 @@ public class BuildLattice {
 
   }
 
+  private int getCurrentHighestIndex(Pair<HNode, HNode> pair, int curIdx) {
+    int recordedIdx = getCurrentHighestIndex(pair);
+    if (recordedIdx > curIdx) {
+      return recordedIdx;
+    } else {
+      return curIdx;
+    }
+  }
+
   private int getCurrentHighestIndex(HNode node, int curIdx) {
     int recordedIdx = getCurrentHighestIndex(node);
     if (recordedIdx > curIdx) {
@@ -731,6 +758,19 @@ public class BuildLattice {
     }
   }
 
+  private int getCurrentHighestIndex(Pair<HNode, HNode> pair) {
+    if (!mapItemToHighestIndex.containsKey(pair)) {
+      mapItemToHighestIndex.put(pair, new Integer(-1));
+    }
+    return mapItemToHighestIndex.get(pair).intValue();
+  }
+
+  private void updateHighestIndex(Pair<HNode, HNode> pair, int idx) {
+    if (idx > getCurrentHighestIndex(pair)) {
+      mapItemToHighestIndex.put(pair, new Integer(idx));
+    }
+  }
+
   private int getCurrentHighestIndex(HNode node) {
     if (!mapHNodeToHighestIndex.containsKey(node)) {
       mapHNodeToHighestIndex.put(node, new Integer(-1));