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

Can we set default action if all the rules failed? #364

Open shabir1 opened 2 years ago

shabir1 commented 2 years ago

Can we set default action if all the rules fail? suppose I wrote 50 rules (50 conditions and 50 actions) and if all 50 conditions failed/false now I want to do some default action. like in try-catch block there is finally at the end, or in if .. else if there is else at the end. Similarly, do we have any default action if none of the rules are satisfied?

vshanbha commented 2 years ago

You could try the following

  1. Add an input "fact" to track if any rule was executed. Lets call it haveRulesExecuted
  2. Set up a RuleListener which checks this haveRulesExecuted flag in afterEvaluate and then set it to true if any rule was successfully evaluated.
  3. Set up a rule that has the highest priority and condition which checks the haveRulesExecuted == false. The highest priority will ensure that this is the last rule executed (provided you have no other rule with same priority) and thus if no other rules are executed this "Default" rule will definitely execute.