7 public class ImplicitSchema {
9 SetAnalysis setanalysis;
10 public ImplicitSchema(State state) {
12 this.setanalysis=new SetAnalysis(state);
15 public void update() {
16 if (Compiler.REPAIR) {
20 updaterelationconstraints();
23 boolean needDomain(RelationDescriptor rd) {
24 return needDR(rd, true);
27 boolean needDR(RelationDescriptor rd,boolean isdomain) {
28 Vector rules=state.vRules;
29 SetDescriptor sd=isdomain?rd.getDomain():rd.getRange();
30 for(int i=0;i<rules.size();i++) {
31 Rule r=(Rule)rules.get(i);
32 if ((r.numQuantifiers()==1)&&
33 (r.getQuantifier(0) instanceof RelationQuantifier)&&
34 (((RelationQuantifier)r.getQuantifier(0)).getRelation()==rd)&&
35 r.getInclusion().getTargetDescriptors().contains(sd)) {
36 SetInclusion rinc=(SetInclusion)r.getInclusion();
37 RelationQuantifier rq=(RelationQuantifier)r.getQuantifier(0);
38 VarDescriptor vd=isdomain?rq.x:rq.y;
39 if ((rinc.getExpr() instanceof VarExpr)&&
40 (((VarExpr)rinc.getExpr()).getVar()==vd)&&
41 (r.getGuardExpr() instanceof BooleanLiteralExpr)&&
42 (((BooleanLiteralExpr)r.getGuardExpr()).getValue()))
48 for(int i=0;i<rules.size();i++) {
49 Rule r=(Rule)rules.get(i);
50 Inclusion inc=r.getInclusion();
51 if (inc.getTargetDescriptors().contains(rd)) {
52 /* Need to check this rule */
54 RelationInclusion rinc=(RelationInclusion)inc;
55 Expr expr=isdomain?rinc.getLeftExpr():rinc.getRightExpr();
56 if (expr instanceof VarExpr) {
57 VarDescriptor vd=((VarExpr)expr).getVar();
59 for (int j=0;j<r.numQuantifiers();j++) {
60 Quantifier q=r.getQuantifier(j);
61 if ((q instanceof SetQuantifier)&&
62 (((SetQuantifier)q).getVar()==vd)&&
63 (sd.allSubsets().contains(((SetQuantifier)q).getSet()))) {
67 if ((q instanceof RelationQuantifier)&&
69 ((((RelationQuantifier)q).x==vd)&&
70 (sd.allSubsets().contains(((RelationQuantifier)q).getRelation().getDomain())))
72 ((((RelationQuantifier)q).y==vd)&&
73 (sd.allSubsets().contains(((RelationQuantifier)q).getRelation().getRange())))
81 continue; /* Proved for this case */
83 for(int j=0;j<rules.size();j++) {
84 Rule r2=(Rule)rules.get(j);
85 Inclusion inc2=r2.getInclusion();
87 if (checkimplication(r,r2,isdomain)) {
95 return true; /* Couldn't prove we didn't need */
101 boolean checkimplication(Rule r1, Rule r2, boolean isdomain) {
102 /* r1 is the relation */
103 /* See if this rule guarantees relation */
105 1. match up quantifiers
106 2. check inclusion condition
107 3. see if guard expr of set rule is more general */
108 RelationInclusion inc1=(RelationInclusion) r1.getInclusion();
109 RelationDescriptor rd=inc1.getRelation();
110 SetDescriptor sd=isdomain?rd.getDomain():rd.getRange();
111 Expr expr=isdomain?inc1.getLeftExpr():inc1.getRightExpr();
113 Inclusion inc2=r2.getInclusion();
114 if (!(inc2 instanceof SetInclusion))
116 SetInclusion sinc2=(SetInclusion)inc2;
117 if (sinc2.getSet()!=sd)
119 if (r1.numQuantifiers()!=r2.numQuantifiers())
121 /* Construct a mapping between quantifiers */
122 int[] mapping=new int[r2.numQuantifiers()];
123 HashMap map=new HashMap();
124 for(int i=0;i<r1.numQuantifiers();i++) {
125 Quantifier q1=r1.getQuantifier(i);
126 boolean foundmapping=false;
127 for (int j=0;j<r2.numQuantifiers();j++) {
129 continue; /* Its already used */
130 Quantifier q2=r2.getQuantifier(j);
131 if (q1 instanceof SetQuantifier && q2 instanceof SetQuantifier&&
132 ((SetQuantifier)q1).getSet()==((SetQuantifier)q2).getSet()) {
134 map.put(((SetQuantifier)q1).getVar(),((SetQuantifier)q2).getVar());
138 if (q1 instanceof RelationQuantifier && q2 instanceof RelationQuantifier &&
139 ((RelationQuantifier)q1).getRelation()==((RelationQuantifier)q2).getRelation()) {
141 map.put(((RelationQuantifier)q1).x,((RelationQuantifier)q2).x);
142 map.put(((RelationQuantifier)q1).y,((RelationQuantifier)q2).y);
151 Expr sexpr=sinc2.getExpr();
152 if (!expr.equals(map,sexpr))
153 return false; /* This rule doesn't add the right thing */
154 DNFRule drule1=r1.getDNFGuardExpr();
155 DNFRule drule2=r2.getDNFGuardExpr();
156 for (int i=0;i<drule1.size();i++) {
157 RuleConjunction rconj1=drule1.get(i);
158 boolean foundmatch=false;
159 for (int j=0;j<drule2.size();j++) {
160 RuleConjunction rconj2=drule2.get(j);
161 /* Need to show than rconj2 is true if rconj1 is true */
162 if (implication(map,rconj1,rconj2)) {
173 boolean implication(HashMap map, RuleConjunction rc1, RuleConjunction rc2) {
174 for(int i=0;i<rc2.size();i++) {
175 /* Check that rc1 has all predicates that rc2 has */
176 DNFExpr de2=rc2.get(i);
177 boolean havematch=false;
178 for(int j=0;j<rc1.size();j++) {
179 DNFExpr de1=rc1.get(i);
180 if (de1.getNegation()!=de2.getNegation())
182 if (de1.getExpr().equals(map,de2.getExpr())) {
193 boolean needRange(RelationDescriptor rd) {
194 return needDR(rd, false);
197 void updaterelationconstraints() {
198 Vector reldescriptors=state.stRelations.getAllDescriptors();
199 for(int i=0;i<reldescriptors.size();i++) {
200 RelationDescriptor rd=(RelationDescriptor) reldescriptors.get(i);
201 if (needDomain(rd)||needRange(rd)) {
203 Constraint c=new Constraint();
204 /* Construct quantifier */
205 LogicStatement ls=null;
207 RelationQuantifier rq=new RelationQuantifier();
208 String varname1=new String("relationvar1");
209 VarDescriptor var1=new VarDescriptor(varname1);
210 String varname2=new String("relationvar2");
211 VarDescriptor var2=new VarDescriptor(varname2);
212 rq.setTuple(var1,var2);
215 c.getSymbolTable().add(var1);
216 c.getSymbolTable().add(var2);
217 var1.setType(rd.getDomain().getType());
218 var2.setType(rd.getRange().getType());
220 if (needDomain(rd)) {
221 VarExpr ve1=new VarExpr(var1);
222 SetExpr se1=new SetExpr(rd.getDomain());
223 se1.td=rd.getDomain().getType();
224 ls=new InclusionPredicate(ve1,se1);
229 VarExpr ve2=new VarExpr(var2);
230 SetExpr se2=new SetExpr(rd.getRange());
231 se2.td=rd.getRange().getType();
232 LogicStatement incpred2=new InclusionPredicate(ve2,se2);
233 if (ls==null) ls=incpred2;
234 else ls=new LogicStatement(LogicStatement.AND,ls,incpred2);
236 rd.addUsage(RelationDescriptor.IMAGE);
238 c.setLogicStatement(ls);
239 state.vConstraints.add(c);
244 void updateconstraints() {
245 Vector setdescriptors=state.stSets.getAllDescriptors();
246 for(int i=0;i<setdescriptors.size();i++) {
247 SetDescriptor sd=(SetDescriptor) setdescriptors.get(i);
248 if(sd.isPartition()) {
249 Constraint c=new Constraint();
250 /* Construct quantifier */
251 SetQuantifier sq=new SetQuantifier();
252 String varname=new String("partitionvar");
253 VarDescriptor var=new VarDescriptor(varname);
254 c.getSymbolTable().add(var);
255 var.setType(sd.getType());
260 /*Construct logic statement*/
261 LogicStatement ls=null;
262 for(int j=0;j<sd.getSubsets().size();j++) {
263 LogicStatement conj=null;
264 for(int k=0;k<sd.getSubsets().size();k++) {
265 VarExpr ve=new VarExpr(var);
266 SetExpr se=new SetExpr((SetDescriptor) sd.getSubsets().get(k));
268 LogicStatement incpred=new InclusionPredicate(ve,se);
270 incpred=new LogicStatement(LogicStatement.NOT ,incpred);
275 conj=new LogicStatement(LogicStatement.AND, conj, incpred);
280 ls=new LogicStatement(LogicStatement.OR, ls, conj);
282 c.setLogicStatement(ls);
283 state.vConstraints.add(c);
289 Vector oldrules=state.vRules;
290 Vector newrules=new Vector();
291 for(int i=0;i<oldrules.size();i++) {
292 Rule r=(Rule)oldrules.get(i);
293 if (r.inclusion instanceof SetInclusion) {
294 SetDescriptor sd=((SetInclusion)r.inclusion).getSet();
295 Set supersets=setanalysis.getSuperset(sd);
297 for(Iterator superit=supersets.iterator();superit.hasNext();) {
298 SetDescriptor sd1=(SetDescriptor)superit.next();
300 nr.setGuardExpr(r.getGuardExpr());
301 nr.quantifiers=r.quantifiers;
302 nr.isstatic=r.isstatic;
303 nr.isdelay=r.isdelay;
304 nr.inclusion=new SetInclusion(((SetInclusion)r.inclusion).elementexpr,sd1);
308 state.implicitrule.put(nr,r);
312 oldrules.addAll(newrules);