j-easy / easy-rules

The simple, stupid rules engine for Java
https://github.com/j-easy/easy-rules/wiki
MIT License
4.86k stars 1.05k forks source link

Problems with cascading all rules in easy-rules 4.1.1-SNAPSHOT #334

Closed tsahay2 closed 3 years ago

tsahay2 commented 3 years ago

I am trying to use composite rule feature of easy rules (UnitRule Group). I have a requirement that I need to capture the failure rule name and perform some action. I figured this can be done via RuleListeners, but the problem is that the flow comes to the rule listeners (afterEvaluate method) as a composite rule.

I am not able to understand how to find the exact primitive rule which failed, and not the composite rule which I am currently getting, as I cannot parse individual rules as they are proxy elements. Any help appreciated!

tsahay2 commented 3 years ago

@benas Can you please take a look at this?

fmbenhassine commented 3 years ago

This is a duplicate of #273, see my comment here: https://github.com/j-easy/easy-rules/issues/273#issuecomment-623017689 . #273 is about ConditionalRuleGroup, but the same comment applies to any composite rule. This is documented in the composite rules section of the wiki, here is an excerpt:

Heads up: The rules engine works against the Rule interface and sees composite rules as regular rules.
For this reason, the RuleListener is called around the composite rule and not around its composing rules.
This is inherent to the composite design pattern.
tsahay2 commented 3 years ago

Hi @benas !

I understand that it is the nature of composite design pattern that the engine will treat it as a single rule.

My requirement was to have composite rules, but also perform an operation in case of a an individual rule failure (e.g insert an audit entry with the rule name). Could you suggest me a way I can do that in composite rule?

Currently I am having an internal map which I populate inside the condition of every rule regarding the decision and when the flow comes out of the engine, I simply parse the map for any rules failed and then perform the operation. Do you have a cleaner approach than this one ?