-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBBinaryExpression.java
407 lines (374 loc) · 14.3 KB
/
BBinaryExpression.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
import java.util.Vector;
/******************************
* Copyright (c) 2003,2019 Kevin Lano
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
* *****************************/
public class BBinaryExpression extends BExpression
{ private BExpression left;
private BExpression right;
private String operator;
public BBinaryExpression(String op, BExpression l,
BExpression r)
{ operator = op;
left = l;
right = r;
}
public String getOperator()
{ return operator; }
public BExpression getLeft()
{ return left; }
public BExpression getRight()
{ return right; }
public Vector rd()
{ Vector res = left.rd();
res = VectorUtil.union(res,right.rd());
return res;
}
public BExpression leftmostArgument()
{ if (left instanceof BBinaryExpression)
{ BBinaryExpression bbe = (BBinaryExpression) left;
return bbe.leftmostArgument();
}
return left;
}
public boolean setValued()
{ // boolean leftset = left.setValued();
// boolean rightset = right.setValued();
if (Expression.comparitors.contains(operator))
{ return false; } // boolean valued
if (operator.equals("\\/")) { return true; }
if (operator.equals("/\\")) { return true; }
if (operator.equals("<+")) { return true; }
if (operator.equals("^")) { return true; }
if (operator.equals("-") &&
(left.setValued() || right.setValued()))
{ return true; }
return false;
}
public BExpression simplify()
{ BExpression bl = left.simplify();
BExpression br = right.simplify();
if (bl == null) { bl = left; }
if (br == null) { br = right; }
boolean leftset = bl.setValued();
boolean rightset = br.setValued();
if ("=".equals(operator))
{ if (leftset && !rightset)
{ br = BExpression.makeSet(br); } // not bl
else if (rightset && !leftset)
{ bl = BExpression.makeSet(bl); } // not br
}
return simplify(operator,bl,br,needsBracket);
}
public static BExpression simplify(String op, BExpression l, BExpression r,
boolean brack)
{ if (l == null) { return r; }
if (r == null) { return l; }
if (op.equals("="))
{ return simplifyEq(l,r); }
if (op.equals(":"))
{ return simplifyIn(l,r); }
if (op.equals("/:"))
{ return simplifyNin(l,r); }
if (op.equals("<:"))
{ return simplifySub(l,r); }
if (op.equals("/<:"))
{ BExpression e = simplifySub(l,r);
return simplifyNot(e);
}
if (op.equals("/="))
{ return simplifyNeq(l,r); }
if (op.equals("&"))
{ return simplifyAnd(l,r); }
if (op.equals("=>"))
{ return simplifyImp(l,r,brack); }
if (op.equals("or"))
{ return simplifyOr(l,r,brack); }
if (op.equals("+") || op.equals("/") || op.equals("*") || op.equals("-"))
{ return simplifyMath(op,l,r,brack); }
return new BBinaryExpression(op,l,r);
}
private static BExpression simplifyEq(BExpression bl, BExpression br)
{ if (("" + bl).equals("" + br))
{ return new BBasicExpression("true"); }
if (bl instanceof BSetExpression &&
br instanceof BSetExpression)
{ BSetExpression bs1 = (BSetExpression) bl;
BSetExpression bs2 = (BSetExpression) br;
if (bs1.isSingleton() && bs2.isSingleton())
{ return new BBinaryExpression("=",
bs1.getElement(0),
bs2.getElement(0));
}
}
return new BBinaryExpression("=",bl,br);
} // Also if different VALUEs return false.
public static BExpression simplifyIn(BExpression bl, BExpression br)
{ // BExpression newleft = bl;
String newop = ":";
if (bl instanceof BSetExpression)
{ BSetExpression bls = (BSetExpression) bl;
if (bls.isEmpty()) { return new BBasicExpression("true"); }
if (bls.isSingleton())
{ BExpression elem = (BExpression) bls.getElement(0);
return simplifyIn(elem,br);
}
else
{ newop = "<:"; } // : being used to mean subset
}
if (br instanceof BSetExpression)
{ BSetExpression bs = (BSetExpression) br;
if (bs.isEmpty())
{ if (bl instanceof BSetExpression)
{ BSetExpression bls = (BSetExpression) bl;
if (bls.isSingleton())
{ return new BBasicExpression("false"); }
else
{ return new BBinaryExpression("=",bl,br); }
}
return new BBinaryExpression("=",bl,br);
}
if (bs.isSingleton())
{ if (bl instanceof BSetExpression)
{ BSetExpression bls = (BSetExpression) bl;
if (bls.isSingleton())
{ return new BBinaryExpression("=",bls.getElement(0),bs.getElement(0)); }
return new BBinaryExpression("<:",bl,br);
}
return new BBinaryExpression(":",bl,br);
}
if (bl instanceof BSetExpression)
{ BSetExpression bls = (BSetExpression) bl;
if (bls.isSubsetOf(bs))
{ return new BBasicExpression("true"); }
}
if (bs.hasElement(bl))
{ return new BBasicExpression("true"); }
}
return new BBinaryExpression(newop,bl,br);
}
public static BExpression simplifyNin(BExpression bl, BExpression br)
{ BExpression e = simplifyIn(bl,br);
return simplifyNot(e);
}
/* if (br instanceof BSetExpression)
{ BSetExpression bs = (BSetExpression) br;
if (bs.isEmpty()) { return new BBasicExpression("true"); }
if (bs.isSingleton())
{ if (bl instanceof BSetExpression) // must be singleton
{ BSetExpression bls = (BSetExpression) bl;
return new BBinaryExpression("/=",bls.getElement(0),
bs.getElement(0));
}
return new BBinaryExpression("/=",bl,
bs.getElement(0));
}
}
return new BBinaryExpression("/:",bl,br);
} */
public static BExpression simplifyNeq(BExpression bl, BExpression br)
{ if ((""+bl).equals(""+br))
{ return new BBasicExpression("false"); }
if (bl instanceof BBinaryExpression &&
br instanceof BSetExpression)
{ BBinaryExpression blbe = (BBinaryExpression) bl;
BSetExpression bsetr = (BSetExpression) br;
if (blbe.operator.equals("/\\") && bsetr.isEmpty())
{ BExpression bll = blbe.left;
if (bll instanceof BSetExpression)
{ BSetExpression bllse = (BSetExpression) bll;
if (bllse.isSingleton())
{ BExpression belem = bllse.getElement(0);
return new BBinaryExpression(":",belem,blbe.right);
}
}
}
}
BExpression e = simplifyEq(bl,br);
return simplifyNot(e);
}
public static BExpression simplifySub(BExpression bl, BExpression br)
{ if (("" + bl).equals("" + br))
{ return new BBasicExpression("true"); }
if (br instanceof BSetExpression)
{ BSetExpression bs = (BSetExpression) br;
if (bl instanceof BSetExpression)
{ BSetExpression bsl = (BSetExpression) bl;
if (bsl.isSubsetOf(bs))
{ return new BBasicExpression("true"); }
}
if (bs.isEmpty())
{ return new BBinaryExpression("=",bl,new BSetExpression()); }
}
if (bl instanceof BSetExpression)
{ BSetExpression bsl = (BSetExpression) bl;
if (bsl.isEmpty()) { return new BBasicExpression("true"); }
if (bsl.isSingleton())
{ return new BBinaryExpression(":",bsl.getElement(0),br); }
// if both singletons, it is =
}
return new BBinaryExpression("<:",bl,br);
}
private static BExpression simplifyAnd(BExpression l, BExpression r)
{ if (l.equals(r)) { return l; }
if (l.conflictsWith(r)) { return new BBasicExpression("false"); }
// if (l.implies(r)) { return l; }
// if (r.implies(l)) { return r; }
if ("TRUE".equals("" + l)) { return r; }
if ("true".equals("" + l)) { return r; }
if ("TRUE".equals("" + r)) { return l; }
if ("true".equals("" + r)) { return l; } // etc
if ("FALSE".equals("" + l)) { return l; }
if ("false".equals("" + l)) { return l; }
if ("FALSE".equals("" + r)) { return r; }
if ("false".equals("" + r)) { return r; }
return new BBinaryExpression("&",l,r);
}
private static BExpression simplifyImp(BExpression l, BExpression r,
boolean bracket)
{ if (l.equals(r)) { return new BBasicExpression("true"); }
// and if (l.implies(r))
if ("TRUE".equals("" + l)) { return r; }
if ("true".equals("" + l)) { return r; }
if ("TRUE".equals("" + r)) { return new BBasicExpression("true"); }
if ("true".equals("" + r)) { return r; }
if ("FALSE".equals("" + l)) { return new BBasicExpression("true"); }
if ("false".equals("" + l)) { return new BBasicExpression("true"); }
if ("FALSE".equals("" + r)) { return simplifyNot(l); }
if ("false".equals("" + r)) { return simplifyNot(l); }
BExpression res = new BBinaryExpression("=>",l,r);
res.setBrackets(bracket);
return res;
}
private static BExpression simplifyOr(BExpression l, BExpression r,
boolean bracket)
{ if (l.equals(r)) { return l; }
// if (l.implies(r)) { return r; }
// if (r.implies(l)) { return l; }
if ("TRUE".equals("" + l)) { return new BBasicExpression("true"); }
if ("true".equals("" + l)) { return new BBasicExpression("true"); }
if ("TRUE".equals("" + r)) { return new BBasicExpression("true"); }
if ("true".equals("" + r)) { return new BBasicExpression("true"); } // etc
if ("FALSE".equals("" + l)) { return r; }
if ("false".equals("" + l)) { return r; }
if ("FALSE".equals("" + r)) { return l; }
if ("false".equals("" + r)) { return l; }
BExpression res = new BBinaryExpression("or",l,r);
res.setBrackets(bracket);
return res;
}
private static BExpression simplifyMath(String op, BExpression l,
BExpression r, boolean brack)
{ BExpression res;
BExpression ls = unmakeSet(l);
BExpression rs = unmakeSet(r);
if (op.equals("-"))
{ if (l.setValued() || r.setValued()) // ls, rs?
{ res = new BBinaryExpression(op,l,r);
res.setBrackets(brack);
return res;
}
}
res = new BBinaryExpression(op,ls,rs);
res.setBrackets(brack);
return res;
} // could also evaluate them.
public static BExpression simplifyNot(BExpression e)
{ // turn < into >= etc
if ("FALSE".equals("" + e)) { return new BBasicExpression("true"); }
if ("false".equals("" + e)) { return new BBasicExpression("true"); }
if ("TRUE".equals("" + e)) { return new BBasicExpression("false"); }
if ("true".equals("" + e)) { return new BBasicExpression("false"); }
if (e instanceof BBinaryExpression)
{ BBinaryExpression be = (BBinaryExpression) e;
String beop = be.operator;
String newop = null;
if (beop.equals("=")) { newop = "/="; }
if (beop.equals("<=")) { newop = ">"; }
if (beop.equals("<")) { newop = ">="; }
if (beop.equals(">=")) { newop = "<"; }
if (beop.equals("/=")) { newop = "="; }
if (beop.equals("!=")) { newop = "="; }
if (beop.equals(">")) { newop = "<="; }
if (beop.equals(":")) { newop = "/:"; }
if (beop.equals("/:")) { newop = ":"; }
if (beop.equals("<:")) { newop = "/<:"; }
if (beop.equals("/<:")) { newop = "<:"; }
// for &, or, =>, not also
if (newop != null)
{ return new BBinaryExpression(newop,be.left,be.right); }
}
return new BUnaryExpression("not",e);
}
public String toString()
{ String res = left + " " + operator + " " + right;
if (needsBracket)
{ return "( " + res + " )"; } // eg, for or inside &
else
{ return res; }
}
public BExpression substituteEq(String oldE, BExpression newE)
{ if (oldE.equals(toString()))
{ return newE; }
BExpression le = left.substituteEq(oldE,newE);
BExpression re = right.substituteEq(oldE,newE);
BExpression res = new BBinaryExpression(operator,le,re);
res.setBrackets(needsBracket);
return res;
} // copy kind also
public boolean conflictsWith(BExpression e)
{ if (e instanceof BBinaryExpression)
{ BBinaryExpression be = (BBinaryExpression) e;
if (be.operator.equals("&"))
{ return conflictsWith(be.left) ||
conflictsWith(be.right);
}
if (be.operator.equals("or"))
{ return conflictsWith(be.left) &&
conflictsWith(be.right);
}
else
{ return conflictsWith(be.operator,
be.left,be.right);
}
}
return false;
}
public boolean conflictsWith(String op, BExpression el,
BExpression er)
{ if (left.toString().equals(el.toString()) &&
right.toString().equals(er.toString()))
{ return Expression.conflictsOp(operator,op); }
if (left.toString().equals(er.toString()) &&
right.toString().equals(el.toString()))
{ return Expression.conflictsReverseOp(operator,op); }
if (operator.equals("="))
{ return conflictsWithEq(op,el,er); }
// if (comparitors.contains(operator))
// { return conflictsWithComp(op,el,er); }
if (operator.equals("&")) // shouldn't occur
{ return left.conflictsWith(op,el,er) ||
right.conflictsWith(op,el,er);
}
if (operator.equals("or"))
{ return left.conflictsWith(op,el,er) &&
right.conflictsWith(op,el,er);
}
return false;
}
public boolean conflictsWithEq(String op, BExpression el,
BExpression er)
{ if (op.equals("="))
{ if (left.toString().equals(el.toString()) &&
right.getKind() == Expression.VALUE &&
er.getKind() == Expression.VALUE &&
!right.toString().equals(er.toString()))
{ return true; }
}
return false;
}
}