when they aren't necessary.
--- /dev/null
+package MCC.IR;
+import MCC.State;
+import java.util.*;
+
+/** This class computes the maximum size of sets and relations */
+
+public class ComputeMaxSize {
+ State state;
+ Hashtable sizemap; /* -1 means infinity */
+
+
+ public ComputeMaxSize(State state) {
+ this.state=state;
+ sizemap=new Hashtable();
+ computesizes();
+ }
+
+ /** This method compute relation and set maximum sizes */
+ private void computesizes() {
+ Vector rules=state.vRules;
+ boolean change=true;
+ Set descriptorset=new HashSet();
+ descriptorset.addAll(state.stSets.getAllDescriptors());
+ descriptorset.addAll(state.stRelations.getAllDescriptors());
+ while(change) {
+ change=false;
+ for(Iterator dit=descriptorset.iterator();dit.hasNext();) {
+ Descriptor d=(Descriptor)dit.next();
+ if (d instanceof ReservedSetDescriptor)
+ continue;
+ int totalsize=0;
+ for(int i=0;i<rules.size();i++) {
+ Rule r=(Rule)rules.get(i);
+ if (r.getInclusion().getTargetDescriptors().contains(d)) {
+ /* This rule may add items to this set or relation */
+ int rulesize=1;
+ for(int j=0;j<r.numQuantifiers();j++) {
+ Quantifier q=r.getQuantifier(j);
+ int size=0;
+ if (q instanceof RelationQuantifier) {
+ Descriptor d2=((RelationQuantifier)q).getRelation();
+ if (sizemap.containsKey(d2)) {
+ size=((Integer)sizemap.get(d2)).intValue();
+ }
+ if ((size!=0)&&(d==d2))
+ size=-1;
+ } if (q instanceof SetQuantifier) {
+ Descriptor d2=((SetQuantifier)q).getSet();
+ if (sizemap.containsKey(d2)) {
+ size=((Integer)sizemap.get(d2)).intValue();
+ }
+ if ((size!=0)&&(d==d2))
+ size=-1;
+ } else {
+ size=-1;
+ }
+ if ((rulesize!=0)&&((size==-1)||(rulesize==-1)))
+ rulesize=-1;
+ else
+ rulesize=rulesize*size;
+ }
+
+ if ((rulesize==-1)||(totalsize==-1))
+ totalsize=-1;
+ else
+ totalsize+=rulesize;
+ }
+ }
+ if (!sizemap.containsKey(d)||((Integer)sizemap.get(d)).intValue()!=totalsize) {
+ change=true;
+ sizemap.put(d,new Integer(totalsize));
+ }
+ }
+ }
+ }
+ int getsize(Descriptor d) {
+ return ((Integer)sizemap.get(d)).intValue();
+ }
+}
+
return false;
}
- public int[] getRepairs(boolean negated) {
+ public int[] getRepairs(boolean negated, Termination t) {
System.out.println(this.getClass().getName());
throw new Error("Unrecognized EXPR");
}
return expr.getInversedRelations();
}
- public int[] getRepairs(boolean negated) {
- return expr.getRepairs(negated);
+ public int[] getRepairs(boolean negated, Termination t) {
+ return expr.getRepairs(negated,t);
}
public Descriptor getDescriptor() {
for(Iterator it=cycles2.iterator();it.hasNext();) {
GraphNode gn=(GraphNode)it.next();
if (termination.conjunctions.contains(gn)) {
+ try {
+ GraphNode.DOTVisitor.visit(new FileOutputStream("graphdebug.dot"),cantremove);
+ } catch (Exception e) {
+ e.printStackTrace();
+ System.exit(-1);
+ }
+
System.out.println("Cycle through conjunction "+gn.getTextLabel() +" which can't be removed.");
return null; // Out of luck
}
/* Searches scope nodes + compensation nodes */
for(Iterator it=termination.scopenodes.iterator();it.hasNext();) {
GraphNode gn=(GraphNode)it.next();
- boolean foundcompensation=false;
+ int count=0;
if (nodes.contains(gn)) {
for (Iterator edgeit=gn.edges();edgeit.hasNext();) {
GraphNode.Edge e=(GraphNode.Edge)edgeit.next();
GraphNode gn2=e.getTarget();
TermNode tn2=(TermNode)gn2.getOwner();
+ if ((tn2.getType()==TermNode.CONSEQUENCE)&&
+ !mustremove.contains(gn2))
+ count++;
+
+
if (tn2.getType()!=TermNode.UPDATE)
continue;
/* We have a compensation node */
}
} else {
if (!mustremove.contains(gn2))
- foundcompensation=true;
+ count++;
}
if (!containsgn)
cantremove.remove(gn);
if (!containsgn2)
cantremove.remove(gn2);
}
- }
- if (!foundcompensation) {
- for (Iterator edgeit=gn.edges();edgeit.hasNext();) {
- GraphNode.Edge e=(GraphNode.Edge)edgeit.next();
- GraphNode gn2=e.getTarget();
- TermNode tn2=(TermNode)gn2.getOwner();
- if (tn2.getType()==TermNode.UPDATE) {
- cantremove.add(gn2);
- break;
+
+ if (count==1) {
+ for (Iterator edgeit=gn.edges();edgeit.hasNext();) {
+ GraphNode.Edge e=(GraphNode.Edge)edgeit.next();
+ GraphNode gn2=e.getTarget();
+ TermNode tn2=(TermNode)gn2.getOwner();
+ if ((tn2.getType()==TermNode.UPDATE||tn2.getType()==TermNode.CONSEQUENCE)&&
+ !mustremove.contains(gn2)) {
+ if (!cantremove.contains(gn2)) {
+ cantremove.add(gn2);
+ change=true;
+ }
+ }
}
}
}
GraphNode gn=(GraphNode)it.next();
System.out.println(gn.getTextLabel());
}
+ System.out.println("Must remove set:");
+ for(Iterator it=mustremove.iterator();it.hasNext();) {
+ GraphNode gn=(GraphNode)it.next();
+ System.out.println(gn.getTextLabel());
+ }
+ System.out.println("Cant remove set:");
+ for(Iterator it=cantremove.iterator();it.hasNext();) {
+ GraphNode gn=(GraphNode)it.next();
+ System.out.println(gn.getTextLabel());
+ }
while(true) {
return setexpr.sd;
}
- public int[] getRepairs(boolean negated) {
+ public int[] getRepairs(boolean negated, Termination t) {
if (setexpr instanceof ImageSetExpr) {
if (negated)
return new int[] {AbstractRepair.REMOVEFROMRELATION};
}
- public int[] getRepairs(boolean negated) {
+ public int[] getRepairs(boolean negated, Termination t) {
if (left instanceof RelationExpr)
return new int[] {AbstractRepair.MODIFYRELATION};
if (left instanceof SizeofExpr) {
op=Opcode.GT;
}
+ int maxsize=t.maxsize.getsize(getDescriptor());
+ int size=getInt(right);
boolean isRelation=((SizeofExpr)left).setexpr instanceof ImageSetExpr;
if (isRelation) {
if (op==Opcode.EQ) {
- if (((IntegerLiteralExpr)right).getValue()==0)
+ if (size==0)
return new int[] {AbstractRepair.REMOVEFROMRELATION};
- else
+ else {
+ if ((maxsize!=-1)&&maxsize<=size)
+ return new int[] {AbstractRepair.ADDTORELATION};
return new int[] {AbstractRepair.ADDTORELATION,
AbstractRepair.REMOVEFROMRELATION};
+ }
} else if (op==Opcode.GE||op==Opcode.GT) {
return new int[]{AbstractRepair.ADDTORELATION};
} else if (op==Opcode.LE||op==Opcode.LT) {
+ if ((op==Opcode.LT&&maxsize!=-1&&maxsize<size)||(op==Opcode.LE&&maxsize!=-1&&maxsize<=size))
+ return new int[0];
return new int[]{AbstractRepair.REMOVEFROMRELATION};
} else if (op==Opcode.NE) {
+ if (maxsize<size&&maxsize!=-1)
+ return new int[0];
return new int[]{AbstractRepair.ADDTORELATION};
} else throw new Error();
} else {
if (op==Opcode.EQ) {
- if (((IntegerLiteralExpr)right).getValue()==0)
+ if (size==0)
return new int[] {AbstractRepair.REMOVEFROMSET};
- else
+ else {
+ if (maxsize<=size&&maxsize!=-1)
+ return new int[] {AbstractRepair.ADDTOSET};
return new int[] {AbstractRepair.ADDTOSET,
AbstractRepair.REMOVEFROMSET};
+ }
} else if (op==Opcode.GE||op==Opcode.GT) {
return new int[] {AbstractRepair.ADDTOSET};
} else if (op==Opcode.LE||op==Opcode.LT) {
+ if ((op==Opcode.LT&&maxsize<size&&maxsize!=-1)||(op==Opcode.LE&&maxsize<=size&&maxsize!=-1))
+ return new int[0];
return new int[] {AbstractRepair.REMOVEFROMSET};
} else if (op==Opcode.NE) {
+ if (maxsize<size&&maxsize!=-1)
+ return new int[0];
return new int[] {AbstractRepair.ADDTOSET};
} else throw new Error();
}
public DNFConstraint constructDNF() {
return new DNFConstraint(this);
}
- abstract public int[] getRepairs(boolean negated);
+ abstract public int[] getRepairs(boolean negated, Termination t);
abstract public Descriptor getDescriptor();
abstract public boolean inverted();
public boolean usesDescriptor(RelationDescriptor rd) {
}
}
}
-
cr.endblock();
cr.endblock();
}
Hashtable conjtonodemap;
Hashtable predtoabstractmap;
Set removedset;
-
+ ComputeMaxSize maxsize;
State state;
public Termination(State state) {
if (!Compiler.REPAIR)
return;
+ for(int i=0;i<state.vRules.size();i++)
+ System.out.println(state.vRules.get(i));
+ for(int i=0;i<state.vConstraints.size();i++)
+ System.out.println(state.vConstraints.get(i));
+
+
+ maxsize=new ComputeMaxSize(state);
+
generateconjunctionnodes();
generatescopenodes();
generaterepairnodes();
Conjunction conj=tn.getConjunction();
for(int i=0;i<conj.size();i++) {
DNFPredicate dp=conj.get(i);
- int[] array=dp.getPredicate().getRepairs(dp.isNegated());
+ int[] array=dp.getPredicate().getRepairs(dp.isNegated(),this);
Descriptor d=dp.getPredicate().getDescriptor();
for(int j=0;j<array.length;j++) {
AbstractRepair ar=new AbstractRepair(dp,array[j],d);