Search code examples
javarule-engineeasy-rules

How can I define that only one rule has to be executed in EasyRules?


I'm using easy-rules to evaluate a set of escenarios, each of them will execute a different logic. I want to make sure that from all the rules registered only one gets executed, I have the following code:

public static void main(String[] args) {
    Facts facts = new Facts();
    facts.put("object", "value");

     Rules rules = new Rules();
     rules.register(new Rule1());
     rules.register(new Rule2());
     rules.register(new Rule3());

     RulesEngine rulesEngine=new DefaultRulesEngine();
     rulesEngine.fire(rules, facts);
}

In the previous example I want to ensure that if Rule1 gets executed Rule2 and Rule3 won't be.

Thanks for your help


Solution

  • To stop the further execution of rules once a match happens you need to configure the rule engine as follows :

    RulesEngineParameters parameters = new RulesEngineParameters()
    .skipOnFirstAppliedRule(true);
    RulesEngine rulesEngine=new DefaultRulesEngine(parameters);
    

    From the Docs :

    enter image description here