Removing various deprecated pieces of code...Fixed some fault checks in AbstractIntef...
[repair.git] / Repair / RepairCompiler / MCC / IR / AbstractRepair.java
1 package MCC.IR;
2 import MCC.State;
3
4 class AbstractRepair {
5     public final static int ADDTOSET=1;
6     public final static int REMOVEFROMSET=2;
7     public final static int ADDTORELATION=3;
8     public final static int REMOVEFROMRELATION=4;
9     public final static int MODIFYRELATION=5;
10
11     DNFPredicate torepair;
12     int type;
13     Descriptor descriptor;
14     Sources sources;
15
16     public String type() {
17         switch(type) {
18         case ADDTOSET:
19             return "AddToSet";
20         case REMOVEFROMSET:
21             return "RemoveToSet";
22         case ADDTORELATION:
23             return "AddToRelation";
24         case REMOVEFROMRELATION:
25             return "RemoveFromRelation";
26         case MODIFYRELATION:
27             return "ModifyRelation";
28         default:
29             return "Unknown";
30         }
31     }
32
33     public boolean isNewObject(boolean isdomain) {
34         if (getType()==ADDTOSET) {
35             return sources.allocSource((SetDescriptor)descriptor);
36         } else if (getType()==ADDTORELATION) {
37             RelationDescriptor rd=(RelationDescriptor)descriptor;
38             return sources.relallocSource(rd,isdomain);
39         } else throw new Error(type());
40     }
41
42     public SetDescriptor getDomainSet() {
43         if (torepair==null)
44             return null;
45         Predicate predicate=torepair.getPredicate();
46         if (!(predicate.getDescriptor() instanceof RelationDescriptor))
47             return null;
48
49         /* Have relation descriptor now */
50         if (predicate instanceof InclusionPredicate) {
51             InclusionPredicate ip=(InclusionPredicate)predicate;
52             if (ip.inverted())
53                 return ip.expr.getSet();
54             else if (ip.setexpr instanceof ImageSetExpr) {
55                 ImageSetExpr ise=(ImageSetExpr)ip.setexpr;
56                 if (ise.isimageset)
57                     return ise.getImageSetExpr().getSet();
58                 else
59                     return ise.getVar().getSet();
60             }
61         } else if (predicate instanceof ExprPredicate) {
62             ExprPredicate ep=(ExprPredicate)predicate;
63
64             if (ep.inverted()&&ep.getType()==ExprPredicate.SIZE)
65                 return sources.relgetSourceSet((RelationDescriptor)predicate.getDescriptor(),true);
66             else if (ep.inverted()&&ep.getType()==ExprPredicate.COMPARISON)
67                 return ((OpExpr)ep.expr).right.getSet();
68             else if (!ep.inverted()) {
69                 switch(ep.getType()) {
70                 case ExprPredicate.SIZE: 
71                     {
72                         SizeofExpr soe=((SizeofExpr)((OpExpr)ep.expr).left);
73                         ImageSetExpr ise=(ImageSetExpr)soe.setexpr;
74                         if (ise.isimageset)
75                             return ise.getImageSetExpr().getSet();
76                         else
77                             return ise.getVar().getSet();
78                     }
79                 case ExprPredicate.COMPARISON:
80                     {
81                         RelationExpr re=((RelationExpr)((OpExpr)ep.expr).left);
82                         return re.expr.getSet();
83                     }
84                 default:
85                     throw new Error("");
86                 }
87             }
88         } else throw new Error("Unrecognized predicate");
89         return null;
90     }
91
92     public SetDescriptor getRangeSet() {
93         if (torepair==null)
94             return null;
95         Predicate predicate=torepair.getPredicate();
96         if (!(predicate.getDescriptor() instanceof RelationDescriptor))
97             return null;
98
99         /* Have relation descriptor now */
100         if (predicate instanceof InclusionPredicate) {
101             InclusionPredicate ip=(InclusionPredicate)predicate;
102             if (!ip.inverted())
103                 return ip.expr.getSet();
104             else if (ip.setexpr instanceof ImageSetExpr) {
105                 ImageSetExpr ise=(ImageSetExpr)ip.setexpr;
106                 if (ise.isimageset)
107                     return ise.getImageSetExpr().getSet();
108                 else
109                     return ise.getVar().getSet();
110             }
111         } else if (predicate instanceof ExprPredicate) {
112             ExprPredicate ep=(ExprPredicate)predicate;
113
114             if (!ep.inverted()&&ep.getType()==ExprPredicate.SIZE)
115                 return sources.relgetSourceSet((RelationDescriptor)predicate.getDescriptor(),false);
116             else if (!ep.inverted()&&ep.getType()==ExprPredicate.COMPARISON)
117                 return ((OpExpr)ep.expr).right.getSet();
118             else if (ep.inverted()) {
119                 switch(ep.getType()) {
120                 case ExprPredicate.SIZE: 
121                     {
122                         SizeofExpr soe=((SizeofExpr)((OpExpr)ep.expr).left);
123                         ImageSetExpr ise=(ImageSetExpr)soe.setexpr;
124                         if (ise.isimageset)
125                             return ise.getImageSetExpr().getSet();
126                         else
127                             return ise.getVar().getSet();
128                     }
129                 case ExprPredicate.COMPARISON:
130                     {
131                         RelationExpr re=((RelationExpr)((OpExpr)ep.expr).left);
132                         return re.expr.getSet();
133                     }
134                 default:
135                     throw new Error("");
136                 }
137             }
138         } else throw new Error("Unrecognized predicate");
139         return null;
140     }
141
142     public int getType() {
143         return type;
144     }
145
146     public DNFPredicate getPredicate() {
147         return torepair;
148     }
149
150     public Descriptor getDescriptor() {
151         return descriptor;
152     }
153
154
155     /** This method tells whether the repair needs to remove objects *
156      *  from the relation, or whether the model definition rules make
157      *  the remove unnecessary.*/
158
159     public boolean needsRemoves(State state) {
160         assert type==MODIFYRELATION;
161         SetDescriptor sd=getPredicate().getPredicate().inverted()?getRangeSet():getDomainSet();
162         return !ConstraintDependence.rulesensurefunction(state,(RelationDescriptor)getDescriptor(), sd, getPredicate().getPredicate().inverted(), true);
163     }
164
165     /** This method tells whether the repair needs to force the
166      *  relation to be function-like.  */
167     
168     public boolean mayNeedFunctionEnforcement(State state) {
169         assert type==MODIFYRELATION;
170         SetDescriptor sd=getPredicate().getPredicate().inverted()?getRangeSet():getDomainSet(); 
171         if (ConstraintDependence.rulesensurefunction(state,(RelationDescriptor)getDescriptor(), sd, getPredicate().getPredicate().inverted(), false))
172             return false;
173         if (ConstraintDependence.constraintsensurefunction(state,(RelationDescriptor)getDescriptor(), sd, getPredicate().getPredicate().inverted()))
174             return false;
175         return true;
176     }
177     
178     public AbstractRepair(DNFPredicate dp,int typ, Descriptor d, Sources s) {
179         torepair=dp;
180         type=typ;
181         descriptor=d;
182         sources=s;
183     }
184 }