55f49522eb3b8d4d0ab834ffe628749282435816
[repair.git] / Repair / RepairCompiler / MCC / IR / Termination.java
1 package MCC.IR;
2 import java.util.*;
3 import java.io.*;
4 import MCC.State;
5 import MCC.Compiler;
6
7 public class Termination {
8     HashSet conjunctions;
9     Hashtable conjunctionmap;
10
11     HashSet abstractrepair;
12     HashSet abstractrepairadd;
13
14     HashSet updatenodes;
15     HashSet consequencenodes;
16
17     HashSet scopenodes;
18     Hashtable scopesatisfy;
19     Hashtable scopefalsify;
20     Hashtable consequence;
21     Hashtable abstractadd;
22     Hashtable abstractremove;
23     Hashtable conjtonodemap;
24     Hashtable predtoabstractmap;
25     Set removedset;
26
27     State state;
28
29     public Termination(State state) {
30         this.state=state;
31         conjunctions=new HashSet();
32         conjunctionmap=new Hashtable();
33         abstractrepair=new HashSet();
34         abstractrepairadd=new HashSet();
35         scopenodes=new HashSet();
36         scopesatisfy=new Hashtable();
37         scopefalsify=new Hashtable();
38         consequence=new Hashtable();
39         updatenodes=new HashSet();
40         consequencenodes=new HashSet();
41         abstractadd=new Hashtable();
42         abstractremove=new Hashtable();
43         conjtonodemap=new Hashtable();
44         predtoabstractmap=new Hashtable();
45         if (!Compiler.REPAIR)
46             return;
47
48         generateconjunctionnodes();
49         generatescopenodes();
50         generaterepairnodes();
51         generatedatastructureupdatenodes();
52         generatecompensationnodes();
53
54         generateabstractedges();
55         generatescopeedges();
56         generateupdateedges();
57
58         HashSet superset=new HashSet();
59         superset.addAll(conjunctions);
60         HashSet closureset=new HashSet();
61         //      closureset.addAll(updatenodes);
62         //superset.addAll(abstractrepair);
63         //superset.addAll(updatenodes);
64         //superset.addAll(scopenodes);
65         //superset.addAll(consequencenodes);
66         GraphNode.computeclosure(superset,closureset);
67         try {
68             GraphNode.DOTVisitor.visit(new FileOutputStream("graph.dot"),superset);
69         } catch (Exception e) {
70             e.printStackTrace();
71             System.exit(-1);
72         }
73         for(Iterator it=updatenodes.iterator();it.hasNext();) {
74             GraphNode gn=(GraphNode)it.next();
75             TermNode tn=(TermNode)gn.getOwner();
76             MultUpdateNode mun=tn.getUpdate();
77             System.out.println(gn.getTextLabel());
78             System.out.println(mun.toString());
79         }
80         GraphAnalysis ga=new GraphAnalysis(this);
81         removedset=ga.doAnalysis();
82         if (removedset==null) {
83             System.out.println("Can't generate terminating repair algorithm!");
84             System.exit(-1);
85         }
86         System.out.println("Removing:");
87         for(Iterator it=removedset.iterator();it.hasNext();) {
88             GraphNode gn=(GraphNode)it.next();
89             System.out.println(gn.getTextLabel());
90         }
91
92         superset=new HashSet();
93         superset.addAll(conjunctions);
94         superset.removeAll(removedset);
95         GraphNode.computeclosure(superset,removedset);
96         try {
97             GraphNode.DOTVisitor.visit(new FileOutputStream("graphfinal.dot"),superset);
98         } catch (Exception e) {
99             e.printStackTrace();
100             System.exit(-1);
101         }
102
103     }
104     
105     void generateconjunctionnodes() {
106         Vector constraints=state.vConstraints;
107         for(int i=0;i<constraints.size();i++) {
108             Constraint c=(Constraint)constraints.get(i);
109             DNFConstraint dnf=c.dnfconstraint;
110             for(int j=0;j<dnf.size();j++) {
111                 TermNode tn=new TermNode(c,dnf.get(j));
112                 GraphNode gn=new GraphNode("Conj"+i+"A"+j,
113                                            "Conj ("+i+","+j+") "+dnf.get(j).name()
114                                            ,tn);
115                 conjunctions.add(gn);
116                 if (!conjunctionmap.containsKey(c))
117                     conjunctionmap.put(c,new HashSet());
118                 ((Set)conjunctionmap.get(c)).add(gn);
119                 conjtonodemap.put(dnf.get(j),gn);
120             }
121             for(int j=0;j<c.numQuantifiers();j++) {
122                 Quantifier q=c.getQuantifier(j);
123                 if (q instanceof SetQuantifier) {
124                     SetQuantifier sq=(SetQuantifier)q;
125                     VarExpr ve=new VarExpr(sq.getVar());
126                     InclusionPredicate ip=new InclusionPredicate(ve,new SetExpr(sq.getSet()));
127                     DNFConstraint dconst=new DNFConstraint(ip);
128                     dconst=dconst.not();
129                     TermNode tn=new TermNode(c,dconst.get(0));
130                     GraphNode gn=new GraphNode("Conj"+i+"AQ"+j,
131                                                "Conj ("+i+","+j+") "+dconst.get(0).name()
132                                                ,tn);
133                     conjunctions.add(gn);
134                     if (!conjunctionmap.containsKey(c))
135                         conjunctionmap.put(c,new HashSet());
136                     ((Set)conjunctionmap.get(c)).add(gn);
137                     conjtonodemap.put(dconst.get(0),gn);
138                 } else if (q instanceof RelationQuantifier) {
139                     RelationQuantifier rq=(RelationQuantifier)q;
140                     VarExpr ve=new VarExpr(rq.y);
141                     InclusionPredicate ip=new InclusionPredicate(ve,new ImageSetExpr(rq.x,rq.getRelation()));
142                     DNFConstraint dconst=new DNFConstraint(ip);
143                     dconst=dconst.not();
144                     TermNode tn=new TermNode(c,dconst.get(0));
145                     GraphNode gn=new GraphNode("Conj"+i+"AQ"+j,
146                                                "Conj ("+i+","+j+") "+dconst.get(0).name()
147                                                ,tn);
148                     conjunctions.add(gn);
149                     if (!conjunctionmap.containsKey(c))
150                         conjunctionmap.put(c,new HashSet());
151                     ((Set)conjunctionmap.get(c)).add(gn);
152                     conjtonodemap.put(dconst.get(0),gn);
153                 }
154             }
155         }
156     }
157
158     void generateupdateedges() {
159         for(Iterator updateiterator=updatenodes.iterator();updateiterator.hasNext();) {
160             GraphNode gn=(GraphNode)updateiterator.next();
161             TermNode tn=(TermNode)gn.getOwner();
162             MultUpdateNode mun=tn.getUpdate();
163             /* Cycle through the rules to look for possible conflicts */
164             for(int i=0;i<state.vRules.size();i++) {
165                 Rule r=(Rule) state.vRules.get(i);  
166                 if (ConcreteInterferes.interferes(mun,r,true)) {
167                     GraphNode scopenode=(GraphNode)scopesatisfy.get(r);
168                     GraphNode.Edge e=new GraphNode.Edge("interferes",scopenode);
169                     gn.addEdge(e);
170                 }
171                 if (ConcreteInterferes.interferes(mun,r,false)) {
172                     GraphNode scopenode=(GraphNode)scopefalsify.get(r);
173                     GraphNode.Edge e=new GraphNode.Edge("interferes",scopenode);
174                     gn.addEdge(e);
175                 }
176             }
177         }
178     }
179
180     void generateabstractedges() {
181         for(Iterator absiterator=abstractrepair.iterator();absiterator.hasNext();) {
182             GraphNode gn=(GraphNode)absiterator.next();
183             TermNode tn=(TermNode)gn.getOwner();
184             AbstractRepair ar=(AbstractRepair)tn.getAbstract();
185         
186             for(Iterator conjiterator=conjunctions.iterator();conjiterator.hasNext();) {
187                 GraphNode gn2=(GraphNode)conjiterator.next();
188                 TermNode tn2=(TermNode)gn2.getOwner();
189                 Conjunction conj=tn2.getConjunction();
190                 Constraint cons=tn2.getConstraint();
191
192                 for(int i=0;i<conj.size();i++) {
193                     DNFPredicate dp=conj.get(i);
194                     if (AbstractInterferes.interferes(ar,cons)||
195                         AbstractInterferes.interferes(ar,dp)) {
196                         GraphNode.Edge e=new GraphNode.Edge("interferes",gn2);
197                         gn.addEdge(e);
198                         break;
199                     }
200                 }
201             }
202
203             for(Iterator scopeiterator=scopenodes.iterator();scopeiterator.hasNext();) {
204                 GraphNode gn2=(GraphNode)scopeiterator.next();
205                 TermNode tn2=(TermNode)gn2.getOwner();
206                 ScopeNode sn2=tn2.getScope();
207                 if (AbstractInterferes.interferes(ar,sn2.getRule(),sn2.getSatisfy())) {
208                     GraphNode.Edge e=new GraphNode.Edge("interferes",gn2);
209                     gn.addEdge(e);
210                 }
211             }
212         }
213     }
214     
215     void generatescopeedges() {
216         for(Iterator scopeiterator=scopenodes.iterator();scopeiterator.hasNext();) {
217             GraphNode gn=(GraphNode)scopeiterator.next();
218             TermNode tn=(TermNode)gn.getOwner();
219             ScopeNode sn=tn.getScope();
220             
221             /* Interference edges with conjunctions */
222             for(Iterator conjiterator=conjunctions.iterator();conjiterator.hasNext();) {
223                 GraphNode gn2=(GraphNode)conjiterator.next();
224                 TermNode tn2=(TermNode)gn2.getOwner();
225                 Conjunction conj=tn2.getConjunction();
226                 Constraint constr=tn2.getConstraint();
227                 for(int i=0;i<conj.size();i++) {
228                     DNFPredicate dp=conj.get(i);
229                     if (AbstractInterferes.interferes(sn,dp)||
230                         AbstractInterferes.interferes(sn.getDescriptor(),sn.getSatisfy(),constr)) {
231                         GraphNode.Edge e=new GraphNode.Edge("interferes",gn2);
232                         GraphNode gnconseq=(GraphNode)consequence.get(sn);
233                         gnconseq.addEdge(e);
234                         break;
235                     }
236                 }
237             }
238
239             /* Now see if this could effect other model defintion rules */
240             for(int i=0;i<state.vRules.size();i++) {
241                 Rule r=(Rule) state.vRules.get(i);
242                 if (AbstractInterferes.interferes(sn.getDescriptor(),sn.getSatisfy(),r,true)) {
243                     GraphNode scopenode=(GraphNode)scopesatisfy.get(r);
244                     GraphNode.Edge e=new GraphNode.Edge("interferes",scopenode);
245                     GraphNode gnconseq=(GraphNode)consequence.get(sn);
246                     gnconseq.addEdge(e);
247                 }
248                 if (AbstractInterferes.interferes(sn.getDescriptor(),sn.getSatisfy(),r,false)) {
249                     GraphNode scopenode=(GraphNode)scopefalsify.get(r);
250                     GraphNode.Edge e=new GraphNode.Edge("interferes",scopenode);
251                     GraphNode gnconseq=(GraphNode)consequence.get(sn);
252                     gnconseq.addEdge(e);
253                 }
254             }
255         }
256     }
257
258     /* Generates the abstract repair nodes */
259     void generaterepairnodes() {
260         /* Generate repairs of conjunctions */
261         for(Iterator conjiterator=conjunctions.iterator();conjiterator.hasNext();) {
262             GraphNode gn=(GraphNode)conjiterator.next();
263             TermNode tn=(TermNode)gn.getOwner();
264             Conjunction conj=tn.getConjunction();
265             for(int i=0;i<conj.size();i++) {
266                 DNFPredicate dp=conj.get(i);
267                 int[] array=dp.getPredicate().getRepairs(dp.isNegated());
268                 Descriptor d=dp.getPredicate().getDescriptor();
269                 for(int j=0;j<array.length;j++) {
270                     AbstractRepair ar=new AbstractRepair(dp,array[j],d);
271                     TermNode tn2=new TermNode(ar);
272                     GraphNode gn2=new GraphNode(gn.getLabel()+"A"+i+"B"+ar.type(),gn.getTextLabel()+" #"+i+" "+ar.type(),tn2);
273                     GraphNode.Edge e=new GraphNode.Edge("abstract",gn2);
274                     gn.addEdge(e);
275                     if (!predtoabstractmap.containsKey(dp))
276                         predtoabstractmap.put(dp,new HashSet());
277                     ((Set)predtoabstractmap.get(dp)).add(gn2);
278                     abstractrepair.add(gn2);
279                 }
280             }
281         }
282         /* Generate additional abstract repairs */
283         Vector setdescriptors=state.stSets.getAllDescriptors();
284         for(int i=0;i<setdescriptors.size();i++) {
285             SetDescriptor sd=(SetDescriptor)setdescriptors.get(i);
286
287             /* XXXXXXX: Not sure what to do here */
288             VarExpr ve=new VarExpr("DUMMY");
289             InclusionPredicate ip=new InclusionPredicate(ve,new SetExpr(sd));
290             
291             DNFPredicate tp=new DNFPredicate(false,ip);
292             AbstractRepair ar=new AbstractRepair(tp, AbstractRepair.ADDTOSET, sd);
293             TermNode tn=new TermNode(ar);
294             GraphNode gn=new GraphNode("AbstractAddSetRule"+i,tn);
295             if (!predtoabstractmap.containsKey(tp))
296                 predtoabstractmap.put(tp,new HashSet());
297             ((Set)predtoabstractmap.get(tp)).add(gn);
298             abstractrepair.add(gn);
299             abstractrepairadd.add(gn);
300             abstractadd.put(sd,gn);
301             
302             DNFPredicate tp2=new DNFPredicate(true,ip);
303             AbstractRepair ar2=new AbstractRepair(tp2, AbstractRepair.REMOVEFROMSET, sd);
304             TermNode tn2=new TermNode(ar2);
305             GraphNode gn2=new GraphNode("AbstractRemSetRule"+i,tn2);
306             if (!predtoabstractmap.containsKey(tp2))
307                 predtoabstractmap.put(tp2,new HashSet());
308             ((Set)predtoabstractmap.get(tp2)).add(gn2);
309             abstractrepair.add(gn2);
310             abstractrepairadd.add(gn2);
311             abstractremove.put(sd,gn2);
312         }
313
314         Vector relationdescriptors=state.stRelations.getAllDescriptors();
315         for(int i=0;i<relationdescriptors.size();i++) {
316             RelationDescriptor rd=(RelationDescriptor)relationdescriptors.get(i);
317
318             /* XXXXXXX: Not sure what to do here */
319             VarDescriptor vd1=new VarDescriptor("DUMMY1");
320             VarExpr ve2=new VarExpr("DUMMY2");
321
322             InclusionPredicate ip=new InclusionPredicate(ve2,new ImageSetExpr(vd1, rd));
323             
324             DNFPredicate tp=new DNFPredicate(false,ip);
325             AbstractRepair ar=new AbstractRepair(tp, AbstractRepair.ADDTORELATION, rd);
326             TermNode tn=new TermNode(ar);
327             GraphNode gn=new GraphNode("AbstractAddRelRule"+i,tn);
328             if (!predtoabstractmap.containsKey(tp))
329                 predtoabstractmap.put(tp,new HashSet());
330             ((Set)predtoabstractmap.get(tp)).add(gn);
331             abstractrepair.add(gn);
332             abstractrepairadd.add(gn);
333             abstractadd.put(rd,gn);
334             
335             DNFPredicate tp2=new DNFPredicate(true,ip);
336             AbstractRepair ar2=new AbstractRepair(tp2, AbstractRepair.REMOVEFROMRELATION, rd);
337             TermNode tn2=new TermNode(ar2);
338             GraphNode gn2=new GraphNode("AbstractRemRelRule"+i,tn2);
339             if (!predtoabstractmap.containsKey(tp2))
340                 predtoabstractmap.put(tp2,new HashSet());
341             ((Set)predtoabstractmap.get(tp2)).add(gn2);
342             abstractrepair.add(gn2);
343             abstractrepairadd.add(gn2);
344             abstractremove.put(rd,gn2);
345         }
346         
347     }
348
349     int compensationcount=0;
350     void generatecompensationnodes() {
351         for(int i=0;i<state.vRules.size();i++) {
352             Rule r=(Rule) state.vRules.get(i);
353             Vector possiblerules=new Vector();
354             /* Construct bindings */
355             /* No need to construct bindings on remove
356                Vector bindings=new Vector();
357                constructbindings(bindings, r,true);
358             */
359             for(int j=0;j<(r.numQuantifiers()+r.getDNFNegGuardExpr().size());j++) {
360                 GraphNode gn=(GraphNode)scopesatisfy.get(r);
361                 TermNode tn=(TermNode) gn.getOwner();
362                 ScopeNode sn=tn.getScope();
363                 MultUpdateNode mun=new MultUpdateNode(sn);
364                 TermNode tn2=new TermNode(mun);
365                 GraphNode gn2=new GraphNode("CompRem"+compensationcount,tn2);
366                 UpdateNode un=new UpdateNode(r);
367                 //              un.addBindings(bindings);
368                 // Not necessary
369                 if (j<r.numQuantifiers()) {
370                     /* Remove quantifier */
371                     Quantifier q=r.getQuantifier(j);
372                     if (q instanceof RelationQuantifier) {
373                         RelationQuantifier rq=(RelationQuantifier)q;
374                         TupleOfExpr toe=new TupleOfExpr(new VarExpr(rq.x),new VarExpr(rq.y),rq.relation);
375                         toe.td=ReservedTypeDescriptor.INT;
376                         Updates u=new Updates(toe,true);
377                         un.addUpdate(u);
378                         if (abstractremove.containsKey(rq.relation)) {
379                             GraphNode agn=(GraphNode)abstractremove.get(rq.relation);
380                             GraphNode.Edge e=new GraphNode.Edge("requires",agn);
381                             gn2.addEdge(e);
382                         } else {
383                             continue; /* Abstract repair doesn't exist */
384                         }
385                     } else if (q instanceof SetQuantifier) {
386                         SetQuantifier sq=(SetQuantifier)q;
387                         ElementOfExpr eoe=new ElementOfExpr(new VarExpr(sq.var),sq.set);
388                         eoe.td=ReservedTypeDescriptor.INT;
389                         Updates u=new Updates(eoe,true);
390                         un.addUpdate(u);
391                         if (abstractremove.containsKey(sq.set)) {
392                             GraphNode agn=(GraphNode)abstractremove.get(sq.set);
393                             GraphNode.Edge e=new GraphNode.Edge("requires",agn);
394                             gn2.addEdge(e);
395                         } else {
396                             continue; /* Abstract repair doesn't exist */
397                         }
398                     } else {
399                         continue;
400                     }
401                 } else {
402                     int c=j-r.numQuantifiers();
403                     if (!processconjunction(un,r.getDNFNegGuardExpr().get(c))) {
404                         continue;
405                     }
406                 }
407                 if (!un.checkupdates()) /* Make sure we have a good update */
408                     continue;
409                 
410                 mun.addUpdate(un);
411
412                 GraphNode.Edge e=new GraphNode.Edge("abstract"+compensationcount,gn2);
413                 compensationcount++;
414                 gn.addEdge(e);
415                 updatenodes.add(gn2);
416             }
417         }
418     }
419
420     void generatedatastructureupdatenodes() {
421         for(Iterator absiterator=abstractrepair.iterator();absiterator.hasNext();) {
422             GraphNode gn=(GraphNode)absiterator.next();
423             TermNode tn=(TermNode) gn.getOwner();
424             AbstractRepair ar=tn.getAbstract();
425             if (ar.getType()==AbstractRepair.ADDTOSET) {
426                 generateaddtosetrelation(gn,ar);
427             } else if (ar.getType()==AbstractRepair.REMOVEFROMSET) {
428                 generateremovefromsetrelation(gn,ar);
429             } else if (ar.getType()==AbstractRepair.ADDTORELATION) {
430                 generateaddtosetrelation(gn,ar);
431             } else if (ar.getType()==AbstractRepair.REMOVEFROMRELATION) {
432                 generateremovefromsetrelation(gn,ar);
433             } else if (ar.getType()==AbstractRepair.MODIFYRELATION) {
434                 /* Generate remove/add pairs */
435                 generateremovefromsetrelation(gn,ar);
436                 generateaddtosetrelation(gn,ar);
437                 /* Generate atomic modify */
438                 generatemodifyrelation(gn,ar);
439             }
440         }
441     }
442
443     int removefromcount=0;
444     void generateremovefromsetrelation(GraphNode gn,AbstractRepair ar) {
445         Vector possiblerules=new Vector();
446         for(int i=0;i<state.vRules.size();i++) {
447             Rule r=(Rule) state.vRules.get(i);
448             if ((r.getInclusion() instanceof SetInclusion)&&
449                 (ar.getDescriptor()==((SetInclusion)r.getInclusion()).getSet()))
450                 possiblerules.add(r);
451             if ((r.getInclusion() instanceof RelationInclusion)&&
452                 (ar.getDescriptor()==((RelationInclusion)r.getInclusion()).getRelation()))
453                 possiblerules.add(r);
454         }
455         if (possiblerules.size()==0)
456             return;
457         int[] count=new int[possiblerules.size()];
458         while(remains(count,possiblerules,true)) {
459             MultUpdateNode mun=new MultUpdateNode(ar,MultUpdateNode.REMOVE);
460             TermNode tn=new TermNode(mun);
461             GraphNode gn2=new GraphNode("UpdateRem"+removefromcount,tn);
462
463             boolean goodflag=true;
464             for(int i=0;i<possiblerules.size();i++) {
465                 Rule r=(Rule)possiblerules.get(i);
466                 UpdateNode un=new UpdateNode(r);
467                 /* Construct bindings */
468                 /* No Need to construct bindings on remove
469                    Vector bindings=new Vector();
470                    constructbindings(bindings, r,true);
471                   un.addBindings(bindings);*/
472                 if (count[i]<r.numQuantifiers()) {
473                     /* Remove quantifier */
474                     Quantifier q=r.getQuantifier(count[i]);
475                     if (q instanceof RelationQuantifier) {
476                         RelationQuantifier rq=(RelationQuantifier)q;
477                         TupleOfExpr toe=new TupleOfExpr(new VarExpr(rq.x),new VarExpr(rq.y),rq.relation);
478                         toe.td=ReservedTypeDescriptor.INT;
479                         Updates u=new Updates(toe,true);
480                         un.addUpdate(u);
481                         if (abstractremove.containsKey(rq.relation)) {
482                             GraphNode agn=(GraphNode)abstractremove.get(rq.relation);
483                             GraphNode.Edge e=new GraphNode.Edge("requires",agn);
484                             gn2.addEdge(e);
485                         } else {
486                             goodflag=false;break; /* Abstract repair doesn't exist */
487                         }
488                     } else if (q instanceof SetQuantifier) {
489                         SetQuantifier sq=(SetQuantifier)q;
490                         ElementOfExpr eoe=new ElementOfExpr(new VarExpr(sq.var),sq.set);
491                         eoe.td=ReservedTypeDescriptor.INT;
492                         Updates u=new Updates(eoe,true);
493                         un.addUpdate(u);
494                         if (abstractremove.containsKey(sq.set)) {
495                             GraphNode agn=(GraphNode)abstractremove.get(sq.set);
496                             GraphNode.Edge e=new GraphNode.Edge("requires",agn);
497                             gn2.addEdge(e);
498                         } else {
499                             goodflag=false;break; /* Abstract repair doesn't exist */
500                         }
501                     } else {goodflag=false;break;}
502                 } else {
503                     int c=count[i]-r.numQuantifiers();
504                     if (!processconjunction(un,r.getDNFNegGuardExpr().get(c))) {
505                         goodflag=false;break;
506                     }
507                 }
508                 if (!un.checkupdates()) {
509                     goodflag=false;
510                     break;
511                 }
512                 mun.addUpdate(un);
513             }
514             if (goodflag) {
515                 GraphNode.Edge e=new GraphNode.Edge("abstract"+removefromcount,gn2);
516                 removefromcount++;
517                 gn.addEdge(e);
518                 updatenodes.add(gn2);
519             }
520             increment(count,possiblerules,true);
521         }
522     }
523
524     static void increment(int count[], Vector rules,boolean isremove) {
525         count[0]++;
526         for(int i=0;i<(rules.size()-1);i++) {
527             int num=isremove?(((Rule)rules.get(i)).numQuantifiers()+(((Rule)rules.get(i)).getDNFNegGuardExpr().size())):((Rule)rules.get(i)).getDNFGuardExpr().size();
528             if (count[i]>=num) {
529                 count[i+1]++;
530                 count[i]=0;
531             } else break;
532         }
533     }
534
535     static boolean remains(int count[], Vector rules, boolean isremove) {
536         for(int i=0;i<rules.size();i++) {
537             int num=isremove?(((Rule)rules.get(i)).numQuantifiers()+(((Rule)rules.get(i)).getDNFNegGuardExpr().size())):((Rule)rules.get(i)).getDNFGuardExpr().size();
538             if (count[i]>=num) {
539                 return false;
540             }
541         }
542         return true;
543     }
544
545     /** This method generates data structure updates to implement the
546      *  abstract atomic modification specified by ar. */
547     int modifycount=0;
548     void generatemodifyrelation(GraphNode gn, AbstractRepair ar) {
549         RelationDescriptor rd=(RelationDescriptor)ar.getDescriptor();
550         ExprPredicate exprPredicate=(ExprPredicate)ar.getPredicate().getPredicate();
551         boolean inverted=exprPredicate.inverted();
552         int leftindex=0;
553         int rightindex=1;
554         if (inverted)
555             leftindex=2;
556         else 
557             rightindex=2;
558
559
560         Vector possiblerules=new Vector();
561         for(int i=0;i<state.vRules.size();i++) {
562             Rule r=(Rule) state.vRules.get(i);
563             if ((r.getInclusion() instanceof RelationInclusion)&&
564                 (rd==((RelationInclusion)r.getInclusion()).getRelation()))
565                 possiblerules.add(r);
566         }
567         if (possiblerules.size()==0)
568             return;
569         int[] count=new int[possiblerules.size()];
570         while(remains(count,possiblerules,false)) {
571             MultUpdateNode mun=new MultUpdateNode(ar,MultUpdateNode.MODIFY);
572             TermNode tn=new TermNode(mun);
573             GraphNode gn2=new GraphNode("UpdateMod"+removefromcount,tn);
574
575             boolean goodflag=true;
576             for(int i=0;i<possiblerules.size();i++) {
577                 Rule r=(Rule)possiblerules.get(i);
578                 UpdateNode un=new UpdateNode(r);
579                 /* No Need to construct bindings on modify */
580                 
581                 int c=count[i];
582                 if (!processconjunction(un,r.getDNFGuardExpr().get(c))) {
583                     goodflag=false;break;
584                 }
585                 RelationInclusion ri=(RelationInclusion)r.getInclusion();
586                 if (!(ri.getLeftExpr() instanceof VarExpr)) {
587                     if (ri.getLeftExpr().isValue()) {
588                         Updates up=new Updates(ri.getLeftExpr(),leftindex);
589                         un.addUpdate(up);
590                     } else
591                         goodflag=false;
592                 } else {
593                     VarDescriptor vd=((VarExpr)ri.getLeftExpr()).getVar();
594                     if (vd.isGlobal()) {
595                         Updates up=new Updates(ri.getLeftExpr(),leftindex);
596                         un.addUpdate(up);
597                     } else if (inverted)
598                         goodflag=false;
599                 }
600                 if (!(ri.getRightExpr() instanceof VarExpr)) {
601                     if (ri.getRightExpr().isValue()) {
602                         Updates up=new Updates(ri.getRightExpr(),rightindex);
603                         un.addUpdate(up);
604                     } else
605                         goodflag=false;
606                 } else {
607                     VarDescriptor vd=((VarExpr)ri.getRightExpr()).getVar();
608                     if (vd.isGlobal()) {
609                         Updates up=new Updates(ri.getRightExpr(),rightindex);
610                         un.addUpdate(up);
611                     } else if (!inverted) 
612                         goodflag=false;
613                 }
614                                 
615                 if (!un.checkupdates()) {
616                     goodflag=false;
617                     break;
618                 }
619                 mun.addUpdate(un);
620             }
621             if (goodflag) {
622                 GraphNode.Edge e=new GraphNode.Edge("abstract"+modifycount,gn2);
623                 modifycount++;
624                 gn.addEdge(e);
625                 updatenodes.add(gn2);
626             }
627             increment(count,possiblerules,false);
628         }
629     }
630
631
632     boolean constructbindings(Vector bindings, Rule r, boolean isremoval) {
633         boolean goodupdate=true;
634         Inclusion inc=r.getInclusion();
635         for(Iterator iterator=r.quantifiers();iterator.hasNext();) {
636             Quantifier q=(Quantifier)iterator.next();
637             if ((q instanceof SetQuantifier)||(q instanceof ForQuantifier)) {
638                 VarDescriptor vd=null;
639                 SetDescriptor set=null;
640                 if (q instanceof SetQuantifier) {
641                     vd=((SetQuantifier)q).getVar();
642                 } else
643                     vd=((ForQuantifier)q).getVar();
644                 if(inc instanceof SetInclusion) {
645                     SetInclusion si=(SetInclusion)inc;
646                     if ((si.elementexpr instanceof VarExpr)&&
647                         (((VarExpr)si.elementexpr).getVar()==vd)) {
648                         /* Can solve for v */
649                         Binding binding=new Binding(vd,0);
650                         bindings.add(binding);
651                     } else
652                         goodupdate=false;
653                 } else if (inc instanceof RelationInclusion) {
654                     RelationInclusion ri=(RelationInclusion)inc;
655                     boolean f1=true;
656                     boolean f2=true;
657                     if ((ri.getLeftExpr() instanceof VarExpr)&&
658                         (((VarExpr)ri.getLeftExpr()).getVar()==vd)) {
659                                 /* Can solve for v */
660                         Binding binding=new Binding(vd,0);
661                         bindings.add(binding);
662                     } else f1=false;
663                     if ((ri.getRightExpr() instanceof VarExpr)&&
664                         (((VarExpr)ri.getRightExpr()).getVar()==vd)) {
665                                 /* Can solve for v */
666                         Binding binding=new Binding(vd,0);
667                         bindings.add(binding);
668                     } else f2=false;
669                     if (!(f1||f2))
670                         goodupdate=false;
671                 } else throw new Error("Inclusion not recognized");
672                 if (!goodupdate)
673                     if (isremoval) {
674                         Binding binding=new Binding(vd);
675                         bindings.add(binding);
676                         goodupdate=true;
677                     } else
678                         break;
679             } else if (q instanceof RelationQuantifier) {
680                 RelationQuantifier rq=(RelationQuantifier)q;
681                 for(int k=0;k<2;k++) {
682                     VarDescriptor vd=(k==0)?rq.x:rq.y;
683                     if(inc instanceof SetInclusion) {
684                         SetInclusion si=(SetInclusion)inc;
685                         if ((si.elementexpr instanceof VarExpr)&&
686                             (((VarExpr)si.elementexpr).getVar()==vd)) {
687                             /* Can solve for v */
688                             Binding binding=new Binding(vd,0);
689                             bindings.add(binding);
690                         } else
691                             goodupdate=false;
692                     } else if (inc instanceof RelationInclusion) {
693                         RelationInclusion ri=(RelationInclusion)inc;
694                         boolean f1=true;
695                         boolean f2=true;
696                         if ((ri.getLeftExpr() instanceof VarExpr)&&
697                             (((VarExpr)ri.getLeftExpr()).getVar()==vd)) {
698                             /* Can solve for v */
699                             Binding binding=new Binding(vd,0);
700                             bindings.add(binding);
701                         } else f1=false;
702                         if ((ri.getRightExpr() instanceof VarExpr)&&
703                             (((VarExpr)ri.getRightExpr()).getVar()==vd)) {
704                             /* Can solve for v */
705                             Binding binding=new Binding(vd,0);
706                             bindings.add(binding);
707                         } else f2=false;
708                         if (!(f1||f2))
709                             goodupdate=false;
710                     } else throw new Error("Inclusion not recognized");
711                     if (!goodupdate)
712                         if (isremoval) {
713                             Binding binding=new Binding(vd);
714                             bindings.add(binding);
715                             goodupdate=true;
716                         } else
717                             break;
718                 }
719                 if (!goodupdate)
720                     break;
721             } else throw new Error("Quantifier not recognized");
722         }
723         return goodupdate;
724     }
725
726     static int addtocount=0;
727     void generateaddtosetrelation(GraphNode gn, AbstractRepair ar) {
728         //      System.out.println("Attempting to generate add to set");
729         //System.out.println(ar.getPredicate().getPredicate().name());
730         //System.out.println(ar.getPredicate().isNegated());
731         for(int i=0;i<state.vRules.size();i++) {
732             Rule r=(Rule) state.vRules.get(i);
733             /* See if this is a good rule*/
734             //System.out.println(r.getGuardExpr().name());
735             if ((r.getInclusion() instanceof SetInclusion&&
736                 ar.getDescriptor()==((SetInclusion)r.getInclusion()).getSet())||
737                 (r.getInclusion() instanceof RelationInclusion&&
738                  ar.getDescriptor()==((RelationInclusion)r.getInclusion()).getRelation())) {
739
740                 /* First solve for quantifiers */
741                 Vector bindings=new Vector();
742                 /* Construct bindings */
743                 //System.out.println("Attempting to generate add to set: #2");
744                 if (!constructbindings(bindings,r,false))
745                     continue;
746                 //System.out.println("Attempting to generate add to set: #3");
747                 //Generate add instruction
748                 DNFRule dnfrule=r.getDNFGuardExpr();
749                 for(int j=0;j<dnfrule.size();j++) {
750                     Inclusion inc=r.getInclusion();
751                     UpdateNode un=new UpdateNode(r);
752                     un.addBindings(bindings);
753                     /* Now build update for tuple/set inclusion condition */
754                     if(inc instanceof SetInclusion) {
755                         SetInclusion si=(SetInclusion)inc;
756                         if (!(si.elementexpr instanceof VarExpr)) {
757                             if (si.elementexpr.isValue()) {
758                                 Updates up=new Updates(si.elementexpr,0);
759                                 un.addUpdate(up);
760                             } else
761                                 continue;
762                         } else {
763                             VarDescriptor vd=((VarExpr)si.elementexpr).getVar();
764                             if (vd.isGlobal()) {
765                                 Updates up=new Updates(si.elementexpr,0);
766                                 un.addUpdate(up);
767                             }
768                         }
769                     } else if (inc instanceof RelationInclusion) {
770                         RelationInclusion ri=(RelationInclusion)inc;
771                         if (!(ri.getLeftExpr() instanceof VarExpr)) {
772                             if (ri.getLeftExpr().isValue()) {
773                                 Updates up=new Updates(ri.getLeftExpr(),0);
774                                 un.addUpdate(up);
775                             } else
776                                 continue;
777                         } else {
778                             VarDescriptor vd=((VarExpr)ri.getLeftExpr()).getVar();
779                             if (vd.isGlobal()) {
780                                 Updates up=new Updates(ri.getLeftExpr(),0);
781                                 un.addUpdate(up);
782                             }
783                         }
784                         if (!(ri.getRightExpr() instanceof VarExpr)) {
785                             if (ri.getRightExpr().isValue()) {
786                                 Updates up=new Updates(ri.getRightExpr(),1);
787                                 un.addUpdate(up);
788                             } else
789                                 continue;
790                         } else {
791                             VarDescriptor vd=((VarExpr)ri.getRightExpr()).getVar();
792                             if (vd.isGlobal()) {
793                                 Updates up=new Updates(ri.getRightExpr(),1);
794                                 un.addUpdate(up);
795                             }
796                         }
797                     }
798                     //Finally build necessary updates to satisfy conjunction
799                     RuleConjunction ruleconj=dnfrule.get(j);
800                     /* Add in updates for quantifiers */
801                     //System.out.println("Attempting to generate add to set #4");
802                     MultUpdateNode mun=new MultUpdateNode(ar,MultUpdateNode.ADD);
803                     TermNode tn=new TermNode(mun);
804                     GraphNode gn2=new GraphNode("UpdateAdd"+addtocount,tn);
805
806                     if (processquantifers(gn2,un, r)&&debugdd()&&
807                         processconjunction(un,ruleconj)&&
808                         un.checkupdates()) {
809                         //System.out.println("Attempting to generate add to set #5");
810                         mun.addUpdate(un);
811                         GraphNode.Edge e=new GraphNode.Edge("abstract"+addtocount,gn2);
812                         addtocount++;
813                         gn.addEdge(e);
814                         updatenodes.add(gn2);}
815                 }
816             }
817         }
818     }
819
820     boolean debugdd() {
821         //System.out.println("Attempting to generate add to set DD");
822         return true;
823     }
824
825     boolean processquantifers(GraphNode gn,UpdateNode un, Rule r) {
826         Inclusion inc=r.getInclusion();
827         for(Iterator iterator=r.quantifiers();iterator.hasNext();) {
828             Quantifier q=(Quantifier)iterator.next();
829             /* Add quantifier */
830             /* FIXME: Analysis to determine when this update is necessary */
831             if (q instanceof RelationQuantifier) {
832                 RelationQuantifier rq=(RelationQuantifier)q;
833                 TupleOfExpr toe=new TupleOfExpr(new VarExpr(rq.x),new VarExpr(rq.y),rq.relation);
834                 toe.td=ReservedTypeDescriptor.INT;
835                 Updates u=new Updates(toe,false);
836                 un.addUpdate(u);
837                 if (abstractremove.containsKey(rq.relation)) {
838                     GraphNode agn=(GraphNode)abstractadd.get(rq.relation);
839                     GraphNode.Edge e=new GraphNode.Edge("requires",agn);
840                     gn.addEdge(e);
841                 } else {
842                     return false;
843                 }
844
845             } else if (q instanceof SetQuantifier) {
846                 SetQuantifier sq=(SetQuantifier)q;
847                 ElementOfExpr eoe=new ElementOfExpr(new VarExpr(sq.var),sq.set);
848                 eoe.td=ReservedTypeDescriptor.INT;
849                 Updates u=new Updates(eoe,false);
850                 un.addUpdate(u);
851                 if (abstractremove.containsKey(sq.set)) {
852                     GraphNode agn=(GraphNode)abstractadd.get(sq.set);
853                     GraphNode.Edge e=new GraphNode.Edge("requires",agn);
854                     gn.addEdge(e);
855                 } else {
856                     return false;
857                 }
858             } else return false;
859         }
860         return true;
861     }
862
863     boolean  processconjunction(UpdateNode un,RuleConjunction ruleconj){
864         boolean okay=true;
865         for(int k=0;k<ruleconj.size();k++) {
866             DNFExpr de=ruleconj.get(k);
867             Expr e=de.getExpr();
868             if (e instanceof OpExpr) {
869                 OpExpr ex=(OpExpr)de.getExpr();
870                 Opcode op=ex.getOpcode();
871                 Updates up=new Updates(ex.left,ex.right,op, de.getNegation());
872                 un.addUpdate(up);
873             } else if (e instanceof ElementOfExpr) {
874                 Updates up=new Updates(e,de.getNegation());
875                 un.addUpdate(up);
876             } else if (e instanceof TupleOfExpr) {
877                 Updates up=new Updates(e,de.getNegation());
878                 un.addUpdate(up);
879             } else if (e instanceof BooleanLiteralExpr) { 
880                 boolean truth=((BooleanLiteralExpr)e).getValue();
881                 if (de.getNegation())
882                     truth=!truth;
883                 if (!truth) {
884                     okay=false;
885                     break;
886                 }
887             } else {
888                 System.out.println(e.getClass().getName());
889                 throw new Error("Unrecognized Expr");
890             }
891         }
892         return okay;
893     }
894
895     void generatescopenodes() {
896         for(int i=0;i<state.vRules.size();i++) {
897             Rule r=(Rule) state.vRules.get(i);
898             ScopeNode satisfy=new ScopeNode(r,true);
899             TermNode tnsatisfy=new TermNode(satisfy);
900             GraphNode gnsatisfy=new GraphNode("SatisfyRule"+i,tnsatisfy);
901             ConsequenceNode cnsatisfy=new ConsequenceNode();
902             TermNode ctnsatisfy=new TermNode(cnsatisfy);
903             GraphNode cgnsatisfy=new GraphNode("ConseqSatisfyRule"+i,ctnsatisfy);
904             consequence.put(satisfy,cgnsatisfy);
905             GraphNode.Edge esat=new GraphNode.Edge("consequencesatisfy"+i,cgnsatisfy);
906             gnsatisfy.addEdge(esat);
907             consequencenodes.add(cgnsatisfy);
908             scopesatisfy.put(r,gnsatisfy);
909             scopenodes.add(gnsatisfy);
910
911             ScopeNode falsify=new ScopeNode(r,false);
912             TermNode tnfalsify=new TermNode(falsify);
913             GraphNode gnfalsify=new GraphNode("FalsifyRule"+i,tnfalsify);
914             ConsequenceNode cnfalsify=new ConsequenceNode();
915             TermNode ctnfalsify=new TermNode(cnfalsify);
916             GraphNode cgnfalsify=new GraphNode("ConseqFalsifyRule"+i,ctnfalsify);
917             consequence.put(falsify,cgnfalsify);
918             GraphNode.Edge efals=new GraphNode.Edge("consequencefalsify"+i,cgnfalsify);
919             gnfalsify.addEdge(efals);
920             consequencenodes.add(cgnfalsify);
921             scopefalsify.put(r,gnfalsify);
922             scopenodes.add(gnfalsify);
923         }
924     }
925 }