Search code examples
drools

Rule activation order while using auto-focus


I have some doubts regarding the order in which the rules are activated if they are given the attribute auto-focus.

For example, if I have the following rule-file:

rule "Rule1" salience 2
agenda-group "group1"
auto-focus
    when
        ...
    then
        ...
end

rule "Rule2" salience 1
agenda-group "group2"
auto-focus
    when
        ...
    then
        ...
end

and I pass a fact which activates both the rules, I observe that the agenda groups are stacked in the order:

group2
group1

which means that Rule2 is executed first.

Now if I reverse the order of the rules:

rule "Rule2" salience 1
agenda-group "group2"
auto-focus
    when
        ...
    then
        ...
end

rule "Rule1" salience 2
agenda-group "group1"
auto-focus
    when
        ...
    then
        ...
end

I observe that Rule1 is fired first now.

Does this mean that salience does not affect the order of activations in case of auto-focus, but instead it depends on the order in which the rules are written in the rules file ?

I went through this discussion regarding this topic but did not find satisfactory answers.

If the behavior I described is correct, is it documented anywhere ?

I am using Drools 6.2.0.


Solution

  • Many of the rule attributes are ad-hoc extensions that have been invented to allow rule authors to reduce the logic of their rules. You should be very careful whenever a SW feature announces "auto(matic)".

    If an activation (which is not the same as executing) sets an agenda-group, then salience will only decide within that group. Salience, on the other hand, does not govern the evaluation order.

    Remove the auto-focus and the agenda-group if you need to handle the decision between two or more agenda-groups by a rule. Call e.g.

    drools.setFocus( "group1" );
    

    on the right hand side of the rule doing the selection.