blob: fd1fed8ae151916e594abdb662c50309b266af4d (
plain) (
blame)
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
|
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.predicate.optimization;
import com.yahoo.document.predicate.BooleanPredicate;
import com.yahoo.document.predicate.Conjunction;
import com.yahoo.document.predicate.Disjunction;
import com.yahoo.document.predicate.Negation;
import com.yahoo.document.predicate.Predicate;
import com.yahoo.document.predicate.PredicateOperator;
import java.util.ArrayList;
import java.util.List;
/**
* Simplifies a predicate by collapsing TRUE/FALSE-children into their parents.
* E.g. if an AND node has a FALSE child, the entire node is replaced by FALSE.
* Replaces single-child AND/OR-nodes with the child.
*
* @author magnarn
* @author bjorncs
*/
public class BooleanSimplifier implements PredicateProcessor {
public Predicate simplifySubTree(Predicate predicate) {
if (predicate == null) {
return null;
}
if (predicate instanceof Conjunction) {
List<Predicate> in = ((PredicateOperator)predicate).getOperands();
List<Predicate> out = new ArrayList<>(in.size());
for (Predicate operand : in) {
operand = simplifySubTree(operand);
if (isFalse(operand)) {
return new BooleanPredicate(false);
} else if (!isTrue(operand)) {
out.add(operand);
}
}
if (out.size() == 1) {
return out.get(0);
} else if (out.size() == 0) {
return new BooleanPredicate(true);
}
((Conjunction)predicate).setOperands(out);
} else if (predicate instanceof Disjunction) {
List<Predicate> in = ((PredicateOperator)predicate).getOperands();
List<Predicate> out = new ArrayList<>(in.size());
for (Predicate operand : in) {
operand = simplifySubTree(operand);
if (isTrue(operand)) {
return new BooleanPredicate(true);
} else if (!isFalse(operand)) {
out.add(operand);
}
}
if (out.size() == 1) {
return out.get(0);
} else if (out.size() == 0) {
return new BooleanPredicate(false);
}
((Disjunction)predicate).setOperands(out);
} else if (predicate instanceof Negation) {
Predicate operand = ((Negation)predicate).getOperand();
operand = simplifySubTree(operand);
if (isTrue(operand)) {
return new BooleanPredicate(false);
} else if (isFalse(operand)) {
return new BooleanPredicate(true);
}
((Negation) predicate).setOperand(operand);
}
return predicate;
}
private boolean isFalse(Predicate predicate) {
if (predicate instanceof BooleanPredicate) {
return !((BooleanPredicate)predicate).getValue();
}
return false;
}
private boolean isTrue(Predicate predicate) {
if (predicate instanceof BooleanPredicate) {
return ((BooleanPredicate)predicate).getValue();
}
return false;
}
@Override
public Predicate process(Predicate predicate, PredicateOptions options) {
return simplifySubTree(predicate);
}
}
|