Skip to content

Commit 08e820f

Browse files
author
Kenneth Ocastro
committed
Add config to show interpretation and failedConditions
1 parent 1370cdf commit 08e820f

File tree

3 files changed

+44
-19
lines changed

3 files changed

+44
-19
lines changed

src/Engine.php

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class Engine
1010
private array $allRules = [];
1111
private ?Rule $targetRule = null;
1212
private Facts $facts;
13+
private bool $showInterpretation = false;
14+
private bool $showFailedConditions = false;
1315

1416
public function __construct()
1517
{
@@ -61,26 +63,37 @@ public function evaluate(): array
6163
}
6264

6365
$result = [];
64-
$failedConditions = [];
65-
66-
// Evaluate the target rule
67-
if ($this->targetRule->evaluate($this->facts, $this->allRules)) {
68-
$result[] = array_merge(
69-
$this->targetRule->triggerEvent($this->facts),
70-
['interpretation' => $this->targetRule->interpretRules()]
71-
);
72-
} else {
73-
$failedConditions = $this->targetRule->getFailedConditions();
74-
75-
$result[] = array_merge(
76-
$this->targetRule->triggerFailureEvent($this->facts),
77-
[
78-
'interpretation' => $this->targetRule->interpretRules(),
79-
'failedConditions' => $failedConditions
80-
]
81-
);
82-
}
66+
$evaluationResult = $this->targetRule->evaluate($this->facts, $this->allRules);
67+
68+
$extra = $this->getExtraData($evaluationResult);
69+
70+
$triggerMethod = $evaluationResult ? 'triggerEvent' : 'triggerFailureEvent';
71+
$result[] = array_merge($this->targetRule->$triggerMethod($this->facts), $extra);
8372

8473
return $result;
8574
}
75+
76+
private function getExtraData(bool $evaluationResult): array
77+
{
78+
$extra = [];
79+
80+
if ($this->showInterpretation) {
81+
$extra['interpretation'] = $this->targetRule->interpretRules();
82+
}
83+
84+
if (!$evaluationResult && $this->showFailedConditions) {
85+
$extra['failedConditions'] = $this->targetRule->getFailedConditions();
86+
}
87+
88+
return $extra;
89+
}
90+
91+
public function showInterpretation(bool $showInterpretation): void
92+
{
93+
$this->showInterpretation = $showInterpretation;
94+
}
95+
public function showFailedConditions(bool $showFailedConditions): void
96+
{
97+
$this->showFailedConditions = $showFailedConditions;
98+
}
8699
}

src/Rule.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class Rule
1111
private array $failureEvent;
1212
private ?string $name;
1313
private array $failedConditions = [];
14+
private array $dependencyRules = [];
1415

1516
public function __construct(array $options)
1617
{
@@ -27,6 +28,11 @@ public function __construct(array $options)
2728
}
2829
}
2930

31+
public function getDependencyRules(): array
32+
{
33+
return $this->dependencyRules;
34+
}
35+
3036
public function getName(): ?string
3137
{
3238
return $this->name;
@@ -54,6 +60,7 @@ private function evaluateAll(array $conditions, Facts $facts, array $allRules):
5460
$dependencyRuleName = $condition['condition'];
5561
if (isset($allRules[$dependencyRuleName])) {
5662
$dependencyRule = $allRules[$dependencyRuleName];
63+
$this->dependencyRules[] = $dependencyRule;
5764
if (!$dependencyRule->evaluate($facts, $allRules)) {
5865
$this->failedConditions[] = $condition;
5966
return false;
@@ -93,6 +100,7 @@ private function evaluateAny(array $conditions, Facts $facts, array $allRules):
93100
$dependencyRuleName = $condition['condition'];
94101
if (isset($allRules[$dependencyRuleName])) {
95102
$dependencyRule = $allRules[$dependencyRuleName];
103+
$this->dependencyRules[] = $dependencyRule;
96104
if ($dependencyRule->evaluate($facts, $allRules)) {
97105
return true;
98106
} else {

tests/EngineTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public function testPlayerIsFouledOut()
2828
$engine->addFact('gameDuration', 40);
2929

3030
$engine->setTargetRule('rule.player.isFouledOut');
31+
$engine->showInterpretation(true);
3132

3233
$result = $engine->evaluate();
3334
$expectedResult = [
@@ -76,6 +77,8 @@ public function testProfileIsCompleted()
7677
]);
7778

7879
$engine->setTargetRule('rule.profile.isCompleted');
80+
$engine->showInterpretation(true);
81+
$engine->showFailedConditions(true);
7982

8083
$result = $engine->evaluate();
8184

@@ -181,6 +184,7 @@ public function testProfileIsSearchable()
181184

182185
// Step 5: Set the target rule
183186
$engine->setTargetRule('profile.isSearchable');
187+
$engine->showInterpretation(true);
184188

185189
// Step 6: Evaluate the engine
186190
$result = $engine->evaluate();

0 commit comments

Comments
 (0)