Small changes to allow:
[repair.git] / Repair / RepairCompiler / MCC / IR / GraphNode.java
index d8d2649a939ae86c0e69adc7e0e7cc8ec9cc0c1b..a450745bd132d11f9b3183b84f5fa2b841c8e791 100755 (executable)
@@ -2,13 +2,12 @@ package MCC.IR;
 
 import java.util.*;
 import java.io.*;
+import MCC.Compiler;
 
 public class GraphNode {
 
-    public static boolean useEdgeLabels;
-
     /* NodeStatus enumeration pattern ***********/
-    
+
     public static final NodeStatus UNVISITED = new NodeStatus("UNVISITED");
     public static final NodeStatus PROCESSING = new NodeStatus("PROCESSING");
     public static final NodeStatus FINISHED = new NodeStatus("FINISHED");
@@ -22,7 +21,7 @@ public class GraphNode {
     /* Edge *****************/
 
     public static class Edge {
-        
+
         private String label;
         private GraphNode target;
        private GraphNode source;
@@ -65,13 +64,23 @@ public class GraphNode {
     int discoverytime = -1;
     int finishingtime = -1; /* used for searches */
 
-    Vector edges = new Vector();  
+    Vector edges = new Vector();
     Vector inedges = new Vector();
     String nodelabel;
     String textlabel;
-    NodeStatus status = UNVISITED;    
+    NodeStatus status = UNVISITED;
     String dotnodeparams = new String();
     Object owner = null;
+    boolean merge=false;
+    String nodeoption="";
+
+    public void setOption(String option) {
+       this.nodeoption=option;
+    }
+
+    public void setMerge() {
+       merge=true;
+    }
 
     public GraphNode(String label) {
         this.nodelabel = label;
@@ -113,6 +122,30 @@ public class GraphNode {
        }
     }
 
+    public static void boundedcomputeclosure(Collection nodes, Collection removed,int depth) {
+       Stack tovisit=new Stack();
+       Stack newvisit=new Stack();
+       tovisit.addAll(nodes);
+       for(int i=0;i<depth&&!tovisit.isEmpty();i++) {
+           while(!tovisit.isEmpty()) {
+               GraphNode gn=(GraphNode)tovisit.pop();
+               for(Iterator it=gn.edges();it.hasNext();) {
+                   Edge edge=(Edge)it.next();
+                   GraphNode target=edge.getTarget();
+                   if (!nodes.contains(target)) {
+                       if ((removed==null)||
+                           (!removed.contains(target))) {
+                           nodes.add(target);
+                           newvisit.push(target);
+                       }
+                   }
+               }
+           }
+           tovisit=newvisit;
+           newvisit=new Stack();
+       }
+    }
+
     public void setDotNodeParameters(String param) {
         if (param == null) {
             throw new NullPointerException();
@@ -123,7 +156,7 @@ public class GraphNode {
             dotnodeparams = new String();
         }
     }
-    
+
     public void setStatus(NodeStatus status) {
         if (status == null) {
             throw new NullPointerException();
@@ -138,7 +171,7 @@ public class GraphNode {
     public String getTextLabel() {
         return textlabel;
     }
-    
+
     public NodeStatus getStatus() {
         return this.status;
     }
@@ -187,17 +220,17 @@ public class GraphNode {
 
 
     public static class DOTVisitor {
-        
+
         java.io.PrintWriter output;
         int tokennumber;
         int color;
-      
+
         private DOTVisitor(java.io.OutputStream output) {
             tokennumber = 0;
             color = 0;
             this.output = new java.io.PrintWriter(output, true);
         }
-        
+
         private String getNewID(String name) {
             tokennumber = tokennumber + 1;
             return new String (name+tokennumber);
@@ -205,7 +238,7 @@ public class GraphNode {
 
         Collection nodes;
        Collection special;
-        
+
         public static void visit(java.io.OutputStream output, Collection nodes) {
            visit(output,nodes,null);
        }
@@ -216,7 +249,7 @@ public class GraphNode {
             visitor.nodes = nodes;
             visitor.make();
         }
-        
+
         private void make() {
             output.println("digraph dotvisitor {");
             output.println("\trotate=90;");
@@ -230,8 +263,8 @@ public class GraphNode {
             traverse();
             output.println("}\n");
         }
-                
-        private void traverse() {            
+
+        private void traverse() {
            Set cycleset=GraphNode.findcycles(nodes);
 
             Iterator i = nodes.iterator();
@@ -239,26 +272,52 @@ public class GraphNode {
                 GraphNode gn = (GraphNode) i.next();
                 Iterator edges = gn.edges();
                 String label = gn.getTextLabel(); // + " [" + gn.discoverytime + "," + gn.finishingtime + "];";
-               String option="";
-               if (cycleset.contains(gn))
-                   option=",style=bold";
+               String option=gn.nodeoption;
                if (special!=null&&special.contains(gn))
                    option+=",shape=box";
-                output.println("\t" + gn.getLabel() + " [label=\"" + label + "\"" + gn.dotnodeparams + option+"];");
+               if (!gn.merge)
+                   output.println("\t" + gn.getLabel() + " [label=\"" + label + "\"" + gn.dotnodeparams + option+"];");
 
+               if (!gn.merge)
                 while (edges.hasNext()) {
                     Edge edge = (Edge) edges.next();
                     GraphNode node = edge.getTarget();
                    if (nodes.contains(node)) {
-                       String edgelabel = useEdgeLabels ? "label=\"" + edge.getLabel() + "\"" : "label=\"\"";
-                       output.println("\t" + gn.getLabel() + " -> " + node.getLabel() + " [" + edgelabel + edge.dotnodeparams + "];");
+                       for(Iterator nodeit=nonmerge(node).iterator();nodeit.hasNext();) {
+                           GraphNode node2=(GraphNode)nodeit.next();
+                           String edgelabel = Compiler.DEBUGGRAPH ? "label=\"" + edge.getLabel() + "\"" : "label=\"\"";
+                           output.println("\t" + gn.getLabel() + " -> " + node2.getLabel() + " [" + edgelabel + edge.dotnodeparams + "];");
+                       }
                    }
                 }
             }
         }
+
+       Set nonmerge(GraphNode gn) {
+           HashSet newset=new HashSet();
+           HashSet toprocess=new HashSet();
+           toprocess.add(gn);
+           while(!toprocess.isEmpty()) {
+               GraphNode gn2=(GraphNode)toprocess.iterator().next();
+               toprocess.remove(gn2);
+               if (!gn2.merge)
+                   newset.add(gn2);
+               else {
+                   Iterator edges = gn2.edges();
+                   while (edges.hasNext()) {
+                       Edge edge = (Edge) edges.next();
+                       GraphNode node = edge.getTarget();
+                       if (!newset.contains(node)&&nodes.contains(node))
+                           toprocess.add(node);
+                   }
+               }
+           }
+           return newset;
+       }
+
     }
 
-    /** This function returns the set of nodes involved in cycles. 
+    /** This function returns the set of nodes involved in cycles.
      * It only considers cycles containing nodes in the set 'nodes'.
     */
     public static Set findcycles(Collection nodes) {
@@ -323,7 +382,7 @@ public class GraphNode {
     }
 
     /**
-     * DFS encapsulates the depth first search algorithm 
+     * DFS encapsulates the depth first search algorithm
      */
     public static class DFS {
 
@@ -334,7 +393,7 @@ public class GraphNode {
        HashMap sccmap;
        HashMap sccmaprev;
 
-        private DFS(Collection nodes) { 
+        private DFS(Collection nodes) {
             this.nodes = nodes;
         }
        /** Calculates the strong connected components for the graph composed
@@ -382,29 +441,29 @@ public class GraphNode {
             if (nodes == null) {
                 throw new NullPointerException();
             }
-            
+
             DFS dfs = new DFS(nodes);
             return dfs.go();
         }
 
-        private boolean go() {           
+        private boolean go() {
             Iterator i;
             time = 0;
             boolean acyclic=true;
             i = nodes.iterator();
             while (i.hasNext()) {
                 GraphNode gn = (GraphNode) i.next();
-                gn.reset();            
-            }            
+                gn.reset();
+            }
 
             i = nodes.iterator();
             while (i.hasNext()) {
                 GraphNode gn = (GraphNode) i.next();
-               assert gn.getStatus() != PROCESSING;                    
+               assert gn.getStatus() != PROCESSING;
                 if (gn.getStatus() == UNVISITED) {
                     if (!dfs(gn))
                        acyclic=false;
-                } 
+                }
             }
            return acyclic;
         }