@@ -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}
0 commit comments