Search code examples
droolskiekie-workbench

Kie workbench Guided decision table Execute only one Rule


I am trying to execute the rules written in Kie workbench and integrated with Kie Exceution server and got the response as expected . But my rquirement is to execute only one specific rule for list of large rules defined. What are the possible ways I can achieve . I used Activation Group , rule flow group but no luck If any one can help me to achieve this .

I created Guided Decision table in KIE Workbench . and source was generated in this way

    package demo.drools_examples;
    //from row number: 1
    rule "Row 1 Rule1"
    dialect "mvel"
    when
        f1 : Account( accountType == "Regular" )
    then
        f1.setBaseAmount( 10.0 );
        f1.setBaseThreshold( 10.0 );
        calculateAmount(f1);
        calculateThreshold(f1);
    end
    //from row number: 2
    rule "Row 2 Rule1"
    dialect "mvel"
    when
        f1 : Account( accountType == "Registered" )
    then
        f1.setBaseAmount( 5.0 );
        f1.setBaseThreshold( 15.0 );
        calculateAmount(f1);
        calculateThreshold(f1);
   end
   //from row number: 3
   rule "Row 3 Rule1"
   dialect "mvel"
   when
        f1 : Account( accountType == "Saving" )
    then
        f1.setBaseAmount( 20.0 );
        f1.setBaseThreshold( 10.0 );
        calculateAmount(f1);
        calculateThreshold(f1);
   end

How can I define saliance , Activation group or any other policy to call only rule 1 rather than calling fireallRules(1).. Please help me


Solution

  • You'll have to call fireAllRules, but with a twist:

    AgendaFilter af = new SingleRuleFilter( "Row 1 Rule1" );
    int nFired = kieSession.fireAllRules( af );
    

    And you only need to write something like this:

    public class SingleRuleFilter implements AgendaFilter {
        private String name;
        public SingleRuleFilter( String name ){
             this.name = name;
        }
        public boolean accept( Activation activation ){
             return activation.getRule().getName().equals( name );
        }
    }