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