Drools clear agenda group - Java @ Desk

Tuesday, March 4, 2014

Drools clear agenda group

Drools clear agenda group

In the last post, we understood how agenda group works and how to set focus in an agenda group.

When a focus is set to any agenda group all the rules of that rule enters into activation state and knowledge session will fire all the rules of that agenda group. What if we may require, if any one rule of an agenda group gets fired no other rule of that agenda group should get fired.

In order to achieve this, we need to clear that agenda group. This can be done in a DRL file as shown below.

Insert Agenda Group in following order :

knowledgeSession.getAgenda().getAgendaGroup("Insufficient Balance").setFocus();
knowledgeSession.getAgenda().getAgendaGroup("Check Balance").setFocus();

So there will be two agenda groups in the stack in the following posistion :
Insufficient Balance at position 0
Check Balance at position 1

rule "Check Bank Balance"
agenda-group "Check Balance"
salience 100
when
    $loanAmount : LoanAmount(monthlyInstallment > bankBalance)
then
    kcontext.getKnowledgeRuntime().getAgenda().getAgendaGroup("Insufficient Balance").clear();
    System.out.println("Agenda Group Cleared");
end

rule "Rule Insufficient Balance"
agenda-group "Insufficient Balance"
when
    $loanAmount : LoanAmount();
then
    $loanAmount.setInsufficientBalance(true);
    System.out.println("Insufficient Funds to perform operation. Send Reminder Email");
    
end


So when rule 1 fires successfully, it will clear the Insufficient Balance agenda group and rule two will not be fired since agenda is cleared, else Insufficient Balance agenda group will be taken out of stack and second rule will be activated.





No comments:

Post a Comment