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 HashSet superset=new HashSet();
131 Constraint constr=(Constraint)state.vConstraints.get(di.constraintnum);
133 if (di.conjunctionnum==-1) {
134 superset.addAll((Set)conjunctionmap.get(constr));
136 DNFConstraint dnf=constr.dnfconstraint;
137 superset.add(conjtonodemap.get(dnf.get(di.conjunctionnum)));
140 GraphNode.boundedcomputeclosure(superset,null,di.depth);
142 GraphNode.DOTVisitor.visit(new FileOutputStream("graph"+di.constraintnum+"-"+di.depth+(di.conjunctionnum==-1?"":"-"+di.conjunctionnum)+".dot"),superset);
143 } catch (Exception e) {
151 /** This method generates a node for each conjunction in the DNF
152 * form of each constraint. It also converts the quantifiers into
153 * conjunctions also - constraints can be satisfied by removing
154 * items from the sets and relations they are quantified over */
156 void generateconjunctionnodes() {
157 Vector constraints=state.vConstraints;
158 // Constructs conjunction nodes
159 for(int i=0;i<constraints.size();i++) {
160 Constraint c=(Constraint)constraints.get(i);
161 DNFConstraint dnf=c.dnfconstraint;
162 for(int j=0;j<dnf.size();j++) {
163 TermNode tn=new TermNode(c,dnf.get(j));
164 GraphNode gn=new GraphNode("Conj"+i+"A"+j,
165 "Conj ("+i+","+j+") "+dnf.get(j).name()
167 conjunctions.add(gn);
168 if (!conjunctionmap.containsKey(c))
169 conjunctionmap.put(c,new HashSet());
170 ((Set)conjunctionmap.get(c)).add(gn);
171 conjtonodemap.put(dnf.get(j),gn);
173 // Construct quantifier "conjunction" nodes
174 for(int j=0;j<c.numQuantifiers();j++) {
175 Quantifier q=c.getQuantifier(j);
176 if (q instanceof SetQuantifier) {
177 SetQuantifier sq=(SetQuantifier)q;
178 VarExpr ve=new VarExpr(sq.getVar());
179 InclusionPredicate ip=new InclusionPredicate(ve,new SetExpr(sq.getSet()));
180 DNFConstraint dconst=new DNFConstraint(ip);
182 TermNode tn=new TermNode(c,dconst.get(0));
183 tn.setquantifiernode();
184 GraphNode gn=new GraphNode("Conj"+i+"AQ"+j,
185 "Conj ("+i+","+j+") "+dconst.get(0).name()
187 conjunctions.add(gn);
188 if (!conjunctionmap.containsKey(c))
189 conjunctionmap.put(c,new HashSet());
190 ((Set)conjunctionmap.get(c)).add(gn);
191 conjtonodemap.put(dconst.get(0),gn);
193 } else if (q instanceof RelationQuantifier) {
194 RelationQuantifier rq=(RelationQuantifier)q;
195 VarExpr ve=new VarExpr(rq.y);
196 InclusionPredicate ip=new InclusionPredicate(ve,new ImageSetExpr(rq.x,rq.getRelation()));
197 DNFConstraint dconst=new DNFConstraint(ip);
199 TermNode tn=new TermNode(c,dconst.get(0));
200 tn.setquantifiernode();
201 GraphNode gn=new GraphNode("Conj"+i+"AQ"+j,
202 "Conj ("+i+","+j+") "+dconst.get(0).name()
204 conjunctions.add(gn);
205 if (!conjunctionmap.containsKey(c))
206 conjunctionmap.put(c,new HashSet());
207 ((Set)conjunctionmap.get(c)).add(gn);
208 conjtonodemap.put(dconst.get(0),gn);
214 void generateupdateedges() {
215 for(Iterator updateiterator=updatenodes.iterator();updateiterator.hasNext();) {
216 GraphNode gn=(GraphNode)updateiterator.next();
217 TermNode tn=(TermNode)gn.getOwner();
218 MultUpdateNode mun=tn.getUpdate();
219 for(int i=0;i<mun.numUpdates();i++) {
220 UpdateNode un=mun.getUpdate(i);
221 for(int j=0;j<un.numUpdates();j++) {
222 Updates u=un.getUpdate(j);
223 if (u.getType()==Updates.ABSTRACT) {
224 Expr e=u.getLeftExpr();
225 boolean negated=u.negate;
226 if (e instanceof TupleOfExpr) {
227 TupleOfExpr toe=(TupleOfExpr)e;
229 GraphNode agn=(GraphNode)abstractremove.get(toe.relation);
230 GraphNode.Edge edge=new GraphNode.Edge("requires",agn);
233 GraphNode agn=(GraphNode)abstractadd.get(toe.relation);
234 GraphNode.Edge edge=new GraphNode.Edge("requires",agn);
237 } else if (e instanceof ElementOfExpr) {
238 ElementOfExpr eoe=(ElementOfExpr)e;
240 GraphNode agn=(GraphNode)abstractremove.get(eoe.set);
241 GraphNode.Edge edge=new GraphNode.Edge("requires",agn);
244 GraphNode agn=(GraphNode)abstractadd.get(eoe.set);
245 GraphNode.Edge edge=new GraphNode.Edge("requires",agn);
248 } else throw new Error("Unrecognized Abstract Update");
252 /* Cycle through the rules to look for possible conflicts */
253 for(int i=0;i<state.vRules.size();i++) {
254 Rule r=(Rule) state.vRules.get(i);
255 if (concreteinterferes.interferes(mun,r,true)) {
256 GraphNode scopenode=(GraphNode)scopesatisfy.get(r);
257 GraphNode.Edge e=new GraphNode.Edge("interferes",scopenode);
260 if (concreteinterferes.interferes(mun,r,false)) {
261 GraphNode scopenode=(GraphNode)scopefalsify.get(r);
262 GraphNode.Edge e=new GraphNode.Edge("interferes",scopenode);
269 void generateabstractedges() {
270 for(Iterator absiterator=abstractrepair.iterator();absiterator.hasNext();) {
271 GraphNode gn=(GraphNode)absiterator.next();
272 TermNode tn=(TermNode)gn.getOwner();
273 AbstractRepair ar=(AbstractRepair)tn.getAbstract();
275 for(Iterator conjiterator=conjunctions.iterator();conjiterator.hasNext();) {
276 GraphNode gn2=(GraphNode)conjiterator.next();
277 TermNode tn2=(TermNode)gn2.getOwner();
278 Conjunction conj=tn2.getConjunction();
279 Constraint cons=tn2.getConstraint();
281 for(int i=0;i<conj.size();i++) {
282 DNFPredicate dp=conj.get(i);
283 if (AbstractInterferes.interferes(ar,cons)||
284 abstractinterferes.interferes(ar,dp)) {
285 GraphNode.Edge e=new GraphNode.Edge("interferes",gn2);
291 for(Iterator scopeiterator=scopenodes.iterator();scopeiterator.hasNext();) {
292 GraphNode gn2=(GraphNode)scopeiterator.next();
293 TermNode tn2=(TermNode)gn2.getOwner();
294 ScopeNode sn2=tn2.getScope();
295 if (AbstractInterferes.interferes(ar,sn2.getRule(),sn2.getSatisfy())) {
296 GraphNode.Edge e=new GraphNode.Edge("interferes",gn2);
303 void generatescopenodes() {
304 for(int i=0;i<state.vRules.size();i++) {
305 Rule r=(Rule) state.vRules.get(i);
306 ScopeNode satisfy=new ScopeNode(r,true);
307 TermNode tnsatisfy=new TermNode(satisfy);
308 GraphNode gnsatisfy=new GraphNode("SatisfyRule"+i,tnsatisfy);
309 ConsequenceNode cnsatisfy=new ConsequenceNode();
310 TermNode ctnsatisfy=new TermNode(cnsatisfy);
311 GraphNode cgnsatisfy=new GraphNode("ConseqSatisfyRule"+i,ctnsatisfy);
312 consequence.put(satisfy,cgnsatisfy);
313 GraphNode.Edge esat=new GraphNode.Edge("consequencesatisfy"+i,cgnsatisfy);
314 gnsatisfy.addEdge(esat);
315 consequencenodes.add(cgnsatisfy);
316 scopesatisfy.put(r,gnsatisfy);
317 scopenodes.add(gnsatisfy);
319 ScopeNode falsify=new ScopeNode(r,false);
320 TermNode tnfalsify=new TermNode(falsify);
321 GraphNode gnfalsify=new GraphNode("FalsifyRule"+i,tnfalsify);
322 ConsequenceNode cnfalsify=new ConsequenceNode();
323 TermNode ctnfalsify=new TermNode(cnfalsify);
324 GraphNode cgnfalsify=new GraphNode("ConseqFalsifyRule"+i,ctnfalsify);
325 consequence.put(falsify,cgnfalsify);
326 GraphNode.Edge efals=new GraphNode.Edge("consequencefalsify"+i,cgnfalsify);
327 gnfalsify.addEdge(efals);
328 consequencenodes.add(cgnfalsify);
329 scopefalsify.put(r,gnfalsify);
330 scopenodes.add(gnfalsify);
334 void generatescopeedges() {
335 for(Iterator scopeiterator=scopenodes.iterator();scopeiterator.hasNext();) {
336 GraphNode gn=(GraphNode)scopeiterator.next();
337 TermNode tn=(TermNode)gn.getOwner();
338 ScopeNode sn=tn.getScope();
340 /* Interference edges with conjunctions */
341 for(Iterator conjiterator=conjunctions.iterator();conjiterator.hasNext();) {
342 GraphNode gn2=(GraphNode)conjiterator.next();
343 TermNode tn2=(TermNode)gn2.getOwner();
344 Conjunction conj=tn2.getConjunction();
345 Constraint constr=tn2.getConstraint();
346 for(int i=0;i<conj.size();i++) {
347 DNFPredicate dp=conj.get(i);
348 if (abstractinterferes.interferes(sn,dp)||
349 AbstractInterferes.interferes(sn.getDescriptor(),sn.getSatisfy(),constr)) {
350 GraphNode.Edge e=new GraphNode.Edge("interferes",gn2);
351 GraphNode gnconseq=(GraphNode)consequence.get(sn);
358 /* Now see if this could effect other model defintion rules */
359 for(int i=0;i<state.vRules.size();i++) {
360 Rule r=(Rule) state.vRules.get(i);
361 if (AbstractInterferes.interferes(sn.getDescriptor(),sn.getSatisfy(),r,true)) {
362 GraphNode scopenode=(GraphNode)scopesatisfy.get(r);
363 GraphNode.Edge e=new GraphNode.Edge("interferes",scopenode);
364 GraphNode gnconseq=(GraphNode)consequence.get(sn);
367 if (AbstractInterferes.interferes(sn.getDescriptor(),sn.getSatisfy(),r,false)) {
368 GraphNode scopenode=(GraphNode)scopefalsify.get(r);
369 GraphNode.Edge e=new GraphNode.Edge("interferes",scopenode);
370 GraphNode gnconseq=(GraphNode)consequence.get(sn);
377 /** This method generates the abstract repair nodes. */
378 void generaterepairnodes() {
379 /* Generate repairs of conjunctions */
380 for(Iterator conjiterator=conjunctions.iterator();conjiterator.hasNext();) {
381 GraphNode gn=(GraphNode)conjiterator.next();
382 TermNode tn=(TermNode)gn.getOwner();
383 Conjunction conj=tn.getConjunction();
384 for(int i=0;i<conj.size();i++) {
385 DNFPredicate dp=conj.get(i);
386 int[] array=dp.getPredicate().getRepairs(dp.isNegated(),this);
387 Descriptor d=dp.getPredicate().getDescriptor();
388 for(int j=0;j<array.length;j++) {
389 AbstractRepair ar=new AbstractRepair(dp,array[j],d,sources);
390 TermNode tn2=new TermNode(ar);
391 GraphNode gn2=new GraphNode(gn.getLabel()+"A"+i+"B"+ar.type(),gn.getTextLabel()+" #"+i+" "+ar.type(),tn2);
392 GraphNode.Edge e=new GraphNode.Edge("abstract",gn2);
394 if (!predtoabstractmap.containsKey(dp))
395 predtoabstractmap.put(dp,new HashSet());
396 ((Set)predtoabstractmap.get(dp)).add(gn2);
397 abstractrepair.add(gn2);
401 /* Generate additional abstract repairs */
402 Vector setdescriptors=state.stSets.getAllDescriptors();
403 for(int i=0;i<setdescriptors.size();i++) {
404 SetDescriptor sd=(SetDescriptor)setdescriptors.get(i);
406 VarExpr ve=new VarExpr("DUMMY");
407 InclusionPredicate ip=new InclusionPredicate(ve,new SetExpr(sd));
408 DNFPredicate tp=new DNFPredicate(false,ip);
409 AbstractRepair ar=new AbstractRepair(tp, AbstractRepair.ADDTOSET, sd,sources);
410 TermNode tn=new TermNode(ar);
411 GraphNode gn=new GraphNode("AbstractAddSetRule"+i,tn);
412 if (!predtoabstractmap.containsKey(tp))
413 predtoabstractmap.put(tp,new HashSet());
414 ((Set)predtoabstractmap.get(tp)).add(gn);
415 abstractrepair.add(gn);
416 abstractrepairadd.add(gn);
417 abstractadd.put(sd,gn);
419 DNFPredicate tp2=new DNFPredicate(true,ip);
420 AbstractRepair ar2=new AbstractRepair(tp2, AbstractRepair.REMOVEFROMSET, sd,sources);
421 TermNode tn2=new TermNode(ar2);
422 GraphNode gn2=new GraphNode("AbstractRemSetRule"+i,tn2);
423 if (!predtoabstractmap.containsKey(tp2))
424 predtoabstractmap.put(tp2,new HashSet());
425 ((Set)predtoabstractmap.get(tp2)).add(gn2);
426 abstractrepair.add(gn2);
427 abstractrepairadd.add(gn2);
428 abstractremove.put(sd,gn2);
431 Vector relationdescriptors=state.stRelations.getAllDescriptors();
432 for(int i=0;i<relationdescriptors.size();i++) {
433 RelationDescriptor rd=(RelationDescriptor)relationdescriptors.get(i);
434 VarDescriptor vd1=new VarDescriptor("DUMMY1");
435 VarExpr ve2=new VarExpr("DUMMY2");
437 InclusionPredicate ip=new InclusionPredicate(ve2,new ImageSetExpr(vd1, rd));
439 DNFPredicate tp=new DNFPredicate(false,ip);
440 AbstractRepair ar=new AbstractRepair(tp, AbstractRepair.ADDTORELATION, rd,sources);
441 TermNode tn=new TermNode(ar);
442 GraphNode gn=new GraphNode("AbstractAddRelRule"+i,tn);
443 if (!predtoabstractmap.containsKey(tp))
444 predtoabstractmap.put(tp,new HashSet());
445 ((Set)predtoabstractmap.get(tp)).add(gn);
446 abstractrepair.add(gn);
447 abstractrepairadd.add(gn);
448 abstractadd.put(rd,gn);
450 DNFPredicate tp2=new DNFPredicate(true,ip);
451 AbstractRepair ar2=new AbstractRepair(tp2, AbstractRepair.REMOVEFROMRELATION, rd,sources);
452 TermNode tn2=new TermNode(ar2);
453 GraphNode gn2=new GraphNode("AbstractRemRelRule"+i,tn2);
454 if (!predtoabstractmap.containsKey(tp2))
455 predtoabstractmap.put(tp2,new HashSet());
456 ((Set)predtoabstractmap.get(tp2)).add(gn2);
457 abstractrepair.add(gn2);
458 abstractrepairadd.add(gn2);
459 abstractremove.put(rd,gn2);
463 int compensationcount=0;
464 void generatecompensationnodes() {
465 for(int i=0;i<state.vRules.size();i++) {
466 Rule r=(Rule) state.vRules.get(i);
467 Vector possiblerules=new Vector();
469 for(int j=0;j<(r.numQuantifiers()+r.getDNFNegGuardExpr().size());j++) {
470 GraphNode gn=(GraphNode)scopesatisfy.get(r);
471 TermNode tn=(TermNode) gn.getOwner();
472 ScopeNode sn=tn.getScope();
473 MultUpdateNode mun=new MultUpdateNode(sn);
474 TermNode tn2=new TermNode(mun);
475 GraphNode gn2=new GraphNode("CompRem"+compensationcount,tn2);
476 UpdateNode un=new UpdateNode(r);
478 if (j<r.numQuantifiers()) {
479 /* Remove quantifier */
480 Quantifier q=r.getQuantifier(j);
481 if (q instanceof RelationQuantifier) {
482 RelationQuantifier rq=(RelationQuantifier)q;
483 TupleOfExpr toe=new TupleOfExpr(new VarExpr(rq.x),new VarExpr(rq.y),rq.relation);
484 toe.td=ReservedTypeDescriptor.INT;
485 Updates u=new Updates(toe,true);
487 if (abstractremove.containsKey(rq.relation)) {
488 GraphNode agn=(GraphNode)abstractremove.get(rq.relation);
489 GraphNode.Edge e=new GraphNode.Edge("requires",agn);
492 continue; /* Abstract repair doesn't exist */
494 } else if (q instanceof SetQuantifier) {
495 SetQuantifier sq=(SetQuantifier)q;
496 ElementOfExpr eoe=new ElementOfExpr(new VarExpr(sq.var),sq.set);
497 eoe.td=ReservedTypeDescriptor.INT;
498 Updates u=new Updates(eoe,true);
500 if (abstractremove.containsKey(sq.set)) {
501 GraphNode agn=(GraphNode)abstractremove.get(sq.set);
502 GraphNode.Edge e=new GraphNode.Edge("requires",agn);
505 continue; /* Abstract repair doesn't exist */
511 /* Negate conjunction */
512 int c=j-r.numQuantifiers();
513 if (!processconjunction(un,r.getDNFNegGuardExpr().get(c),null)) {
517 if (!un.checkupdates()) /* Make sure we have a good update */
521 GraphNode.Edge e=new GraphNode.Edge("abstract"+compensationcount,gn2);
524 updatenodes.add(gn2);
529 void generatedatastructureupdatenodes() {
530 for(Iterator absiterator=abstractrepair.iterator();absiterator.hasNext();) {
531 GraphNode gn=(GraphNode)absiterator.next();
532 System.out.println("Analysing: "+gn.getTextLabel());
533 TermNode tn=(TermNode) gn.getOwner();
534 AbstractRepair ar=tn.getAbstract();
535 if (ar.getType()==AbstractRepair.ADDTOSET) {
536 generateaddtosetrelation(gn,ar);
537 } else if (ar.getType()==AbstractRepair.REMOVEFROMSET) {
538 generateremovefromsetrelation(gn,ar);
539 } else if (ar.getType()==AbstractRepair.ADDTORELATION) {
540 generateaddtosetrelation(gn,ar);
541 } else if (ar.getType()==AbstractRepair.REMOVEFROMRELATION) {
542 generateremovefromsetrelation(gn,ar);
543 } else if (ar.getType()==AbstractRepair.MODIFYRELATION) {
544 /* Generate remove/add pairs */
545 generateremovefromsetrelation(gn,ar);
546 generateaddtosetrelation(gn,ar);
547 /* Generate atomic modify */
548 generatemodifyrelation(gn,ar);
554 /** This method generates concrete data structure updates which
555 * remove an object (or tuple) from a set (or relation).*/
557 int removefromcount=0;
558 void generateremovefromsetrelation(GraphNode gn,AbstractRepair ar) {
559 /* Construct the set of all rules which could add something to the given set or relation */
561 Vector possiblerules=new Vector();
562 for(int i=0;i<state.vRules.size();i++) {
563 Rule r=(Rule) state.vRules.get(i);
564 if ((r.getInclusion() instanceof SetInclusion)&&
565 (ar.getDescriptor()==((SetInclusion)r.getInclusion()).getSet()))
566 possiblerules.add(r);
567 if ((r.getInclusion() instanceof RelationInclusion)&&
568 (ar.getDescriptor()==((RelationInclusion)r.getInclusion()).getRelation()))
569 possiblerules.add(r);
571 if (possiblerules.size()==0)
574 /* Loop through different ways of falsifying each of these rules */
575 int[] count=new int[possiblerules.size()];
576 while(remains(count,possiblerules,true)) {
577 MultUpdateNode mun=new MultUpdateNode(ar,MultUpdateNode.REMOVE);
578 TermNode tn=new TermNode(mun);
579 GraphNode gn2=new GraphNode("UpdateRem"+removefromcount,tn);
581 boolean goodflag=true;
582 for(int i=0;i<possiblerules.size();i++) {
583 Rule r=(Rule)possiblerules.get(i);
584 UpdateNode un=new UpdateNode(r);
586 if (count[i]<r.numQuantifiers()) {
587 /* Remove quantifier */
588 Quantifier q=r.getQuantifier(count[i]);
589 if (q instanceof RelationQuantifier) {
590 RelationQuantifier rq=(RelationQuantifier)q;
591 TupleOfExpr toe=new TupleOfExpr(new VarExpr(rq.x),new VarExpr(rq.y),rq.relation);
592 toe.td=ReservedTypeDescriptor.INT;
593 Updates u=new Updates(toe,true);
595 if (abstractremove.containsKey(rq.relation)) {
596 GraphNode agn=(GraphNode)abstractremove.get(rq.relation);
597 GraphNode.Edge e=new GraphNode.Edge("requires",agn);
600 goodflag=false;break; /* Abstract repair doesn't exist */
602 } else if (q instanceof SetQuantifier) {
603 SetQuantifier sq=(SetQuantifier)q;
604 ElementOfExpr eoe=new ElementOfExpr(new VarExpr(sq.var),sq.set);
605 eoe.td=ReservedTypeDescriptor.INT;
606 Updates u=new Updates(eoe,true);
608 if (abstractremove.containsKey(sq.set)) {
609 GraphNode agn=(GraphNode)abstractremove.get(sq.set);
610 GraphNode.Edge e=new GraphNode.Edge("requires",agn);
613 goodflag=false;break; /* Abstract repair doesn't exist */
615 } else {goodflag=false;break;}
617 int c=count[i]-r.numQuantifiers();
618 if (!processconjunction(un,r.getDNFNegGuardExpr().get(c),null)) {
619 goodflag=false;break;
622 if (!un.checkupdates()) {
629 GraphNode.Edge e=new GraphNode.Edge("abstract"+removefromcount,gn2);
632 updatenodes.add(gn2);
634 increment(count,possiblerules,true);
638 /** This method increments to the next possibility. */
640 static private void increment(int count[], Vector rules,boolean isremove) {
642 for(int i=0;i<(rules.size()-1);i++) {
643 int num=isremove?(((Rule)rules.get(i)).numQuantifiers()+(((Rule)rules.get(i)).getDNFNegGuardExpr().size())):((Rule)rules.get(i)).getDNFGuardExpr().size();
652 /** This method test if there remain any possibilities to loop
654 static private boolean remains(int count[], Vector rules, boolean isremove) {
655 for(int i=0;i<rules.size();i++) {
656 int num=isremove?(((Rule)rules.get(i)).numQuantifiers()+(((Rule)rules.get(i)).getDNFNegGuardExpr().size())):((Rule)rules.get(i)).getDNFGuardExpr().size();
664 /** This method generates data structure updates to implement the
665 * abstract atomic modification specified by ar. */
668 void generatemodifyrelation(GraphNode gn, AbstractRepair ar) {
669 RelationDescriptor rd=(RelationDescriptor)ar.getDescriptor();
670 ExprPredicate exprPredicate=(ExprPredicate)ar.getPredicate().getPredicate();
671 boolean inverted=exprPredicate.inverted();
679 // construct set of possible rules
680 Vector possiblerules=new Vector();
681 for(int i=0;i<state.vRules.size();i++) {
682 Rule r=(Rule) state.vRules.get(i);
683 if ((r.getInclusion() instanceof RelationInclusion)&&
684 (rd==((RelationInclusion)r.getInclusion()).getRelation()))
685 possiblerules.add(r);
687 if (possiblerules.size()==0)
690 // increment through this set
691 int[] count=new int[possiblerules.size()];
692 while(remains(count,possiblerules,false)) {
693 MultUpdateNode mun=new MultUpdateNode(ar,MultUpdateNode.MODIFY);
694 TermNode tn=new TermNode(mun);
695 GraphNode gn2=new GraphNode("UpdateMod"+removefromcount,tn);
697 boolean goodflag=true;
698 for(int i=0;i<possiblerules.size();i++) {
699 Rule r=(Rule)possiblerules.get(i);
700 UpdateNode un=new UpdateNode(r);
703 if (!processconjunction(un,r.getDNFGuardExpr().get(c),null)) {
704 goodflag=false;break;
706 RelationInclusion ri=(RelationInclusion)r.getInclusion();
707 if (!(ri.getLeftExpr() instanceof VarExpr)) {
708 if (ri.getLeftExpr().isValue(ri.getRelation().getDomain().getType())) {
709 Updates up=new Updates(ri.getLeftExpr(),leftindex,ri.getRelation().getDomain().getType());
714 else un.addInvariant(ri.getLeftExpr());
717 VarDescriptor vd=((VarExpr)ri.getLeftExpr()).getVar();
719 Updates up=new Updates(ri.getLeftExpr(),leftindex,null);
724 if (!(ri.getRightExpr() instanceof VarExpr)) {
725 if (ri.getRightExpr().isValue(ri.getRelation().getRange().getType())) {
726 Updates up=new Updates(ri.getRightExpr(),rightindex,ri.getRelation().getRange().getType());
732 un.addInvariant(ri.getLeftExpr());
735 VarDescriptor vd=((VarExpr)ri.getRightExpr()).getVar();
737 Updates up=new Updates(ri.getRightExpr(),rightindex,null);
739 } else if (!inverted)
743 if (!un.checkupdates()) {
750 GraphNode.Edge e=new GraphNode.Edge("abstract"+modifycount,gn2);
753 updatenodes.add(gn2);
755 increment(count,possiblerules,false);
759 /** Generate concrete data structure update to add an object(or
760 * tuple) to a set (or relation). */
762 static int addtocount=0;
763 void generateaddtosetrelation(GraphNode gn, AbstractRepair ar) {
764 for(int i=0;i<state.vRules.size();i++) {
765 Rule r=(Rule) state.vRules.get(i);
768 /* See if this is a good rule*/
769 if ((r.getInclusion() instanceof SetInclusion&&
770 ar.getDescriptor()==((SetInclusion)r.getInclusion()).getSet())||
771 (r.getInclusion() instanceof RelationInclusion&&
772 ar.getDescriptor()==((RelationInclusion)r.getInclusion()).getRelation())) {
774 /* First solve for quantifiers */
775 Vector bindings=new Vector();
776 /* Construct bindings */
778 Hashtable setmapping=new Hashtable();
779 System.out.println("Attempting to construct bindings");
781 if (!constructbindings(bindings,r,ar,setmapping,false))
783 System.out.println("Bindings constructed");
784 //Generate add instruction
785 DNFRule dnfrule=r.getDNFGuardExpr();
787 for(int j=0;j<dnfrule.size();j++) {
788 Inclusion inc=r.getInclusion();
789 UpdateNode un=new UpdateNode(r);
790 un.addBindings(bindings);
791 /* Now build update for tuple/set inclusion condition */
792 if(inc instanceof SetInclusion) {
793 SetInclusion si=(SetInclusion)inc;
794 Expr e=si.elementexpr;
796 while(e instanceof CastExpr) {
797 CastExpr ce=(CastExpr)e;
798 if (ce.getType()!=si.getSet().getType())
803 if (!(e instanceof VarExpr)) {
804 if (e.isValue(si.getSet().getType())) {
805 Updates up=new Updates(e,0,si.getSet().getType());
808 /* We're an add to set*/
809 ArrayAnalysis.AccessPath rap=arrayanalysis.analyzeExpr(r,e);
810 SetDescriptor set=sources.setSource(si.getSet())?
811 sources.getSourceSet(si.getSet()):null;
814 ArrayAnalysis.AccessPath ap=arrayanalysis.getSet(set);
815 if (rap==ArrayAnalysis.AccessPath.NONE)
819 if (!constructarrayupdate(un, e, rap, 0))
823 VarDescriptor vd=((VarExpr)e).getVar();
825 Updates up=new Updates(e,0,null);
829 } else if (inc instanceof RelationInclusion) {
830 RelationInclusion ri=(RelationInclusion)inc;
832 Expr e=ri.getLeftExpr();
834 while(e instanceof CastExpr) {
835 CastExpr ce=(CastExpr)e;
836 if (ce.getType()!=ri.getRelation().getDomain().getType())
840 if (!(e instanceof VarExpr)) {
841 if (e.isValue(ri.getRelation().getDomain().getType())) {
842 Updates up=new Updates(e,0,ri.getRelation().getDomain().getType());
843 if (ar.getDomainSet()!=null)
844 setmapping.put(e,ar.getDomainSet());
847 /* We don't handly relation modifies */
848 if (ar.getType()==AbstractRepair.MODIFYRELATION)
850 /* We're an add to relation*/
851 ArrayAnalysis.AccessPath rap=arrayanalysis.analyzeExpr(r,e);
852 SetDescriptor set=sources.relsetSource(ri.getRelation(),true /* Domain*/)?
853 sources.relgetSourceSet(ri.getRelation(),true):null;
856 ArrayAnalysis.AccessPath ap=arrayanalysis.getSet(set);
857 if (rap==ArrayAnalysis.AccessPath.NONE||
859 !constructarrayupdate(un, e, rap, 0))
861 if (ar.getDomainSet()!=null)
862 setmapping.put(e,ar.getDomainSet());
866 VarDescriptor vd=((VarExpr)e).getVar();
868 Updates up=new Updates(e,0,null);
869 if (ar.getDomainSet()!=null)
870 setmapping.put(e,ar.getDomainSet());
877 while(e instanceof CastExpr) {
878 CastExpr ce=(CastExpr)e;
879 if (ce.getType()!=ri.getRelation().getRange().getType())
883 if (!(e instanceof VarExpr)) {
884 if (e.isValue(ri.getRelation().getRange().getType())) {
885 Updates up=new Updates(e,1,ri.getRelation().getRange().getType());
888 /* We don't handly relation modifies */
889 if (ar.getType()==AbstractRepair.MODIFYRELATION)
891 /* We're an add to relation*/
892 ArrayAnalysis.AccessPath rap=arrayanalysis.analyzeExpr(r,e);
893 SetDescriptor set=sources.relsetSource(ri.getRelation(),false /* Range*/)?
894 sources.relgetSourceSet(ri.getRelation(),false):null;
897 ArrayAnalysis.AccessPath ap=arrayanalysis.getSet(set);
899 if (rap==ArrayAnalysis.AccessPath.NONE||
901 !constructarrayupdate(un, e, rap, 1))
903 if (ar.getRangeSet()!=null)
904 setmapping.put(e,ar.getRangeSet());
907 VarDescriptor vd=((VarExpr)e).getVar();
909 Updates up=new Updates(e,1,null);
910 if (ar.getRangeSet()!=null)
911 setmapping.put(e,ar.getRangeSet());
916 System.out.println("Built inclusion condition updates.");
917 //Finally build necessary updates to satisfy conjunction
918 RuleConjunction ruleconj=dnfrule.get(j);
920 /* Add in updates for quantifiers */
921 MultUpdateNode mun=new MultUpdateNode(ar,MultUpdateNode.ADD);
922 TermNode tn=new TermNode(mun);
923 GraphNode gn2=new GraphNode("UpdateAdd"+addtocount,tn);
925 if (debugmsg("Start processing quantifiers")&&
926 processquantifiers(gn2,un, r,setmapping)&&
927 debugmsg("Finished processing quantifiers")&&
928 processconjunction(un,ruleconj,setmapping)&&
929 debugmsg("Finished processing conjunction")&&
931 debugmsg("Updates checked")) {
933 GraphNode.Edge e=new GraphNode.Edge("abstract"+addtocount,gn2);
936 updatenodes.add(gn2);
944 boolean debugmsg(String st) {
945 System.out.println(st);
949 boolean constructarrayupdate(UpdateNode un, Expr lexpr, ArrayAnalysis.AccessPath ap, int slotnumber) {
950 System.out.println("Constructing array update");
952 for (int i=ap.numFields()-1;i>=0;i--) {
956 e=((DotExpr)e).getExpr();
958 while (e instanceof CastExpr)
959 e=((CastExpr)e).getExpr();
961 DotExpr de=(DotExpr)e;
962 FieldDescriptor fd=ap.getField(i);
963 if (fd instanceof ArrayDescriptor) {
964 // We have an ArrayDescriptor!
965 Expr index=de.getIndex();
966 if (!index.isValue(null)) {/* Not assignable */
967 System.out.println("ERROR:Index isn't assignable");
970 Updates updates=new Updates(index,i,ap,lexpr,slotnumber);
971 un.addUpdate(updates);
977 /** This method constructs bindings for an update using rule
978 * r. The boolean flag isremoval indicates that the update
979 * performs a removal. The function returns true if it is able to
980 * generate a valid set of bindings and false otherwise. */
982 boolean constructbindings(Vector bindings, Rule r, AbstractRepair ar, Hashtable setmapping, boolean isremoval) {
983 boolean goodupdate=true;
984 Inclusion inc=r.getInclusion();
986 for(Iterator iterator=r.quantifiers();iterator.hasNext();) {
987 Quantifier q=(Quantifier)iterator.next();
988 if ((q instanceof SetQuantifier)||(q instanceof ForQuantifier)) {
989 VarDescriptor vd=null;
990 SetDescriptor set=null;
991 if (q instanceof SetQuantifier) {
992 vd=((SetQuantifier)q).getVar();
993 set=((SetQuantifier)q).getSet();
995 vd=((ForQuantifier)q).getVar();
997 if(inc instanceof SetInclusion) {
998 SetInclusion si=(SetInclusion)inc;
999 Expr tmpe=si.elementexpr;
1000 Expr e=si.elementexpr;
1002 while(e instanceof CastExpr) {
1003 CastExpr ce=(CastExpr)e;
1004 if (ce.getType()!=si.getSet().getType())
1009 if ((e instanceof VarExpr)&&
1010 (((VarExpr)e).getVar()==vd)) {
1011 /* Can solve for v */
1012 if (!si.getSet().getType().isSubtypeOf(set.getType()))
1014 Binding binding=new Binding(vd,0);
1015 bindings.add(binding);
1019 } else if (inc instanceof RelationInclusion) {
1020 RelationInclusion ri=(RelationInclusion)inc;
1024 Expr e=ri.getLeftExpr();
1026 while(e instanceof CastExpr) {
1027 CastExpr ce=(CastExpr)e;
1028 if (ce.getType()!=ri.getRelation().getDomain().getType())
1033 if ((e instanceof VarExpr)&&
1034 (((VarExpr)e).getVar()==vd)) {
1035 /* Can solve for v */
1036 Binding binding=new Binding(vd,0);
1037 if (!ri.getRelation().getDomain().getType().isSubtypeOf(set.getType()))
1039 if (ar.getDomainSet()!=null)
1040 setmapping.put(ri.getLeftExpr(),ar.getDomainSet());
1041 bindings.add(binding);
1045 e=ri.getRightExpr();
1047 while(e instanceof CastExpr) {
1048 CastExpr ce=(CastExpr)e;
1049 if (ce.getType()!=ri.getRelation().getRange().getType())
1054 if ((e instanceof VarExpr)&&
1055 (((VarExpr)e).getVar()==vd)) {
1056 /* Can solve for v */
1057 Binding binding=new Binding(vd,1);
1058 if (!ri.getRelation().getRange().getType().isSubtypeOf(set.getType()))
1060 if (ar.getRangeSet()!=null)
1061 setmapping.put(ri.getRightExpr(),ar.getRangeSet());
1062 bindings.add(binding);
1066 } else throw new Error("Inclusion not recognized");
1069 /* Removals don't need bindings anymore
1070 Binding binding=new Binding(vd);
1071 bindings.add(binding);*/
1073 } else if (q instanceof SetQuantifier) {
1074 /* Create new element to bind to */
1075 // search if the set 'set' has a size
1076 Binding binding=new Binding(vd,set,exactsize.getsize(set)==1);
1077 bindings.add(binding);
1082 } else if (q instanceof RelationQuantifier) {
1083 RelationQuantifier rq=(RelationQuantifier)q;
1084 for(int k=0;k<2;k++) {
1085 VarDescriptor vd=(k==0)?rq.x:rq.y;
1086 TypeDescriptor td=(k==0)?rq.getRelation().getDomain().getType():rq.getRelation().getRange().getType();
1087 if(inc instanceof SetInclusion) {
1088 SetInclusion si=(SetInclusion)inc;
1090 Expr e=si.elementexpr;
1092 while(e instanceof CastExpr) {
1093 CastExpr ce=(CastExpr)e;
1094 if (ce.getType()!=td)
1099 if ((e instanceof VarExpr)&&
1100 (((VarExpr)e).getVar()==vd)) {
1101 /* Can solve for v */
1102 Binding binding=new Binding(vd,0);
1104 if (!si.getSet().getType().isSubtypeOf(td))
1106 bindings.add(binding);
1109 } else if (inc instanceof RelationInclusion) {
1110 RelationInclusion ri=(RelationInclusion)inc;
1115 Expr e=ri.getLeftExpr();
1117 while(e instanceof CastExpr) {
1118 CastExpr ce=(CastExpr)e;
1119 if (ce.getType()!=ri.getRelation().getDomain().getType())
1123 if ((ri.getLeftExpr() instanceof VarExpr)&&
1124 (((VarExpr)ri.getLeftExpr()).getVar()==vd)) {
1125 /* Can solve for v */
1126 Binding binding=new Binding(vd,0);
1127 if (!ri.getRelation().getDomain().getType().isSubtypeOf(td))
1129 if (ar.getDomainSet()!=null)
1130 setmapping.put(ri.getLeftExpr(),ar.getDomainSet());
1131 bindings.add(binding);
1135 e=ri.getRightExpr();
1137 while(e instanceof CastExpr) {
1138 CastExpr ce=(CastExpr)e;
1139 if (ce.getType()!=ri.getRelation().getRange().getType())
1143 if ((ri.getRightExpr() instanceof VarExpr)&&
1144 (((VarExpr)ri.getRightExpr()).getVar()==vd)) {
1145 /* Can solve for v */
1146 Binding binding=new Binding(vd,1);
1147 if (!ri.getRelation().getRange().getType().isSubtypeOf(td))
1149 if (ar.getRangeSet()!=null)
1150 setmapping.put(ri.getRightExpr(),ar.getRangeSet());
1151 bindings.add(binding);
1155 } else throw new Error("Inclusion not recognized");
1158 /* Removals don't need bindings anymore
1159 Binding binding=new Binding(vd);
1160 bindings.add(binding);*/
1167 } else throw new Error("Quantifier not recognized");
1172 /** Adds updates that add an item to the appropriate set or
1173 * relation quantified over by the model definition rule.. */
1175 boolean processquantifiers(GraphNode gn,UpdateNode un, Rule r,Hashtable setmapping) {
1176 Inclusion inc=r.getInclusion();
1177 for(Iterator iterator=r.quantifiers();iterator.hasNext();) {
1178 Quantifier q=(Quantifier)iterator.next();
1179 /* Add quantifier */
1180 if (q instanceof RelationQuantifier) {
1181 RelationQuantifier rq=(RelationQuantifier)q;
1182 TupleOfExpr toe=new TupleOfExpr(new VarExpr(rq.x),new VarExpr(rq.y),rq.relation);
1183 toe.td=ReservedTypeDescriptor.INT;
1184 Updates u=new Updates(toe,false);
1186 if (abstractadd.containsKey(rq.relation)) {
1187 GraphNode agn=(GraphNode)abstractadd.get(rq.relation);
1188 GraphNode.Edge e=new GraphNode.Edge("requires",agn);
1194 } else if (q instanceof SetQuantifier) {
1195 SetQuantifier sq=(SetQuantifier)q;
1196 if (un.getBinding(sq.var).getType()==Binding.SEARCH) {
1197 Binding b=un.getBinding(sq.var);
1198 Constraint reqc=exactsize.getConstraint(b.getSet());
1199 constraintdependence.requiresConstraint(gn,reqc);
1200 continue; /* Don't need to ensure addition for search */
1202 VarExpr ve=new VarExpr(sq.var);
1203 SetDescriptor sd=findExpr(setmapping, ve);
1204 if (sd!=null&&sd.isSubset(sq.set))
1205 continue; /* this update is trivially true */
1207 ElementOfExpr eoe=new ElementOfExpr(ve,sq.set);
1208 eoe.td=ReservedTypeDescriptor.INT;
1209 Updates u=new Updates(eoe,false);
1211 if (abstractadd.containsKey(sq.set)) {
1212 GraphNode agn=(GraphNode)abstractadd.get(sq.set);
1213 GraphNode.Edge e=new GraphNode.Edge("requires",agn);
1218 } else return false;
1223 static SetDescriptor findExpr(Hashtable setmapping, Expr e) {
1224 if (setmapping==null)
1226 Set kset=setmapping.keySet();
1227 for(Iterator it=kset.iterator();it.hasNext();) {
1228 Expr expr=(Expr)it.next();
1229 if (expr.equals(null,e)) {
1230 return (SetDescriptor)setmapping.get(expr);
1236 /** This method generates the necessary updates to satisfy the
1237 * conjunction ruleconj. */
1239 boolean processconjunction(UpdateNode un,RuleConjunction ruleconj,Hashtable setmapping) {
1241 for(int k=0;k<ruleconj.size();k++) {
1242 DNFExpr de=ruleconj.get(k);
1243 Expr e=de.getExpr();
1244 if (e instanceof OpExpr) {
1245 OpExpr ex=(OpExpr)de.getExpr();
1246 Opcode op=ex.getOpcode();
1247 Updates up=new Updates(ex.left,ex.right,op, de.getNegation());
1249 } else if (e instanceof ElementOfExpr) {
1250 SetDescriptor sd=findExpr(setmapping, ((ElementOfExpr)e).element);
1251 if (sd!=null&&sd.isSubset(((ElementOfExpr)e).set))
1252 continue; /* this update is trivially true */
1253 Updates up=new Updates(e,de.getNegation());
1255 } else if (e instanceof TupleOfExpr) {
1256 Updates up=new Updates(e,de.getNegation());
1258 } else if (e instanceof BooleanLiteralExpr) {
1259 boolean truth=((BooleanLiteralExpr)e).getValue();
1260 if (de.getNegation())
1267 System.out.println(e.getClass().getName());
1268 throw new Error("Unrecognized Expr");
1274 /** This method sees if when the quantifiers listed in set are
1275 * fixed, whether there can be more than one unique binding for
1276 * the constraint or model definition rule qs.*/
1277 public boolean analyzeQuantifiers(Quantifiers qs,Set set) {
1278 for(int i=0;i<qs.numQuantifiers();i++) {
1279 Quantifier q=qs.getQuantifier(i);
1280 if (set.contains(q))
1282 if (q instanceof SetQuantifier) {
1283 SetDescriptor sd=((SetQuantifier)q).getSet();
1284 if (maxsize.getsize(sd)<=1&&
1285 maxsize.getsize(sd)>=0)
1287 } else if (q instanceof RelationQuantifier) {
1288 RelationDescriptor rd=((RelationQuantifier)q).getRelation();
1289 if (maxsize.getsize(rd)<=1&&
1290 maxsize.getsize(rd)>=0)
1298 public boolean mutuallyexclusive(SetDescriptor sd1, SetDescriptor sd2) {
1299 if (mutualexclusive(sd1,sd2)||
1300 mutualexclusive(sd2,sd1))
1306 private boolean mutualexclusive(SetDescriptor sd1, SetDescriptor sd2) {
1307 Vector rules=state.vRules;
1308 for(int i=0;i<rules.size();i++) {
1309 Rule r=(Rule)rules.get(i);
1310 if (r.getInclusion().getTargetDescriptors().contains(sd1)) {
1311 /* Rule may add items to sd1 */
1312 SetInclusion si=(SetInclusion)r.getInclusion();
1313 Expr ve=si.getExpr();
1314 DNFRule drule=r.getDNFGuardExpr();
1315 for(int j=0;j<drule.size();j++) {
1316 RuleConjunction rconj=drule.get(j);
1317 boolean containsexclusion=false;
1318 for (int k=0;k<rconj.size();k++) {
1319 DNFExpr dexpr=rconj.get(k);
1320 if (dexpr.getNegation()&&
1321 dexpr.getExpr() instanceof ElementOfExpr&&
1322 ((ElementOfExpr)dexpr.getExpr()).element.equals(null,ve)) {
1323 SetDescriptor sd=((ElementOfExpr)dexpr.getExpr()).set;
1324 if (sd.isSubset(sd2))
1325 containsexclusion=true;
1328 if (!containsexclusion)