1
|
|
|
<?php |
2
|
|
|
namespace JClaveau\LogicalFilter\Rule; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Logical negation: |
6
|
|
|
* @see https://en.wikipedia.org/wiki/Negation |
7
|
|
|
*/ |
8
|
|
|
class NotRule extends AbstractOperationRule |
9
|
|
|
{ |
10
|
|
|
/** @var string operator */ |
11
|
|
|
const operator = 'not'; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
*/ |
15
|
34 |
|
public function __construct( AbstractRule $operand=null, array $options=[] ) |
16
|
|
|
{ |
17
|
34 |
|
if ( ! empty($options)) { |
18
|
11 |
|
$this->setOptions($options); |
19
|
11 |
|
} |
20
|
|
|
|
21
|
|
|
// Negation has only one operand. If more is required, group them |
22
|
|
|
// into an AndRule |
23
|
34 |
|
if ($operand) { |
24
|
20 |
|
$this->addOperand($operand); |
25
|
20 |
|
} |
26
|
34 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
*/ |
30
|
28 |
|
public function isNormalizationAllowed(array $current_simplification_options=[]) |
31
|
|
|
{ |
32
|
28 |
|
$operand = $this->getOperandAt(0); |
33
|
|
|
|
34
|
28 |
|
return null !== $operand; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Transforms all composite rules in the tree of operands into |
39
|
|
|
* atomic rules. |
40
|
|
|
* |
41
|
|
|
* @todo use get_class instead of instanceof to avoid order issue |
42
|
|
|
* in the conditions. |
43
|
|
|
* |
44
|
|
|
* @return array |
45
|
|
|
*/ |
46
|
30 |
|
public function negateOperand($remove_generated_negations=false, array $current_simplification_options) |
|
|
|
|
47
|
|
|
{ |
48
|
30 |
|
if ( ! $this->isNormalizationAllowed($current_simplification_options)) { |
49
|
21 |
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
16 |
|
$operand = $this->getOperandAt(0); |
53
|
|
|
|
54
|
16 |
|
if (method_exists($operand, 'getField')) { |
55
|
13 |
|
$field = $operand->getField(); |
56
|
13 |
|
} |
57
|
|
|
|
58
|
16 |
|
if ($operand instanceof AboveRule) { |
59
|
5 |
|
$new_rule = new OrRule([ |
60
|
5 |
|
new BelowRule($field, $operand->getMinimum()), |
|
|
|
|
61
|
5 |
|
new EqualRule($field, $operand->getMinimum()), |
|
|
|
|
62
|
5 |
|
]); |
63
|
5 |
|
} |
64
|
14 |
|
elseif ($operand instanceof BelowRule) { |
65
|
|
|
// ! (v > a) : v <= a : (v < a || a = v) |
66
|
3 |
|
$new_rule = new OrRule([ |
67
|
3 |
|
new AboveRule($field, $operand->getMaximum()), |
|
|
|
|
68
|
3 |
|
new EqualRule($field, $operand->getMaximum()), |
|
|
|
|
69
|
3 |
|
]); |
70
|
3 |
|
} |
71
|
14 |
|
elseif ($operand instanceof NotRule) { |
72
|
|
|
// ! ( ! a) : a |
73
|
1 |
|
$new_rule = $operand->getOperandAt(0); |
74
|
1 |
|
} |
75
|
14 |
|
elseif ($operand instanceof EqualRule && null === $operand->getValue()) { |
76
|
2 |
|
$new_rule = new NotEqualRule($field, null); |
|
|
|
|
77
|
2 |
|
} |
78
|
13 |
|
elseif ($operand instanceof EqualRule) { |
79
|
|
|
// ! (v = a) : (v < a) || (v > a) |
80
|
9 |
|
if ($this->getOption('not_equal.normalization', $current_simplification_options)) { |
81
|
6 |
|
$new_rule = new OrRule([ |
82
|
6 |
|
new AboveRule($field, $operand->getValue()), |
83
|
6 |
|
new BelowRule($field, $operand->getValue()), |
84
|
6 |
|
]); |
85
|
6 |
|
} |
86
|
|
|
else { |
87
|
4 |
|
$new_rule = new NotEqualRule( $field, $operand->getValue() ); |
88
|
|
|
} |
89
|
9 |
|
} |
90
|
9 |
|
elseif ($operand instanceof NotEqualRule) { |
91
|
|
|
$new_rule = new EqualRule( $field, $operand->getValue() ); |
92
|
|
|
} |
93
|
9 |
|
elseif ($operand instanceof AndRule) { |
94
|
|
|
// @see https://github.com/jclaveau/php-logical-filter/issues/40 |
95
|
|
|
// ! (B && A) : (!B && A) || (B && !A) || (!B && !A) |
96
|
|
|
// ! (A && B && C) : |
97
|
|
|
// (!A && !B && !C) |
98
|
|
|
// || (!A && B && C) || (!A && !B && C) || (!A && B && !C) |
99
|
|
|
// || (A && !B && C) || (!A && !B && C) || (A && !B && !C) |
100
|
|
|
// || (A && B && !C) || (!A && B && !C) || (A && !B && !C) |
101
|
|
|
|
102
|
|
|
// We combine all possibilities of rules and themselves negated |
103
|
2 |
|
$new_rule = new OrRule; |
104
|
2 |
|
$child_operands = $operand->getOperands(); |
105
|
|
|
|
106
|
2 |
|
$current_operand = array_shift($child_operands); |
107
|
2 |
|
$current_operand_possibilities = new OrRule([ |
108
|
2 |
|
new AndRule([ |
109
|
2 |
|
$current_operand->copy(), |
110
|
2 |
|
]), |
111
|
2 |
|
new AndRule([ |
112
|
2 |
|
new NotRule($current_operand->copy()), |
113
|
2 |
|
]), |
114
|
2 |
|
]); |
115
|
|
|
|
116
|
|
|
// for every remaining operand, we duplicate the already made |
117
|
|
|
// combinations and add on half of them !$next_operand |
118
|
|
|
// and $next_operand on the other half |
119
|
2 |
|
while ($next_operand = array_shift($child_operands)) { |
120
|
2 |
|
$next_operand_possibilities = $current_operand_possibilities->copy(); |
121
|
|
|
|
122
|
2 |
|
$tmp = []; |
123
|
2 |
|
foreach ($current_operand_possibilities->getOperands() as $current_operand_possibility) { |
124
|
2 |
|
$tmp[] = $current_operand_possibility->addOperand( $next_operand->copy() ); |
|
|
|
|
125
|
2 |
|
} |
126
|
2 |
|
$current_operand_possibilities->setOperands($tmp); |
127
|
|
|
|
128
|
2 |
|
foreach ($next_operand_possibilities->getOperands() as $next_operand_possibility) { |
129
|
2 |
|
$current_operand_possibilities->addOperand( |
130
|
2 |
|
$next_operand_possibility->addOperand( new NotRule($next_operand->copy()) ) |
|
|
|
|
131
|
2 |
|
); |
132
|
2 |
|
} |
133
|
2 |
|
} |
134
|
|
|
|
135
|
|
|
// We remove the only possibility where no rule is negated |
136
|
2 |
|
$combinations = $current_operand_possibilities->getOperands(); |
137
|
2 |
|
array_shift($combinations); // The first rule contains no negation |
138
|
2 |
|
$new_rule->setOperands( $combinations ) |
139
|
|
|
// ->dump(true) |
140
|
|
|
; |
141
|
2 |
|
} |
142
|
8 |
|
elseif ($operand instanceof OrRule) { |
143
|
|
|
|
144
|
|
|
// $operand->dump(true); |
145
|
|
|
if ( $operand instanceof InRule |
146
|
7 |
|
&& ! $operand->isNormalizationAllowed($current_simplification_options) |
147
|
7 |
|
) { |
148
|
|
|
// ['not', ['field', 'in', [2, 3]]] <=> ['field', '!in', [2, 3]] |
149
|
1 |
|
$new_rule = new NotInRule( |
150
|
1 |
|
$operand->getField(), |
151
|
1 |
|
$operand->getPossibilities() |
152
|
1 |
|
); |
153
|
1 |
|
} |
154
|
|
|
else { |
155
|
|
|
// ! (A || B) : !A && !B |
156
|
|
|
// ! (A || B || C || D) : !A && !B && !C && !D |
157
|
7 |
|
$new_rule = new AndRule; |
158
|
7 |
|
foreach ($operand->getOperands() as $sub_operand) { |
159
|
7 |
|
$negation = new NotRule; |
160
|
7 |
|
$negation = $negation->setOperandsOrReplaceByOperation( |
161
|
7 |
|
[$sub_operand->copy()], |
162
|
|
|
$current_simplification_options |
163
|
7 |
|
); |
164
|
7 |
|
$new_rule->addOperand( $negation ); |
165
|
7 |
|
} |
166
|
|
|
} |
167
|
|
|
// $new_rule->dump(!true); |
168
|
7 |
|
} |
169
|
|
|
else { |
170
|
1 |
|
throw new \LogicException( |
171
|
1 |
|
'Removing NotRule(' . var_export($operand, true) . ') ' |
172
|
1 |
|
. ' not implemented' |
173
|
1 |
|
); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
// $new_rule->dump(!true); |
177
|
|
|
|
178
|
15 |
|
return $new_rule; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Replace all the OrRules of the RuleTree by one OrRule at its root. |
183
|
|
|
* |
184
|
|
|
* @todo rename as RootifyDisjunjctions? |
185
|
|
|
* @todo return $this (implements a Rule monad?) |
186
|
|
|
* |
187
|
|
|
* @return OrRule copied operands with one OR at its root |
188
|
|
|
*/ |
189
|
24 |
|
public function rootifyDisjunctions($simplification_options) |
190
|
|
|
{ |
191
|
24 |
|
if ( ! $this->isNormalizationAllowed($simplification_options)) { |
192
|
24 |
|
return $this; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
$this->moveSimplificationStepForward( self::rootify_disjunctions, $simplification_options ); |
196
|
|
|
|
197
|
|
|
foreach ($this->operands as $id => $operand) { |
198
|
|
|
if ($operand instanceof AbstractOperationRule) { |
199
|
|
|
$this->operands[$id] = $operand->rootifyDisjunctions($simplification_options); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
return $this; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Not rules can only have one operand. |
208
|
|
|
* |
209
|
|
|
* @return $this |
210
|
|
|
*/ |
211
|
30 |
|
public function unifyAtomicOperands($simplification_strategy_step = false, array $contextual_options) |
212
|
|
|
{ |
213
|
30 |
|
if ( ! $this->isNormalizationAllowed($contextual_options)) { |
214
|
27 |
|
return $this; |
215
|
|
|
} |
216
|
|
|
|
217
|
13 |
|
if ($simplification_strategy_step) { |
218
|
|
|
$this->moveSimplificationStepForward( self::unify_atomic_operands, $contextual_options ); |
219
|
|
|
} |
220
|
|
|
|
221
|
13 |
|
return $this; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @param array $options + show_instance=false Display the operator of the rule or its instance id |
226
|
|
|
* |
227
|
|
|
* @return array |
228
|
|
|
*/ |
229
|
24 |
|
public function toArray(array $options=[]) |
230
|
|
|
{ |
231
|
|
|
$default_options = [ |
232
|
24 |
|
'show_instance' => false, |
233
|
24 |
|
'semantic' => false, |
234
|
24 |
|
]; |
235
|
24 |
|
foreach ($default_options as $default_option => &$default_value) { |
236
|
24 |
|
if ( ! isset($options[ $default_option ])) { |
237
|
24 |
|
$options[ $default_option ] = $default_value; |
238
|
24 |
|
} |
239
|
24 |
|
} |
240
|
|
|
|
241
|
24 |
|
if ( ! $options['show_instance'] && isset($this->cache['array'])) { |
242
|
2 |
|
return $this->cache['array']; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
$array = [ |
246
|
24 |
|
$options['show_instance'] ? $this->getInstanceId() : self::operator, |
247
|
24 |
|
$this->getOperandAt(0) ? $this->getOperandAt(0)->toArray($options) : false, |
|
|
|
|
248
|
24 |
|
]; |
249
|
|
|
|
250
|
|
|
// TODO make a dedicated cache entry for semantic array? |
251
|
24 |
|
if ( ! $options['show_instance'] && ! $options['semantic']) { |
252
|
13 |
|
return $this->cache['array'] = $array; |
253
|
|
|
} |
254
|
|
|
else { |
255
|
24 |
|
return $array; |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
*/ |
261
|
1 |
|
public function toString(array $options=[]) |
262
|
|
|
{ |
263
|
1 |
|
$operator = self::operator; |
264
|
1 |
|
if ( ! $this->operands) { |
|
|
|
|
265
|
|
|
return "['{$operator}']"; |
266
|
|
|
} |
267
|
|
|
|
268
|
1 |
|
$indent_unit = isset($options['indent_unit']) ? $options['indent_unit'] : ''; |
269
|
1 |
|
$line_break = $indent_unit ? "\n" : ''; |
270
|
|
|
|
271
|
1 |
|
$out = "['{$operator}'," |
272
|
|
|
. $line_break |
273
|
1 |
|
. ($indent_unit ? : ' ') |
274
|
1 |
|
. str_replace($line_break, $line_break.$indent_unit, $this->getOperandAt(0)->toString($options)) |
|
|
|
|
275
|
1 |
|
. ',' |
276
|
1 |
|
. $line_break |
277
|
1 |
|
. ']' |
278
|
1 |
|
; |
279
|
|
|
|
280
|
1 |
|
return $out; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* This method is meant to be used during simplification that would |
285
|
|
|
* need to change the class of the current instance by a normal one. |
286
|
|
|
* |
287
|
|
|
* @return OrRule The current instance (of or or subclass) or a new OrRule |
288
|
|
|
*/ |
289
|
14 |
|
public function setOperandsOrReplaceByOperation($new_operands, array $contextual_options) |
290
|
|
|
{ |
291
|
14 |
|
if (count($new_operands) > 1) { |
292
|
1 |
|
foreach ($new_operands as &$new_operand) { |
293
|
1 |
|
if ($new_operand instanceof AbstractRule) { |
294
|
1 |
|
$new_operand = $new_operand->toString(); |
|
|
|
|
295
|
1 |
|
} |
296
|
1 |
|
} |
297
|
|
|
|
298
|
1 |
|
throw new \InvalidArgumentException( |
299
|
|
|
"Negations can handle only one operand instead of: \n" |
300
|
1 |
|
.var_export($new_operands, true) |
301
|
1 |
|
); |
302
|
|
|
} |
303
|
|
|
|
304
|
14 |
|
$new_operand = reset($new_operands); |
305
|
|
|
// $new_operand->dump(); |
306
|
|
|
|
307
|
14 |
|
if ($new_operand instanceof NotRule) { |
308
|
1 |
|
$operands = $new_operand->getOperands(); |
309
|
1 |
|
return reset( $operands ); |
|
|
|
|
310
|
|
|
} |
311
|
14 |
|
elseif ($new_operand instanceof EqualRule && ! $this->getOption('not_equal.normalization', $contextual_options)) { |
312
|
6 |
|
return new NotEqualRule( $new_operand->getField(), $new_operand->getValue(), $this->options ); |
|
|
|
|
313
|
|
|
} |
314
|
10 |
|
elseif ($new_operand instanceof InRule && ! $this->getOption('not_in.normalization', $contextual_options)) { |
315
|
2 |
|
return new NotInRule( $new_operand->getField(), $new_operand->getPossibilities(), $this->options ); |
|
|
|
|
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
try { |
319
|
|
|
// Don't use addOperand here to allow inheritance for optimizations (e.g. NotInRule) |
320
|
9 |
|
$out = $this->setOperands( $new_operands ); |
321
|
|
|
} |
322
|
9 |
|
catch (\LogicException $e) { |
323
|
|
|
$out = new NotRule( $new_operand ); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
// $out->dump(); |
327
|
|
|
|
328
|
9 |
|
return $out; |
|
|
|
|
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* |
333
|
|
|
*/ |
334
|
|
|
public function hasSolution(array $contextual_options=[]) |
335
|
|
|
{ |
336
|
|
|
$operand = $this->getOperandAt(0); |
337
|
|
|
|
338
|
|
|
return $operand instanceof AbstractAtomicRule ? ! $operand->hasSolution($contextual_options) : true; |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/**/ |
342
|
|
|
} |
343
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.