javarule-engine

Are the rules fired in easy rules not exclusive based on priority?


I am trying to work with EasyRules.

I have created 3 rules A, B & C with priorities 1, 2 & 3 respectively.

Even though rule A is evaluated to true and has the highest priority, rules with lower priority (i.e. B & C) are being evaluated.

How does this rule priority work?

Is there a work around where if a rule with higher priority is evaluated to true, then rules with lower priority are not evaluated?


Solution

  • Priority has no relation with whether a rule is executed or not. It indicates only the execution order of the rules

    From the documentation :

    Each rule in Easy Rules has a priority. This represents the default order in which registered rules are fired. By default, lower values represent higher priorities.

    You can also read :

    A composite rule is triggered if all conditions of its composing rules are satisfied. When a composite rule is applied, actions of all composing rules are performed in the natural order of rules which is rules priorities by default.

    If you want the engine to skip rules after a rule is applied, you should provide the engine with the param skipOnFirstAppliedRule set to true.

    RulesEngine  rulesEngine = RulesEngineBuilder.aNewRulesEngine()
                              .withSkipOnFirstAppliedRule(true)
                              .build();
    
    // add your rules in the RulesEngine
    rulesEngine.registerRule(new A());
    rulesEngine.registerRule(new B());
    rulesEngine.registerRule(new C());
    
    // execute the rules
    rulesEngine.fireRules();
    

    In your case, only the A rule will be executed.