4 class AbstractInterferes {
5 Termination termination;
7 public AbstractInterferes(Termination t) {
11 /** Does performing the AbstractRepair ar satisfy (or falsify if satisfy=false)
14 static public boolean interfereswithrule(AbstractRepair ar, Rule r, boolean satisfy) {
16 boolean mayremove=false;
17 switch (ar.getType()) {
18 case AbstractRepair.ADDTOSET:
19 case AbstractRepair.ADDTORELATION:
20 if (interferesquantifier(ar.getDescriptor(), true, r, satisfy))
24 case AbstractRepair.REMOVEFROMSET:
25 case AbstractRepair.REMOVEFROMRELATION:
26 if (interferesquantifier(ar.getDescriptor(), false, r, satisfy))
30 case AbstractRepair.MODIFYRELATION:
31 if (interferesquantifier(ar.getDescriptor(), true, r, satisfy))
33 if (interferesquantifier(ar.getDescriptor(), false, r, satisfy))
39 throw new Error("Unrecognized Abstract Repair");
43 drule=r.getDNFGuardExpr();
45 drule=r.getDNFNegGuardExpr();
47 for(int i=0;i<drule.size();i++) {
48 RuleConjunction rconj=drule.get(i);
49 for(int j=0;j<rconj.size();j++) {
50 DNFExpr dexpr=rconj.get(j);
51 Expr expr=dexpr.getExpr();
52 if (expr.usesDescriptor(ar.getDescriptor())) {
54 if ((mayadd&&!dexpr.getNegation())||(mayremove&&dexpr.getNegation()))
62 public boolean checkrelationconstraint(AbstractRepair ar, Constraint c) {
63 if (c.numQuantifiers()==1&&
64 (c.getQuantifier(0) instanceof RelationQuantifier)) {
65 RelationQuantifier rq=(RelationQuantifier)c.getQuantifier(0);
66 if (rq.getRelation()==ar.getDescriptor()) {
67 Hashtable ht=new Hashtable();
68 if (ar.getDomainSet()!=null)
69 ht.put(rq.x,ar.getDomainSet());
70 if (ar.getRangeSet()!=null)
71 ht.put(rq.y,ar.getRangeSet());
72 DNFConstraint dconst=c.dnfconstraint;
74 for(int i=0;i<dconst.size();i++) {
75 Conjunction conj=dconst.get(i);
77 for(int j=0;j<conj.size();j++) {
78 DNFPredicate dpred=conj.get(j);
79 Predicate p=dpred.getPredicate();
80 if ((p instanceof InclusionPredicate)&&(!dpred.isNegated())) {
81 InclusionPredicate ip=(InclusionPredicate)p;
82 if (ip.expr instanceof VarExpr&&
83 ip.setexpr.getDescriptor() instanceof SetDescriptor) {
84 VarDescriptor vd=((VarExpr)ip.expr).getVar();
85 if (ht.containsKey(vd)) {
86 SetDescriptor td=(SetDescriptor)ip.setexpr.getDescriptor();
87 SetDescriptor s=(SetDescriptor)ht.get(vd);
102 /** This method checks whether a modify relation abstract repair
103 * to satisfy ar may violate dp. It returns true if there is no
106 private boolean interferemodifies(DNFPredicate ar,DNFPredicate dp) {
107 boolean neg1=ar.isNegated();
108 Opcode op1=((ExprPredicate)ar.getPredicate()).getOp();
109 Expr rexpr1=((OpExpr)((ExprPredicate)ar.getPredicate()).expr).right;
110 Expr lexpr1=((OpExpr)((ExprPredicate)ar.getPredicate()).expr).left;
111 RelationDescriptor updated_des=(RelationDescriptor)((ExprPredicate)ar.getPredicate()).getDescriptor();
113 boolean neg2=dp.isNegated();
114 Opcode op2=((ExprPredicate)dp.getPredicate()).getOp();
115 Expr rexpr2=((OpExpr)((ExprPredicate)dp.getPredicate()).expr).right;
116 Expr lexpr2=((OpExpr)((ExprPredicate)dp.getPredicate()).expr).left;
118 /* Translate the opcodes */
119 op1=Opcode.translateOpcode(neg1,op1);
120 op2=Opcode.translateOpcode(neg2,op2);
122 if (((op1==Opcode.EQ)||(op1==Opcode.GE)||(op1==Opcode.LE))&&
123 ((op2==Opcode.EQ)||(op2==Opcode.GE)||(op2==Opcode.LE))&&
124 !rexpr2.usesDescriptor(updated_des)) {
125 Hashtable varmap=new Hashtable();
129 boolean initialrelation=true;
130 boolean onetoone=true;
132 if ((l1 instanceof VarExpr)&&
133 (l2 instanceof VarExpr)) {
134 VarDescriptor vd1=((VarExpr)l1).getVar();
135 VarDescriptor vd2=((VarExpr)l2).getVar();
138 } else if ((l1 instanceof RelationExpr)&&
139 (l2 instanceof RelationExpr)) {
140 RelationExpr re1=(RelationExpr)l1;
141 RelationExpr re2=(RelationExpr)l2;
142 if (re1.getRelation()!=re2.getRelation()||
143 re1.inverted()!=re2.inverted())
146 if (!initialrelation) {
148 ConstraintDependence.rulesensurefunction(termination.state, re1.getRelation(),re1.getExpr().getSet(),!re1.inverted(),true)||
149 ConstraintDependence.constraintsensurefunction(termination.state, re1.getRelation(),re1.getExpr().getSet(),!re1.inverted(),true)))
151 //need check one-to-one property here
152 } else initialrelation=false;
155 } else return false; // bad match
157 Set freevars=rexpr1.freeVars();
158 for(Iterator it=freevars.iterator();it.hasNext();) {
159 VarDescriptor vd=(VarDescriptor)it.next();
161 continue; //globals are fine
162 else if (varmap.containsKey(vd)&&onetoone) //the mapped variable is fine if we have a 1-1 mapping
164 else if (termination.maxsize.getsize(vd.getSet())==1)
168 return rexpr1.equals(varmap,rexpr2);
173 /** Does performing the AbstractRepair ar falsify the predicate dp */
175 public boolean interfereswithpredicate(AbstractRepair ar, DNFPredicate dp) {
176 if ((ar.getDescriptor()!=dp.getPredicate().getDescriptor()) &&
177 // ((ar.getDescriptor() instanceof SetDescriptor)||
178 // If the second predicate uses the size of the set, modifying the set size could falsify it...
179 !dp.getPredicate().usesDescriptor(ar.getDescriptor()))
183 /* This if handles all the c comparisons in the paper */
184 if (ar.getDescriptor()==dp.getPredicate().getDescriptor()&&
185 (ar.getType()==AbstractRepair.ADDTOSET||ar.getType()==AbstractRepair.ADDTORELATION)&&
186 (ar.getPredicate().getPredicate() instanceof ExprPredicate)&&
187 (dp.getPredicate() instanceof ExprPredicate)&&
188 (dp.getPredicate().inverted()==ar.getPredicate().getPredicate().inverted())&&
189 (((ExprPredicate)dp.getPredicate()).getType()==ExprPredicate.SIZE)) {
190 boolean neg1=ar.getPredicate().isNegated();
191 Opcode op1=((ExprPredicate)ar.getPredicate().getPredicate()).getOp();
192 int size1=((ExprPredicate)ar.getPredicate().getPredicate()).rightSize();
193 boolean neg2=dp.isNegated();
194 Opcode op2=((ExprPredicate)dp.getPredicate()).getOp();
195 int size2=((ExprPredicate)dp.getPredicate()).rightSize();
196 op1=Opcode.translateOpcode(neg1,op1);
197 op2=Opcode.translateOpcode(neg2,op2);
199 if ((op1==Opcode.EQ)||(op1==Opcode.NE)||(op1==Opcode.GT)||op1==Opcode.GE) {
201 if((op1==Opcode.EQ)||(op1==Opcode.GE))
203 if((op1==Opcode.GT)||(op1==Opcode.NE))
206 if (((op2==Opcode.EQ)&&(size1a==size2))||
207 ((op2==Opcode.NE)&&(size1a!=size2))||
210 ((op2==Opcode.LE)&&(size1a<=size2))||
211 ((op2==Opcode.LT)&&(size1a<size2)))
215 if (ar.getDescriptor()==dp.getPredicate().getDescriptor()&&
216 (ar.getType()==AbstractRepair.ADDTOSET||ar.getType()==AbstractRepair.ADDTORELATION)&&
217 (dp.getPredicate() instanceof ExprPredicate)&&
218 (((ExprPredicate)dp.getPredicate()).getType()==ExprPredicate.SIZE)) {
219 boolean neg2=dp.isNegated();
220 Opcode op2=((ExprPredicate)dp.getPredicate()).getOp();
221 int size2=((ExprPredicate)dp.getPredicate()).rightSize();
223 op2=Opcode.translateOpcode(neg2,op2);
225 int maxsize=termination.maxsize.getsize(dp.getPredicate().getDescriptor());
228 ((op2==Opcode.LT&&size2>maxsize)||
229 (op2==Opcode.LE&&size2>=maxsize)||
230 (op2==Opcode.EQ&&size2>=maxsize)))
233 if (((op2==Opcode.NE)&&(size2==0))||
238 if (ar.getDescriptor()==dp.getPredicate().getDescriptor()&&
239 (ar.getType()==AbstractRepair.MODIFYRELATION)&&
240 (dp.getPredicate() instanceof ExprPredicate)&&
241 (dp.getPredicate().inverted()==ar.getPredicate().getPredicate().inverted())&&
242 (((ExprPredicate)dp.getPredicate()).getType()==ExprPredicate.COMPARISON)) {
244 boolean neg1=ar.getPredicate().isNegated();
245 Opcode op1=((ExprPredicate)ar.getPredicate().getPredicate()).getOp();
246 boolean isInt1=((ExprPredicate)ar.getPredicate().getPredicate()).isRightInt();
247 int size1=isInt1?((ExprPredicate)ar.getPredicate().getPredicate()).rightSize():0;
248 Expr expr1=((OpExpr)((ExprPredicate)ar.getPredicate().getPredicate()).expr).right;
251 boolean neg2=dp.isNegated();
252 Opcode op2=((ExprPredicate)dp.getPredicate()).getOp();
253 boolean isInt2=((ExprPredicate)dp.getPredicate()).isRightInt();
254 int size2=isInt2?((ExprPredicate)dp.getPredicate()).rightSize():0;
255 Expr expr2=((OpExpr)((ExprPredicate)dp.getPredicate()).expr).right;
258 /* If the left sides of the comparisons are both from
259 different sets, the update is orthogonal to the expr dp */
261 Expr lexpr1=((RelationExpr)((OpExpr)((ExprPredicate)ar.getPredicate().getPredicate()).expr).getLeftExpr()).getExpr();
262 Expr lexpr2=((RelationExpr)((OpExpr)((ExprPredicate)dp.getPredicate()).expr).getLeftExpr()).getExpr();
263 SetDescriptor sd1=lexpr1.getSet();
264 SetDescriptor sd2=lexpr2.getSet();
265 if (termination.mutuallyexclusive(sd1,sd2))
269 /* Translate the opcodes */
270 op1=Opcode.translateOpcode(neg1,op1);
271 op2=Opcode.translateOpcode(neg2,op2);
273 /* Obvious cases which can't interfere */
274 if (((op1==Opcode.GT)||
279 if (((op1==Opcode.LT)||
285 if (interferemodifies(ar.getPredicate(),dp))
288 if (isInt1&&isInt2) {
289 if (((op1==Opcode.EQ)||(op1==Opcode.GE)||(op1==Opcode.LE))&&
290 ((op2==Opcode.EQ)||(op2==Opcode.GE)||(op2==Opcode.LE))&&
300 size1a++; /*FLAGNE this is current behavior for NE repairs */
306 if (((op1==Opcode.GE)||(op1==Opcode.GT))&&
307 ((op2==Opcode.LE)||(op2==Opcode.EQ)||(op2==Opcode.LT))&&
310 if (((op1==Opcode.LE)||(op1==Opcode.LT))&&
311 ((op2==Opcode.GE)||(op2==Opcode.EQ)||(op2==Opcode.GT))&&
314 if ((op1==Opcode.EQ)&&(op2==Opcode.EQ)&&
317 if ((op1==Opcode.EQ)&&
318 ((op2==Opcode.LE)||(op2==Opcode.LT))&&
321 if ((op1==Opcode.EQ)&&
322 ((op2==Opcode.GE)||(op2==Opcode.GT))&&
325 if (op2==Opcode.NE) /*FLAGNE this is current behavior for NE repairs */
328 if ((op1==Opcode.NE)&&
332 if ((op1==Opcode.NE)&& /* FLAGNE relies on increasing or decreasing by 1 */
333 ((op2==Opcode.GT)||(op2==Opcode.GE))&&
336 if ((op1==Opcode.NE)&& /* FLAGNE relies on increasing or decreasing by 1 */
337 ((op2==Opcode.LT)||(op2==Opcode.LE))&&
342 /* This handles all the c comparisons in the paper */
343 if (ar.getDescriptor()==dp.getPredicate().getDescriptor()&&
344 (ar.getType()==AbstractRepair.REMOVEFROMSET||ar.getType()==AbstractRepair.REMOVEFROMRELATION)&&
345 (ar.getPredicate().getPredicate() instanceof ExprPredicate)&&
346 (dp.getPredicate() instanceof ExprPredicate)&&
347 (dp.getPredicate().inverted()==ar.getPredicate().getPredicate().inverted())&&
348 (((ExprPredicate)dp.getPredicate()).getType()==ExprPredicate.SIZE)) {
349 boolean neg1=ar.getPredicate().isNegated();
350 Opcode op1=((ExprPredicate)ar.getPredicate().getPredicate()).getOp();
351 int size1=((ExprPredicate)ar.getPredicate().getPredicate()).rightSize();
352 boolean neg2=dp.isNegated();
353 Opcode op2=((ExprPredicate)dp.getPredicate()).getOp();
354 int size2=((ExprPredicate)dp.getPredicate()).rightSize();
355 /* Translate the opcodes */
356 op1=Opcode.translateOpcode(neg1,op1);
357 op2=Opcode.translateOpcode(neg2,op2);
359 if (((op1==Opcode.EQ)||(op1==Opcode.LT)||op1==Opcode.LE)||(op1==Opcode.NE)) {
362 if((op1==Opcode.EQ)||(op1==Opcode.LE))
364 if((op1==Opcode.LT)||(op1==Opcode.NE))
367 if (((op2==Opcode.EQ)&&(size1a==size2))||
368 ((op2==Opcode.NE)&&(size1a!=size2))||
371 ((op2==Opcode.GE)&&(size1a>=size2))||
372 ((op2==Opcode.GT)&&(size1a>size2)))
377 if (ar.getDescriptor()==dp.getPredicate().getDescriptor()&&
378 (ar.getType()==AbstractRepair.REMOVEFROMSET||ar.getType()==AbstractRepair.REMOVEFROMRELATION)&&
379 (dp.getPredicate() instanceof ExprPredicate)&&
380 (((ExprPredicate)dp.getPredicate()).getType()==ExprPredicate.SIZE)) {
381 boolean neg2=dp.isNegated();
382 Opcode op2=((ExprPredicate)dp.getPredicate()).getOp();
383 int size2=((ExprPredicate)dp.getPredicate()).rightSize();
384 op2=Opcode.translateOpcode(neg2,op2);
386 if (((op2==Opcode.EQ)&&(size2==0))||
391 if ((ar.getDescriptor()==dp.getPredicate().getDescriptor())&&
392 (ar.getType()==AbstractRepair.ADDTOSET||ar.getType()==AbstractRepair.ADDTORELATION)&&
393 (dp.getPredicate() instanceof InclusionPredicate)&&
394 (dp.isNegated()==false))
395 return false; /* Could only satisfy this predicate */
397 if ((ar.getDescriptor()==dp.getPredicate().getDescriptor())&&
398 (ar.getType()==AbstractRepair.REMOVEFROMSET||ar.getType()==AbstractRepair.REMOVEFROMRELATION)&&
399 (dp.getPredicate() instanceof InclusionPredicate)&&
400 (dp.isNegated()==true))
401 return false; /* Could only satisfy this predicate */
405 /** Does the increase (or decrease) in scope of a model defintion
406 * rule represented by sn falsify the predicate dp. */
408 public boolean scopeinterfereswithpredicate(ScopeNode sn, DNFPredicate dp) {
409 if (!sn.getSatisfy()&&(sn.getDescriptor() instanceof SetDescriptor)) {
411 Set target=r.getInclusion().getTargetDescriptors();
413 for(int i=0;i<r.numQuantifiers();i++) {
414 Quantifier q=r.getQuantifier(i);
415 if (q instanceof SetQuantifier) {
416 SetQuantifier sq=(SetQuantifier) q;
417 if (target.contains(sq.getSet())) {
424 sn.getDescriptor()==dp.getPredicate().getDescriptor()&&
425 (dp.getPredicate() instanceof ExprPredicate)&&
426 (((ExprPredicate)dp.getPredicate()).getType()==ExprPredicate.SIZE)) {
427 boolean neg=dp.isNegated();
428 Opcode op=((ExprPredicate)dp.getPredicate()).getOp();
429 int size=((ExprPredicate)dp.getPredicate()).rightSize();
430 op=Opcode.translateOpcode(neg,op);
432 if ((op==Opcode.GE)&&
433 ((size==0)||(size==1)))
435 if ((op==Opcode.GT)&&
440 return interferes(sn.getDescriptor(), sn.getSatisfy(),dp);
443 /** Does increasing (or decreasing if satisfy=false) the size of
444 * the set or relation des falsify the predicate dp. */
446 private boolean interferes(Descriptor des, boolean satisfy, DNFPredicate dp) {
447 if ((des!=dp.getPredicate().getDescriptor()) &&
448 //((des instanceof SetDescriptor)||
449 !dp.getPredicate().usesDescriptor(des))//)
452 /* This if handles all the c comparisons in the paper */
453 if (des==dp.getPredicate().getDescriptor()&&
455 (dp.getPredicate() instanceof ExprPredicate)&&
456 (((ExprPredicate)dp.getPredicate()).getType()==ExprPredicate.SIZE)) {
457 boolean neg2=dp.isNegated();
458 Opcode op2=((ExprPredicate)dp.getPredicate()).getOp();
459 int size2=((ExprPredicate)dp.getPredicate()).rightSize();
460 op2=Opcode.translateOpcode(neg2,op2);
463 int maxsize=termination.maxsize.getsize(dp.getPredicate().getDescriptor());
466 ((op2==Opcode.LT&&size2>maxsize)||
467 (op2==Opcode.LE&&size2>=maxsize)||
468 (op2==Opcode.EQ&&size2>=maxsize)))
471 if ((op2==Opcode.GE)||
473 (op2==Opcode.NE)&&(size2==0))
477 /* This if handles all the c comparisons in the paper */
478 if (des==dp.getPredicate().getDescriptor()&&
480 (dp.getPredicate() instanceof ExprPredicate)&&
481 (((ExprPredicate)dp.getPredicate()).getType()==ExprPredicate.SIZE)) {
482 boolean neg2=dp.isNegated();
483 Opcode op2=((ExprPredicate)dp.getPredicate()).getOp();
484 int size2=((ExprPredicate)dp.getPredicate()).rightSize();
485 op2=Opcode.translateOpcode(neg2,op2);
487 if (((op2==Opcode.EQ)&&(size2==0))||
493 if ((des==dp.getPredicate().getDescriptor())&&
495 (dp.getPredicate() instanceof InclusionPredicate)&&
496 (dp.isNegated()==false))
497 return false; /* Could only satisfy this predicate */
499 if ((des==dp.getPredicate().getDescriptor())&&
501 (dp.getPredicate() instanceof InclusionPredicate)&&
502 (dp.isNegated()==true))
503 return false; /* Could only satisfy this predicate */
508 /** This method test whether satisfying (or falsifying depending
509 * on the flag satisfy) a rule that adds an object(or tuple) to
510 * the set(or relation) descriptor may increase (or decrease
511 * depending on the flag satisfyrule) the scope of a constraint or
512 * model defintion rule r. */
514 static private boolean interferesquantifier(Descriptor des, boolean satisfy, Quantifiers r, boolean satisfyrule) {
515 for(int i=0;i<r.numQuantifiers();i++) {
516 Quantifier q=r.getQuantifier(i);
517 if (q instanceof RelationQuantifier||q instanceof SetQuantifier) {
518 if (q.getRequiredDescriptors().contains(des)&&(satisfy==satisfyrule))
520 } else if (q instanceof ForQuantifier) {
521 if (q.getRequiredDescriptors().contains(des))
523 } else throw new Error("Unrecognized Quantifier");
528 static public boolean interferesquantifier(AbstractRepair ar, Quantifiers q) {
529 if (ar.getType()==AbstractRepair.ADDTOSET||ar.getType()==AbstractRepair.ADDTORELATION)
530 return interferesquantifier(ar.getDescriptor(),true,q,true);
534 static public boolean interfereswithquantifier(Descriptor des, boolean satisfy, Quantifiers q) {
535 return interferesquantifier(des, satisfy, q,true);
538 static public boolean interfereswithrule(Descriptor des, boolean satisfy, Rule r, boolean satisfyrule) {
539 if (interferesquantifier(des,satisfy,r,satisfyrule))
542 DNFRule drule=r.getDNFGuardExpr();
543 for(int i=0;i<drule.size();i++) {
544 RuleConjunction rconj=drule.get(i);
545 for(int j=0;j<rconj.size();j++) {
546 DNFExpr dexpr=rconj.get(j);
547 Expr expr=dexpr.getExpr();
548 boolean negated=dexpr.getNegation();
556 boolean satisfiesrule=(satisfy^negated);/*XOR of these */
557 if (satisfiesrule==satisfyrule) {
558 /* Effect is the one being tested for */
559 /* Only expr's to be concerned with are TupleOfExpr and
561 if (expr.getRequiredDescriptors().contains(des)) {
562 if (((expr instanceof ElementOfExpr)||
563 (expr instanceof TupleOfExpr))&&
564 (expr.getRequiredDescriptors().size()==1))
567 throw new Error("Unrecognized EXPR");