8 private Hashtable sizemap;
9 private Hashtable constraintmap;
10 private SetAnalysis setanalysis;
11 private Hashtable minsize;
12 private Hashtable constraintensureminsize;
14 public ExactSize(State state) {
16 this.sizemap=new Hashtable();
17 this.minsize=new Hashtable();
18 this.constraintensureminsize=new Hashtable();
19 this.constraintmap=new Hashtable();
20 this.setanalysis=state.setanalysis;
24 public int minSize(SetDescriptor sd) {
25 SizeObject so=new SizeObject(sd);
26 if (minsize.containsKey(so))
27 return ((Integer)minsize.get(so)).intValue();
32 public Constraint ensuresMinSize(SetDescriptor sd) {
33 SizeObject so=new SizeObject(sd);
34 return (Constraint)constraintensureminsize.get(so);
37 public int getsize(SetDescriptor sd) {
38 SizeObject so=new SizeObject(sd);
39 if (sizemap.containsKey(so))
40 return ((Integer)sizemap.get(so)).intValue();
44 public Constraint getConstraint(SetDescriptor sd) {
45 SizeObject so=new SizeObject(sd);
46 return (Constraint)constraintmap.get(so);
49 public int getsize(RelationDescriptor rd, SetDescriptor sd, boolean inverted) {
50 Iterator it=setanalysis.getSuperset(sd).iterator();
52 SizeObject so=new SizeObject(rd,sd,inverted);
53 if (sizemap.containsKey(so))
54 return ((Integer)sizemap.get(so)).intValue();
57 sd=(SetDescriptor)it.next();
62 public Constraint getConstraint(RelationDescriptor rd, SetDescriptor sd, boolean inverted) {
63 Iterator it=setanalysis.getSuperset(sd).iterator();
65 SizeObject so=new SizeObject(rd,sd,inverted);
66 if (constraintmap.containsKey(so))
67 return ((Constraint)constraintmap.get(so));
70 sd=(SetDescriptor)it.next();
75 private void constructminsizes() {
77 HashSet usedsources=new HashSet();
80 for(int i=0;i<state.vRules.size();i++) {
81 Rule r=(Rule)state.vRules.get(i);
82 //model defition rule with no condition
83 if ((!(r.getGuardExpr() instanceof BooleanLiteralExpr))||
84 (!((BooleanLiteralExpr)r.getGuardExpr()).getValue()))
86 //inclusion condition needs to be safe
87 if ((!(r.getInclusion() instanceof SetInclusion))||
88 (!((SetInclusion)r.getInclusion()).getExpr().isSafe()))
90 //needs exactly 1 quantifier which is a set quantifier
91 if (r.numQuantifiers()!=1||
92 (!(r.getQuantifier(0) instanceof SetQuantifier)))
94 SetQuantifier sq=(SetQuantifier)r.getQuantifier(0);
95 SetDescriptor sd=sq.getSet();
97 Constraint source=null;
100 source=getConstraint(sd);
101 } else if (minSize(sd)>0) {
103 source=ensuresMinSize(sd);
106 size=1; //would need more in depth analysis otherwise
107 SetDescriptor newsd=((SetInclusion)r.getInclusion()).getSet();
108 if (usedsources.contains(newsd))
109 continue; //avoid dependence cycles in our analysis
110 //need to force repair to fix one of the constraints in the cycle
111 int currentsize=minSize(newsd);
112 if (size>currentsize) {
113 SizeObject so=new SizeObject(newsd);
114 minsize.put(so, new Integer(size));
115 constraintensureminsize.put(so,source);
116 usedsources.add(source);
117 System.out.println("Set "+newsd.toString()+" has minimum size "+size);
125 private void computesizes() {
126 for(Iterator it=state.stSets.descriptors();it.hasNext();) {
127 SetDescriptor sd=(SetDescriptor)it.next();
128 for(int i=0;i<state.vConstraints.size();i++) {
129 Constraint c=(Constraint)state.vConstraints.get(i);
130 if (c.numQuantifiers()!=0)
132 DNFConstraint dconst=c.dnfconstraint;
134 boolean matches=true;
135 for(int j=0;j<dconst.size();j++) {
136 Conjunction conj=dconst.get(j);
137 boolean goodmatch=false;
138 for(int k=0;k<conj.size();k++) {
139 DNFPredicate dpred=conj.get(k);
140 if (!dpred.isNegated()) {
141 Predicate p=dpred.getPredicate();
142 if (p instanceof ExprPredicate) {
143 ExprPredicate ep=(ExprPredicate)p;
144 if (ep.getType()==ExprPredicate.SIZE&&
145 ep.getOp()==Opcode.EQ&&
146 ep.getDescriptor()==sd&&
149 oldsize=ep.rightSize();
153 if (oldsize==ep.rightSize()) {
164 break; //this constraint won't work
168 System.out.println("Set "+sd.toString()+" has size "+oldsize);
169 SizeObject so=new SizeObject(sd);
170 sizemap.put(so,new Integer(oldsize));
171 constraintmap.put(so,c);
176 for(Iterator it=state.stRelations.descriptors();it.hasNext();) {
177 RelationDescriptor rd=(RelationDescriptor)it.next();
178 for(int i=0;i<state.vConstraints.size();i++) {
179 Constraint c=(Constraint)state.vConstraints.get(i);
180 if (c.numQuantifiers()!=1||!(c.getQuantifier(0) instanceof SetQuantifier))
182 SetQuantifier q=(SetQuantifier) c.getQuantifier(0);
184 DNFConstraint dconst=c.dnfconstraint;
186 boolean matches=true;
187 boolean inverted=false;
188 for(int j=0;j<dconst.size();j++) {
189 Conjunction conj=dconst.get(j);
190 boolean goodmatch=false;
191 for(int k=0;k<conj.size();k++) {
192 DNFPredicate dpred=conj.get(k);
193 if (!dpred.isNegated()) {
194 Predicate p=dpred.getPredicate();
195 if (p instanceof ExprPredicate) {
196 ExprPredicate ep=(ExprPredicate)p;
197 if (ep.getType()==ExprPredicate.SIZE&&
198 ep.getOp()==Opcode.EQ&&
199 ep.getDescriptor()==rd&&
201 ((ImageSetExpr)((SizeofExpr)((OpExpr)ep.expr).left).getSetExpr()).getVar()==q.getVar()) {
203 oldsize=ep.rightSize();
205 inverted=ep.inverted();
208 if (oldsize==ep.rightSize()&&inverted==ep.inverted()) {
219 break; //this constraint won't work
223 System.out.println("Set "+rd.toString()+" has size "+oldsize);
224 SizeObject so=new SizeObject(rd,q.getSet(),inverted);
225 sizemap.put(so,new Integer(oldsize));
226 constraintmap.put(so,c);