Updates
[repair.git] / Repair / RepairCompiler / MCC / IR / ImplicitSchema.java
1 package MCC.IR;
2 import MCC.State;
3 import java.util.*;
4
5 public class ImplicitSchema {
6     State state;
7     SetAnalysis setanalysis;
8     public ImplicitSchema(State state) {
9         this.state=state;
10         this.setanalysis=new SetAnalysis(state);
11     }
12
13     public void update() {
14         updaterules();
15         updateconstraints();
16         updaterelationconstraints();
17     }
18
19     void updaterelationconstraints() {
20         Vector reldescriptors=state.stRelations.getAllDescriptors();
21         for(int i=0;i<reldescriptors.size();i++) {
22             RelationDescriptor rd=(RelationDescriptor) reldescriptors.get(i);
23             Constraint c=new Constraint();
24             
25             /* Construct quantifier */
26             RelationQuantifier rq=new RelationQuantifier();
27             String varname1=new String("partitionvar1");
28             String varname2=new String("partitionvar2");
29             VarDescriptor var1=new VarDescriptor(varname1);
30             VarDescriptor var2=new VarDescriptor(varname2);
31             c.getSymbolTable().add(var1);
32             c.getSymbolTable().add(var2);
33             var1.setType(rd.getDomain().getType());
34             var2.setType(rd.getRange().getType());
35             rq.setTuple(var1,var2);
36             rq.setRelation(rd);
37             c.addQuantifier(rq);
38
39             VarExpr ve1=new VarExpr(varname1);
40             SetExpr se1=new SetExpr(rd.getDomain());
41             LogicStatement incpred1=new InclusionPredicate(ve1,se1);
42
43             VarExpr ve2=new VarExpr(varname2);
44             SetExpr se2=new SetExpr(rd.getRange());
45             LogicStatement incpred2=new InclusionPredicate(ve2,se2);
46             c.setLogicStatement(new LogicStatement(LogicStatement.AND,incpred1,incpred2));
47             state.vConstraints.add(c);
48         }
49     }
50
51     void updateconstraints() {
52         Vector setdescriptors=state.stSets.getAllDescriptors();
53         for(int i=0;i<setdescriptors.size();i++) {
54             SetDescriptor sd=(SetDescriptor) setdescriptors.get(i);
55             if(sd.isPartition()) {
56                 Constraint c=new Constraint();
57
58                 /* Construct quantifier */
59                 SetQuantifier sq=new SetQuantifier();
60                 String varname=new String("partitionvar");
61                 VarDescriptor var=new VarDescriptor(varname);
62                 c.getSymbolTable().add(var);
63                 var.setType(sd.getType());
64                 sq.setVar(var);
65                 sq.setSet(sd);
66                 c.addQuantifier(sq);
67
68                 /*Construct logic statement*/
69                 LogicStatement ls=null;
70                 for(int j=0;j<sd.getSubsets().size();j++) {
71                     LogicStatement conj=null;
72                     for(int k=0;k<sd.getSubsets().size();k++) {
73                         VarExpr ve=new VarExpr(varname);
74                         SetExpr se=new SetExpr((SetDescriptor) sd.getSubsets().get(k));
75                         LogicStatement incpred=new InclusionPredicate(ve,se);
76                         if (j!=k) {
77                             incpred=new LogicStatement(LogicStatement.NOT ,incpred);
78                         }
79                         if (conj==null)
80                             conj=incpred;
81                         else 
82                             conj=new LogicStatement(LogicStatement.AND, conj, incpred);
83                     }
84                     if (ls==null)
85                         ls=conj;
86                     else 
87                         ls=new LogicStatement(LogicStatement.OR, ls, conj);
88                 }
89                 c.setLogicStatement(ls);
90                 state.vConstraints.add(c);
91             }
92         }
93     }
94     
95     void updaterules() {
96         Vector oldrules=state.vRules;
97         Vector newrules=new Vector();
98         for(int i=0;i<oldrules.size();i++) {
99             Rule r=(Rule)oldrules.get(i);
100             if (r.inclusion instanceof SetInclusion) {
101                 SetDescriptor sd=((SetInclusion)r.inclusion).getSet();
102                 Set supersets=setanalysis.getSuperset(sd);
103                 if (supersets!=null)
104                     for(Iterator superit=supersets.iterator();superit.hasNext();) {
105                         SetDescriptor sd1=(SetDescriptor)superit.next();
106                         Rule nr=new Rule();
107                         nr.guard=r.guard;
108                         nr.quantifiers=r.quantifiers;
109                         nr.isstatic=r.isstatic;
110                         nr.isdelay=r.isdelay;
111                         nr.inclusion=new SetInclusion(((SetInclusion)r.inclusion).elementexpr,sd1);
112                         nr.st=r.st;
113                         newrules.add(nr);
114                     }
115             }
116         }
117         oldrules.addAll(newrules);
118     }
119 }