Committing changes to leftsize->rightSize, more comments, and handling
[repair.git] / Repair / RepairCompiler / MCC / IR / ImplicitSchema.java
1 package MCC.IR;
2 import MCC.State;
3 import MCC.Compiler;
4
5 import java.util.*;
6
7 public class ImplicitSchema {
8     State state;
9     SetAnalysis setanalysis;
10     public ImplicitSchema(State state) {
11         this.state=state;
12         this.setanalysis=new SetAnalysis(state);
13     }
14
15     public void update() {
16         if (Compiler.REPAIR) {
17             updaterules();
18         }
19         updateconstraints();
20         updaterelationconstraints();
21     }
22
23     boolean needDomain(RelationDescriptor rd) {
24         return needDR(rd, true);
25     }
26
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)
31             return false;
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()))
45                     return false;
46             }
47         }
48
49
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 */
55                 boolean good=false;
56                 RelationInclusion rinc=(RelationInclusion)inc;
57                 Expr expr=isdomain?rinc.getLeftExpr():rinc.getRightExpr();
58                 if (expr instanceof VarExpr) {
59                     VarDescriptor vd=((VarExpr)expr).getVar();
60                     assert vd!=null;
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()))) {
66                             good=true;
67                             break;
68                         }
69                         if ((q instanceof RelationQuantifier)&&
70                             (
71                             ((((RelationQuantifier)q).x==vd)&&
72                             (sd.allSubsets().contains(((RelationQuantifier)q).getRelation().getDomain())))
73                             ||
74                             ((((RelationQuantifier)q).y==vd)&&
75                             (sd.allSubsets().contains(((RelationQuantifier)q).getRelation().getRange())))
76                             )) {
77                             good=true;
78                             break;
79                         }
80                         
81                     }
82                     if (good)
83                         continue; /* Proved for this case */
84                 }
85                 for(int j=0;j<rules.size();j++) {
86                     Rule r2=(Rule)rules.get(j);
87                     Inclusion inc2=r2.getInclusion();
88                     
89                     if (checkimplication(r,r2,isdomain)) {
90                         good=true;
91                         break;
92                     }
93                 }
94                 if (good)
95                     continue;
96
97                 return true; /* Couldn't prove we didn't need */
98             }
99         }
100         return false;
101     }
102
103     boolean checkimplication(Rule r1, Rule r2, boolean isdomain) {
104         /* r1 is the relation */
105         /* See if this rule guarantees relation */
106         /* Steps:
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();
114         
115         Inclusion inc2=r2.getInclusion();
116         if (!(inc2 instanceof SetInclusion))
117             return false;
118         SetInclusion sinc2=(SetInclusion)inc2;
119         if (sinc2.getSet()!=sd)
120             return false;
121         if (r1.numQuantifiers()!=r2.numQuantifiers())
122             return false; 
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++) {
130                 if (mapping[j]==1)
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()) {
135                     mapping[j]=1;
136                     map.put(((SetQuantifier)q1).getVar(),((SetQuantifier)q2).getVar());
137                     foundmapping=true;
138                     break;
139                 }
140                 if (q1 instanceof RelationQuantifier && q2 instanceof RelationQuantifier &&
141                     ((RelationQuantifier)q1).getRelation()==((RelationQuantifier)q2).getRelation()) {
142                     mapping[j]=1;
143                     map.put(((RelationQuantifier)q1).x,((RelationQuantifier)q2).x);
144                     map.put(((RelationQuantifier)q1).y,((RelationQuantifier)q2).y);
145                     foundmapping=true;
146                     break;
147                 }
148             }
149             if (!foundmapping)
150                 return false;
151         }
152         /* Built mapping */
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)) {
165                     foundmatch=true;
166                     break;
167                 }
168             }
169             if (!foundmatch)
170                 return false;
171         }
172         return true;
173     }
174
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())
183                     continue;
184                 if (de1.getExpr().equals(map,de2.getExpr())) {
185                     havematch=true;
186                     break;
187                 }
188             }
189             if (!havematch)
190                 return false;
191         }
192         return true;
193     }
194
195     boolean needRange(RelationDescriptor rd) {
196         return needDR(rd, false);
197     }
198
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)) {
204                 
205                 Constraint c=new Constraint();
206                 /* Construct quantifier */
207                 LogicStatement ls=null;
208
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);
215                 rq.setRelation(rd);
216                 c.addQuantifier(rq);
217                 c.getSymbolTable().add(var1);
218                 c.getSymbolTable().add(var2);
219                 var1.setType(rd.getDomain().getType());
220                 var2.setType(rd.getRange().getType());
221
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);
227                 }
228
229
230                 if (needRange(rd)) {
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);
237                 }
238                 rd.addUsage(RelationDescriptor.IMAGE);
239
240                 c.setLogicStatement(ls);
241                 state.vConstraints.add(c);
242             }
243         }
244     }
245
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());
258                 sq.setVar(var);
259                 sq.setSet(sd);
260                 c.addQuantifier(sq);
261
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));
269                         se.td=sd.getType();
270                         LogicStatement incpred=new InclusionPredicate(ve,se);
271                         if (j!=k) {
272                             incpred=new LogicStatement(LogicStatement.NOT ,incpred);
273                         }
274                         if (conj==null)
275                             conj=incpred;
276                         else 
277                             conj=new LogicStatement(LogicStatement.AND, conj, incpred);
278                     }
279                     if (ls==null)
280                         ls=conj;
281                     else 
282                         ls=new LogicStatement(LogicStatement.OR, ls, conj);
283                 }
284                 c.setLogicStatement(ls);
285                 state.vConstraints.add(c);
286             }
287         }
288     }
289     
290     void updaterules() {
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);
298                 if (supersets!=null)
299                     for(Iterator superit=supersets.iterator();superit.hasNext();) {
300                         SetDescriptor sd1=(SetDescriptor)superit.next();
301                         Rule nr=new Rule();
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);
307                         nr.st=r.st;
308                         nr.setnogenerate();
309                         newrules.add(nr);
310                         state.implicitrule.put(nr,r);
311                     }
312             }
313         }
314         oldrules.addAll(newrules);
315     }
316 }