changes: now Inference engine works fine with the EyeTracking benchmark.
[IRC.git] / Robust / src / Analysis / SSJava / FlowGraph.java
index f553fccfa76a1d93a8f2bede47b45080d0975c94..4054f701a78eef73b4eed01519d7151e7f2d6275 100644 (file)
@@ -41,6 +41,8 @@ public class FlowGraph {
 
   Map<MethodInvokeNode, FlowReturnNode> mapMethodInvokeNodeToFlowReturnNode;
 
+  Map<Descriptor, ClassDescriptor> mapIntersectionDescToEnclosingDescriptor;
+
   public static int interseed = 0;
 
   boolean debug = true;
@@ -57,6 +59,7 @@ public class FlowGraph {
     this.mapFlowNodeToOutEdgeSet = new HashMap<FlowNode, Set<FlowEdge>>();
     this.mapFlowNodeToInEdgeSet = new HashMap<FlowNode, Set<FlowEdge>>();
     this.mapMethodInvokeNodeToFlowReturnNode = new HashMap<MethodInvokeNode, FlowReturnNode>();
+    this.mapIntersectionDescToEnclosingDescriptor = new HashMap<Descriptor, ClassDescriptor>();
 
     if (!md.isStatic()) {
       // create a node for 'this' varialbe
@@ -74,6 +77,15 @@ public class FlowGraph {
 
   }
 
+  public void addMapInterLocNodeToEnclosingDescriptor(Descriptor interDesc, ClassDescriptor desc) {
+    System.out.println("##### INTERLOC=" + interDesc + "    enclosing desc=" + desc);
+    mapIntersectionDescToEnclosingDescriptor.put(interDesc, desc);
+  }
+
+  public ClassDescriptor getEnclosingDescriptor(Descriptor interDesc) {
+    return mapIntersectionDescToEnclosingDescriptor.get(interDesc);
+  }
+
   public Map<NTuple<Descriptor>, FlowNode> getMapDescTupleToInferNode() {
     return mapDescTupleToInferNode;
   }
@@ -338,6 +350,7 @@ public class FlowGraph {
 
   public FlowNode createNewFlowNode(NTuple<Descriptor> tuple) {
 
+    // System.out.println("createNewFlowNode=" + tuple);
     if (!mapDescTupleToInferNode.containsKey(tuple)) {
       FlowNode node = new FlowNode(tuple);
       mapDescTupleToInferNode.put(tuple, node);
@@ -383,12 +396,29 @@ public class FlowGraph {
     Set<FlowEdge> outEdgeSet = getOutEdgeSet(fn);
     for (Iterator iterator = outEdgeSet.iterator(); iterator.hasNext();) {
       FlowEdge edge = (FlowEdge) iterator.next();
-      FlowNode dstNode = edge.getDst();
+      FlowNode originalDstNode = edge.getDst();
+
+      Set<FlowNode> dstNodeSet = new HashSet<FlowNode>();
+      if (originalDstNode instanceof FlowReturnNode) {
+        FlowReturnNode rnode = (FlowReturnNode) originalDstNode;
+        Set<NTuple<Descriptor>> rtupleSetFromRNode = rnode.getReturnTupleSet();
+        Set<NTuple<Descriptor>> rtupleSet = getReturnTupleSet(rtupleSetFromRNode);
+        for (Iterator iterator2 = rtupleSet.iterator(); iterator2.hasNext();) {
+          NTuple<Descriptor> rtuple = (NTuple<Descriptor>) iterator2.next();
+          dstNodeSet.add(getFlowNode(rtuple));
+        }
+      } else {
+        dstNodeSet.add(originalDstNode);
+      }
 
-      if (!visited.contains(dstNode)) {
-        visited.add(dstNode);
-        recurLocalReachFlowNodeSet(dstNode, visited);
+      for (Iterator iterator2 = dstNodeSet.iterator(); iterator2.hasNext();) {
+        FlowNode dstNode = (FlowNode) iterator2.next();
+        if (!visited.contains(dstNode)) {
+          visited.add(dstNode);
+          recurLocalReachFlowNodeSet(dstNode, visited);
+        }
       }
+
     }
 
   }
@@ -421,15 +451,47 @@ public class FlowGraph {
 
     Set<FlowNode> nodeSet = getNodeSet();
     for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
-      FlowNode flowNode = (FlowNode) iterator.next();
-      if (flowNode.getCurrentDescTuple().startsWith(prefix)) {
-        recurReachableSetFrom(flowNode, reachableSet);
+      FlowNode originalSrcNode = (FlowNode) iterator.next();
+
+      Set<FlowNode> srcNodeSet = new HashSet<FlowNode>();
+      if (originalSrcNode instanceof FlowReturnNode) {
+        FlowReturnNode rnode = (FlowReturnNode) originalSrcNode;
+        Set<NTuple<Descriptor>> rtupleSetFromRNode = rnode.getReturnTupleSet();
+        Set<NTuple<Descriptor>> rtupleSet = getReturnTupleSet(rtupleSetFromRNode);
+        // System.out.println("#rnode=" + rnode + "  rtupleSet=" + rtupleSet);
+        for (Iterator iterator2 = rtupleSet.iterator(); iterator2.hasNext();) {
+          NTuple<Descriptor> rtuple = (NTuple<Descriptor>) iterator2.next();
+          if (rtuple.startsWith(prefix)) {
+            // System.out.println("rtuple=" + rtuple + "   give it to recur=" + originalSrcNode);
+            recurReachableSetFrom(originalSrcNode, reachableSet);
+          }
+        }
+      } else {
+        if (originalSrcNode.getCurrentDescTuple().startsWith(prefix)) {
+          recurReachableSetFrom(originalSrcNode, reachableSet);
+        }
       }
+
     }
 
     return reachableSet;
   }
 
+  public Set<NTuple<Descriptor>> getReturnTupleSet(Set<NTuple<Descriptor>> in) {
+
+    Set<NTuple<Descriptor>> normalTupleSet = new HashSet<NTuple<Descriptor>>();
+    for (Iterator iterator2 = in.iterator(); iterator2.hasNext();) {
+      NTuple<Descriptor> tuple = (NTuple<Descriptor>) iterator2.next();
+      FlowNode tupleNode = getFlowNode(tuple);
+      if (tupleNode instanceof FlowReturnNode) {
+        normalTupleSet.addAll(getReturnTupleSet(((FlowReturnNode) tupleNode).getReturnTupleSet()));
+      } else {
+        normalTupleSet.add(tuple);
+      }
+    }
+    return normalTupleSet;
+  }
+
   // private void getReachFlowNodeSetFrom(FlowNode fn, Set<FlowNode> visited) {
   //
   // for (Iterator iterator = fn.getOutEdgeSet().iterator();
@@ -497,7 +559,7 @@ public class FlowGraph {
 
       Descriptor localDesc = fn.getDescTuple().get(0);
 
-      if (fn.isIntermediate()) {
+      if (fn.isIntermediate() && fn.getDescTuple().size() == 1) {
         Location interLoc = new Location(md, localDesc.getSymbol());
         interLoc.setLocDescriptor(localDesc);
         locTuple.add(interLoc);
@@ -519,6 +581,8 @@ public class FlowGraph {
             loc.setLocDescriptor(curDesc);
             if (curDesc instanceof VarDescriptor) {
               cd = ((VarDescriptor) curDesc).getType().getClassDesc();
+            } else if (curDesc instanceof InterDescriptor) {
+              cd = mapIntersectionDescToEnclosingDescriptor.get(curDesc);
             } else {
               // otherwise it should be the last element
               cd = null;
@@ -566,8 +630,9 @@ public class FlowGraph {
 
           if (incomingNode instanceof FlowReturnNode) {
             FlowReturnNode rnode = (FlowReturnNode) incomingNode;
-            Set<NTuple<Descriptor>> nodeTupleSet = rnode.getTupleSet();
-            for (Iterator iterator3 = nodeTupleSet.iterator(); iterator3.hasNext();) {
+            Set<NTuple<Descriptor>> nodeTupleSet = rnode.getReturnTupleSet();
+            Set<NTuple<Descriptor>> rtupleSet = getReturnTupleSet(nodeTupleSet);
+            for (Iterator iterator3 = rtupleSet.iterator(); iterator3.hasNext();) {
               NTuple<Descriptor> nodeTuple = (NTuple<Descriptor>) iterator3.next();
               FlowNode fn = getFlowNode(nodeTuple);
               if (!visited.contains(fn)) {
@@ -794,4 +859,20 @@ public class FlowGraph {
     bw.write("}\n");
   }
 
+  public void removeEdge(NTuple<Descriptor> from, NTuple<Descriptor> to) {
+
+    Set<FlowEdge> toberemoved = new HashSet<FlowEdge>();
+    Set<FlowEdge> edgeSet = getOutEdgeSet(getFlowNode(from));
+
+    for (Iterator iterator = edgeSet.iterator(); iterator.hasNext();) {
+      FlowEdge flowEdge = (FlowEdge) iterator.next();
+      if (flowEdge.getInitTuple().equals(from) && flowEdge.getEndTuple().equals(to)) {
+        toberemoved.add(flowEdge);
+      }
+    }
+
+    edgeSet.removeAll(toberemoved);
+
+  }
+
 }
\ No newline at end of file