7 public class Termination {
9 Hashtable conjunctionmap;
11 HashSet abstractrepair;
12 HashSet abstractrepairadd;
15 HashSet consequencenodes;
18 Hashtable scopesatisfy;
19 Hashtable scopefalsify;
20 Hashtable consequence;
21 Hashtable abstractadd;
22 Hashtable abstractremove;
23 Hashtable conjtonodemap;
24 Hashtable predtoabstractmap;
29 public Termination(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();
48 generateconjunctionnodes();
50 generaterepairnodes();
51 generatedatastructureupdatenodes();
52 generatecompensationnodes();
54 generateabstractedges();
56 generateupdateedges();
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);
68 GraphNode.DOTVisitor.visit(new FileOutputStream("graph.dot"),superset);
69 } catch (Exception e) {
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());
80 GraphAnalysis ga=new GraphAnalysis(this);
81 removedset=ga.doAnalysis();
82 if (removedset==null) {
83 System.out.println("Can't generate terminating repair algorithm!");
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());
92 superset=new HashSet();
93 superset.addAll(conjunctions);
94 superset.removeAll(removedset);
95 GraphNode.computeclosure(superset,removedset);
97 GraphNode.DOTVisitor.visit(new FileOutputStream("graphfinal.dot"),superset);
98 } catch (Exception e) {
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()
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);
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);
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()
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);
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()
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);
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);
171 if (ConcreteInterferes.interferes(mun,r,false)) {
172 GraphNode scopenode=(GraphNode)scopefalsify.get(r);
173 GraphNode.Edge e=new GraphNode.Edge("interferes",scopenode);
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();
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();
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);
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);
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();
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);
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);
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);
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);
275 if (!predtoabstractmap.containsKey(dp))
276 predtoabstractmap.put(dp,new HashSet());
277 ((Set)predtoabstractmap.get(dp)).add(gn2);
278 abstractrepair.add(gn2);
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);
287 /* XXXXXXX: Not sure what to do here */
288 VarExpr ve=new VarExpr("DUMMY");
289 InclusionPredicate ip=new InclusionPredicate(ve,new SetExpr(sd));
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);
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);
314 Vector relationdescriptors=state.stRelations.getAllDescriptors();
315 for(int i=0;i<relationdescriptors.size();i++) {
316 RelationDescriptor rd=(RelationDescriptor)relationdescriptors.get(i);
318 /* XXXXXXX: Not sure what to do here */
319 VarDescriptor vd1=new VarDescriptor("DUMMY1");
320 VarExpr ve2=new VarExpr("DUMMY2");
322 InclusionPredicate ip=new InclusionPredicate(ve2,new ImageSetExpr(vd1, rd));
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);
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);
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);
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);
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);
378 if (abstractremove.containsKey(rq.relation)) {
379 GraphNode agn=(GraphNode)abstractremove.get(rq.relation);
380 GraphNode.Edge e=new GraphNode.Edge("requires",agn);
383 continue; /* Abstract repair doesn't exist */
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);
391 if (abstractremove.containsKey(sq.set)) {
392 GraphNode agn=(GraphNode)abstractremove.get(sq.set);
393 GraphNode.Edge e=new GraphNode.Edge("requires",agn);
396 continue; /* Abstract repair doesn't exist */
402 int c=j-r.numQuantifiers();
403 if (!processconjunction(un,r.getDNFNegGuardExpr().get(c))) {
407 if (!un.checkupdates()) /* Make sure we have a good update */
412 GraphNode.Edge e=new GraphNode.Edge("abstract"+compensationcount,gn2);
415 updatenodes.add(gn2);
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);
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);
455 if (possiblerules.size()==0)
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);
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);
481 if (abstractremove.containsKey(rq.relation)) {
482 GraphNode agn=(GraphNode)abstractremove.get(rq.relation);
483 GraphNode.Edge e=new GraphNode.Edge("requires",agn);
486 goodflag=false;break; /* Abstract repair doesn't exist */
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);
494 if (abstractremove.containsKey(sq.set)) {
495 GraphNode agn=(GraphNode)abstractremove.get(sq.set);
496 GraphNode.Edge e=new GraphNode.Edge("requires",agn);
499 goodflag=false;break; /* Abstract repair doesn't exist */
501 } else {goodflag=false;break;}
503 int c=count[i]-r.numQuantifiers();
504 if (!processconjunction(un,r.getDNFNegGuardExpr().get(c))) {
505 goodflag=false;break;
508 if (!un.checkupdates()) {
515 GraphNode.Edge e=new GraphNode.Edge("abstract"+removefromcount,gn2);
518 updatenodes.add(gn2);
520 increment(count,possiblerules,true);
524 static void increment(int count[], Vector rules,boolean isremove) {
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();
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();
545 /** This method generates data structure updates to implement the
546 * abstract atomic modification specified by ar. */
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();
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);
567 if (possiblerules.size()==0)
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);
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 */
582 if (!processconjunction(un,r.getDNFGuardExpr().get(c))) {
583 goodflag=false;break;
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);
593 VarDescriptor vd=((VarExpr)ri.getLeftExpr()).getVar();
595 Updates up=new Updates(ri.getLeftExpr(),leftindex);
600 if (!(ri.getRightExpr() instanceof VarExpr)) {
601 if (ri.getRightExpr().isValue()) {
602 Updates up=new Updates(ri.getRightExpr(),rightindex);
607 VarDescriptor vd=((VarExpr)ri.getRightExpr()).getVar();
609 Updates up=new Updates(ri.getRightExpr(),rightindex);
611 } else if (!inverted)
615 if (!un.checkupdates()) {
622 GraphNode.Edge e=new GraphNode.Edge("abstract"+modifycount,gn2);
625 updatenodes.add(gn2);
627 increment(count,possiblerules,false);
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();
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);
653 } else if (inc instanceof RelationInclusion) {
654 RelationInclusion ri=(RelationInclusion)inc;
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);
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);
671 } else throw new Error("Inclusion not recognized");
674 Binding binding=new Binding(vd);
675 bindings.add(binding);
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);
692 } else if (inc instanceof RelationInclusion) {
693 RelationInclusion ri=(RelationInclusion)inc;
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);
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);
710 } else throw new Error("Inclusion not recognized");
713 Binding binding=new Binding(vd);
714 bindings.add(binding);
721 } else throw new Error("Quantifier not recognized");
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())) {
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))
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);
763 VarDescriptor vd=((VarExpr)si.elementexpr).getVar();
765 Updates up=new Updates(si.elementexpr,0);
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);
778 VarDescriptor vd=((VarExpr)ri.getLeftExpr()).getVar();
780 Updates up=new Updates(ri.getLeftExpr(),0);
784 if (!(ri.getRightExpr() instanceof VarExpr)) {
785 if (ri.getRightExpr().isValue()) {
786 Updates up=new Updates(ri.getRightExpr(),1);
791 VarDescriptor vd=((VarExpr)ri.getRightExpr()).getVar();
793 Updates up=new Updates(ri.getRightExpr(),1);
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);
806 if (processquantifers(gn2,un, r)&&debugdd()&&
807 processconjunction(un,ruleconj)&&
809 //System.out.println("Attempting to generate add to set #5");
811 GraphNode.Edge e=new GraphNode.Edge("abstract"+addtocount,gn2);
814 updatenodes.add(gn2);}
821 //System.out.println("Attempting to generate add to set DD");
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();
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);
837 if (abstractremove.containsKey(rq.relation)) {
838 GraphNode agn=(GraphNode)abstractadd.get(rq.relation);
839 GraphNode.Edge e=new GraphNode.Edge("requires",agn);
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);
851 if (abstractremove.containsKey(sq.set)) {
852 GraphNode agn=(GraphNode)abstractadd.get(sq.set);
853 GraphNode.Edge e=new GraphNode.Edge("requires",agn);
863 boolean processconjunction(UpdateNode un,RuleConjunction ruleconj){
865 for(int k=0;k<ruleconj.size();k++) {
866 DNFExpr de=ruleconj.get(k);
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());
873 } else if (e instanceof ElementOfExpr) {
874 Updates up=new Updates(e,de.getNegation());
876 } else if (e instanceof TupleOfExpr) {
877 Updates up=new Updates(e,de.getNegation());
879 } else if (e instanceof BooleanLiteralExpr) {
880 boolean truth=((BooleanLiteralExpr)e).getValue();
881 if (de.getNegation())
888 System.out.println(e.getClass().getName());
889 throw new Error("Unrecognized Expr");
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);
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);