ALS
ALS

Reputation: 658

Cannot access the modified fact in the same agenda-group while evaluating when condition in drools

I have a list of rules with same agenda-group. These rules are interdependent. The rule monthly PI is dependent upon rule annual PI and the value of annual PI is also a fact for drools working memory. It works only when I change the agenda-group name or remove lock-on-active true (but goes into a loop even if i use no-loop). Inside when condition for monthly PI rule I'm checking if annual PI is not null then trigger the rule but it doesnt work as it doesnt fetch the updated value for it. Kindly suggest me what to do ?

rule "Annual P&I one"
@attribute("interestRate,originalLoanAmount")
agenda-group "loaneconomics"
salience 500
lock-on-active true
when 
$loanFact:LoanFact30Yr(loan.loanEcon.interestRate!=null)
then
System.out.println("::::Annual P&I Working ::::: ");

modify($loanFact){
$loanFact.getLoanRuleResult().getLoanEconomics().setAnnualPI( /*FORMULA*/  );
}
end

rule "Monthly P&I one"
@attribute("AnnualPI")
agenda-group "loaneconomics"
salience 400
lock-on-active true
when 
$loanFact:LoanFact30Yr( $loanFact.getLoanRuleResult().getLoanEconomics().getAnnualPI() != null )
then
System.out.println(":::: Monthly P&I Working ::::: ");
System.out.println("Monthly Payments ::: " + $loanFact.getLoanRuleResult().getLoanEconomics().getAnnualPI()/12f);
modify($loanFact){
getLoanRuleResult().getLoanEconomics().setToorakMonthlyPI( $loanFact.getLoanRuleResult().getLoanEconomics().getAnnualPI()/12f );
}
end

Upvotes: 0

Views: 149

Answers (2)

Nitesh Kumar
Nitesh Kumar

Reputation: 11

Try using update method in the higher salience rule, just before the end. The update method will take in the Fact attribute, that is being modified in higher salience rule. Using update method the DRL engine will re-evaluate the when condition of the lower salience rule.

Upvotes: 0

Esteban Aliverti
Esteban Aliverti

Reputation: 6322

lock-on-active will basically prevent any new activation during the current fireAllRules() invocation. Take a look at this answer to see possible solutions for your problem: what is the difference between no-loop and lock-on-active in drools

Upvotes: 0

Related Questions