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 ?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With