fix generation of graph files
[repair.git] / Repair / RepairCompiler / MCC / IR / GraphNode.java
index de8fe3252e36334ddd4f5f294dd3a56f2cf8dfd8..80d34a9a5ac626a786e6c2fcc5a2212b1ed73834 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;
@@ -64,15 +63,24 @@ public class GraphNode {
 
     int discoverytime = -1;
     int finishingtime = -1; /* used for searches */
-    int scc = -1;
 
-    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;
@@ -114,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();
@@ -124,7 +156,7 @@ public class GraphNode {
             dotnodeparams = new String();
         }
     }
-    
+
     public void setStatus(NodeStatus status) {
         if (status == null) {
             throw new NullPointerException();
@@ -139,7 +171,7 @@ public class GraphNode {
     public String getTextLabel() {
         return textlabel;
     }
-    
+
     public NodeStatus getStatus() {
         return this.status;
     }
@@ -167,15 +199,6 @@ public class GraphNode {
 
     void resetscc() {
        status = UNVISITED;
-       scc = -1;
-    }
-
-    public int getSCC() {
-       return scc;
-    }
-
-    void setSCC(int s) {
-       scc=s;
     }
 
     void discover(int time) {
@@ -197,31 +220,36 @@ 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);
         }
 
         Collection nodes;
-        
+       Collection special;
+
         public static void visit(java.io.OutputStream output, Collection nodes) {
+           visit(output,nodes,null);
+       }
+
+        public static void visit(java.io.OutputStream output, Collection nodes, Collection special) {
             DOTVisitor visitor = new DOTVisitor(output);
+           visitor.special=special;
             visitor.nodes = nodes;
             visitor.make();
-
         }
-        
+
         private void make() {
             output.println("digraph dotvisitor {");
             output.println("\trotate=90;");
@@ -235,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();
@@ -244,90 +272,100 @@ 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";
-                output.println("\t" + gn.getLabel() + " [label=\"" + label + "\"" + gn.dotnodeparams + option+"];");
+               String option=gn.nodeoption;
+               if (special!=null&&special.contains(gn))
+                   option+=",shape=box";
+               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 + "];");
+                       }
                    }
                 }
             }
         }
-    }
 
-    /* XXXXXXXX  Should use SCC algorithm here - will change */
-    public static Set findcycles(Collection nodes) {
-       Stack st=new Stack();
-       HashSet acyclic=new HashSet();
-       HashSet cycles=new HashSet();
-       for(Iterator it=nodes.iterator();it.hasNext();) {
-           GraphNode node=(GraphNode)it.next();
-           if (acyclic.contains(node))
-               continue;
-           if (cycles.contains(node))
-               continue;
-           findcycles(cycles, acyclic, st,node,nodes);
+       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;
        }
-       return cycles;
+
     }
 
-    private static boolean findcycles(Set cycles, Set acyclic, Stack visited, GraphNode gn, Collection nodes) {
-       if (visited.contains(gn)) {/* Found cycle */
-           cycles.addAll(visited.subList(visited.indexOf(gn),visited.size()));  /* Add this cycle */
-           return true;
-       }
-       boolean acyclicflag=true;
-       visited.push(gn);
-       for(Iterator it=gn.edges();it.hasNext();) {
-           Edge e=(Edge) it.next();
-           GraphNode node = e.getTarget();
-           if (!nodes.contains(node))
-               continue; /* Don't visit stuff outside set */
-           if (acyclic.contains(node))
-               continue;
-           if (findcycles(cycles,acyclic,visited,node,nodes)) {
-               /* Found cycle */
-               acyclicflag=false;
-           }
+    /** 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) {
+       HashSet cycleset=new HashSet();
+       SCC scc=DFS.computeSCC(nodes);
+       if (!scc.hasCycles())
+           return cycleset;
+       for(int i=0;i<scc.numSCC();i++) {
+           if (scc.hasCycle(i))
+               cycleset.addAll(scc.getSCC(i));
        }
-       visited.pop();
-       if (acyclicflag) {
-           acyclic.add(gn); /* no cycles through gn */
-           return false;
-       } else
-           return true; /* found cycle */
+       return cycleset;
     }
-    
+
     public static class SCC {
-       boolean hascycles;
-       HashMap map;
+       boolean acyclic;
+       HashMap map,revmap;
        int numscc;
-       public SCC(boolean hascycles, HashMap map,int numscc) {
-           this.hascycles=hascycles;
+       public SCC(boolean acyclic, HashMap map,HashMap revmap,int numscc) {
+           this.acyclic=acyclic;
            this.map=map;
+           this.revmap=revmap;
            this.numscc=numscc;
        }
 
+       /** Returns whether the graph has any cycles */
        public boolean hasCycles() {
-           return hascycles;
+           return !acyclic;
        }
 
+       /** Returns the number of Strongly Connected Components */
        public int numSCC() {
            return numscc;
        }
 
+       /** Returns the strongly connected component number for the GraphNode gn*/
+       public int getComponent(GraphNode gn) {
+           return ((Integer)revmap.get(gn)).intValue();
+       }
+
+       /** Returns the set of nodes in the strongly connected component i*/
        public Set getSCC(int i) {
            Integer scc=new Integer(i);
            return (Set)map.get(scc);
        }
 
-       boolean hascycle(int i) {
+       /** Returns whether the strongly connected component i contains a cycle */
+       boolean hasCycle(int i) {
            Integer scc=new Integer(i);
            Set s=(Set)map.get(scc);
            if (s.size()>1)
@@ -344,7 +382,7 @@ public class GraphNode {
     }
 
     /**
-     * DFS encapsulates the depth first search algorithm 
+     * DFS encapsulates the depth first search algorithm
      */
     public static class DFS {
 
@@ -353,19 +391,22 @@ public class GraphNode {
         Collection nodes;
        Vector finishingorder=null;
        HashMap sccmap;
+       HashMap sccmaprev;
 
-        private DFS(Collection nodes) { 
+        private DFS(Collection nodes) {
             this.nodes = nodes;
         }
-
+       /** Calculates the strong connected components for the graph composed
+        *  of the set of nodes 'nodes'*/
        public static SCC computeSCC(Collection nodes) {
            if (nodes==null) {
                throw new NullPointerException();
            }
            DFS dfs=new DFS(nodes);
            dfs.sccmap=new HashMap();
+           dfs.sccmaprev=new HashMap();
            dfs.finishingorder=new Vector();
-           boolean hascycles=dfs.go();
+           boolean acyclic=dfs.go();
             for (Iterator it = nodes.iterator();it.hasNext();) {
                 GraphNode gn = (GraphNode) it.next();
                 gn.resetscc();
@@ -377,18 +418,18 @@ public class GraphNode {
                    dfs.sccindex++; /* Increment scc index */
                }
            }
-           return new SCC(hascycles,dfs.sccmap,dfs.sccindex);
+           return new SCC(acyclic,dfs.sccmap,dfs.sccmaprev,dfs.sccindex);
        }
 
        void dfsprev(GraphNode gn) {
            if (gn.getStatus()==FINISHED||!nodes.contains(gn))
                return;
            gn.setStatus(FINISHED);
-           gn.setSCC(sccindex);
            Integer i=new Integer(sccindex);
            if (!sccmap.containsKey(i))
                sccmap.put(i,new HashSet());
            ((Set)sccmap.get(i)).add(gn);
+           sccmaprev.put(gn,i);
            for(Iterator edgeit=gn.inedges();edgeit.hasNext();) {
                Edge e=(Edge)edgeit.next();
                GraphNode gn2=e.getSource();
@@ -400,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;
         }