Search code examples
drools

Triggering more then 1 rules in DROOLS DRL File


I have a drl file containing 10 rules. Each has a condition like -

Rule 1 
when 
      field1 == "X"
then
      status == "A"

Rule 2
when 
      field1 == "Y"
then
      status == "A"

So as seen based on value of variable field1 rules are to be executed.

Now I have a object containing more then 1 value like List. So if the List contains both values X & Y, only Rule 1 is applied and then processing is stopped. Rule 2 is never triggered.

Is there any way I can ask DROOLS to continue applying Rules even if 1 rule is successful ?


Solution

  • Try calling fireAllRules(); available in Drools session.

    It provides you following variances:

            fireAllRules();
            fireAllRules(AgendaFilter agendaFilter) 
            fireAllRules(AgendaFilter agendaFilter, int max)
            fireAllRules(int max)
    

    In last two variances, max is the upper limit on the rules to be fired. In your example, if you use max as 10, it can fire all 10 rules provided they meet the criteria.

    Please Note: The rules should be configured in such a way that they qualify for execution based on the updated state. Request you to review the criteria fragment (the when clause of the rules and ensure that they indeed result into positive evaluation during execution.

    e.g. if rules are written like below:

    Rule 1 
    when 
      field1 == "X"
    then
      status == "A"
    
    Rule 2
    when 
      field1 == "Y"
    then
      status == "A"
    

    then either field1 == "X" is true or field1 == "Y" is true and thus will trigger one rule at MAX.

    But if we write the rules like

    Rule 1 
    when 
      field1 == "X" || field1 == "Y" 
    then
      status == "A"
    
    Rule 2
    when 
      field1 == "X" || field1 == "Y" 
    then
      status == "A"
    

    then both the rules i.e. Rule 1 and Rule 2 will qualify if field1 is X or Y.

    Hope this helps.