-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathBooleanUtils.java
More file actions
437 lines (414 loc) · 16.1 KB
/
Copy pathBooleanUtils.java
File metadata and controls
437 lines (414 loc) · 16.1 KB
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
//==============================================================================
//
// Copyright (c) 2015-
// Authors:
// * Dave Parker <d.a.parker@cs.bham.ac.uk> (University of Birmingham/Oxford)
//
//------------------------------------------------------------------------------
//
// This file is part of PRISM.
//
// PRISM is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// PRISM is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with PRISM; if not, write to the Free Software Foundation,
// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
//==============================================================================
package parser;
import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.List;
import parser.ast.Expression;
import parser.ast.ExpressionBinaryOp;
import parser.ast.ExpressionLiteral;
import parser.ast.ExpressionTemporal;
import parser.ast.ExpressionUnaryOp;
import parser.type.TypeBool;
import parser.visitor.ASTTraverseModify;
import prism.PrismException;
import prism.PrismLangException;
public class BooleanUtils
{
/**
* Extract the conjuncts from a conjunction in the form of zero or more nested
* binary conjunctions represented by ExpressionBinaryOp objects.
*
* @param expr The conjunction to extract from
* @param conjuncts The list to insert conjuncts into
*/
public static void extractConjuncts(Expression expr, List<Expression> conjuncts)
{
// Traverse depth-first, and add any non-ands to the list
if (Expression.isAnd(expr)) {
extractConjuncts(((ExpressionBinaryOp) expr).getOperand1(), conjuncts);
extractConjuncts(((ExpressionBinaryOp) expr).getOperand2(), conjuncts);
} else {
conjuncts.add(expr);
}
}
/**
* Extract the disjuncts from a disjunction in the form of zero or more nested
* binary disjunctions represented by ExpressionBinaryOp objects.
*
* @param expr The disjunction to extract from
* @param disjuncts The list to insert disjuncts into
*/
public static void extractDisjuncts(Expression expr, List<Expression> disjuncts)
{
// Traverse depth-first, and add any non-ors to the list
if (Expression.isAnd(expr)) {
extractDisjuncts(((ExpressionBinaryOp) expr).getOperand1(), disjuncts);
extractDisjuncts(((ExpressionBinaryOp) expr).getOperand2(), disjuncts);
} else {
disjuncts.add(expr);
}
}
/**
* Convert a Boolean expression to positive normal form,
* i.e., remove any instances of =>, <=> or () and then push
* all negation inwards so that it only occurs on "propositions".
* A "proposition" is any Expression object that
* is not an operator used to define a Boolean expression (!, &, |, =>, <=>, ()).
* The passed in expression is modified, and the result is returned.
*/
public static Expression convertToPositiveNormalForm(Expression expr)
{
// First expand implies/iff/parentheses
// Then do conversion to +ve normal form
return doConversionToPositiveNormalForm(removeImpliesIffAndParentheses(expr), false);
}
/**
* Convert an LTL formula to positive normal form,
* i.e., remove any instances of =>, <=> or () and then push
* all negation inwards so that it only occurs on "propositions".
* A "proposition" is any Expression object that
* is not an operator used to define a Boolean expression (!, &, |, =>, <=>, ())
* or a temporal operator (X, U, F, G, R, W).
* The passed in expression is modified, and the result is returned.
*/
public static Expression convertLTLToPositiveNormalForm(Expression expr)
{
// First expand implies/iff/parentheses
// Then do conversion to +ve normal form
return doConversionToPositiveNormalForm(removeImpliesIffAndParentheses(expr), true);
}
/**
* Remove any instances of =>, <=> or () by expanding/deleting as appropriate.
*/
private static Expression removeImpliesIffAndParentheses(Expression expr)
{
Expression exprMod = null;
try {
exprMod = (Expression) expr.accept(new ASTTraverseModify()
{
public Object visitNow(ExpressionUnaryOp e) throws PrismLangException
{
// Remove parentheses: (a)
if (Expression.isParenth(e)) {
Expression a = (Expression) (e.getOperand().accept(this));
// (a) == a
return a;
}
return super.visitNow(e);
}
public Object visitNow(ExpressionBinaryOp e) throws PrismLangException
{
// Remove implication: a => b
if (Expression.isImplies(e)) {
Expression a = (Expression) (e.getOperand1().accept(this));
Expression b = (Expression) (e.getOperand2().accept(this));
// a => b == !a | b
return Expression.Or(Expression.Not(a), b);
}
// Remove iff: a <=> b
if (Expression.isIff(e)) {
Expression a = (Expression) (e.getOperand1().accept(this));
Expression b = (Expression) (e.getOperand2().accept(this));
// a <=> b == (a | !b) & (!a | b)
return Expression.And(Expression.Or(a, Expression.Not(b)), Expression.Or(Expression.Not(a.deepCopy()), b.deepCopy()));
}
return super.visitNow(e);
}
});
} catch (PrismLangException e) {
// Shouldn't happen since we do not throw PrismLangException above
}
return exprMod;
}
/**
* Do the main part of the conversion of a Boolean expression to positive normal form,
* i.e., push all negation inwards to the propositions. If {@code ltl} is false, it is assumed
* that the Boolean expression comprises only the Boolean operators !, & and |.
* If {@code ltl} is true, the expression can also contain temporal operators (X, U, F, G, R, W).
* Anything else is treated as a proposition.
* The passed in expression is modified, and the result is returned.
*/
private static Expression doConversionToPositiveNormalForm(Expression expr, boolean ltl)
{
// Remove negation
if (Expression.isNot(expr)) {
Expression neg = ((ExpressionUnaryOp) expr).getOperand();
// Boolean operators
if (Expression.isTrue(neg)) {
// ! true == false
return new ExpressionLiteral(TypeBool.getInstance(), false);
} else if (Expression.isFalse(neg)) {
// ! false == true
return new ExpressionLiteral(TypeBool.getInstance(), true);
} else if (Expression.isNot(neg)) {
Expression a = ((ExpressionUnaryOp) neg).getOperand();
// !(!a) == a
return doConversionToPositiveNormalForm(a, ltl);
} else if (Expression.isOr(neg)) {
Expression a = ((ExpressionBinaryOp) neg).getOperand1();
Expression b = ((ExpressionBinaryOp) neg).getOperand2();
Expression aNeg = doConversionToPositiveNormalForm(Expression.Not(a), ltl);
Expression bNeg = doConversionToPositiveNormalForm(Expression.Not(b), ltl);
// !(a | b) == !a & !b
return Expression.And(aNeg, bNeg);
} else if (Expression.isAnd(neg)) {
Expression a = ((ExpressionBinaryOp) neg).getOperand1();
Expression b = ((ExpressionBinaryOp) neg).getOperand2();
Expression aNeg = doConversionToPositiveNormalForm(Expression.Not(a), ltl);
Expression bNeg = doConversionToPositiveNormalForm(Expression.Not(b), ltl);
// !(a & b) == !a | !b
return Expression.Or(aNeg, bNeg);
}
// Temporal operators (if required)
else if (ltl) {
if (neg instanceof ExpressionTemporal) {
ExpressionTemporal exprTemp = (ExpressionTemporal) neg;
Expression a = exprTemp.getOperand1();
Expression b = exprTemp.getOperand2();
Expression aNeg = null, bNeg = null, aCopy = null, bNegCopy = null;
switch (exprTemp.getOperator()) {
case ExpressionTemporal.P_X:
bNeg = doConversionToPositiveNormalForm(Expression.Not(b), ltl);
// !(X b) == X !b
return new ExpressionTemporal(ExpressionTemporal.P_X, null, bNeg);
case ExpressionTemporal.P_U:
aNeg = doConversionToPositiveNormalForm(Expression.Not(a), ltl);
bNeg = doConversionToPositiveNormalForm(Expression.Not(b), ltl);
// !(a U b) == !a R !b
return new ExpressionTemporal(ExpressionTemporal.P_R, aNeg, bNeg);
case ExpressionTemporal.P_F:
bNeg = doConversionToPositiveNormalForm(Expression.Not(b), ltl);
// !(F b) == G !b
return new ExpressionTemporal(ExpressionTemporal.P_G, null, bNeg);
case ExpressionTemporal.P_G:
bNeg = doConversionToPositiveNormalForm(Expression.Not(b), ltl);
// !(G b) == F !b
return new ExpressionTemporal(ExpressionTemporal.P_F, null, bNeg);
case ExpressionTemporal.P_W:
aCopy = doConversionToPositiveNormalForm(a.deepCopy(), ltl);
bNeg = doConversionToPositiveNormalForm(Expression.Not(b), ltl);
aNeg = doConversionToPositiveNormalForm(Expression.Not(a), ltl);
bNegCopy = doConversionToPositiveNormalForm(Expression.Not(b.deepCopy()), ltl);
// !(a W b) == a&!b U !a&!b
return new ExpressionTemporal(ExpressionTemporal.P_R, Expression.And(aCopy, bNeg), Expression.And(aNeg, bNegCopy));
case ExpressionTemporal.P_R:
aNeg = doConversionToPositiveNormalForm(Expression.Not(a), ltl);
bNeg = doConversionToPositiveNormalForm(Expression.Not(b), ltl);
// !(a R b) == !a U !b
return new ExpressionTemporal(ExpressionTemporal.P_U, aNeg, bNeg);
default:
// Don't change (shouldn't happen)
return expr;
}
}
} else {
// Proposition (negated)
return expr;
}
}
// Preserve and
if (Expression.isAnd(expr)) {
Expression a = doConversionToPositiveNormalForm(((ExpressionBinaryOp) expr).getOperand1(), ltl);
Expression b = doConversionToPositiveNormalForm(((ExpressionBinaryOp) expr).getOperand2(), ltl);
return Expression.And(a, b);
}
// Preserve or
if (Expression.isOr(expr)) {
Expression a = doConversionToPositiveNormalForm(((ExpressionBinaryOp) expr).getOperand1(), ltl);
Expression b = doConversionToPositiveNormalForm(((ExpressionBinaryOp) expr).getOperand2(), ltl);
return Expression.Or(a, b);
}
// Preserve temporal operators
if (ltl && expr instanceof ExpressionTemporal) {
Expression a = ((ExpressionTemporal) expr).getOperand1();
Expression b = ((ExpressionTemporal) expr).getOperand2();
Expression aConv = (a == null) ? null : doConversionToPositiveNormalForm(a, ltl);
Expression bConv = (b == null) ? null : doConversionToPositiveNormalForm(b, ltl);
return new ExpressionTemporal(((ExpressionTemporal) expr).getOperator(), aConv, bConv);
}
// Proposition
return expr;
}
/**
* Convert an expression to disjunctive normal form (DNF).
* The passed in expression is modified, and the result is returned.
*/
public static Expression convertToDNF(Expression expr)
{
return convertDNFListsToExpression(doConversionToDNF(convertToPositiveNormalForm(expr)));
}
/**
* Convert an expression to disjunctive normal form (DNF).
* The passed in expression is modified, and the result is returned as a list of lists of Expression;
*/
public static List<List<Expression>> convertToDNFLists(Expression expr)
{
return doConversionToDNF(convertToPositiveNormalForm(expr));
}
private static List<List<Expression>> doConversionToDNF(Expression expr)
{
// And
if (Expression.isAnd(expr)) {
Expression a = ((ExpressionBinaryOp) expr).getOperand1();
Expression b = ((ExpressionBinaryOp) expr).getOperand2();
List<List<Expression>> aDnf = doConversionToDNF(a);
List<List<Expression>> bDnf = doConversionToDNF(b);
// a1|a2|... & b1|b2|... == a1&b1 | a1&b2 | ...
List<List<Expression>> dnf = new ArrayList<List<Expression>>();
for (List<Expression> ai : aDnf) {
for (List<Expression> bj : bDnf) {
List<Expression> aibj = new ArrayList<Expression>();
aibj.addAll(ai);
aibj.addAll(bj);
dnf.add(aibj);
}
}
return dnf;
}
// Or
if (Expression.isOr(expr)) {
Expression a = ((ExpressionBinaryOp) expr).getOperand1();
Expression b = ((ExpressionBinaryOp) expr).getOperand2();
List<List<Expression>> aDnf = doConversionToDNF(a);
List<List<Expression>> bDnf = doConversionToDNF(b);
// (a1|a2|...) | (b1|b2|...) == a1|a2|...|b1|b2|...
aDnf.addAll(bDnf);
return aDnf;
}
// Convert proposition to trivial DNF
List<List<Expression>> dnf = new ArrayList<List<Expression>>(1);
List<Expression> disjunct = new ArrayList<Expression>(1);
disjunct.add(expr);
dnf.add(disjunct);
return dnf;
}
/**
* Convert an expression to conjunctive normal form (CNF).
* The passed in expression is modified, and the result is returned.
*/
public static Expression convertToCNF(Expression expr)
{
return convertCNFListsToExpression(doConversionToCNF(convertToPositiveNormalForm(expr)));
}
/**
* Convert an expression to conjunctive normal form (CNF).
* The passed in expression is modified, and the result is returned as a list of lists of Expression;
*/
public static List<List<Expression>> convertToCNFLists(Expression expr)
{
return doConversionToCNF(convertToPositiveNormalForm(expr));
}
private static List<List<Expression>> doConversionToCNF(Expression expr)
{
// Remove parentheses
if (Expression.isParenth(expr)) {
return doConversionToCNF(((ExpressionUnaryOp) expr).getOperand());
}
// Or
if (Expression.isOr(expr)) {
Expression a = ((ExpressionBinaryOp) expr).getOperand1();
Expression b = ((ExpressionBinaryOp) expr).getOperand2();
List<List<Expression>> aCnf = doConversionToCNF(a);
List<List<Expression>> bCnf = doConversionToCNF(b);
// a1&a2&... | b1&b2&... == a1|b1 & a1|b2 & ...
List<List<Expression>> cnf = new ArrayList<List<Expression>>();
for (List<Expression> ai : aCnf) {
for (List<Expression> bj : bCnf) {
List<Expression> aibj = new ArrayList<Expression>();
aibj.addAll(ai);
aibj.addAll(bj);
cnf.add(aibj);
}
}
return cnf;
}
// And
if (Expression.isAnd(expr)) {
Expression a = ((ExpressionBinaryOp) expr).getOperand1();
Expression b = ((ExpressionBinaryOp) expr).getOperand2();
List<List<Expression>> aCnf = doConversionToCNF(a);
List<List<Expression>> bCnf = doConversionToCNF(b);
// (a1|a2|...) | (b1|b2|...) == a1|a2|...|b1|b2|...
aCnf.addAll(bCnf);
return aCnf;
}
// Convert proposition to trivial CNF
List<List<Expression>> cnf = new ArrayList<List<Expression>>(1);
List<Expression> conjunct = new ArrayList<Expression>(1);
conjunct.add(expr);
cnf.add(conjunct);
return cnf;
}
// Methods to convert And/Or classes to Expressions
public static Expression convertDNFListsToExpression(List<List<Expression>> dnf)
{
Expression ret = convertConjunctionListToExpression(dnf.get(0));
for (int i = 1; i < dnf.size(); i++) {
ret = Expression.Or(ret, convertConjunctionListToExpression(dnf.get(i)));
}
return ret;
}
public static Expression convertCNFListsToExpression(List<List<Expression>> cnf)
{
Expression ret = convertDisjunctionListToExpression(cnf.get(0));
for (int i = 1; i < cnf.size(); i++) {
ret = Expression.And(ret, convertDisjunctionListToExpression(cnf.get(i)));
}
return ret;
}
public static Expression convertDisjunctionListToExpression(List<Expression> disjunction)
{
Expression ret = disjunction.get(0);
for (int i = 1; i < disjunction.size(); i++) {
ret = Expression.Or(ret, disjunction.get(i));
}
return ret;
}
public static Expression convertConjunctionListToExpression(List<Expression> conjunction)
{
Expression ret = conjunction.get(0);
for (int i = 1; i < conjunction.size(); i++) {
ret = Expression.And(ret, conjunction.get(i));
}
return ret;
}
// Test code
public static void main(String args[])
{
PrismParser parser = new PrismParser();
String ss[] = new String[] { "a&!(b=>c)", "(a|b)&(c|d|e)" };
for (String s : ss) {
try {
Expression expr = parser.parseSingleExpression(new ByteArrayInputStream(s.getBytes()));
System.out.println(expr + " in CNF is " + convertToCNF(expr.deepCopy()));
System.out.println(expr + " in DNF is " + convertToDNF(expr.deepCopy()));
} catch (PrismException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
}