2 bug fixes to getRequiredConstraints
[repair.git] / Repair / RepairCompiler / MCC / IR / Termination.java
index ff9a293a7874e36a63ad491afbc1ccfcb7d2205b..8585655710d9b7362f79815640074d404bfb38f4 100755 (executable)
@@ -31,6 +31,12 @@ public class Termination {
     ExactSize exactsize;
     ArrayAnalysis arrayanalysis;
     Sources sources;
+    static String conjoption="style=bold";
+    static String absrepoption="shape=box,color=blue,style=bold";
+    static String updateoption="shape=box,color=red,style=bold";
+    static String scopeoption="color=brown";
+    static String conseqoption="style=bold,color=green";
+    static String compoption="shape=box,color=purple,style=bold";
 
     public Termination(State state) {
        this.state=state;
@@ -67,20 +73,27 @@ public class Termination {
        generateconjunctionnodes();
        constraintdependence=new ConstraintDependence(state,this);
 
+        debugmsg("*****Generating scope nodes*****");
        generatescopenodes();
+        debugmsg("*****Generating repair nodes*****");
        generaterepairnodes();
+        debugmsg("*****Generating data structure nodes*****");
        generatedatastructureupdatenodes();
-       generatecompensationnodes();
-
+        debugmsg("*****Generating compensation nodes*****");
+       if (!Compiler.OMITCOMP)
+           generatecompensationnodes();
+        debugmsg("*****Generating abstract edges*****");
        generateabstractedges();
+        debugmsg("*****Generating scope edges*****");
        generatescopeedges();
+        debugmsg("*****Generating update edges*****");
        generateupdateedges();
 
 
        HashSet superset=new HashSet();
        superset.addAll(conjunctions);
        HashSet closureset=new HashSet();
-
+        debugmsg("Computing closure");
        GraphNode.computeclosure(superset,closureset);
        try {
            GraphNode.DOTVisitor.visit(new FileOutputStream("graph.dot"),superset);
@@ -164,6 +177,7 @@ public class Termination {
                GraphNode gn=new GraphNode("Conj"+i+"A"+j,
                                           "Conj ("+i+","+j+") "+dnf.get(j).name()
                                           ,tn);
+               gn.setOption(conjoption);
                conjunctions.add(gn);
                if (!conjunctionmap.containsKey(c))
                    conjunctionmap.put(c,new HashSet());
@@ -184,6 +198,7 @@ public class Termination {
                    GraphNode gn=new GraphNode("Conj"+i+"AQ"+j,
                                               "Conj ("+i+","+j+") "+dconst.get(0).name()
                                               ,tn);
+                   gn.setOption(conjoption);
                    conjunctions.add(gn);
                    if (!conjunctionmap.containsKey(c))
                        conjunctionmap.put(c,new HashSet());
@@ -201,6 +216,7 @@ public class Termination {
                    GraphNode gn=new GraphNode("Conj"+i+"AQ"+j,
                                               "Conj ("+i+","+j+") "+dconst.get(0).name()
                                               ,tn);
+                   gn.setOption(conjoption);
                    conjunctions.add(gn);
                    if (!conjunctionmap.containsKey(c))
                        conjunctionmap.put(c,new HashSet());
@@ -227,22 +243,22 @@ public class Termination {
                            TupleOfExpr toe=(TupleOfExpr)e;
                            if (negated) {
                                GraphNode agn=(GraphNode)abstractremove.get(toe.relation);
-                               GraphNode.Edge edge=new GraphNode.Edge("requires",agn);
+                               GraphNode.Edge edge=new GraphNode.Edge("requirestupleremove",agn);
                                gn.addEdge(edge);
                            } else {
                                GraphNode agn=(GraphNode)abstractadd.get(toe.relation);
-                               GraphNode.Edge edge=new GraphNode.Edge("requires",agn);
+                               GraphNode.Edge edge=new GraphNode.Edge("requirestupleadd",agn);
                                gn.addEdge(edge);
                            }
                        } else if (e instanceof ElementOfExpr) {
                            ElementOfExpr eoe=(ElementOfExpr)e;
                            if (negated) {
                                GraphNode agn=(GraphNode)abstractremove.get(eoe.set);
-                               GraphNode.Edge edge=new GraphNode.Edge("requires",agn);
+                               GraphNode.Edge edge=new GraphNode.Edge("requireselementremove",agn);
                                gn.addEdge(edge);
                            } else {
                                GraphNode agn=(GraphNode)abstractadd.get(eoe.set);
-                               GraphNode.Edge edge=new GraphNode.Edge("requires",agn);
+                               GraphNode.Edge edge=new GraphNode.Edge("requireselementadd",agn);
                                gn.addEdge(edge);
                            }
                        } else throw new Error("Unrecognized Abstract Update");
@@ -252,14 +268,20 @@ public class Termination {
            /* Cycle through the rules to look for possible conflicts */
            for(int i=0;i<state.vRules.size();i++) {
                Rule r=(Rule) state.vRules.get(i);
+                if (Compiler.DEBUGGRAPH) {
+                    System.out.println(gn.getLabel()+"--->"+((GraphNode)scopesatisfy.get(r)).getLabel());
+                }
                if (concreteinterferes.interferes(mun,r,true)) {
                    GraphNode scopenode=(GraphNode)scopesatisfy.get(r);
-                   GraphNode.Edge e=new GraphNode.Edge("interferes",scopenode);
+                   GraphNode.Edge e=new GraphNode.Edge("satisfyscopeinterferes",scopenode);
                    gn.addEdge(e);
                }
+                if (Compiler.DEBUGGRAPH) {
+                    System.out.println(gn.getLabel()+"--->"+((GraphNode)scopefalsify.get(r)).getLabel());
+                }
                if (concreteinterferes.interferes(mun,r,false)) {
                    GraphNode scopenode=(GraphNode)scopefalsify.get(r);
-                   GraphNode.Edge e=new GraphNode.Edge("interferes",scopenode);
+                   GraphNode.Edge e=new GraphNode.Edge("falsifyscopeinterferes",scopenode);
                    gn.addEdge(e);
                }
            }
@@ -282,17 +304,17 @@ public class Termination {
                if (abstractinterferes.checkrelationconstraint(ar, cons))
                    continue;
                 if (AbstractInterferes.interferesquantifier(ar,cons)) {
-                    GraphNode.Edge e=new GraphNode.Edge("interferes",gn2);
+                    GraphNode.Edge e=new GraphNode.Edge("interferesquantifier",gn2);
                     gn.addEdge(e);
                 } else {
                     for(int i=0;i<conj.size();i++) {
                         DNFPredicate dp=conj.get(i);
-                        if (getConstraint(gn)!=null&&
-                            abstractinterferes.interferemodifies(ar,getConstraint(gn),dp,cons))
-                            continue;
 
-                        if (abstractinterferes.interfereswithpredicate(ar,dp)) {
-                            GraphNode.Edge e=new GraphNode.Edge("interferes",gn2);
+                        if (!abstractinterferes.interfereswithpredicate(ar,dp))
+                            continue;
+                        if (getConstraint(gn)==null||
+                            !abstractinterferes.interferemodifies(ar,getConstraint(gn),dp,cons)) {
+                            GraphNode.Edge e=new GraphNode.Edge("interferesmodify",gn2);
                             gn.addEdge(e);
                             break;
                         }
@@ -304,7 +326,7 @@ public class Termination {
                TermNode tn2=(TermNode)gn2.getOwner();
                ScopeNode sn2=tn2.getScope();
                if (AbstractInterferes.interfereswithrule(ar,sn2.getRule(),sn2.getSatisfy())) {
-                   GraphNode.Edge e=new GraphNode.Edge("interferes",gn2);
+                   GraphNode.Edge e=new GraphNode.Edge("interfereswithrule",gn2);
                    gn.addEdge(e);
                }
            }
@@ -329,9 +351,13 @@ public class Termination {
            ScopeNode satisfy=new ScopeNode(r,true);
            TermNode tnsatisfy=new TermNode(satisfy);
            GraphNode gnsatisfy=new GraphNode("SatisfyRule"+i,tnsatisfy);
+           gnsatisfy.setOption(scopeoption);
+            if (Compiler.MERGENODES)
+                gnsatisfy.setMerge();
            ConsequenceNode cnsatisfy=new ConsequenceNode();
            TermNode ctnsatisfy=new TermNode(cnsatisfy);
            GraphNode cgnsatisfy=new GraphNode("ConseqSatisfyRule"+i,ctnsatisfy);
+           cgnsatisfy.setOption(conseqoption);
            consequence.put(satisfy,cgnsatisfy);
            GraphNode.Edge esat=new GraphNode.Edge("consequencesatisfy"+i,cgnsatisfy);
            gnsatisfy.addEdge(esat);
@@ -342,9 +368,13 @@ public class Termination {
            ScopeNode falsify=new ScopeNode(r,false);
            TermNode tnfalsify=new TermNode(falsify);
            GraphNode gnfalsify=new GraphNode("FalsifyRule"+i,tnfalsify);
+           gnfalsify.setOption(scopeoption);
+            if (Compiler.MERGENODES)
+                gnfalsify.setMerge();
            ConsequenceNode cnfalsify=new ConsequenceNode();
            TermNode ctnfalsify=new TermNode(cnfalsify);
            GraphNode cgnfalsify=new GraphNode("ConseqFalsifyRule"+i,ctnfalsify);
+           cgnfalsify.setOption(conseqoption);
            consequence.put(falsify,cgnfalsify);
            GraphNode.Edge efals=new GraphNode.Edge("consequencefalsify"+i,cgnfalsify);
            gnfalsify.addEdge(efals);
@@ -370,7 +400,7 @@ public class Termination {
                    DNFPredicate dp=conj.get(i);
                    if (abstractinterferes.scopeinterfereswithpredicate(sn,dp)||
                        AbstractInterferes.interfereswithquantifier(sn.getDescriptor(),sn.getSatisfy(),constr)) {
-                       GraphNode.Edge e=new GraphNode.Edge("interferes",gn2);
+                       GraphNode.Edge e=new GraphNode.Edge("interferesconjunction",gn2);
                        GraphNode gnconseq=(GraphNode)consequence.get(sn);
                        gnconseq.addEdge(e);
                        break;
@@ -383,13 +413,13 @@ public class Termination {
                Rule r=(Rule) state.vRules.get(i);
                if (AbstractInterferes.interfereswithrule(sn.getDescriptor(),sn.getSatisfy(),r,true)) {
                    GraphNode scopenode=(GraphNode)scopesatisfy.get(r);
-                   GraphNode.Edge e=new GraphNode.Edge("interferes",scopenode);
+                   GraphNode.Edge e=new GraphNode.Edge("interferesscopesatisfy2",scopenode);
                    GraphNode gnconseq=(GraphNode)consequence.get(sn);
                    gnconseq.addEdge(e);
                }
                if (AbstractInterferes.interfereswithrule(sn.getDescriptor(),sn.getSatisfy(),r,false)) {
                    GraphNode scopenode=(GraphNode)scopefalsify.get(r);
-                   GraphNode.Edge e=new GraphNode.Edge("interferes",scopenode);
+                   GraphNode.Edge e=new GraphNode.Edge("interferesscopefalsify2",scopenode);
                    GraphNode gnconseq=(GraphNode)consequence.get(sn);
                    gnconseq.addEdge(e);
                }
@@ -434,10 +464,40 @@ public class Termination {
                }
 
                for(int j=0;j<array.length;j++) {
+                    if (array[j]==AbstractRepair.ADDTOSET) {
+
+                        System.out.println("1");
+                        if ((dp.getPredicate() instanceof ExprPredicate)&&
+                            (((ExprPredicate)dp.getPredicate()).getType()==ExprPredicate.SIZE)) {
+                            System.out.println("2");
+                            boolean neg=dp.isNegated();
+                            Opcode op=((ExprPredicate)dp.getPredicate()).getOp();
+                            int size=((ExprPredicate)dp.getPredicate()).rightSize();
+                            op=Opcode.translateOpcode(neg,op);
+                            Descriptor des=((ExprPredicate)dp.getPredicate()).getDescriptor();
+                            if (des instanceof SetDescriptor) {
+                                System.out.println("3");
+
+                                int minsize=exactsize.minSize((SetDescriptor)des);
+                                Constraint reqc=exactsize.ensuresMinSize((SetDescriptor)des);
+                                if (((size==minsize)&&(op==Opcode.EQ))||
+                                    ((size<=minsize)&&(op==Opcode.GE))||
+                                    ((size<minsize)&&(op==Opcode.GT))) {
+                                    System.out.println("4");
+                                    constraintdependence.requiresConstraint(gn,reqc);
+                                    //force good ordering
+                                    continue; //no repair action needed here...
+                                }
+                            }
+                        }
+                    }
+
                    AbstractRepair ar=new AbstractRepair(dp,array[j],d,sources);
                    TermNode tn2=new TermNode(ar);
-                   GraphNode gn2=new GraphNode(gn.getLabel()+"A"+i+"B"+ar.type(),gn.getTextLabel()+" #"+i+" "+ar.type(),tn2);
-                   GraphNode.Edge e=new GraphNode.Edge("abstract",gn2);
+                   //              GraphNode gn2=new GraphNode(gn.getLabel()+"A"+i+"B"+ar.type(),gn.getTextLabel()+" #"+i+" "+ar.type(),tn2);
+                   GraphNode gn2=new GraphNode(gn.getLabel()+"A"+i+"B"+ar.type(),dp.name()+" "+ar.type(),tn2);
+                   gn2.setOption(absrepoption);
+                   GraphNode.Edge e=new GraphNode.Edge("abstractedge1",gn2);
                    gn.addEdge(e);
                    if (!predtoabstractmap.containsKey(dp))
                        predtoabstractmap.put(dp,new HashSet());
@@ -457,6 +517,7 @@ public class Termination {
            AbstractRepair ar=new AbstractRepair(tp, AbstractRepair.ADDTOSET, sd,sources);
            TermNode tn=new TermNode(ar);
            GraphNode gn=new GraphNode("AbstractAddSetRule"+i,tn);
+           gn.setOption(absrepoption);
            if (!predtoabstractmap.containsKey(tp))
                predtoabstractmap.put(tp,new HashSet());
            ((Set)predtoabstractmap.get(tp)).add(gn);
@@ -468,6 +529,7 @@ public class Termination {
            AbstractRepair ar2=new AbstractRepair(tp2, AbstractRepair.REMOVEFROMSET, sd,sources);
            TermNode tn2=new TermNode(ar2);
            GraphNode gn2=new GraphNode("AbstractRemSetRule"+i,tn2);
+           gn2.setOption(absrepoption);
            if (!predtoabstractmap.containsKey(tp2))
                predtoabstractmap.put(tp2,new HashSet());
            ((Set)predtoabstractmap.get(tp2)).add(gn2);
@@ -488,6 +550,7 @@ public class Termination {
            AbstractRepair ar=new AbstractRepair(tp, AbstractRepair.ADDTORELATION, rd,sources);
            TermNode tn=new TermNode(ar);
            GraphNode gn=new GraphNode("AbstractAddRelRule"+i,tn);
+           gn.setOption(absrepoption);
            if (!predtoabstractmap.containsKey(tp))
                predtoabstractmap.put(tp,new HashSet());
            ((Set)predtoabstractmap.get(tp)).add(gn);
@@ -499,6 +562,7 @@ public class Termination {
            AbstractRepair ar2=new AbstractRepair(tp2, AbstractRepair.REMOVEFROMRELATION, rd,sources);
            TermNode tn2=new TermNode(ar2);
            GraphNode gn2=new GraphNode("AbstractRemRelRule"+i,tn2);
+           gn2.setOption(absrepoption);
            if (!predtoabstractmap.containsKey(tp2))
                predtoabstractmap.put(tp2,new HashSet());
            ((Set)predtoabstractmap.get(tp2)).add(gn2);
@@ -521,6 +585,7 @@ public class Termination {
                MultUpdateNode mun=new MultUpdateNode(sn);
                TermNode tn2=new TermNode(mun);
                GraphNode gn2=new GraphNode("CompRem"+compensationcount,tn2);
+               gn2.setOption(compoption);
                UpdateNode un=new UpdateNode(r);
 
                if (j<r.numQuantifiers()) {
@@ -534,7 +599,7 @@ public class Termination {
                        un.addUpdate(u);
                        if (abstractremove.containsKey(rq.relation)) {
                            GraphNode agn=(GraphNode)abstractremove.get(rq.relation);
-                           GraphNode.Edge e=new GraphNode.Edge("requires",agn);
+                           GraphNode.Edge e=new GraphNode.Edge("requiresremove1",agn);
                            gn2.addEdge(e);
                        } else {
                            continue; /* Abstract repair doesn't exist */
@@ -547,7 +612,7 @@ public class Termination {
                        un.addUpdate(u);
                        if (abstractremove.containsKey(sq.set)) {
                            GraphNode agn=(GraphNode)abstractremove.get(sq.set);
-                           GraphNode.Edge e=new GraphNode.Edge("requires",agn);
+                           GraphNode.Edge e=new GraphNode.Edge("requiresremove2",agn);
                            gn2.addEdge(e);
                        } else {
                            continue; /* Abstract repair doesn't exist */
@@ -562,11 +627,11 @@ public class Termination {
                        continue;
                    }
                }
-               if (!un.checkupdates()) /* Make sure we have a good update */
+               if (!un.checkupdates(state)) /* Make sure we have a good update */
                    continue;
 
                mun.addUpdate(un);
-               GraphNode.Edge e=new GraphNode.Edge("abstract"+compensationcount,gn2);
+               GraphNode.Edge e=new GraphNode.Edge("abstractcomp"+compensationcount,gn2);
                compensationcount++;
                gn.addEdge(e);
                updatenodes.add(gn2);
@@ -626,6 +691,7 @@ public class Termination {
            MultUpdateNode mun=new MultUpdateNode(ar,MultUpdateNode.REMOVE);
            TermNode tn=new TermNode(mun);
            GraphNode gn2=new GraphNode("UpdateRem"+removefromcount,tn);
+           gn2.setOption(updateoption);
 
            boolean goodflag=true;
            for(int i=0;i<possiblerules.size();i++) {
@@ -643,7 +709,7 @@ public class Termination {
                        un.addUpdate(u);
                        if (abstractremove.containsKey(rq.relation)) {
                            GraphNode agn=(GraphNode)abstractremove.get(rq.relation);
-                           GraphNode.Edge e=new GraphNode.Edge("requires",agn);
+                           GraphNode.Edge e=new GraphNode.Edge("requiresremove3",agn);
                            gn2.addEdge(e);
                        } else {
                            goodflag=false;break; /* Abstract repair doesn't exist */
@@ -656,7 +722,7 @@ public class Termination {
                        un.addUpdate(u);
                        if (abstractremove.containsKey(sq.set)) {
                            GraphNode agn=(GraphNode)abstractremove.get(sq.set);
-                           GraphNode.Edge e=new GraphNode.Edge("requires",agn);
+                           GraphNode.Edge e=new GraphNode.Edge("requiresremove4",agn);
                            gn2.addEdge(e);
                        } else {
                            goodflag=false;break; /* Abstract repair doesn't exist */
@@ -668,14 +734,14 @@ public class Termination {
                        goodflag=false;break;
                    }
                }
-               if (!un.checkupdates()) {
+               if (!un.checkupdates(state)) {
                    goodflag=false;
                    break;
                }
                mun.addUpdate(un);
            }
            if (goodflag) {
-               GraphNode.Edge e=new GraphNode.Edge("abstract"+(removefromcount++),gn2);
+               GraphNode.Edge e=new GraphNode.Edge("abstract3"+(removefromcount++),gn2);
                gn.addEdge(e);
                updatenodes.add(gn2);
            }
@@ -741,6 +807,7 @@ public class Termination {
            MultUpdateNode mun=new MultUpdateNode(ar,MultUpdateNode.MODIFY);
            TermNode tn=new TermNode(mun);
            GraphNode gn2=new GraphNode("UpdateMod"+modifycount,tn);
+           gn2.setOption(updateoption);
 
            boolean goodflag=true;
            for(int i=0;i<possiblerules.size();i++) {
@@ -788,14 +855,14 @@ public class Termination {
                        goodflag=false;
                }
 
-               if (!un.checkupdates()) {
+               if (!un.checkupdates(state)) {
                    goodflag=false;
                    break;
                }
                mun.addUpdate(un);
            }
            if (goodflag) {
-               GraphNode.Edge e=new GraphNode.Edge("abstract"+modifycount,gn2);
+               GraphNode.Edge e=new GraphNode.Edge("abstract4"+modifycount,gn2);
                modifycount++;
                gn.addEdge(e);
                updatenodes.add(gn2);
@@ -847,7 +914,6 @@ public class Termination {
                                continue endloop;
                            e=ce.getExpr();
                        }
-
                        if (!(e instanceof VarExpr)) {
                            if (e.isValue(si.getSet().getType())) {
                                Updates up=new Updates(e,0,si.getSet().getType());
@@ -969,16 +1035,17 @@ public class Termination {
                    MultUpdateNode mun=new MultUpdateNode(ar,MultUpdateNode.ADD);
                    TermNode tn=new TermNode(mun);
                    GraphNode gn2=new GraphNode("UpdateAdd"+addtocount,tn);
+                   gn2.setOption(updateoption);
 
                    if (debugmsg("Start processing quantifiers")&&
                        processquantifiers(gn2,un, r,setmapping)&&
                        debugmsg("Finished processing quantifiers")&&
                        processconjunction(un,ruleconj,setmapping)&&
                        debugmsg("Finished processing conjunction")&&
-                       un.checkupdates()&&
+                       un.checkupdates(state)&&
                        debugmsg("Updates checked")) {
                        mun.addUpdate(un);
-                       GraphNode.Edge e=new GraphNode.Edge("abstract"+addtocount,gn2);
+                       GraphNode.Edge e=new GraphNode.Edge("abstract5"+addtocount,gn2);
                        addtocount++;
                        gn.addEdge(e);
                        updatenodes.add(gn2);
@@ -1056,7 +1123,7 @@ public class Termination {
                    if ((e instanceof VarExpr)&&
                        (((VarExpr)e).getVar()==vd)) {
                        /* Can solve for v */
-                       if (!si.getSet().getType().isSubtypeOf(set.getType()))
+                       if (set==null||!si.getSet().getType().isSubtypeOf(set.getType()))
                            return false;
                        Binding binding=new Binding(vd,0);
                        bindings.add(binding);
@@ -1232,7 +1299,7 @@ public class Termination {
                un.addUpdate(u);
                if (abstractadd.containsKey(rq.relation)) {
                    GraphNode agn=(GraphNode)abstractadd.get(rq.relation);
-                   GraphNode.Edge e=new GraphNode.Edge("requires",agn);
+                   GraphNode.Edge e=new GraphNode.Edge("requiresadd5",agn);
                    gn.addEdge(e);
                } else {
                    return false;
@@ -1256,7 +1323,7 @@ public class Termination {
                un.addUpdate(u);
                if (abstractadd.containsKey(sq.set)) {
                    GraphNode agn=(GraphNode)abstractadd.get(sq.set);
-                   GraphNode.Edge e=new GraphNode.Edge("requires",agn);
+                   GraphNode.Edge e=new GraphNode.Edge("requiresadd6",agn);
                    gn.addEdge(e);
                } else {
                    return false;