Search code examples
droolsrule-enginerule

How to implement rule If I want to execute only one rule rather than execute all rules in Drools Rule Engine?


I want to implement rule engine in which if only one condition executes then it will not check other specified conditions.

rule "Print out lower-case tokens"
when
    Token ( coveredText == coveredText.toLowerCase )
then
    System.out.println("Found a lower case token with text");
end


rule "Print out long tokens with more than 5 characters"
    when
        Token ( tokenText : coveredText, end - begin > 5 )
    then
        System.out.println("Found a long token with more than 5 characters \"" + tokenText + "\"");
end

In above example, if coveredText and its lowercase are equals then I don't want to check another rule.
How can I implement this kind of nature in Drools Rule Engine ?


Solution

  • This can be achieved by using agenda groups in Drools.

    Quote :

    Agenda groups are a way to partition the Agenda into groups and to control which groups can execute. By default, all rules are in the agenda group "MAIN". The "agenda-group" attribute lets you specify a different agenda group for the rule. Initially, a Working Memory has its focus on the Agenda group "MAIN". A group's rules will only fire when the group receives the focus.

    For example when rule "Print out lower-case tokens" fires, you can set the focus to another group (Not the group of the two rules you mentioned). Of course you want the first rule the be triggered first. This can be done with a higher salience value:

    Each rule has an integer salience attribute which defaults to zero and can be negative or positive. Salience is a form of priority where rules with higher salience values are given higher priority when ordered in the Activation queue.