javadrools

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

Solution

  • 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