1 package eu.simuline.relana.expressions;
2
3 import eu.simuline.relana.model.Deficiency;
4
5 import eu.simuline.relana.model.SInstance;
6
7 import java.util.Set;
8 import java.util.HashSet;
9
10
11
12
13
14
15
16
17
18
19
20
21
22 public abstract class Formula {
23
24
25
26
27
28
29 @edu.umd.cs.findbugs.annotations.SuppressWarnings
30 (value = "IC_SUPERCLASS_USES_SUBCLASS_DURING_INITIALIZATION",
31 justification = "check in main method: field correctly initialized ")
32 public static final Formula EMPTY_EXPRESSION =
33 Const.create(new HashSet<Deficiency>(), Type.getEmpty());
34
35
36
37
38 public static final class Const extends Formula {
39
40
41
42
43
44 private final Set<Deficiency> val;
45 private final Type type;
46
47
48
49
50
51 @SuppressWarnings("checkstyle:MethodParamPad")
52 public Const(Set<Deficiency> val, Type type) {
53 this.val = val;
54 this.type = type;
55 if (this.type != null && !this.type.isValid(val)) {
56 throw new IllegalArgumentException
57 ("invalid set " + this.val
58 + " for type " + this.type + ". ");
59 }
60
61 }
62
63
64
65
66
67 private static Const create(Set<Deficiency> val, Type type) {
68 return new Const(val, type);
69 }
70
71 public Formula remove(SInstance serv, Deficiency def) {
72 return this;
73 }
74
75 public Formula add(SInstance serv, Deficiency def) {
76 return this;
77 }
78
79 public Formula substitute(SInstance serv, Formula form) {
80 return this;
81 }
82
83 public Set<SInstance> getVars() {
84 return new HashSet<SInstance>();
85 }
86 public Set<Deficiency> getConst() {
87 return this.val;
88 }
89 public Set<Deficiency> getMax() {
90 return this.val;
91 }
92 public Set<Deficiency> getMin() {
93 return this.val;
94 }
95 public String toString() {
96 return "<>{" + this.val + "}";
97 }
98 }
99
100
101
102
103 public static final class Var extends Formula {
104
105
106
107
108
109 private final SInstance varS;
110 private final String name;
111
112
113
114
115
116 public Var(SInstance varS, String name) {
117 this.varS = varS;
118 this.name = name;
119 }
120
121
122
123
124
125 public Formula remove(SInstance serv, Deficiency def) {
126 throw new eu.simuline.util.NotYetImplementedException();
127
128 }
129
130 public Formula add(SInstance serv, Deficiency def) {
131 throw new eu.simuline.util.NotYetImplementedException();
132
133 }
134
135 public Formula substitute(SInstance serv, Formula form) {
136
137
138
139
140 return (serv == this.varS) ? form : this;
141 }
142
143 public Set<SInstance> getVars() {
144 Set<SInstance> res = new HashSet<SInstance>();
145 res.add(this.varS);
146 return res;
147 }
148
149 public Set<Deficiency> getConst() {
150 return null;
151 }
152
153 public Set<Deficiency> getMax() {
154 return this.varS.getType().asSet();
155 }
156
157 public Set<Deficiency> getMin() {
158 return new HashSet<Deficiency>();
159 }
160
161 public String toString() {
162 StringBuffer res = new StringBuffer();
163 res.append("$" + this.name + "$");
164 return res.toString();
165 }
166 }
167
168
169
170
171 public static final class Comp extends Formula {
172
173
174
175
176
177 private Set<Deficiency> min;
178 private Set<Deficiency> max;
179
180 private final Operation.Eval oper;
181 private final Set<Formula> args;
182
183
184
185
186
187 private Comp(Operation.Eval oper, Set<Formula> args) {
188 this.oper = oper;
189 this.args = args;
190
191 Set<Set<Deficiency>> param = new HashSet<Set<Deficiency>>();
192 for (Formula arg : args) {
193 param.add(arg.getMin());
194 }
195 this.min = oper.eval(param);
196
197 param = new HashSet<Set<Deficiency>>();
198 for (Formula arg : args) {
199 param.add(arg.getMax());
200 }
201 this.max = this.oper.eval(param);
202
203 if (!this.oper.getOperation().isIsoAntitone()) {
204
205 Set<Deficiency> inter;
206 inter = this.max;
207 this.max = this.min;
208 this.min = inter;
209 }
210 }
211
212
213
214
215
216 private static Comp create(Operation.Eval oper,
217 Set<Formula> args) {
218 return new Comp(oper, args);
219 }
220
221 public Formula remove(SInstance serv, Deficiency def) {
222 Set<Formula> newArgs = new HashSet<Formula>();
223 for (Formula form : args) {
224 newArgs.add(form.remove(serv, def));
225 }
226 return getFormula(this.oper, newArgs);
227 }
228
229 public Formula add(SInstance serv, Deficiency def) {
230 Set<Formula> newArgs = new HashSet<Formula>();
231 for (Formula form : args) {
232 newArgs.add(form.add(serv, def));
233 }
234 return getFormula(this.oper, newArgs);
235 }
236
237 public Formula substitute(SInstance serv, Formula form) {
238 Set<Formula> newArgs = new HashSet<Formula>();
239 for (Formula arg : this.args) {
240 newArgs.add(arg.substitute(serv, form));
241 }
242 return getFormula(this.oper, newArgs);
243 }
244
245 public Set<SInstance> getVars() {
246 Set<SInstance> res = new HashSet<SInstance>();
247 for (Formula arg : args) {
248 res.addAll(arg.getVars());
249 }
250 return res;
251 }
252
253 public Set<Deficiency> getConst() {
254 return null;
255 }
256
257 public Set<Deficiency> getMax() {
258 return this.max;
259 }
260
261 public Set<Deficiency> getMin() {
262 return this.min;
263 }
264
265 public String toString() {
266 StringBuffer res = new StringBuffer();
267 res.append(this.oper);
268 res.append('(');
269 res.append(this.args);
270 res.append(')');
271 return res.toString();
272 }
273 }
274
275
276
277
278
279
280
281
282
283
284
285 private Formula() {
286 }
287
288 public static Formula getFormula(Operation.Eval oper, Set<Formula> args) {
289 Formula res = Comp.create(oper, args);
290 return (res.getMin().size() == res.getMax().size())
291 ? Const.create(res.getMin(), null)
292 : res;
293 }
294
295
296
297
298
299 public abstract Formula remove(SInstance serv, Deficiency def);
300 public abstract Formula add(SInstance serv, Deficiency def);
301 public abstract Formula substitute(SInstance serv, Formula form);
302
303 public abstract Set<SInstance> getVars();
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320 public abstract Set<Deficiency> getConst();
321 public abstract Set<Deficiency> getMin();
322 public abstract Set<Deficiency> getMax();
323
324 public final boolean equals(Object obj) {
325 return super.equals(obj);
326 }
327
328 public final int hashCode() {
329 return super.hashCode();
330 }
331
332
333 public static final void main(String[] args) {
334 System.out.println("EE: " + EMPTY_EXPRESSION);
335 }
336
337 }