Fixed lot of random bugs. Added code generate strings for expr's.
[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("relationvar1");
28             String varname2=new String("relationvar2");
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             VarExpr ve1=new VarExpr(varname1);
39             SetExpr se1=new SetExpr(rd.getDomain());
40             LogicStatement incpred1=new InclusionPredicate(ve1,se1);
41             VarExpr ve2=new VarExpr(varname2);
42             SetExpr se2=new SetExpr(rd.getRange());
43             LogicStatement incpred2=new InclusionPredicate(ve2,se2);
44             c.setLogicStatement(new LogicStatement(LogicStatement.AND,incpred1,incpred2));
45             state.vConstraints.add(c);
46         }
47     }
48
49     void updateconstraints() {
50         Vector setdescriptors=state.stSets.getAllDescriptors();
51         for(int i=0;i<setdescriptors.size();i++) {
52             SetDescriptor sd=(SetDescriptor) setdescriptors.get(i);
53             if(sd.isPartition()) {
54                 Constraint c=new Constraint();
55                 /* Construct quantifier */
56                 SetQuantifier sq=new SetQuantifier();
57                 String varname=new String("partitionvar");
58                 VarDescriptor var=new VarDescriptor(varname);
59                 c.getSymbolTable().add(var);
60                 var.setType(sd.getType());
61                 sq.setVar(var);
62                 sq.setSet(sd);
63                 c.addQuantifier(sq);
64
65                 /*Construct logic statement*/
66                 LogicStatement ls=null;
67                 for(int j=0;j<sd.getSubsets().size();j++) {
68                     LogicStatement conj=null;
69                     for(int k=0;k<sd.getSubsets().size();k++) {
70                         VarExpr ve=new VarExpr(varname);
71                         SetExpr se=new SetExpr((SetDescriptor) sd.getSubsets().get(k));
72                         LogicStatement incpred=new InclusionPredicate(ve,se);
73                         if (j!=k) {
74                             incpred=new LogicStatement(LogicStatement.NOT ,incpred);
75                         }
76                         if (conj==null)
77                             conj=incpred;
78                         else 
79                             conj=new LogicStatement(LogicStatement.AND, conj, incpred);
80                     }
81                     if (ls==null)
82                         ls=conj;
83                     else 
84                         ls=new LogicStatement(LogicStatement.OR, ls, conj);
85                 }
86                 c.setLogicStatement(ls);
87                 state.vConstraints.add(c);
88             }
89         }
90     }
91     
92     void updaterules() {
93         Vector oldrules=state.vRules;
94         Vector newrules=new Vector();
95         for(int i=0;i<oldrules.size();i++) {
96             Rule r=(Rule)oldrules.get(i);
97             if (r.inclusion instanceof SetInclusion) {
98                 SetDescriptor sd=((SetInclusion)r.inclusion).getSet();
99                 Set supersets=setanalysis.getSuperset(sd);
100                 if (supersets!=null)
101                     for(Iterator superit=supersets.iterator();superit.hasNext();) {
102                         SetDescriptor sd1=(SetDescriptor)superit.next();
103                         Rule nr=new Rule();
104                         nr.guard=r.guard;
105                         nr.quantifiers=r.quantifiers;
106                         nr.isstatic=r.isstatic;
107                         nr.isdelay=r.isdelay;
108                         nr.inclusion=new SetInclusion(((SetInclusion)r.inclusion).elementexpr,sd1);
109                         nr.st=r.st;
110                         newrules.add(nr);
111                     }
112             }
113         }
114         oldrules.addAll(newrules);
115     }
116 }