Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 678
unknown Avatar asked Dec 19 '25 19:12

unknown


1 Answers

If you only have a few rules, agenda groups mentioned by @K.C. might be too 'heavy' for your purpose. In simpler cases I'd just add a fact to mark that the rules should not be fired anymore, like this

declare AlreadyProcessed
end

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


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

And as mentioned, you can control the execution order via salience if needed.

like image 78
kaskelotti Avatar answered Dec 24 '25 03:12

kaskelotti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!