6 public class Termination {
8 Hashtable conjunctionmap;
10 HashSet abstractrepair;
12 HashSet consequencenodes;
15 Hashtable scopesatisfy;
16 Hashtable scopefalsify;
17 Hashtable consequence;
21 public Termination(State state) {
23 conjunctions=new HashSet();
24 conjunctionmap=new Hashtable();
25 abstractrepair=new HashSet();
26 scopenodes=new HashSet();
27 scopesatisfy=new Hashtable();
28 scopefalsify=new Hashtable();
29 consequence=new Hashtable();
30 updatenodes=new HashSet();
31 consequencenodes=new HashSet();
33 generateconjunctionnodes();
35 generaterepairnodes();
36 generatedatastructureupdatenodes();
38 generateabstractedges();
40 generateupdateedges();
42 HashSet superset=new HashSet();
43 superset.addAll(conjunctions);
44 superset.addAll(abstractrepair);
45 superset.addAll(updatenodes);
46 superset.addAll(scopenodes);
47 superset.addAll(consequencenodes);
49 GraphNode.DOTVisitor.visit(new FileOutputStream("graph.dot"),superset);
50 } catch (Exception e) {
56 void generateconjunctionnodes() {
57 Vector constraints=state.vConstraints;
58 for(int i=0;i<constraints.size();i++) {
59 Constraint c=(Constraint)constraints.get(i);
60 DNFConstraint dnf=c.dnfconstraint;
61 for(int j=0;j<dnf.size();j++) {
62 TermNode tn=new TermNode(c,dnf.get(j));
63 GraphNode gn=new GraphNode("Conjunction"+i+"B"+j,tn);
65 conjunctionmap.put(c,gn);
70 void generateupdateedges() {
71 for(Iterator updateiterator=updatenodes.iterator();updateiterator.hasNext();) {
72 GraphNode gn=(GraphNode)updateiterator.next();
73 TermNode tn=(TermNode)gn.getOwner();
74 MultUpdateNode mun=tn.getUpdate();
75 /* Cycle through the rules to look for possible conflicts */
76 for(int i=0;i<state.vRules.size();i++) {
77 Rule r=(Rule) state.vRules.get(i);
78 if (ConcreteInterferes.interferes(mun,r,true)) {
79 GraphNode scopenode=(GraphNode)scopesatisfy.get(r);
80 GraphNode.Edge e=new GraphNode.Edge("interferes",scopenode);
83 if (ConcreteInterferes.interferes(mun,r,false)) {
84 GraphNode scopenode=(GraphNode)scopefalsify.get(r);
85 GraphNode.Edge e=new GraphNode.Edge("interferes",scopenode);
92 void generateabstractedges() {
93 for(Iterator absiterator=abstractrepair.iterator();absiterator.hasNext();) {
94 GraphNode gn=(GraphNode)absiterator.next();
95 TermNode tn=(TermNode)gn.getOwner();
96 AbstractRepair ar=(AbstractRepair)tn.getAbstract();
98 for(Iterator conjiterator=conjunctions.iterator();conjiterator.hasNext();) {
99 GraphNode gn2=(GraphNode)conjiterator.next();
100 TermNode tn2=(TermNode)gn2.getOwner();
101 Conjunction conj=tn2.getConjunction();
102 for(int i=0;i<conj.size();i++) {
103 DNFPredicate dp=conj.get(i);
104 if (AbstractInterferes.interferes(ar,dp)) {
105 GraphNode.Edge e=new GraphNode.Edge("interferes",gn2);
114 void generatescopeedges() {
115 for(Iterator scopeiterator=scopenodes.iterator();scopeiterator.hasNext();) {
116 GraphNode gn=(GraphNode)scopeiterator.next();
117 TermNode tn=(TermNode)gn.getOwner();
118 ScopeNode sn=tn.getScope();
120 /* Interference edges with conjunctions */
121 for(Iterator conjiterator=conjunctions.iterator();conjiterator.hasNext();) {
122 GraphNode gn2=(GraphNode)conjiterator.next();
123 TermNode tn2=(TermNode)gn2.getOwner();
124 Conjunction conj=tn2.getConjunction();
125 for(int i=0;i<conj.size();i++) {
126 DNFPredicate dp=conj.get(i);
127 if (AbstractInterferes.interferes(sn.getDescriptor(),sn.getSatisfy(),dp)) {
128 GraphNode.Edge e=new GraphNode.Edge("interferes",gn2);
129 GraphNode gnconseq=(GraphNode)consequence.get(sn);
136 /* Now see if this could effect other model defintion rules */
137 for(int i=0;i<state.vRules.size();i++) {
138 Rule r=(Rule) state.vRules.get(i);
139 if (AbstractInterferes.interferes(sn.getDescriptor(),sn.getSatisfy(),r,true)) {
140 GraphNode scopenode=(GraphNode)scopesatisfy.get(r);
141 GraphNode.Edge e=new GraphNode.Edge("interferes",scopenode);
142 GraphNode gnconseq=(GraphNode)consequence.get(sn);
145 if (AbstractInterferes.interferes(sn.getDescriptor(),sn.getSatisfy(),r,false)) {
146 GraphNode scopenode=(GraphNode)scopefalsify.get(r);
147 GraphNode.Edge e=new GraphNode.Edge("interferes",scopenode);
148 GraphNode gnconseq=(GraphNode)consequence.get(sn);
156 void generaterepairnodes() {
157 for(Iterator conjiterator=conjunctions.iterator();conjiterator.hasNext();) {
158 GraphNode gn=(GraphNode)conjiterator.next();
159 TermNode tn=(TermNode)gn.getOwner();
160 Conjunction conj=tn.getConjunction();
161 for(int i=0;i<conj.size();i++) {
162 DNFPredicate dp=conj.get(i);
163 int[] array=dp.getPredicate().getRepairs(dp.isNegated());
164 Descriptor d=dp.getPredicate().getDescriptor();
165 for(int j=0;j<array.length;j++) {
166 AbstractRepair ar=new AbstractRepair(dp,array[j],d);
167 TermNode tn2=new TermNode(ar);
168 GraphNode gn2=new GraphNode(gn.getLabel()+"A"+i+"B"+j,tn2);
169 GraphNode.Edge e=new GraphNode.Edge("abstract",gn2);
171 abstractrepair.add(gn2);
177 void generatedatastructureupdatenodes() {
178 for(Iterator absiterator=abstractrepair.iterator();absiterator.hasNext();) {
179 GraphNode gn=(GraphNode)absiterator.next();
180 TermNode tn=(TermNode) gn.getOwner();
181 AbstractRepair ar=tn.getAbstract();
182 if (ar.getType()==AbstractRepair.ADDTOSET) {
183 generateaddtoset(gn,ar);
184 } else if (ar.getType()==AbstractRepair.REMOVEFROMSET) {
185 generateremovefromset(gn,ar);
186 } else if (ar.getType()==AbstractRepair.ADDTORELATION) {
187 generateaddtorelation(gn,ar);
188 } else if (ar.getType()==AbstractRepair.REMOVEFROMRELATION) {
189 generateremovefromrelation(gn,ar);
190 } else if (ar.getType()==AbstractRepair.MODIFYRELATION) {
191 generatemodifyrelation(gn,ar);
196 void generateremovefromset(GraphNode gn,AbstractRepair ar) {
197 Vector possiblerules=new Vector();
198 for(int i=0;i<state.vRules.size();i++) {
199 Rule r=(Rule) state.vRules.get(i);
200 if ((r.getInclusion() instanceof SetInclusion)&&
201 (ar.getDescriptor()==((SetInclusion)r.getInclusion()).getSet()))
202 possiblerules.add(r);
204 int[] count=new int[possiblerules.size()];
205 while(remains(count,possiblerules)) {
206 MultUpdateNode mun=new MultUpdateNode(ar);
207 for(int i=0;i<possiblerules.size();i++) {
208 UpdateNode un=new UpdateNode();
212 increment(count,possiblerules);
216 static void increment(int count[], Vector rules) {
218 for(int i=0;i<(rules.size()-1);i++) {
219 if (count[i]>=(((Rule)rules.get(i)).numQuantifiers()+(((Rule)rules.get(i)).getDNFNegGuardExpr().size()))) {
226 static boolean remains(int count[], Vector rules) {
227 for(int i=0;i<rules.size();i++) {
228 if (count[i]>=(((Rule)rules.get(i)).numQuantifiers()+(((Rule)rules.get(i)).getDNFNegGuardExpr().size()))) {
235 void generateremovefromrelation(GraphNode gn,AbstractRepair ar) {}
236 void generateaddtorelation(GraphNode gn,AbstractRepair ar) {}
237 void generatemodifyrelation(GraphNode gn, AbstractRepair ar) {}
239 static int addtocount=0;
240 void generateaddtoset(GraphNode gn, AbstractRepair ar) {
241 for(int i=0;i<state.vRules.size();i++) {
242 Rule r=(Rule) state.vRules.get(i);
243 if (r.getInclusion() instanceof SetInclusion) {
244 if (ar.getDescriptor()==((SetInclusion)r.getInclusion()).getSet()) {
245 //Generate add instruction
246 DNFRule dnfrule=r.getDNFGuardExpr();
247 for(int j=0;j<dnfrule.size();j++) {
248 Inclusion inc=r.getInclusion();
249 UpdateNode un=new UpdateNode();
250 /* First solve for quantifiers */
251 boolean goodupdate=true;
252 for(Iterator iterator=r.quantifiers();iterator.hasNext();) {
253 Quantifier q=(Quantifier)iterator.next();
254 boolean foundall=true;
255 if ((q instanceof SetQuantifier)||(q instanceof ForQuantifier)) {
256 VarDescriptor vd=null;
257 SetDescriptor set=null;
258 if (q instanceof SetQuantifier) {
259 vd=((SetQuantifier)q).getVar();
261 vd=((ForQuantifier)q).getVar();
262 if(inc instanceof SetInclusion) {
263 SetInclusion si=(SetInclusion)inc;
264 if ((si.elementexpr instanceof VarExpr)&&
265 (((VarExpr)si.elementexpr).getVar()==vd)) {
266 /* Can solve for v */
267 Binding binding=new Binding(vd,0);
268 un.addBinding(binding);
271 } else if (inc instanceof RelationInclusion) {
272 RelationInclusion ri=(RelationInclusion)inc;
275 if ((ri.getLeftExpr() instanceof VarExpr)&&
276 (((VarExpr)ri.getLeftExpr()).getVar()==vd)) {
277 /* Can solve for v */
278 Binding binding=new Binding(vd,0);
279 un.addBinding(binding);
281 if ((ri.getRightExpr() instanceof VarExpr)&&
282 (((VarExpr)ri.getRightExpr()).getVar()==vd)) {
283 /* Can solve for v */
284 Binding binding=new Binding(vd,0);
285 un.addBinding(binding);
289 } else throw new Error("Inclusion not recognized");
290 } else if (q instanceof RelationQuantifier) {
291 RelationQuantifier rq=(RelationQuantifier)q;
292 for(int k=0;k<2;k++) {
293 VarDescriptor vd=(k==0)?rq.x:rq.y;
294 if(inc instanceof SetInclusion) {
295 SetInclusion si=(SetInclusion)inc;
296 if ((si.elementexpr instanceof VarExpr)&&
297 (((VarExpr)si.elementexpr).getVar()==vd)) {
298 /* Can solve for v */
299 Binding binding=new Binding(vd,0);
300 un.addBinding(binding);
303 } else if (inc instanceof RelationInclusion) {
304 RelationInclusion ri=(RelationInclusion)inc;
307 if ((ri.getLeftExpr() instanceof VarExpr)&&
308 (((VarExpr)ri.getLeftExpr()).getVar()==vd)) {
309 /* Can solve for v */
310 Binding binding=new Binding(vd,0);
311 un.addBinding(binding);
313 if ((ri.getRightExpr() instanceof VarExpr)&&
314 (((VarExpr)ri.getRightExpr()).getVar()==vd)) {
315 /* Can solve for v */
316 Binding binding=new Binding(vd,0);
317 un.addBinding(binding);
321 } else throw new Error("Inclusion not recognized");
323 } else throw new Error("Quantifier not recognized");
328 /* Now build update for tuple/set inclusion condition */
329 if(inc instanceof SetInclusion) {
330 SetInclusion si=(SetInclusion)inc;
331 if (!(si.elementexpr instanceof VarExpr)) {
332 Updates up=new Updates(si.elementexpr,0);
336 if (inc instanceof RelationInclusion) {
337 RelationInclusion ri=(RelationInclusion)inc;
338 if (!(ri.getLeftExpr() instanceof VarExpr)) {
339 Updates up=new Updates(ri.getLeftExpr(),0);
342 if (!(ri.getRightExpr() instanceof VarExpr)) {
343 Updates up=new Updates(ri.getRightExpr(),0);
347 //Finally build necessary updates to satisfy conjunction
348 RuleConjunction ruleconj=dnfrule.get(j);
349 for(int k=0;k<ruleconj.size();k++) {
350 DNFExpr de=ruleconj.get(k);
352 if (e instanceof OpExpr) {
353 OpExpr ex=(OpExpr)de.getExpr();
354 Opcode op=ex.getOpcode();
355 if (de.getNegation()) {
356 /* remove negation through opcode translation */
359 else if (op==Opcode.GE)
361 else if (op==Opcode.EQ)
363 else if (op==Opcode.NE)
365 else if (op==Opcode.LT)
367 else if (op==Opcode.LE)
370 Updates up=new Updates(ex.left,ex.right,op);
372 } else if (e instanceof ElementOfExpr) {
373 Updates up=new Updates(e,de.getNegation());
375 } else if (e instanceof TupleOfExpr) {
376 Updates up=new Updates(e,de.getNegation());
378 } else throw new Error("Error #213");
381 MultUpdateNode mun=new MultUpdateNode(ar);
383 TermNode tn=new TermNode(mun);
384 GraphNode gn2=new GraphNode("Update"+addtocount,tn);
385 GraphNode.Edge e=new GraphNode.Edge("abstract"+addtocount,gn2);
388 updatenodes.add(gn2);
395 void generatescopenodes() {
396 for(int i=0;i<state.vRules.size();i++) {
397 Rule r=(Rule) state.vRules.get(i);
398 ScopeNode satisfy=new ScopeNode(r,true);
399 TermNode tnsatisfy=new TermNode(satisfy);
400 GraphNode gnsatisfy=new GraphNode("SatisfyRule"+i,tnsatisfy);
401 ConsequenceNode cnsatisfy=new ConsequenceNode();
402 TermNode ctnsatisfy=new TermNode(cnsatisfy);
403 GraphNode cgnsatisfy=new GraphNode("ConseqSatisfyRule"+i,ctnsatisfy);
404 consequence.put(satisfy,cgnsatisfy);
405 GraphNode.Edge esat=new GraphNode.Edge("consequencesatisfy"+i,cgnsatisfy);
406 gnsatisfy.addEdge(esat);
407 consequencenodes.add(cgnsatisfy);
408 scopesatisfy.put(r,gnsatisfy);
409 scopenodes.add(gnsatisfy);
411 ScopeNode falsify=new ScopeNode(r,false);
412 TermNode tnfalsify=new TermNode(falsify);
413 GraphNode gnfalsify=new GraphNode("FalsifyRule"+i,tnfalsify);
414 ConsequenceNode cnfalsify=new ConsequenceNode();
415 TermNode ctnfalsify=new TermNode(cnfalsify);
416 GraphNode cgnfalsify=new GraphNode("ConseqFalsifyRule"+i,ctnfalsify);
417 consequence.put(falsify,cgnfalsify);
418 GraphNode.Edge efals=new GraphNode.Edge("consequencefalsify"+i,cgnfalsify);
419 gnfalsify.addEdge(efals);
420 consequencenodes.add(cgnfalsify);
421 scopefalsify.put(r,gnfalsify);
422 scopenodes.add(gnfalsify);