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;
26 ComputeMaxSize maxsize;
28 AbstractInterferes abstractinterferes;
29 ConcreteInterferes concreteinterferes;
30 ConstraintDependence constraintdependence;
32 ArrayAnalysis arrayanalysis;
35 public Termination(State state) {
37 conjunctions=new HashSet();
38 conjunctionmap=new Hashtable();
39 abstractrepair=new HashSet();
40 abstractrepairadd=new HashSet();
41 scopenodes=new HashSet();
42 scopesatisfy=new Hashtable();
43 scopefalsify=new Hashtable();
44 consequence=new Hashtable();
45 updatenodes=new HashSet();
46 consequencenodes=new HashSet();
47 abstractadd=new Hashtable();
48 abstractremove=new Hashtable();
49 conjtonodemap=new Hashtable();
50 predtoabstractmap=new Hashtable();
55 for(int i=0;i<state.vRules.size();i++)
56 System.out.println(state.vRules.get(i));
57 for(int i=0;i<state.vConstraints.size();i++)
58 System.out.println(state.vConstraints.get(i));
60 sources=new Sources(state);
61 maxsize=new ComputeMaxSize(state);
62 exactsize=new ExactSize(state);
63 arrayanalysis=new ArrayAnalysis(state,this);
65 abstractinterferes=new AbstractInterferes(this);
66 concreteinterferes=new ConcreteInterferes(this);
67 generateconjunctionnodes();
68 constraintdependence=new ConstraintDependence(state,this);
71 generaterepairnodes();
72 generatedatastructureupdatenodes();
73 generatecompensationnodes();
75 generateabstractedges();
77 generateupdateedges();
80 HashSet superset=new HashSet();
81 superset.addAll(conjunctions);
82 HashSet closureset=new HashSet();
84 GraphNode.computeclosure(superset,closureset);
86 GraphNode.DOTVisitor.visit(new FileOutputStream("graph.dot"),superset);
87 } catch (Exception e) {
92 generatedebuggraphs();
94 for(Iterator it=updatenodes.iterator();it.hasNext();) {
95 GraphNode gn=(GraphNode)it.next();
96 TermNode tn=(TermNode)gn.getOwner();
97 MultUpdateNode mun=tn.getUpdate();
98 System.out.println(gn.getTextLabel());
99 System.out.println(mun.toString());
101 GraphAnalysis ga=new GraphAnalysis(this);
102 removedset=ga.doAnalysis();
103 if (removedset==null) {
104 System.out.println("Can't generate terminating repair algorithm!");
108 System.out.println("Removing:");
109 for(Iterator it=removedset.iterator();it.hasNext();) {
110 GraphNode gn=(GraphNode)it.next();
111 System.out.println(gn.getTextLabel());
114 superset=new HashSet();
115 superset.addAll(conjunctions);
116 superset.removeAll(removedset);
117 GraphNode.computeclosure(superset,removedset);
119 GraphNode.DOTVisitor.visit(new FileOutputStream("graphfinal.dot"),superset);
120 } catch (Exception e) {
124 constraintdependence.traversedependences(this);
127 void generatedebuggraphs() {
128 for (int i=0;i<Compiler.debuggraphs.size();i++) {
129 DebugItem di=(DebugItem) Compiler.debuggraphs.get(i);
130 Constraint constr=(Constraint)state.vConstraints.get(di.constraintnum);
131 HashSet superset=new HashSet();
132 superset.addAll((Set)conjunctionmap.get(constr));
133 HashSet closureset=new HashSet();
135 GraphNode.boundedcomputeclosure(superset,closureset,di.depth);
137 GraphNode.DOTVisitor.visit(new FileOutputStream("graph"+di.constraintnum+"-"+di.depth+".dot"),superset);
138 } catch (Exception e) {
146 /** This method generates a node for each conjunction in the DNF
147 * form of each constraint. It also converts the quantifiers into
148 * conjunctions also - constraints can be satisfied by removing
149 * items from the sets and relations they are quantified over */
151 void generateconjunctionnodes() {
152 Vector constraints=state.vConstraints;
153 // Constructs conjunction nodes
154 for(int i=0;i<constraints.size();i++) {
155 Constraint c=(Constraint)constraints.get(i);
156 DNFConstraint dnf=c.dnfconstraint;
157 for(int j=0;j<dnf.size();j++) {
158 TermNode tn=new TermNode(c,dnf.get(j));
159 GraphNode gn=new GraphNode("Conj"+i+"A"+j,
160 "Conj ("+i+","+j+") "+dnf.get(j).name()
162 conjunctions.add(gn);
163 if (!conjunctionmap.containsKey(c))
164 conjunctionmap.put(c,new HashSet());
165 ((Set)conjunctionmap.get(c)).add(gn);
166 conjtonodemap.put(dnf.get(j),gn);
168 // Construct quantifier "conjunction" nodes
169 for(int j=0;j<c.numQuantifiers();j++) {
170 Quantifier q=c.getQuantifier(j);
171 if (q instanceof SetQuantifier) {
172 SetQuantifier sq=(SetQuantifier)q;
173 VarExpr ve=new VarExpr(sq.getVar());
174 InclusionPredicate ip=new InclusionPredicate(ve,new SetExpr(sq.getSet()));
175 DNFConstraint dconst=new DNFConstraint(ip);
177 TermNode tn=new TermNode(c,dconst.get(0));
178 tn.setquantifiernode();
179 GraphNode gn=new GraphNode("Conj"+i+"AQ"+j,
180 "Conj ("+i+","+j+") "+dconst.get(0).name()
182 conjunctions.add(gn);
183 if (!conjunctionmap.containsKey(c))
184 conjunctionmap.put(c,new HashSet());
185 ((Set)conjunctionmap.get(c)).add(gn);
186 conjtonodemap.put(dconst.get(0),gn);
188 } else if (q instanceof RelationQuantifier) {
189 RelationQuantifier rq=(RelationQuantifier)q;
190 VarExpr ve=new VarExpr(rq.y);
191 InclusionPredicate ip=new InclusionPredicate(ve,new ImageSetExpr(rq.x,rq.getRelation()));
192 DNFConstraint dconst=new DNFConstraint(ip);
194 TermNode tn=new TermNode(c,dconst.get(0));
195 tn.setquantifiernode();
196 GraphNode gn=new GraphNode("Conj"+i+"AQ"+j,
197 "Conj ("+i+","+j+") "+dconst.get(0).name()
199 conjunctions.add(gn);
200 if (!conjunctionmap.containsKey(c))
201 conjunctionmap.put(c,new HashSet());
202 ((Set)conjunctionmap.get(c)).add(gn);
203 conjtonodemap.put(dconst.get(0),gn);
210 void generateupdateedges() {
211 for(Iterator updateiterator=updatenodes.iterator();updateiterator.hasNext();) {
212 GraphNode gn=(GraphNode)updateiterator.next();
213 TermNode tn=(TermNode)gn.getOwner();
214 MultUpdateNode mun=tn.getUpdate();
215 for(int i=0;i<mun.numUpdates();i++) {
216 UpdateNode un=mun.getUpdate(i);
217 for(int j=0;j<un.numUpdates();j++) {
218 Updates u=un.getUpdate(j);
219 if (u.getType()==Updates.ABSTRACT) {
220 Expr e=u.getLeftExpr();
221 boolean negated=u.negate;
222 if (e instanceof TupleOfExpr) {
223 TupleOfExpr toe=(TupleOfExpr)e;
225 GraphNode agn=(GraphNode)abstractremove.get(toe.relation);
226 GraphNode.Edge edge=new GraphNode.Edge("requires",agn);
229 GraphNode agn=(GraphNode)abstractadd.get(toe.relation);
230 GraphNode.Edge edge=new GraphNode.Edge("requires",agn);
233 } else if (e instanceof ElementOfExpr) {
234 ElementOfExpr eoe=(ElementOfExpr)e;
236 GraphNode agn=(GraphNode)abstractremove.get(eoe.set);
237 GraphNode.Edge edge=new GraphNode.Edge("requires",agn);
240 GraphNode agn=(GraphNode)abstractadd.get(eoe.set);
241 GraphNode.Edge edge=new GraphNode.Edge("requires",agn);
244 } else throw new Error("Unrecognized Abstract Update");
249 /* Cycle through the rules to look for possible conflicts */
250 for(int i=0;i<state.vRules.size();i++) {
251 Rule r=(Rule) state.vRules.get(i);
252 if (concreteinterferes.interferes(mun,r,true)) {
253 GraphNode scopenode=(GraphNode)scopesatisfy.get(r);
254 GraphNode.Edge e=new GraphNode.Edge("interferes",scopenode);
257 if (concreteinterferes.interferes(mun,r,false)) {
258 GraphNode scopenode=(GraphNode)scopefalsify.get(r);
259 GraphNode.Edge e=new GraphNode.Edge("interferes",scopenode);
266 void generateabstractedges() {
267 for(Iterator absiterator=abstractrepair.iterator();absiterator.hasNext();) {
268 GraphNode gn=(GraphNode)absiterator.next();
269 TermNode tn=(TermNode)gn.getOwner();
270 AbstractRepair ar=(AbstractRepair)tn.getAbstract();
272 for(Iterator conjiterator=conjunctions.iterator();conjiterator.hasNext();) {
273 GraphNode gn2=(GraphNode)conjiterator.next();
274 TermNode tn2=(TermNode)gn2.getOwner();
275 Conjunction conj=tn2.getConjunction();
276 Constraint cons=tn2.getConstraint();
278 for(int i=0;i<conj.size();i++) {
279 DNFPredicate dp=conj.get(i);
280 System.out.println("Checking "+gn.getTextLabel()+" --> "+gn2.getTextLabel());
281 if (AbstractInterferes.interferes(ar,cons)||
282 abstractinterferes.interferes(ar,dp)) {
283 GraphNode.Edge e=new GraphNode.Edge("interferes",gn2);
290 for(Iterator scopeiterator=scopenodes.iterator();scopeiterator.hasNext();) {
291 GraphNode gn2=(GraphNode)scopeiterator.next();
292 TermNode tn2=(TermNode)gn2.getOwner();
293 ScopeNode sn2=tn2.getScope();
294 if (AbstractInterferes.interferes(ar,sn2.getRule(),sn2.getSatisfy())) {
295 GraphNode.Edge e=new GraphNode.Edge("interferes",gn2);
302 void generatescopenodes() {
303 for(int i=0;i<state.vRules.size();i++) {
304 Rule r=(Rule) state.vRules.get(i);
305 ScopeNode satisfy=new ScopeNode(r,true);
306 TermNode tnsatisfy=new TermNode(satisfy);
307 GraphNode gnsatisfy=new GraphNode("SatisfyRule"+i,tnsatisfy);
308 ConsequenceNode cnsatisfy=new ConsequenceNode();
309 TermNode ctnsatisfy=new TermNode(cnsatisfy);
310 GraphNode cgnsatisfy=new GraphNode("ConseqSatisfyRule"+i,ctnsatisfy);
311 consequence.put(satisfy,cgnsatisfy);
312 GraphNode.Edge esat=new GraphNode.Edge("consequencesatisfy"+i,cgnsatisfy);
313 gnsatisfy.addEdge(esat);
314 consequencenodes.add(cgnsatisfy);
315 scopesatisfy.put(r,gnsatisfy);
316 scopenodes.add(gnsatisfy);
318 ScopeNode falsify=new ScopeNode(r,false);
319 TermNode tnfalsify=new TermNode(falsify);
320 GraphNode gnfalsify=new GraphNode("FalsifyRule"+i,tnfalsify);
321 ConsequenceNode cnfalsify=new ConsequenceNode();
322 TermNode ctnfalsify=new TermNode(cnfalsify);
323 GraphNode cgnfalsify=new GraphNode("ConseqFalsifyRule"+i,ctnfalsify);
324 consequence.put(falsify,cgnfalsify);
325 GraphNode.Edge efals=new GraphNode.Edge("consequencefalsify"+i,cgnfalsify);
326 gnfalsify.addEdge(efals);
327 consequencenodes.add(cgnfalsify);
328 scopefalsify.put(r,gnfalsify);
329 scopenodes.add(gnfalsify);
333 void generatescopeedges() {
334 for(Iterator scopeiterator=scopenodes.iterator();scopeiterator.hasNext();) {
335 GraphNode gn=(GraphNode)scopeiterator.next();
336 TermNode tn=(TermNode)gn.getOwner();
337 ScopeNode sn=tn.getScope();
339 /* Interference edges with conjunctions */
340 for(Iterator conjiterator=conjunctions.iterator();conjiterator.hasNext();) {
341 GraphNode gn2=(GraphNode)conjiterator.next();
342 TermNode tn2=(TermNode)gn2.getOwner();
343 Conjunction conj=tn2.getConjunction();
344 Constraint constr=tn2.getConstraint();
345 for(int i=0;i<conj.size();i++) {
346 DNFPredicate dp=conj.get(i);
347 if (abstractinterferes.interferes(sn,dp)||
348 AbstractInterferes.interferes(sn.getDescriptor(),sn.getSatisfy(),constr)) {
349 GraphNode.Edge e=new GraphNode.Edge("interferes",gn2);
350 GraphNode gnconseq=(GraphNode)consequence.get(sn);
357 /* Now see if this could effect other model defintion rules */
358 for(int i=0;i<state.vRules.size();i++) {
359 Rule r=(Rule) state.vRules.get(i);
360 if (AbstractInterferes.interferes(sn.getDescriptor(),sn.getSatisfy(),r,true)) {
361 GraphNode scopenode=(GraphNode)scopesatisfy.get(r);
362 GraphNode.Edge e=new GraphNode.Edge("interferes",scopenode);
363 GraphNode gnconseq=(GraphNode)consequence.get(sn);
366 if (AbstractInterferes.interferes(sn.getDescriptor(),sn.getSatisfy(),r,false)) {
367 GraphNode scopenode=(GraphNode)scopefalsify.get(r);
368 GraphNode.Edge e=new GraphNode.Edge("interferes",scopenode);
369 GraphNode gnconseq=(GraphNode)consequence.get(sn);
376 /** This method generates the abstract repair nodes. */
377 void generaterepairnodes() {
378 /* Generate repairs of conjunctions */
379 for(Iterator conjiterator=conjunctions.iterator();conjiterator.hasNext();) {
380 GraphNode gn=(GraphNode)conjiterator.next();
381 TermNode tn=(TermNode)gn.getOwner();
382 Conjunction conj=tn.getConjunction();
383 for(int i=0;i<conj.size();i++) {
384 DNFPredicate dp=conj.get(i);
385 int[] array=dp.getPredicate().getRepairs(dp.isNegated(),this);
386 Descriptor d=dp.getPredicate().getDescriptor();
387 for(int j=0;j<array.length;j++) {
388 AbstractRepair ar=new AbstractRepair(dp,array[j],d);
389 TermNode tn2=new TermNode(ar);
390 GraphNode gn2=new GraphNode(gn.getLabel()+"A"+i+"B"+ar.type(),gn.getTextLabel()+" #"+i+" "+ar.type(),tn2);
391 GraphNode.Edge e=new GraphNode.Edge("abstract",gn2);
393 if (!predtoabstractmap.containsKey(dp))
394 predtoabstractmap.put(dp,new HashSet());
395 ((Set)predtoabstractmap.get(dp)).add(gn2);
396 abstractrepair.add(gn2);
400 /* Generate additional abstract repairs */
401 Vector setdescriptors=state.stSets.getAllDescriptors();
402 for(int i=0;i<setdescriptors.size();i++) {
403 SetDescriptor sd=(SetDescriptor)setdescriptors.get(i);
405 VarExpr ve=new VarExpr("DUMMY");
406 InclusionPredicate ip=new InclusionPredicate(ve,new SetExpr(sd));
407 DNFPredicate tp=new DNFPredicate(false,ip);
408 AbstractRepair ar=new AbstractRepair(tp, AbstractRepair.ADDTOSET, sd);
409 TermNode tn=new TermNode(ar);
410 GraphNode gn=new GraphNode("AbstractAddSetRule"+i,tn);
411 if (!predtoabstractmap.containsKey(tp))
412 predtoabstractmap.put(tp,new HashSet());
413 ((Set)predtoabstractmap.get(tp)).add(gn);
414 abstractrepair.add(gn);
415 abstractrepairadd.add(gn);
416 abstractadd.put(sd,gn);
418 DNFPredicate tp2=new DNFPredicate(true,ip);
419 AbstractRepair ar2=new AbstractRepair(tp2, AbstractRepair.REMOVEFROMSET, sd);
420 TermNode tn2=new TermNode(ar2);
421 GraphNode gn2=new GraphNode("AbstractRemSetRule"+i,tn2);
422 if (!predtoabstractmap.containsKey(tp2))
423 predtoabstractmap.put(tp2,new HashSet());
424 ((Set)predtoabstractmap.get(tp2)).add(gn2);
425 abstractrepair.add(gn2);
426 abstractrepairadd.add(gn2);
427 abstractremove.put(sd,gn2);
430 Vector relationdescriptors=state.stRelations.getAllDescriptors();
431 for(int i=0;i<relationdescriptors.size();i++) {
432 RelationDescriptor rd=(RelationDescriptor)relationdescriptors.get(i);
433 VarDescriptor vd1=new VarDescriptor("DUMMY1");
434 VarExpr ve2=new VarExpr("DUMMY2");
436 InclusionPredicate ip=new InclusionPredicate(ve2,new ImageSetExpr(vd1, rd));
438 DNFPredicate tp=new DNFPredicate(false,ip);
439 AbstractRepair ar=new AbstractRepair(tp, AbstractRepair.ADDTORELATION, rd);
440 TermNode tn=new TermNode(ar);
441 GraphNode gn=new GraphNode("AbstractAddRelRule"+i,tn);
442 if (!predtoabstractmap.containsKey(tp))
443 predtoabstractmap.put(tp,new HashSet());
444 ((Set)predtoabstractmap.get(tp)).add(gn);
445 abstractrepair.add(gn);
446 abstractrepairadd.add(gn);
447 abstractadd.put(rd,gn);
449 DNFPredicate tp2=new DNFPredicate(true,ip);
450 AbstractRepair ar2=new AbstractRepair(tp2, AbstractRepair.REMOVEFROMRELATION, rd);
451 TermNode tn2=new TermNode(ar2);
452 GraphNode gn2=new GraphNode("AbstractRemRelRule"+i,tn2);
453 if (!predtoabstractmap.containsKey(tp2))
454 predtoabstractmap.put(tp2,new HashSet());
455 ((Set)predtoabstractmap.get(tp2)).add(gn2);
456 abstractrepair.add(gn2);
457 abstractrepairadd.add(gn2);
458 abstractremove.put(rd,gn2);
462 int compensationcount=0;
463 void generatecompensationnodes() {
464 for(int i=0;i<state.vRules.size();i++) {
465 Rule r=(Rule) state.vRules.get(i);
466 Vector possiblerules=new Vector();
468 for(int j=0;j<(r.numQuantifiers()+r.getDNFNegGuardExpr().size());j++) {
469 GraphNode gn=(GraphNode)scopesatisfy.get(r);
470 TermNode tn=(TermNode) gn.getOwner();
471 ScopeNode sn=tn.getScope();
472 MultUpdateNode mun=new MultUpdateNode(sn);
473 TermNode tn2=new TermNode(mun);
474 GraphNode gn2=new GraphNode("CompRem"+compensationcount,tn2);
475 UpdateNode un=new UpdateNode(r);
477 if (j<r.numQuantifiers()) {
478 /* Remove quantifier */
479 Quantifier q=r.getQuantifier(j);
480 if (q instanceof RelationQuantifier) {
481 RelationQuantifier rq=(RelationQuantifier)q;
482 TupleOfExpr toe=new TupleOfExpr(new VarExpr(rq.x),new VarExpr(rq.y),rq.relation);
483 toe.td=ReservedTypeDescriptor.INT;
484 Updates u=new Updates(toe,true);
486 if (abstractremove.containsKey(rq.relation)) {
487 GraphNode agn=(GraphNode)abstractremove.get(rq.relation);
488 GraphNode.Edge e=new GraphNode.Edge("requires",agn);
491 continue; /* Abstract repair doesn't exist */
493 } else if (q instanceof SetQuantifier) {
494 SetQuantifier sq=(SetQuantifier)q;
495 ElementOfExpr eoe=new ElementOfExpr(new VarExpr(sq.var),sq.set);
496 eoe.td=ReservedTypeDescriptor.INT;
497 Updates u=new Updates(eoe,true);
499 if (abstractremove.containsKey(sq.set)) {
500 GraphNode agn=(GraphNode)abstractremove.get(sq.set);
501 GraphNode.Edge e=new GraphNode.Edge("requires",agn);
504 continue; /* Abstract repair doesn't exist */
510 /* Negate conjunction */
511 int c=j-r.numQuantifiers();
512 if (!processconjunction(un,r.getDNFNegGuardExpr().get(c))) {
516 if (!un.checkupdates()) /* Make sure we have a good update */
520 GraphNode.Edge e=new GraphNode.Edge("abstract"+compensationcount,gn2);
523 updatenodes.add(gn2);
528 void generatedatastructureupdatenodes() {
529 for(Iterator absiterator=abstractrepair.iterator();absiterator.hasNext();) {
530 GraphNode gn=(GraphNode)absiterator.next();
531 TermNode tn=(TermNode) gn.getOwner();
532 AbstractRepair ar=tn.getAbstract();
533 if (ar.getType()==AbstractRepair.ADDTOSET) {
534 generateaddtosetrelation(gn,ar);
535 } else if (ar.getType()==AbstractRepair.REMOVEFROMSET) {
536 generateremovefromsetrelation(gn,ar);
537 } else if (ar.getType()==AbstractRepair.ADDTORELATION) {
538 generateaddtosetrelation(gn,ar);
539 } else if (ar.getType()==AbstractRepair.REMOVEFROMRELATION) {
540 generateremovefromsetrelation(gn,ar);
541 } else if (ar.getType()==AbstractRepair.MODIFYRELATION) {
542 /* Generate remove/add pairs */
543 generateremovefromsetrelation(gn,ar);
544 generateaddtosetrelation(gn,ar);
545 /* Generate atomic modify */
546 generatemodifyrelation(gn,ar);
552 /** This method generates concrete data structure updates which
553 * remove an object (or tuple) from a set (or relation).*/
555 int removefromcount=0;
556 void generateremovefromsetrelation(GraphNode gn,AbstractRepair ar) {
557 /* Construct the set of all rules which could add something to the given set or relation */
559 Vector possiblerules=new Vector();
560 for(int i=0;i<state.vRules.size();i++) {
561 Rule r=(Rule) state.vRules.get(i);
562 if ((r.getInclusion() instanceof SetInclusion)&&
563 (ar.getDescriptor()==((SetInclusion)r.getInclusion()).getSet()))
564 possiblerules.add(r);
565 if ((r.getInclusion() instanceof RelationInclusion)&&
566 (ar.getDescriptor()==((RelationInclusion)r.getInclusion()).getRelation()))
567 possiblerules.add(r);
569 if (possiblerules.size()==0)
572 /* Loop through different ways of falsifying each of these rules */
573 int[] count=new int[possiblerules.size()];
574 while(remains(count,possiblerules,true)) {
575 MultUpdateNode mun=new MultUpdateNode(ar,MultUpdateNode.REMOVE);
576 TermNode tn=new TermNode(mun);
577 GraphNode gn2=new GraphNode("UpdateRem"+removefromcount,tn);
579 boolean goodflag=true;
580 for(int i=0;i<possiblerules.size();i++) {
581 Rule r=(Rule)possiblerules.get(i);
582 UpdateNode un=new UpdateNode(r);
584 if (count[i]<r.numQuantifiers()) {
585 /* Remove quantifier */
586 Quantifier q=r.getQuantifier(count[i]);
587 if (q instanceof RelationQuantifier) {
588 RelationQuantifier rq=(RelationQuantifier)q;
589 TupleOfExpr toe=new TupleOfExpr(new VarExpr(rq.x),new VarExpr(rq.y),rq.relation);
590 toe.td=ReservedTypeDescriptor.INT;
591 Updates u=new Updates(toe,true);
593 if (abstractremove.containsKey(rq.relation)) {
594 GraphNode agn=(GraphNode)abstractremove.get(rq.relation);
595 GraphNode.Edge e=new GraphNode.Edge("requires",agn);
598 goodflag=false;break; /* Abstract repair doesn't exist */
600 } else if (q instanceof SetQuantifier) {
601 SetQuantifier sq=(SetQuantifier)q;
602 ElementOfExpr eoe=new ElementOfExpr(new VarExpr(sq.var),sq.set);
603 eoe.td=ReservedTypeDescriptor.INT;
604 Updates u=new Updates(eoe,true);
606 if (abstractremove.containsKey(sq.set)) {
607 GraphNode agn=(GraphNode)abstractremove.get(sq.set);
608 GraphNode.Edge e=new GraphNode.Edge("requires",agn);
611 goodflag=false;break; /* Abstract repair doesn't exist */
613 } else {goodflag=false;break;}
615 int c=count[i]-r.numQuantifiers();
616 if (!processconjunction(un,r.getDNFNegGuardExpr().get(c))) {
617 goodflag=false;break;
620 if (!un.checkupdates()) {
627 GraphNode.Edge e=new GraphNode.Edge("abstract"+removefromcount,gn2);
630 updatenodes.add(gn2);
632 increment(count,possiblerules,true);
636 /** This method increments to the next possibility. */
638 static private void increment(int count[], Vector rules,boolean isremove) {
640 for(int i=0;i<(rules.size()-1);i++) {
641 int num=isremove?(((Rule)rules.get(i)).numQuantifiers()+(((Rule)rules.get(i)).getDNFNegGuardExpr().size())):((Rule)rules.get(i)).getDNFGuardExpr().size();
650 /** This method test if there remain any possibilities to loop
652 static private boolean remains(int count[], Vector rules, boolean isremove) {
653 for(int i=0;i<rules.size();i++) {
654 int num=isremove?(((Rule)rules.get(i)).numQuantifiers()+(((Rule)rules.get(i)).getDNFNegGuardExpr().size())):((Rule)rules.get(i)).getDNFGuardExpr().size();
662 /** This method generates data structure updates to implement the
663 * abstract atomic modification specified by ar. */
666 void generatemodifyrelation(GraphNode gn, AbstractRepair ar) {
667 RelationDescriptor rd=(RelationDescriptor)ar.getDescriptor();
668 ExprPredicate exprPredicate=(ExprPredicate)ar.getPredicate().getPredicate();
669 boolean inverted=exprPredicate.inverted();
677 // construct set of possible rules
678 Vector possiblerules=new Vector();
679 for(int i=0;i<state.vRules.size();i++) {
680 Rule r=(Rule) state.vRules.get(i);
681 if ((r.getInclusion() instanceof RelationInclusion)&&
682 (rd==((RelationInclusion)r.getInclusion()).getRelation()))
683 possiblerules.add(r);
685 if (possiblerules.size()==0)
688 // increment through this set
689 int[] count=new int[possiblerules.size()];
690 while(remains(count,possiblerules,false)) {
691 MultUpdateNode mun=new MultUpdateNode(ar,MultUpdateNode.MODIFY);
692 TermNode tn=new TermNode(mun);
693 GraphNode gn2=new GraphNode("UpdateMod"+removefromcount,tn);
695 boolean goodflag=true;
696 for(int i=0;i<possiblerules.size();i++) {
697 Rule r=(Rule)possiblerules.get(i);
698 UpdateNode un=new UpdateNode(r);
701 if (!processconjunction(un,r.getDNFGuardExpr().get(c))) {
702 goodflag=false;break;
704 RelationInclusion ri=(RelationInclusion)r.getInclusion();
705 if (!(ri.getLeftExpr() instanceof VarExpr)) {
706 if (ri.getLeftExpr().isValue(ri.getRelation().getDomain().getType())) {
707 Updates up=new Updates(ri.getLeftExpr(),leftindex,ri.getRelation().getDomain().getType());
712 else un.addInvariant(ri.getLeftExpr());
715 VarDescriptor vd=((VarExpr)ri.getLeftExpr()).getVar();
717 Updates up=new Updates(ri.getLeftExpr(),leftindex,null);
722 if (!(ri.getRightExpr() instanceof VarExpr)) {
723 if (ri.getRightExpr().isValue(ri.getRelation().getRange().getType())) {
724 Updates up=new Updates(ri.getRightExpr(),rightindex,ri.getRelation().getRange().getType());
730 un.addInvariant(ri.getLeftExpr());
733 VarDescriptor vd=((VarExpr)ri.getRightExpr()).getVar();
735 Updates up=new Updates(ri.getRightExpr(),rightindex,null);
737 } else if (!inverted)
741 if (!un.checkupdates()) {
748 GraphNode.Edge e=new GraphNode.Edge("abstract"+modifycount,gn2);
751 updatenodes.add(gn2);
753 increment(count,possiblerules,false);
757 /** Generate concrete data structure update to add an object(or
758 * tuple) to a set (or relation). */
760 static int addtocount=0;
761 void generateaddtosetrelation(GraphNode gn, AbstractRepair ar) {
762 for(int i=0;i<state.vRules.size();i++) {
763 Rule r=(Rule) state.vRules.get(i);
764 /* See if this is a good rule*/
765 if ((r.getInclusion() instanceof SetInclusion&&
766 ar.getDescriptor()==((SetInclusion)r.getInclusion()).getSet())||
767 (r.getInclusion() instanceof RelationInclusion&&
768 ar.getDescriptor()==((RelationInclusion)r.getInclusion()).getRelation())) {
770 /* First solve for quantifiers */
771 Vector bindings=new Vector();
772 /* Construct bindings */
773 if (!constructbindings(bindings,r,false))
775 //Generate add instruction
776 DNFRule dnfrule=r.getDNFGuardExpr();
777 for(int j=0;j<dnfrule.size();j++) {
778 Inclusion inc=r.getInclusion();
779 UpdateNode un=new UpdateNode(r);
780 un.addBindings(bindings);
781 /* Now build update for tuple/set inclusion condition */
782 if(inc instanceof SetInclusion) {
783 SetInclusion si=(SetInclusion)inc;
784 if (!(si.elementexpr instanceof VarExpr)) {
785 if (si.elementexpr.isValue(si.getSet().getType())) {
786 Updates up=new Updates(si.elementexpr,0,si.getSet().getType());
789 /* We're an add to set*/
790 System.out.println("Rule: "+r);
791 ArrayAnalysis.AccessPath rap=arrayanalysis.analyzeExpr(r,si.elementexpr);
792 System.out.println("Attempting perform array add");
793 SetDescriptor set=sources.setSource(si.getSet())?
794 sources.getSourceSet(si.getSet()):null;
797 System.out.println("Non-null source set");
798 ArrayAnalysis.AccessPath ap=arrayanalysis.getSet(set);
799 if (rap==ArrayAnalysis.AccessPath.NONE)
801 System.out.println("A");
804 System.out.println("B");
805 if (!constructarrayupdate(un, si.elementexpr, rap, 0))
807 System.out.println("C");
810 VarDescriptor vd=((VarExpr)si.elementexpr).getVar();
812 Updates up=new Updates(si.elementexpr,0,null);
816 } else if (inc instanceof RelationInclusion) {
817 RelationInclusion ri=(RelationInclusion)inc;
818 if (!(ri.getLeftExpr() instanceof VarExpr)) {
819 if (ri.getLeftExpr().isValue(ri.getRelation().getDomain().getType())) {
820 Updates up=new Updates(ri.getLeftExpr(),0,ri.getRelation().getDomain().getType());
823 /* We don't handly relation modifies */
824 if (ar.getType()==AbstractRepair.MODIFYRELATION)
826 /* We're an add to relation*/
827 ArrayAnalysis.AccessPath rap=arrayanalysis.analyzeExpr(r,ri.getLeftExpr());
828 SetDescriptor set=sources.relsetSource(ri.getRelation(),true /* Domain*/)?
829 sources.relgetSourceSet(ri.getRelation(),true):null;
832 ArrayAnalysis.AccessPath ap=arrayanalysis.getSet(set);
834 if (rap==ArrayAnalysis.AccessPath.NONE||
836 !constructarrayupdate(un, ri.getLeftExpr(), rap, 0))
840 VarDescriptor vd=((VarExpr)ri.getLeftExpr()).getVar();
842 Updates up=new Updates(ri.getLeftExpr(),0,null);
846 if (!(ri.getRightExpr() instanceof VarExpr)) {
847 if (ri.getRightExpr().isValue(ri.getRelation().getRange().getType())) {
848 Updates up=new Updates(ri.getRightExpr(),1,ri.getRelation().getRange().getType());
851 /* We don't handly relation modifies */
852 if (ar.getType()==AbstractRepair.MODIFYRELATION)
854 /* We're an add to relation*/
855 ArrayAnalysis.AccessPath rap=arrayanalysis.analyzeExpr(r,ri.getRightExpr());
856 SetDescriptor set=sources.relsetSource(ri.getRelation(),false /* Range*/)?
857 sources.relgetSourceSet(ri.getRelation(),false):null;
860 ArrayAnalysis.AccessPath ap=arrayanalysis.getSet(set);
862 if (rap==ArrayAnalysis.AccessPath.NONE||
864 !constructarrayupdate(un, ri.getRightExpr(), rap, 1))
868 VarDescriptor vd=((VarExpr)ri.getRightExpr()).getVar();
870 Updates up=new Updates(ri.getRightExpr(),1,null);
875 //Finally build necessary updates to satisfy conjunction
876 RuleConjunction ruleconj=dnfrule.get(j);
878 /* Add in updates for quantifiers */
879 MultUpdateNode mun=new MultUpdateNode(ar,MultUpdateNode.ADD);
880 TermNode tn=new TermNode(mun);
881 GraphNode gn2=new GraphNode("UpdateAdd"+addtocount,tn);
883 if (processquantifiers(gn2,un, r)&&
884 processconjunction(un,ruleconj)&&
887 GraphNode.Edge e=new GraphNode.Edge("abstract"+addtocount,gn2);
890 updatenodes.add(gn2);
897 boolean constructarrayupdate(UpdateNode un, Expr lexpr, ArrayAnalysis.AccessPath ap, int slotnumber) {
898 System.out.println("Constructing array update");
900 for (int i=ap.numFields()-1;i>=0;i--) {
904 e=((DotExpr)e).getExpr();
906 while (e instanceof CastExpr)
907 e=((CastExpr)e).getExpr();
909 DotExpr de=(DotExpr)e;
910 FieldDescriptor fd=ap.getField(i);
911 if (fd instanceof ArrayDescriptor) {
912 // We have an ArrayDescriptor!
913 Expr index=de.getIndex();
914 if (!index.isValue(null)) {/* Not assignable */
915 System.out.println("ERROR:Index isn't assignable");
918 Updates updates=new Updates(index,i,ap,lexpr,slotnumber);
919 un.addUpdate(updates);
925 /** This method constructs bindings for an update using rule
926 * r. The boolean flag isremoval indicates that the update
927 * performs a removal. The function returns true if it is able to
928 * generate a valid set of bindings and false otherwise. */
930 boolean constructbindings(Vector bindings, Rule r, boolean isremoval) {
931 boolean goodupdate=true;
932 Inclusion inc=r.getInclusion();
933 for(Iterator iterator=r.quantifiers();iterator.hasNext();) {
934 Quantifier q=(Quantifier)iterator.next();
935 if ((q instanceof SetQuantifier)||(q instanceof ForQuantifier)) {
936 VarDescriptor vd=null;
937 SetDescriptor set=null;
938 if (q instanceof SetQuantifier) {
939 vd=((SetQuantifier)q).getVar();
940 set=((SetQuantifier)q).getSet();
942 vd=((ForQuantifier)q).getVar();
944 if(inc instanceof SetInclusion) {
945 SetInclusion si=(SetInclusion)inc;
946 if ((si.elementexpr instanceof VarExpr)&&
947 (((VarExpr)si.elementexpr).getVar()==vd)) {
948 /* Can solve for v */
949 Binding binding=new Binding(vd,0);
950 bindings.add(binding);
954 } else if (inc instanceof RelationInclusion) {
955 RelationInclusion ri=(RelationInclusion)inc;
958 if ((ri.getLeftExpr() instanceof VarExpr)&&
959 (((VarExpr)ri.getLeftExpr()).getVar()==vd)) {
960 /* Can solve for v */
961 Binding binding=new Binding(vd,0);
962 bindings.add(binding);
964 if ((ri.getRightExpr() instanceof VarExpr)&&
965 (((VarExpr)ri.getRightExpr()).getVar()==vd)) {
966 /* Can solve for v */
967 Binding binding=new Binding(vd,0);
968 bindings.add(binding);
972 } else throw new Error("Inclusion not recognized");
975 /* Removals don't need bindings anymore
976 Binding binding=new Binding(vd);
977 bindings.add(binding);*/
979 } else if (q instanceof SetQuantifier) {
980 /* Create new element to bind to */
981 // search if the set 'set' has a size
982 Binding binding=new Binding(vd,set,exactsize.getsize(set)==1);
983 bindings.add(binding);
988 } else if (q instanceof RelationQuantifier) {
989 RelationQuantifier rq=(RelationQuantifier)q;
990 for(int k=0;k<2;k++) {
991 VarDescriptor vd=(k==0)?rq.x:rq.y;
992 if(inc instanceof SetInclusion) {
993 SetInclusion si=(SetInclusion)inc;
994 if ((si.elementexpr instanceof VarExpr)&&
995 (((VarExpr)si.elementexpr).getVar()==vd)) {
996 /* Can solve for v */
997 Binding binding=new Binding(vd,0);
998 bindings.add(binding);
1001 } else if (inc instanceof RelationInclusion) {
1002 RelationInclusion ri=(RelationInclusion)inc;
1005 if ((ri.getLeftExpr() instanceof VarExpr)&&
1006 (((VarExpr)ri.getLeftExpr()).getVar()==vd)) {
1007 /* Can solve for v */
1008 Binding binding=new Binding(vd,0);
1009 bindings.add(binding);
1011 if ((ri.getRightExpr() instanceof VarExpr)&&
1012 (((VarExpr)ri.getRightExpr()).getVar()==vd)) {
1013 /* Can solve for v */
1014 Binding binding=new Binding(vd,0);
1015 bindings.add(binding);
1019 } else throw new Error("Inclusion not recognized");
1022 /* Removals don't need bindings anymore
1023 Binding binding=new Binding(vd);
1024 bindings.add(binding);*/
1031 } else throw new Error("Quantifier not recognized");
1036 /** Adds updates that add an item to the appropriate set or
1037 * relation quantified over by the model definition rule.. */
1039 boolean processquantifiers(GraphNode gn,UpdateNode un, Rule r) {
1040 Inclusion inc=r.getInclusion();
1041 for(Iterator iterator=r.quantifiers();iterator.hasNext();) {
1042 Quantifier q=(Quantifier)iterator.next();
1043 /* Add quantifier */
1044 /* FIXME: Analysis to determine when this update is necessary */
1045 if (q instanceof RelationQuantifier) {
1046 RelationQuantifier rq=(RelationQuantifier)q;
1047 TupleOfExpr toe=new TupleOfExpr(new VarExpr(rq.x),new VarExpr(rq.y),rq.relation);
1048 toe.td=ReservedTypeDescriptor.INT;
1049 Updates u=new Updates(toe,false);
1051 if (abstractadd.containsKey(rq.relation)) {
1052 GraphNode agn=(GraphNode)abstractadd.get(rq.relation);
1053 GraphNode.Edge e=new GraphNode.Edge("requires",agn);
1059 } else if (q instanceof SetQuantifier) {
1060 SetQuantifier sq=(SetQuantifier)q;
1061 if (un.getBinding(sq.var).getType()==Binding.SEARCH) {
1062 Binding b=un.getBinding(sq.var);
1063 Constraint reqc=exactsize.getConstraint(b.getSet());
1064 constraintdependence.requiresConstraint(gn,reqc);
1065 continue; /* Don't need to ensure addition for search */
1068 ElementOfExpr eoe=new ElementOfExpr(new VarExpr(sq.var),sq.set);
1069 eoe.td=ReservedTypeDescriptor.INT;
1070 Updates u=new Updates(eoe,false);
1072 if (abstractadd.containsKey(sq.set)) {
1073 GraphNode agn=(GraphNode)abstractadd.get(sq.set);
1074 GraphNode.Edge e=new GraphNode.Edge("requires",agn);
1079 } else return false;
1084 /** This method generates the necessary updates to satisfy the
1085 * conjunction ruleconj. */
1087 boolean processconjunction(UpdateNode un,RuleConjunction ruleconj){
1089 for(int k=0;k<ruleconj.size();k++) {
1090 DNFExpr de=ruleconj.get(k);
1091 Expr e=de.getExpr();
1092 if (e instanceof OpExpr) {
1093 OpExpr ex=(OpExpr)de.getExpr();
1094 Opcode op=ex.getOpcode();
1095 Updates up=new Updates(ex.left,ex.right,op, de.getNegation());
1097 } else if (e instanceof ElementOfExpr) {
1098 Updates up=new Updates(e,de.getNegation());
1100 } else if (e instanceof TupleOfExpr) {
1101 Updates up=new Updates(e,de.getNegation());
1103 } else if (e instanceof BooleanLiteralExpr) {
1104 boolean truth=((BooleanLiteralExpr)e).getValue();
1105 if (de.getNegation())
1112 System.out.println(e.getClass().getName());
1113 throw new Error("Unrecognized Expr");
1119 public boolean analyzeQuantifiers(Quantifiers qs,Set set) {
1120 for(int i=0;i<qs.numQuantifiers();i++) {
1121 Quantifier q=qs.getQuantifier(i);
1122 if (set.contains(q))
1124 if (q instanceof SetQuantifier) {
1125 SetDescriptor sd=((SetQuantifier)q).getSet();
1126 if (maxsize.getsize(sd)<=1&&
1127 maxsize.getsize(sd)>=0)
1129 } else if (q instanceof RelationQuantifier) {
1130 RelationDescriptor rd=((RelationQuantifier)q).getRelation();
1131 if (maxsize.getsize(rd)<=1&&
1132 maxsize.getsize(rd)>=0)
1140 public boolean mutuallyexclusive(SetDescriptor sd1, SetDescriptor sd2) {
1141 if (mutualexclusive(sd1,sd2)||
1142 mutualexclusive(sd2,sd1))
1148 private boolean mutualexclusive(SetDescriptor sd1, SetDescriptor sd2) {
1149 Vector rules=state.vRules;
1150 for(int i=0;i<rules.size();i++) {
1151 Rule r=(Rule)rules.get(i);
1152 if (r.getInclusion().getTargetDescriptors().contains(sd1)) {
1153 /* Rule may add items to sd1 */
1154 SetInclusion si=(SetInclusion)r.getInclusion();
1155 Expr ve=si.getExpr();
1156 DNFRule drule=r.getDNFGuardExpr();
1157 for(int j=0;j<drule.size();j++) {
1158 RuleConjunction rconj=drule.get(j);
1159 boolean containsexclusion=false;
1160 for (int k=0;k<rconj.size();k++) {
1161 DNFExpr dexpr=rconj.get(k);
1162 if (dexpr.getNegation()&&
1163 dexpr.getExpr() instanceof ElementOfExpr&&
1164 ((ElementOfExpr)dexpr.getExpr()).element.equals(null,ve)) {
1165 SetDescriptor sd=((ElementOfExpr)dexpr.getExpr()).set;
1166 if (sd.isSubset(sd2))
1167 containsexclusion=true;
1170 if (!containsexclusion)