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.interferesquantifier(ar,cons)||
284 abstractinterferes.interfereswithpredicate(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.interfereswithrule(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.scopeinterfereswithpredicate(sn,dp)||
349 AbstractInterferes.interfereswithquantifier(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.interfereswithrule(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.interfereswithrule(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);
943 boolean debugmsg(String st) {
944 System.out.println(st);
948 boolean constructarrayupdate(UpdateNode un, Expr lexpr, ArrayAnalysis.AccessPath ap, int slotnumber) {
949 System.out.println("Constructing array update");
951 for (int i=ap.numFields()-1;i>=0;i--) {
955 e=((DotExpr)e).getExpr();
957 while (e instanceof CastExpr)
958 e=((CastExpr)e).getExpr();
960 DotExpr de=(DotExpr)e;
961 FieldDescriptor fd=ap.getField(i);
962 if (fd instanceof ArrayDescriptor) {
963 // We have an ArrayDescriptor!
964 Expr index=de.getIndex();
965 if (!index.isValue(null)) {/* Not assignable */
966 System.out.println("ERROR:Index isn't assignable");
969 Updates updates=new Updates(index,i,ap,lexpr,slotnumber);
970 un.addUpdate(updates);
976 /** This method constructs bindings for an update using rule
977 * r. The boolean flag isremoval indicates that the update
978 * performs a removal. The function returns true if it is able to
979 * generate a valid set of bindings and false otherwise. */
981 boolean constructbindings(Vector bindings, Rule r, AbstractRepair ar, Hashtable setmapping, boolean isremoval) {
982 boolean goodupdate=true;
983 Inclusion inc=r.getInclusion();
985 for(Iterator iterator=r.quantifiers();iterator.hasNext();) {
986 Quantifier q=(Quantifier)iterator.next();
987 if ((q instanceof SetQuantifier)||(q instanceof ForQuantifier)) {
988 VarDescriptor vd=null;
989 SetDescriptor set=null;
990 if (q instanceof SetQuantifier) {
991 vd=((SetQuantifier)q).getVar();
992 set=((SetQuantifier)q).getSet();
994 vd=((ForQuantifier)q).getVar();
996 if(inc instanceof SetInclusion) {
997 SetInclusion si=(SetInclusion)inc;
998 Expr tmpe=si.elementexpr;
999 Expr e=si.elementexpr;
1001 while(e instanceof CastExpr) {
1002 CastExpr ce=(CastExpr)e;
1003 if (ce.getType()!=si.getSet().getType())
1008 if ((e instanceof VarExpr)&&
1009 (((VarExpr)e).getVar()==vd)) {
1010 /* Can solve for v */
1011 if (!si.getSet().getType().isSubtypeOf(set.getType()))
1013 Binding binding=new Binding(vd,0);
1014 bindings.add(binding);
1018 } else if (inc instanceof RelationInclusion) {
1019 RelationInclusion ri=(RelationInclusion)inc;
1023 Expr e=ri.getLeftExpr();
1025 while(e instanceof CastExpr) {
1026 CastExpr ce=(CastExpr)e;
1027 if (ce.getType()!=ri.getRelation().getDomain().getType())
1032 if ((e instanceof VarExpr)&&
1033 (((VarExpr)e).getVar()==vd)) {
1034 /* Can solve for v */
1035 Binding binding=new Binding(vd,0);
1036 if (!ri.getRelation().getDomain().getType().isSubtypeOf(set.getType()))
1038 if (ar.getDomainSet()!=null)
1039 setmapping.put(ri.getLeftExpr(),ar.getDomainSet());
1040 bindings.add(binding);
1044 e=ri.getRightExpr();
1046 while(e instanceof CastExpr) {
1047 CastExpr ce=(CastExpr)e;
1048 if (ce.getType()!=ri.getRelation().getRange().getType())
1053 if ((e instanceof VarExpr)&&
1054 (((VarExpr)e).getVar()==vd)) {
1055 /* Can solve for v */
1056 Binding binding=new Binding(vd,1);
1057 if (!ri.getRelation().getRange().getType().isSubtypeOf(set.getType()))
1059 if (ar.getRangeSet()!=null)
1060 setmapping.put(ri.getRightExpr(),ar.getRangeSet());
1061 bindings.add(binding);
1065 } else throw new Error("Inclusion not recognized");
1068 /* Removals don't need bindings anymore
1069 Binding binding=new Binding(vd);
1070 bindings.add(binding);*/
1072 } else if (q instanceof SetQuantifier) {
1073 /* Create new element to bind to */
1074 // search if the set 'set' has a size
1075 Binding binding=new Binding(vd,set,exactsize.getsize(set)==1);
1076 bindings.add(binding);
1081 } else if (q instanceof RelationQuantifier) {
1082 RelationQuantifier rq=(RelationQuantifier)q;
1083 for(int k=0;k<2;k++) {
1084 VarDescriptor vd=(k==0)?rq.x:rq.y;
1085 TypeDescriptor td=(k==0)?rq.getRelation().getDomain().getType():rq.getRelation().getRange().getType();
1086 if(inc instanceof SetInclusion) {
1087 SetInclusion si=(SetInclusion)inc;
1089 Expr e=si.elementexpr;
1091 while(e instanceof CastExpr) {
1092 CastExpr ce=(CastExpr)e;
1093 if (ce.getType()!=td)
1098 if ((e instanceof VarExpr)&&
1099 (((VarExpr)e).getVar()==vd)) {
1100 /* Can solve for v */
1101 Binding binding=new Binding(vd,0);
1103 if (!si.getSet().getType().isSubtypeOf(td))
1105 bindings.add(binding);
1108 } else if (inc instanceof RelationInclusion) {
1109 RelationInclusion ri=(RelationInclusion)inc;
1114 Expr e=ri.getLeftExpr();
1116 while(e instanceof CastExpr) {
1117 CastExpr ce=(CastExpr)e;
1118 if (ce.getType()!=ri.getRelation().getDomain().getType())
1122 if ((ri.getLeftExpr() instanceof VarExpr)&&
1123 (((VarExpr)ri.getLeftExpr()).getVar()==vd)) {
1124 /* Can solve for v */
1125 Binding binding=new Binding(vd,0);
1126 if (!ri.getRelation().getDomain().getType().isSubtypeOf(td))
1128 if (ar.getDomainSet()!=null)
1129 setmapping.put(ri.getLeftExpr(),ar.getDomainSet());
1130 bindings.add(binding);
1134 e=ri.getRightExpr();
1136 while(e instanceof CastExpr) {
1137 CastExpr ce=(CastExpr)e;
1138 if (ce.getType()!=ri.getRelation().getRange().getType())
1142 if ((ri.getRightExpr() instanceof VarExpr)&&
1143 (((VarExpr)ri.getRightExpr()).getVar()==vd)) {
1144 /* Can solve for v */
1145 Binding binding=new Binding(vd,1);
1146 if (!ri.getRelation().getRange().getType().isSubtypeOf(td))
1148 if (ar.getRangeSet()!=null)
1149 setmapping.put(ri.getRightExpr(),ar.getRangeSet());
1150 bindings.add(binding);
1154 } else throw new Error("Inclusion not recognized");
1157 /* Removals don't need bindings anymore
1158 Binding binding=new Binding(vd);
1159 bindings.add(binding);*/
1166 } else throw new Error("Quantifier not recognized");
1171 /** Adds updates that add an item to the appropriate set or
1172 * relation quantified over by the model definition rule.. */
1174 boolean processquantifiers(GraphNode gn,UpdateNode un, Rule r,Hashtable setmapping) {
1175 Inclusion inc=r.getInclusion();
1176 for(Iterator iterator=r.quantifiers();iterator.hasNext();) {
1177 Quantifier q=(Quantifier)iterator.next();
1178 /* Add quantifier */
1179 if (q instanceof RelationQuantifier) {
1180 RelationQuantifier rq=(RelationQuantifier)q;
1181 TupleOfExpr toe=new TupleOfExpr(new VarExpr(rq.x),new VarExpr(rq.y),rq.relation);
1182 toe.td=ReservedTypeDescriptor.INT;
1183 Updates u=new Updates(toe,false);
1185 if (abstractadd.containsKey(rq.relation)) {
1186 GraphNode agn=(GraphNode)abstractadd.get(rq.relation);
1187 GraphNode.Edge e=new GraphNode.Edge("requires",agn);
1192 } else if (q instanceof SetQuantifier) {
1193 SetQuantifier sq=(SetQuantifier)q;
1194 if (un.getBinding(sq.var).getType()==Binding.SEARCH) {
1195 Binding b=un.getBinding(sq.var);
1196 Constraint reqc=exactsize.getConstraint(b.getSet());
1197 constraintdependence.requiresConstraint(gn,reqc);
1198 continue; /* Don't need to ensure addition for search */
1200 VarExpr ve=new VarExpr(sq.var);
1201 SetDescriptor sd=findExpr(setmapping, ve);
1202 if (sd!=null&&sd.isSubset(sq.set))
1203 continue; /* this update is trivially true */
1205 ElementOfExpr eoe=new ElementOfExpr(ve,sq.set);
1206 eoe.td=ReservedTypeDescriptor.INT;
1207 Updates u=new Updates(eoe,false);
1209 if (abstractadd.containsKey(sq.set)) {
1210 GraphNode agn=(GraphNode)abstractadd.get(sq.set);
1211 GraphNode.Edge e=new GraphNode.Edge("requires",agn);
1216 } else return false;
1221 static SetDescriptor findExpr(Hashtable setmapping, Expr e) {
1222 if (setmapping==null)
1224 Set kset=setmapping.keySet();
1225 for(Iterator it=kset.iterator();it.hasNext();) {
1226 Expr expr=(Expr)it.next();
1227 if (expr.equals(null,e)) {
1228 return (SetDescriptor)setmapping.get(expr);
1234 /** This method generates the necessary updates to satisfy the
1235 * conjunction ruleconj. */
1237 boolean processconjunction(UpdateNode un,RuleConjunction ruleconj,Hashtable setmapping) {
1239 for(int k=0;k<ruleconj.size();k++) {
1240 DNFExpr de=ruleconj.get(k);
1241 Expr e=de.getExpr();
1242 if (e instanceof OpExpr) {
1243 OpExpr ex=(OpExpr)de.getExpr();
1244 Opcode op=ex.getOpcode();
1245 Updates up=new Updates(ex.left,ex.right,op, de.getNegation());
1247 } else if (e instanceof ElementOfExpr) {
1248 SetDescriptor sd=findExpr(setmapping, ((ElementOfExpr)e).element);
1249 if (sd!=null&&sd.isSubset(((ElementOfExpr)e).set))
1250 continue; /* this update is trivially true */
1251 Updates up=new Updates(e,de.getNegation());
1253 } else if (e instanceof TupleOfExpr) {
1254 Updates up=new Updates(e,de.getNegation());
1256 } else if (e instanceof BooleanLiteralExpr) {
1257 boolean truth=((BooleanLiteralExpr)e).getValue();
1258 if (de.getNegation())
1265 System.out.println(e.getClass().getName());
1266 throw new Error("Unrecognized Expr");
1272 /** This method sees if when the quantifiers listed in set are
1273 * fixed, whether there can be more than one unique binding for
1274 * the constraint or model definition rule qs.*/
1275 public boolean analyzeQuantifiers(Quantifiers qs,Set set) {
1276 for(int i=0;i<qs.numQuantifiers();i++) {
1277 Quantifier q=qs.getQuantifier(i);
1278 if (set.contains(q))
1280 if (q instanceof SetQuantifier) {
1281 SetDescriptor sd=((SetQuantifier)q).getSet();
1282 if (maxsize.getsize(sd)<=1&&
1283 maxsize.getsize(sd)>=0)
1285 } else if (q instanceof RelationQuantifier) {
1286 RelationDescriptor rd=((RelationQuantifier)q).getRelation();
1287 if (maxsize.getsize(rd)<=1&&
1288 maxsize.getsize(rd)>=0)
1296 public boolean mutuallyexclusive(SetDescriptor sd1, SetDescriptor sd2) {
1297 if (mutualexclusive(sd1,sd2)||
1298 mutualexclusive(sd2,sd1))
1304 private boolean mutualexclusive(SetDescriptor sd1, SetDescriptor sd2) {
1305 Vector rules=state.vRules;
1306 for(int i=0;i<rules.size();i++) {
1307 Rule r=(Rule)rules.get(i);
1308 if (r.getInclusion().getTargetDescriptors().contains(sd1)) {
1309 /* Rule may add items to sd1 */
1310 SetInclusion si=(SetInclusion)r.getInclusion();
1311 Expr ve=si.getExpr();
1312 DNFRule drule=r.getDNFGuardExpr();
1313 for(int j=0;j<drule.size();j++) {
1314 RuleConjunction rconj=drule.get(j);
1315 boolean containsexclusion=false;
1316 for (int k=0;k<rconj.size();k++) {
1317 DNFExpr dexpr=rconj.get(k);
1318 if (dexpr.getNegation()&&
1319 dexpr.getExpr() instanceof ElementOfExpr&&
1320 ((ElementOfExpr)dexpr.getExpr()).element.equals(null,ve)) {
1321 SetDescriptor sd=((ElementOfExpr)dexpr.getExpr()).set;
1322 if (sd.isSubset(sd2))
1323 containsexclusion=true;
1326 if (!containsexclusion)