Drools - Multiple rules fire even though they all belong to the same activation group

drools activation group decision table
drools fire specific rule
how to execute multiple rules in drools
ruleflow-group in drools
drools agenda group
drools rule execution order
drools rules not firing in spring boot
drools fireallrules

I have a set of rules as follows:

rule "Default Margin By Grade"
ruleflow-group "MarginByGrade"
enabled false
when
    $mg : MarginByGrade()
    $u  : PriceUnit( resale==null, trimGrade memberOf $mg.grades  )
then    
end

rule "Grade Margin By Group, Style" extends "Default Margin By Grade"
activation-group "Margin By Grade"
salience 500
when
    MarginByGrade(this == $mg, $u.model memberOf $mg.models,  $u.style memberOf $mg.styles)
then
    System.out.println("Found match : " + $mg);
end

rule "Grade Margin By Style" extends "Default Margin By Grade"
activation-group "Margin By Grade"
salience 100
when
    MarginByGrade(this == $mg, models == null, $u.style memberOf $mg.styles)
then
    System.out.println("Found match : " + $mg);
end

rule "Grade Margin By Group" extends "Default Margin By Grade"
activation-group "Margin By Grade"
salience 50
when
    MarginByGrade(this == $mg, prefixes memberOf prefixes, styles == null)
then
    System.out.println("Found match : " + $mg);
end

rule "Margin by Grade" extends "Default Margin By Grade"
salience 5
activation-group "Margin By Grade"
when
    MarginByGrade(this == $mg, prefixes == null, styles == null)
then
    System.out.println("Found match : " + $mg);
end

The rules are triggered based on a Rule Flow (hence the 'ruleflow-group' attribute. My requirement is that once the rule with the highest salience fires, the rules with the lower salience should not fire. Yet when I run provide a fact that activates multiple rules, all activated rules get fired:

Start Process: Mon Sep 19 15:58:39 EDT 2011
Found match : MarginByGrade( prefixes=null, styles=null, grades=[C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S], margin=26.0000 )
Found match : MarginByGrade( prefixes=[015, 215], styles=[572], grades=[A, B, D], margin=25.5000 )
Found match : MarginByGrade( prefixes=[015, 010], styles=[515, 215, 572], grades=[C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S], margin=24.5000 )
015572  D 015572  D933079 FN 175->null
Dispose Session: Mon Sep 19 15:58:39 EDT 2011

What am I doing wrong? I'm using Drools Expert 5.2.0-Final.

Well you are using activation-group and not ruleflow-group right?.. The activation-group will cause that only one rule get executed and as you mention the one with higher salience will be executed if it is activated. Cheers

Chapter 7. Running - JBoss Community Confluence, The term "entry point" is related to the fact that we have multiple partitions in a Working The default Agenda Group is "MAIN", with all rules which do not specify an rules belonging to a given Agenda Group before one has had a chance to fire. is a set of rules bound together by the same "activation-group" rule attribute. Rules that belong to the same activation-group, identified by this attribute's string value, will only fire exclusively. In other words, the first rule in an activation-group to fire will cancel the other rules' activations, i.e., stop them from firing.

Stateless Session:

Be-aware of activation-group in case you are working with collection of Facts and stateless session, since only one rule will fire, it would skip other facts. Best solution is to use Salience and a processed flag on facts

Mastering JBoss Drools 6, As the consequence of this rule, the kcontext default variable is used to Through this, it can activate the next agenda group just like it did on the plain Java code. rules in groups; however, sometimes, we need these groups to have an even in the same activation group, the first one to fire will cancel the other four rules. If multiple rules have the same salience, there won't be an execution order among them, but the engine will execute them at the same level. So If Rule 1 has salience 10, Rule 2 has 10, and Rule 3

The activation group will fire once for a single MarginByGrade

From your console output, you have at least 3 MarginByGrade, so it's correct to fire different rules for each of these 3 MarginByGrade

Drools Jboss Rules 5. X Developer's Guide, share the same activation—group attribute. If one of these rules fire for any customer, then the other rule won't fire for any customer, agenda—groupI Every rule has an agenda group. By default it is MAIN; however, it can be overridden. This allows us to partition Drools agenda into multiple groups that can be executed  The execution of rule "A to B" changes the state of B to FINISHED, which activates both, rules "B to C" and "B to D", placing their Activations onto the Agenda. From this moment on, both rules may fire and, therefore, they are said to be "in conflict".

Understanding Execution Control in Drools using Simple Example , In previous chapter we implemented a drools project to understand various attributes If your KieBase defines multiple rules and if you want to selectively execute a subset of them, Drools provides It can be assigned for all rules. Rules that belong to activation-group fire in similar fashion to "if..else if..else" block in java. Drools is a Business Rules Management System (BRMS) solution. It provides a core Business Rules Engine (BRE), a web authoring and rules management application (Drools Workbench), full runtime support for Decision Model and Notation (DMN) models at Conformance level 3 and an Eclipse IDE plugin for core development.

3.5. Rule, Rules that belong to the same named activation-group will only fire The Activtion group attribute is any string, as long as the string is identical for all the rules you in Drools we refer to this list of Facts as a Tuple - each element in the array is a Drools will autobox them to their corresponding object types (even if you are  Then there can be a series of rules whose job is to look for things that may raise suspicion, and if they fire, they simply assert a new SuspiciousAccount() instance. All the other rules just have conditions like "not SuspiciousAccount()" or "SuspiciousAccount()" depending on their needs.

Drools Expert User Guide, The logic itself may even be simple but the rules change quite often. rule "​Cancel the alarm when all the fires have gone" when not Fire() $alarm When a rule has multiple literal conditions for a single object type, they are linked together. Rules that belong to the same activation-group, identified by this attribute's string  As you saw the .drl (rule file) has its own syntax, let us cover some part of the Rule syntax in this chapter. The above conditions check if the Account balance is 200 or the Customer name is “Vivek”. A variable name in Drools starts with a Dollar ($) symbol. Drools can work with all the native Java types and even Enum.

Comments
  • Thanks for the answer. I only want one rule to fire (the one with the highest salience).
  • So which one is correct: activation-group or ruleflow-group ??
  • Not sure about the ruleflow, but activation-group acts as an exclusive group which you can use to solve the above mentioned issue. Quoting documentation: Rules that belong to the same activation-group, identified by this attribute's string value, will only fire exclusively. In other words, the first rule in an activation-group to fire will cancel the other rules' activations, i.e., stop them from firing.