Search code examples
javadroolsrule-enginekie

Drools 7.2: Building a rule engine (monitor mode and high-performance)


I'm building a rule engine which exits after the first rule is matched. The ordering of the rules is done using Salience, lock-on-active (to prevent re-matches) and activation-group to exit as soon as the input matches the first rule.

The business now has a new requirement for a monitor mode where the engine has to continue to match the input to a rule which is not in monitor mode.

For example:

Object(attr1 = 1, attr2 = 2)

monitor     Rule 1: if (attr1 = 1) 
non-monitor Rule 2: if(attr = 1 and attr2= 2)
non-monitor Rule 3: ..

Here, it should match both rule 1 and rule 2, but MUST(for higher-performance) exit as soon as rule 2 is matched since it's in a non-monitor mode. Monitor mode rules are just used to see if they get evaluated and we trigger events on the backend for our business purposes.

I'm currently using PackageDescBuilder, RuleDescrBuilder etc for loading our rules dynamically from the database. A simple StatelessKieSession is used to evaluate and store results within the predicate as part of the RHS.

    KieContainer kContainer = ks.newKieContainer(kr.getDefaultReleaseId());
    StatelessKieSession kieSession = kContainer.newStatelessKieSession();
    kieSession.execute(predicate);

Q: How can the new rules be built and what concepts should I explore here for this purpose?

I'm referring to this documentation here: https://docs.jboss.org/drools/release/7.2.0.Final/drools-docs/html_single/#_rule_attributes

Thanks!


Solution

  • If any number of "monitor" rules need to be fired before rule firing should stop after the first "non-monitor" rule, the simplest approach would be to call fireUntilHalt and call halt() on the session after the first "non-monitor" has fired.

    Either put a halt() on the end of each "non-monitor" rule or use a listener.