Search code examples
drools

Drools trigger one rule per item in list input for StatelessKieSession


I am using decision tables and would like to trigger one rule per input item.

I am using decision have set the Sequential = true and defined all rules as part of the same ACTIVATION-GROUP.

When I trigger the drools rules engine using below it just evaluates for the first input item and others are ignored. The behavior I want is to evaluate at most 1 rule per input item (rule order defined by the Salience).

kieStatelessSession.execute(inputList)

I can get this working by sending one item at a time to the kieStatelessSession, but would prefer to execute all at once.

I am using Drools verison 6.5.0.FINAL and Java 7.


Solution

  • There is no out of the box support in Drools for what you are trying to achieve. If you want your rules to be evaluated once for each fact, you will need to code it yourself.

    One approach could be to have another type of facts to mark when one of the inputs is processed:

    declare Marker
      fact : Object
    end
    
    //Bellow are the rules that should be coming from your decision table.
    //Each rule will do whatever it needs to do, and then it will create a 
    //Marker fact for the fact that was processed.
    //These rules now include a "not" Conditional Element to avoid a fact to be
    //evaluated more than once.
    
    rule "Rule 1"
    salience 100
    when 
      $fact: Object(...)   //your conditions
      not Marker(fact == $fact)
    then
      //...   Your logic
      insert(new Marker($fact)); 
    end
    
    ...
    
    
    rule "Rule 50"
    salience 50
    when 
      $fact: Object(...)   //your conditions
      not Marker(fact == $fact)
    then
      //...   Your logic
      insert(new Marker($fact)); 
    end
    

    Hope it helps,