5 public class ImplicitSchema {
7 SetAnalysis setanalysis;
8 public ImplicitSchema(State state) {
10 this.setanalysis=new SetAnalysis(state);
13 public void update() {
16 updaterelationconstraints();
19 boolean needDomain(RelationDescriptor rd) {
20 return needDR(rd, true);
23 boolean needDR(RelationDescriptor rd,boolean isdomain) {
24 Vector rules=state.vRules;
25 SetDescriptor sd=isdomain?rd.getDomain():rd.getRange();
26 for(int i=0;i<rules.size();i++) {
27 Rule r=(Rule)rules.get(i);
28 if ((r.numQuantifiers()==1)&&
29 (r.getQuantifier(0) instanceof RelationQuantifier)&&
30 (((RelationQuantifier)r.getQuantifier(0)).getRelation()==rd)&&
31 r.getInclusion().getTargetDescriptors().contains(sd)) {
32 SetInclusion rinc=(SetInclusion)r.getInclusion();
33 RelationQuantifier rq=(RelationQuantifier)r.getQuantifier(0);
34 VarDescriptor vd=isdomain?rq.x:rq.y;
35 if ((rinc.getExpr() instanceof VarExpr)&&
36 (((VarExpr)rinc.getExpr()).getVar()==vd)&&
37 (r.getGuardExpr() instanceof BooleanLiteralExpr)&&
38 (((BooleanLiteralExpr)r.getGuardExpr()).getValue()))
44 for(int i=0;i<rules.size();i++) {
45 Rule r=(Rule)rules.get(i);
46 Inclusion inc=r.getInclusion();
47 if (inc.getTargetDescriptors().contains(rd)) {
48 /* Need to check this rule */
50 RelationInclusion rinc=(RelationInclusion)inc;
51 Expr expr=isdomain?rinc.getLeftExpr():rinc.getRightExpr();
52 if (expr instanceof VarExpr) {
53 VarDescriptor vd=((VarExpr)expr).getVar();
55 for (int j=0;j<r.numQuantifiers();j++) {
56 Quantifier q=r.getQuantifier(j);
57 if ((q instanceof SetQuantifier)&&
58 (((SetQuantifier)q).getVar()==vd)&&
59 (sd.allSubsets().contains(((SetQuantifier)q).getSet()))) {
63 if ((q instanceof RelationQuantifier)&&
65 ((((RelationQuantifier)q).x==vd)&&
66 (sd.allSubsets().contains(((RelationQuantifier)q).getRelation().getDomain())))
68 ((((RelationQuantifier)q).y==vd)&&
69 (sd.allSubsets().contains(((RelationQuantifier)q).getRelation().getRange())))
77 continue; /* Proved for this case */
79 for(int j=0;j<rules.size();j++) {
80 Rule r2=(Rule)rules.get(j);
81 Inclusion inc2=r2.getInclusion();
83 if (checkimplication(r,r2,isdomain)) {
91 return true; /* Couldn't prove we didn't need */
97 boolean checkimplication(Rule r1, Rule r2, boolean isdomain) {
98 /* r1 is the relation */
99 /* See if this rule guarantees relation */
101 1. match up quantifiers
102 2. check inclusion condition
103 3. see if guard expr of set rule is more general */
104 RelationInclusion inc1=(RelationInclusion) r1.getInclusion();
105 RelationDescriptor rd=inc1.getRelation();
106 SetDescriptor sd=isdomain?rd.getDomain():rd.getRange();
107 Expr expr=isdomain?inc1.getLeftExpr():inc1.getRightExpr();
109 Inclusion inc2=r2.getInclusion();
110 if (!(inc2 instanceof SetInclusion))
112 SetInclusion sinc2=(SetInclusion)inc2;
113 if (sinc2.getSet()!=sd)
115 if (r1.numQuantifiers()!=r2.numQuantifiers())
117 /* Construct a mapping between quantifiers */
118 int[] mapping=new int[r2.numQuantifiers()];
119 HashMap map=new HashMap();
120 for(int i=0;i<r1.numQuantifiers();i++) {
121 Quantifier q1=r1.getQuantifier(i);
122 boolean foundmapping=false;
123 for (int j=0;j<r2.numQuantifiers();j++) {
125 continue; /* Its already used */
126 Quantifier q2=r2.getQuantifier(j);
127 if (q1 instanceof SetQuantifier && q2 instanceof SetQuantifier&&
128 ((SetQuantifier)q1).getSet()==((SetQuantifier)q2).getSet()) {
130 map.put(((SetQuantifier)q1).getVar(),((SetQuantifier)q2).getVar());
134 if (q1 instanceof RelationQuantifier && q2 instanceof RelationQuantifier &&
135 ((RelationQuantifier)q1).getRelation()==((RelationQuantifier)q2).getRelation()) {
137 map.put(((RelationQuantifier)q1).x,((RelationQuantifier)q2).x);
138 map.put(((RelationQuantifier)q1).y,((RelationQuantifier)q2).y);
147 Expr sexpr=sinc2.getExpr();
148 if (!expr.equals(map,sexpr))
149 return false; /* This rule doesn't add the right thing */
150 DNFRule drule1=r1.getDNFGuardExpr();
151 DNFRule drule2=r2.getDNFGuardExpr();
152 for (int i=0;i<drule1.size();i++) {
153 RuleConjunction rconj1=drule1.get(i);
154 boolean foundmatch=false;
155 for (int j=0;j<drule2.size();j++) {
156 RuleConjunction rconj2=drule2.get(j);
157 /* Need to show than rconj2 is true if rconj1 is true */
158 if (implication(map,rconj1,rconj2)) {
169 boolean implication(HashMap map, RuleConjunction rc1, RuleConjunction rc2) {
170 for(int i=0;i<rc2.size();i++) {
171 /* Check that rc1 has all predicates that rc2 has */
172 DNFExpr de2=rc2.get(i);
173 boolean havematch=false;
174 for(int j=0;j<rc1.size();j++) {
175 DNFExpr de1=rc1.get(i);
176 if (de1.getNegation()!=de2.getNegation())
178 if (de1.getExpr().equals(map,de2.getExpr())) {
189 boolean needRange(RelationDescriptor rd) {
190 return needDR(rd, false);
193 void updaterelationconstraints() {
194 Vector reldescriptors=state.stRelations.getAllDescriptors();
195 for(int i=0;i<reldescriptors.size();i++) {
196 RelationDescriptor rd=(RelationDescriptor) reldescriptors.get(i);
197 if (needDomain(rd)||needRange(rd)) {
199 Constraint c=new Constraint();
200 /* Construct quantifier */
201 LogicStatement ls=null;
203 RelationQuantifier rq=new RelationQuantifier();
204 String varname1=new String("relationvar1");
205 VarDescriptor var1=new VarDescriptor(varname1);
206 String varname2=new String("relationvar2");
207 VarDescriptor var2=new VarDescriptor(varname2);
208 rq.setTuple(var1,var2);
211 c.getSymbolTable().add(var1);
212 c.getSymbolTable().add(var2);
213 var1.setType(rd.getDomain().getType());
214 var2.setType(rd.getRange().getType());
216 if (needDomain(rd)) {
217 VarExpr ve1=new VarExpr(var1);
218 SetExpr se1=new SetExpr(rd.getDomain());
219 se1.td=rd.getDomain().getType();
220 ls=new InclusionPredicate(ve1,se1);
225 VarExpr ve2=new VarExpr(var2);
226 SetExpr se2=new SetExpr(rd.getRange());
227 se2.td=rd.getRange().getType();
228 LogicStatement incpred2=new InclusionPredicate(ve2,se2);
229 if (ls==null) ls=incpred2;
230 else ls=new LogicStatement(LogicStatement.AND,ls,incpred2);
232 rd.addUsage(RelationDescriptor.IMAGE);
234 c.setLogicStatement(ls);
235 state.vConstraints.add(c);
240 void updateconstraints() {
241 Vector setdescriptors=state.stSets.getAllDescriptors();
242 for(int i=0;i<setdescriptors.size();i++) {
243 SetDescriptor sd=(SetDescriptor) setdescriptors.get(i);
244 if(sd.isPartition()) {
245 Constraint c=new Constraint();
246 /* Construct quantifier */
247 SetQuantifier sq=new SetQuantifier();
248 String varname=new String("partitionvar");
249 VarDescriptor var=new VarDescriptor(varname);
250 c.getSymbolTable().add(var);
251 var.setType(sd.getType());
256 /*Construct logic statement*/
257 LogicStatement ls=null;
258 for(int j=0;j<sd.getSubsets().size();j++) {
259 LogicStatement conj=null;
260 for(int k=0;k<sd.getSubsets().size();k++) {
261 VarExpr ve=new VarExpr(var);
262 SetExpr se=new SetExpr((SetDescriptor) sd.getSubsets().get(k));
264 LogicStatement incpred=new InclusionPredicate(ve,se);
266 incpred=new LogicStatement(LogicStatement.NOT ,incpred);
271 conj=new LogicStatement(LogicStatement.AND, conj, incpred);
276 ls=new LogicStatement(LogicStatement.OR, ls, conj);
278 c.setLogicStatement(ls);
279 state.vConstraints.add(c);
285 Vector oldrules=state.vRules;
286 Vector newrules=new Vector();
287 for(int i=0;i<oldrules.size();i++) {
288 Rule r=(Rule)oldrules.get(i);
289 if (r.inclusion instanceof SetInclusion) {
290 SetDescriptor sd=((SetInclusion)r.inclusion).getSet();
291 Set supersets=setanalysis.getSuperset(sd);
293 for(Iterator superit=supersets.iterator();superit.hasNext();) {
294 SetDescriptor sd1=(SetDescriptor)superit.next();
296 nr.setGuardExpr(r.getGuardExpr());
297 nr.quantifiers=r.quantifiers;
298 nr.isstatic=r.isstatic;
299 nr.isdelay=r.isdelay;
300 nr.inclusion=new SetInclusion(((SetInclusion)r.inclusion).elementexpr,sd1);
306 oldrules.addAll(newrules);