From: bdemsky Date: Thu, 1 Apr 2004 16:23:06 +0000 (+0000) Subject: Added Strongly Connected Component support into GraphNodes. X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=6460157f14fd4a96e59946c74267200a243c9951;p=repair.git Added Strongly Connected Component support into GraphNodes. --- diff --git a/Repair/RepairCompiler/MCC/IR/GraphAnalysis.java b/Repair/RepairCompiler/MCC/IR/GraphAnalysis.java index aebb865..96a8645 100755 --- a/Repair/RepairCompiler/MCC/IR/GraphAnalysis.java +++ b/Repair/RepairCompiler/MCC/IR/GraphAnalysis.java @@ -118,16 +118,45 @@ public class GraphAnalysis { for(Iterator it=termination.conjunctions.iterator();it.hasNext();) { GraphNode gn=(GraphNode)it.next(); - if (!cantremove.contains(gn)) { - cantremove.add(gn); - Set cycle=GraphNode.findcycles(cantremove); - if (cycle.contains(gn)) { - if (!mustremove.contains(gn)) { - change=true; - mustremove.add(gn); + boolean foundnocycle=false; + + for (Iterator edgeit=gn.edges();edgeit.hasNext();) { + GraphNode.Edge e=(GraphNode.Edge)edgeit.next(); + GraphNode gn2=e.getTarget(); + TermNode tn2=(TermNode)gn2.getOwner(); + if (tn2.getType()!=TermNode.ABSTRACT) + continue; + for (Iterator edgeit2=gn2.edges();edgeit2.hasNext();) { + GraphNode.Edge e2=(GraphNode.Edge)edgeit2.next(); + GraphNode gn3=e2.getTarget(); + TermNode tn3=(TermNode)gn3.getOwner(); + if (tn3.getType()!=TermNode.UPDATE) + continue; + boolean containsgn=cantremove.contains(gn); + boolean containsgn3=cantremove.contains(gn3); + cantremove.add(gn); + cantremove.add(gn3); + Set cycle=GraphNode.findcycles(cantremove); + if (cycle.contains(gn3)) { + if (!mustremove.contains(gn3)) { + change=true; + mustremove.add(gn3); + } } + if (!mustremove.contains(gn3)&&!cycle.contains(gn)) { + foundnocycle=true; + } + if (!containsgn) + cantremove.remove(gn); + if (!containsgn3) + cantremove.remove(gn3); + } + } + if(!foundnocycle) { + if (!mustremove.contains(gn)) { + change=true; + mustremove.add(gn); } - cantremove.remove(gn); } } couldremove.removeAll(mustremove); @@ -140,7 +169,8 @@ public class GraphAnalysis { continue; //recalculate System.out.println("Searching set of "+couldremove.size()+" nodes."); - System.out.println("Eliminated "+mustremove.size()+" nodes"); + System.out.println("Eliminated must "+mustremove.size()+" nodes"); + System.out.println("Eliminated cant "+cantremove.size()+" nodes"); System.out.println("Searching following set:"); for(Iterator it=couldremove.iterator();it.hasNext();) { GraphNode gn=(GraphNode)it.next(); diff --git a/Repair/RepairCompiler/MCC/IR/GraphNode.java b/Repair/RepairCompiler/MCC/IR/GraphNode.java index f5448e3..de8fe32 100755 --- a/Repair/RepairCompiler/MCC/IR/GraphNode.java +++ b/Repair/RepairCompiler/MCC/IR/GraphNode.java @@ -25,6 +25,7 @@ public class GraphNode { private String label; private GraphNode target; + private GraphNode source; private String dotnodeparams = new String(); public Edge(String label, GraphNode target) { @@ -36,6 +37,14 @@ public class GraphNode { return label; } + public void setSource(GraphNode s) { + this.source=s; + } + + public GraphNode getSource() { + return source; + } + public GraphNode getTarget() { return target; } @@ -55,7 +64,10 @@ public class GraphNode { int discoverytime = -1; int finishingtime = -1; /* used for searches */ + int scc = -1; + Vector edges = new Vector(); + Vector inedges = new Vector(); String nodelabel; String textlabel; NodeStatus status = UNVISITED; @@ -136,29 +148,51 @@ public class GraphNode { return edges.iterator(); } + public Iterator inedges() { + return inedges.iterator(); + } + public void addEdge(Edge newedge) { + newedge.setSource(this); edges.addElement(newedge); + GraphNode tonode=newedge.getTarget(); + tonode.inedges.addElement(newedge); } - public void reset() { - discoverytime = -1; - finishingtime = -1; - status = UNVISITED; + void reset() { + discoverytime = -1; + finishingtime = -1; + status = UNVISITED; } - public void discover(int time) { - discoverytime = time; - status = PROCESSING; + void resetscc() { + status = UNVISITED; + scc = -1; } - public void finish(int time) { + public int getSCC() { + return scc; + } + + void setSCC(int s) { + scc=s; + } + + void discover(int time) { + discoverytime = time; + status = PROCESSING; + } + + void finish(int time) { assert status == PROCESSING; - finishingtime = time; + finishingtime = time; status = FINISHED; } + /** Returns finishing time for dfs */ + public int getFinishingTime() { - return finishingtime; + return finishingtime; } @@ -270,18 +304,98 @@ public class GraphNode { return true; /* found cycle */ } + public static class SCC { + boolean hascycles; + HashMap map; + int numscc; + public SCC(boolean hascycles, HashMap map,int numscc) { + this.hascycles=hascycles; + this.map=map; + this.numscc=numscc; + } + + public boolean hasCycles() { + return hascycles; + } + + public int numSCC() { + return numscc; + } + + public Set getSCC(int i) { + Integer scc=new Integer(i); + return (Set)map.get(scc); + } + + boolean hascycle(int i) { + Integer scc=new Integer(i); + Set s=(Set)map.get(scc); + if (s.size()>1) + return true; + Object [] array=s.toArray(); + GraphNode gn=(GraphNode)array[0]; + for(Iterator it=gn.edges();it.hasNext();) { + Edge e=(Edge)it.next(); + if (e.getTarget()==gn) + return true; /* Self Cycle */ + } + return false; + } + } + /** * DFS encapsulates the depth first search algorithm */ public static class DFS { int time = 0; + int sccindex = 0; Collection nodes; + Vector finishingorder=null; + HashMap sccmap; private DFS(Collection nodes) { this.nodes = nodes; } + public static SCC computeSCC(Collection nodes) { + if (nodes==null) { + throw new NullPointerException(); + } + DFS dfs=new DFS(nodes); + dfs.sccmap=new HashMap(); + dfs.finishingorder=new Vector(); + boolean hascycles=dfs.go(); + for (Iterator it = nodes.iterator();it.hasNext();) { + GraphNode gn = (GraphNode) it.next(); + gn.resetscc(); + } + for(int i=dfs.finishingorder.size()-1;i>=0;i--) { + GraphNode gn=(GraphNode)dfs.finishingorder.get(i); + if (gn.getStatus() == UNVISITED) { + dfs.dfsprev(gn); + dfs.sccindex++; /* Increment scc index */ + } + } + return new SCC(hascycles,dfs.sccmap,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); + for(Iterator edgeit=gn.inedges();edgeit.hasNext();) { + Edge e=(Edge)edgeit.next(); + GraphNode gn2=e.getSource(); + dfsprev(gn2); + } + } + public static boolean depthFirstSearch(Collection nodes) { if (nodes == null) { throw new NullPointerException(); @@ -315,7 +429,7 @@ public class GraphNode { private boolean dfs(GraphNode gn) { boolean acyclic=true; - gn.discover(time++); + gn.discover(time++); Iterator edges = gn.edges(); while (edges.hasNext()) { @@ -330,7 +444,8 @@ public class GraphNode { acyclic=false; } } - + if (finishingorder!=null) + finishingorder.add(gn); gn.finish(time++); return acyclic; } diff --git a/Repair/RepairCompiler/MCC/IR/ImplicitSchema.java b/Repair/RepairCompiler/MCC/IR/ImplicitSchema.java index b00c06b..4e34613 100755 --- a/Repair/RepairCompiler/MCC/IR/ImplicitSchema.java +++ b/Repair/RepairCompiler/MCC/IR/ImplicitSchema.java @@ -27,6 +27,8 @@ public class ImplicitSchema { boolean needDR(RelationDescriptor rd,boolean isdomain) { Vector rules=state.vRules; SetDescriptor sd=isdomain?rd.getDomain():rd.getRange(); + if (sd instanceof ReservedSetDescriptor) + return false; for(int i=0;i