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 if (sd instanceof ReservedSetDescriptor)
32 for(int i=0;i<rules.size();i++) {
33 Rule r=(Rule)rules.get(i);
34 if ((r.numQuantifiers()==1)&&
35 (r.getQuantifier(0) instanceof RelationQuantifier)&&
36 (((RelationQuantifier)r.getQuantifier(0)).getRelation()==rd)&&
37 r.getInclusion().getTargetDescriptors().contains(sd)) {
38 SetInclusion rinc=(SetInclusion)r.getInclusion();
39 RelationQuantifier rq=(RelationQuantifier)r.getQuantifier(0);
40 VarDescriptor vd=isdomain?rq.x:rq.y;
41 if ((rinc.getExpr() instanceof VarExpr)&&
42 (((VarExpr)rinc.getExpr()).getVar()==vd)&&
43 (r.getGuardExpr() instanceof BooleanLiteralExpr)&&
44 (((BooleanLiteralExpr)r.getGuardExpr()).getValue()))
50 for(int i=0;i<rules.size();i++) {
51 Rule r=(Rule)rules.get(i);
52 Inclusion inc=r.getInclusion();
53 if (inc.getTargetDescriptors().contains(rd)) {
54 /* Need to check this rule */
56 RelationInclusion rinc=(RelationInclusion)inc;
57 Expr expr=isdomain?rinc.getLeftExpr():rinc.getRightExpr();
58 if (expr instanceof VarExpr) {
59 VarDescriptor vd=((VarExpr)expr).getVar();
61 for (int j=0;j<r.numQuantifiers();j++) {
62 Quantifier q=r.getQuantifier(j);
63 if ((q instanceof SetQuantifier)&&
64 (((SetQuantifier)q).getVar()==vd)&&
65 (sd.allSubsets().contains(((SetQuantifier)q).getSet()))) {
69 if ((q instanceof RelationQuantifier)&&
71 ((((RelationQuantifier)q).x==vd)&&
72 (sd.allSubsets().contains(((RelationQuantifier)q).getRelation().getDomain())))
74 ((((RelationQuantifier)q).y==vd)&&
75 (sd.allSubsets().contains(((RelationQuantifier)q).getRelation().getRange())))
83 continue; /* Proved for this case */
85 for(int j=0;j<rules.size();j++) {
86 Rule r2=(Rule)rules.get(j);
87 Inclusion inc2=r2.getInclusion();
89 if (checkimplication(r,r2,isdomain)) {
97 return true; /* Couldn't prove we didn't need */
103 boolean checkimplication(Rule r1, Rule r2, boolean isdomain) {
104 /* r1 is the relation */
105 /* See if this rule guarantees relation */
107 1. match up quantifiers
108 2. check inclusion condition
109 3. see if guard expr of set rule is more general */
110 RelationInclusion inc1=(RelationInclusion) r1.getInclusion();
111 RelationDescriptor rd=inc1.getRelation();
112 SetDescriptor sd=isdomain?rd.getDomain():rd.getRange();
113 Expr expr=isdomain?inc1.getLeftExpr():inc1.getRightExpr();
115 Inclusion inc2=r2.getInclusion();
116 if (!(inc2 instanceof SetInclusion))
118 SetInclusion sinc2=(SetInclusion)inc2;
119 if (sinc2.getSet()!=sd)
121 if (r1.numQuantifiers()!=r2.numQuantifiers())
123 /* Construct a mapping between quantifiers */
124 int[] mapping=new int[r2.numQuantifiers()];
125 HashMap map=new HashMap();
126 for(int i=0;i<r1.numQuantifiers();i++) {
127 Quantifier q1=r1.getQuantifier(i);
128 boolean foundmapping=false;
129 for (int j=0;j<r2.numQuantifiers();j++) {
131 continue; /* Its already used */
132 Quantifier q2=r2.getQuantifier(j);
133 if (q1 instanceof SetQuantifier && q2 instanceof SetQuantifier&&
134 ((SetQuantifier)q1).getSet()==((SetQuantifier)q2).getSet()) {
136 map.put(((SetQuantifier)q1).getVar(),((SetQuantifier)q2).getVar());
140 if (q1 instanceof RelationQuantifier && q2 instanceof RelationQuantifier &&
141 ((RelationQuantifier)q1).getRelation()==((RelationQuantifier)q2).getRelation()) {
143 map.put(((RelationQuantifier)q1).x,((RelationQuantifier)q2).x);
144 map.put(((RelationQuantifier)q1).y,((RelationQuantifier)q2).y);
153 Expr sexpr=sinc2.getExpr();
154 if (!expr.equals(map,sexpr))
155 return false; /* This rule doesn't add the right thing */
156 DNFRule drule1=r1.getDNFGuardExpr();
157 DNFRule drule2=r2.getDNFGuardExpr();
158 for (int i=0;i<drule1.size();i++) {
159 RuleConjunction rconj1=drule1.get(i);
160 boolean foundmatch=false;
161 for (int j=0;j<drule2.size();j++) {
162 RuleConjunction rconj2=drule2.get(j);
163 /* Need to show than rconj2 is true if rconj1 is true */
164 if (implication(map,rconj1,rconj2)) {
175 boolean implication(HashMap map, RuleConjunction rc1, RuleConjunction rc2) {
176 for(int i=0;i<rc2.size();i++) {
177 /* Check that rc1 has all predicates that rc2 has */
178 DNFExpr de2=rc2.get(i);
179 boolean havematch=false;
180 for(int j=0;j<rc1.size();j++) {
181 DNFExpr de1=rc1.get(i);
182 if (de1.getNegation()!=de2.getNegation())
184 if (de1.getExpr().equals(map,de2.getExpr())) {
195 boolean needRange(RelationDescriptor rd) {
196 return needDR(rd, false);
199 void updaterelationconstraints() {
200 Vector reldescriptors=state.stRelations.getAllDescriptors();
201 for(int i=0;i<reldescriptors.size();i++) {
202 RelationDescriptor rd=(RelationDescriptor) reldescriptors.get(i);
203 if (needDomain(rd)||needRange(rd)) {
205 Constraint c=new Constraint();
206 /* Construct quantifier */
207 LogicStatement ls=null;
209 RelationQuantifier rq=new RelationQuantifier();
210 String varname1=new String("relationvar1");
211 VarDescriptor var1=new VarDescriptor(varname1);
212 String varname2=new String("relationvar2");
213 VarDescriptor var2=new VarDescriptor(varname2);
214 rq.setTuple(var1,var2);
217 c.getSymbolTable().add(var1);
218 c.getSymbolTable().add(var2);
219 var1.setType(rd.getDomain().getType());
220 var2.setType(rd.getRange().getType());
222 if (needDomain(rd)) {
223 VarExpr ve1=new VarExpr(var1);
224 SetExpr se1=new SetExpr(rd.getDomain());
225 se1.td=rd.getDomain().getType();
226 ls=new InclusionPredicate(ve1,se1);
231 VarExpr ve2=new VarExpr(var2);
232 SetExpr se2=new SetExpr(rd.getRange());
233 se2.td=rd.getRange().getType();
234 LogicStatement incpred2=new InclusionPredicate(ve2,se2);
235 if (ls==null) ls=incpred2;
236 else ls=new LogicStatement(LogicStatement.AND,ls,incpred2);
238 rd.addUsage(RelationDescriptor.IMAGE);
240 c.setLogicStatement(ls);
241 state.vConstraints.add(c);
246 void updateconstraints() {
247 Vector setdescriptors=state.stSets.getAllDescriptors();
248 for(int i=0;i<setdescriptors.size();i++) {
249 SetDescriptor sd=(SetDescriptor) setdescriptors.get(i);
250 if(sd.isPartition()) {
251 Constraint c=new Constraint();
252 /* Construct quantifier */
253 SetQuantifier sq=new SetQuantifier();
254 String varname=new String("partitionvar");
255 VarDescriptor var=new VarDescriptor(varname);
256 c.getSymbolTable().add(var);
257 var.setType(sd.getType());
262 /*Construct logic statement*/
263 LogicStatement ls=null;
264 for(int j=0;j<sd.getSubsets().size();j++) {
265 LogicStatement conj=null;
266 for(int k=0;k<sd.getSubsets().size();k++) {
267 VarExpr ve=new VarExpr(var);
268 SetExpr se=new SetExpr((SetDescriptor) sd.getSubsets().get(k));
270 LogicStatement incpred=new InclusionPredicate(ve,se);
272 incpred=new LogicStatement(LogicStatement.NOT ,incpred);
277 conj=new LogicStatement(LogicStatement.AND, conj, incpred);
282 ls=new LogicStatement(LogicStatement.OR, ls, conj);
284 c.setLogicStatement(ls);
285 state.vConstraints.add(c);
291 Vector oldrules=state.vRules;
292 Vector newrules=new Vector();
293 for(int i=0;i<oldrules.size();i++) {
294 Rule r=(Rule)oldrules.get(i);
295 if (r.inclusion instanceof SetInclusion) {
296 SetDescriptor sd=((SetInclusion)r.inclusion).getSet();
297 Set supersets=setanalysis.getSuperset(sd);
299 for(Iterator superit=supersets.iterator();superit.hasNext();) {
300 SetDescriptor sd1=(SetDescriptor)superit.next();
302 nr.setGuardExpr(r.getGuardExpr());
303 nr.quantifiers=r.quantifiers;
304 nr.isstatic=r.isstatic;
305 nr.isdelay=r.isdelay;
306 nr.inclusion=new SetInclusion(((SetInclusion)r.inclusion).elementexpr,sd1);
310 state.implicitrule.put(nr,r);
314 oldrules.addAll(newrules);