-1

I do not understand a few parts of this drl code, but I'll just post one question here:

https://github.com/kiegroup/kogito-examples/blob/stable/ruleunit-quarkus-example/src/main/resources/org/kie/kogito/queries/RuleUnitQuery.drl

The full drl code is here:

package org.kie.kogito.queries;
unit LoanUnit;
import org.kie.kogito.queries.LoanApplication;
import org.kie.kogito.queries.AllAmounts;
rule SmallDepositApprove when
    $l: /loanApplications[ applicant.age >= 20, deposit < 1000, amount <= 2000 ]
then
    modify($l) { setApproved(true) };
end
rule SmallDepositReject when
    $l: /loanApplications[ applicant.age >= 20, deposit < 1000, amount > 2000 ]
then
    modify($l) { setApproved(false) };
end
rule LargeDepositApprove when
    $l: /loanApplications[ applicant.age >= 20, deposit >= 1000, amount <= maxAmount ]
then
    modify($l) { setApproved(true) };
end
rule LargeDepositReject when
    $l: /loanApplications[ applicant.age >= 20, deposit >= 1000, amount > maxAmount ]
then
    modify($l) { setApproved(false) };
end
rule NotAdultApplication when
    $l: /loanApplications[ applicant.age < 20 ]
then
    modify($l) { setApproved(false) };
end
query FindApproved
    $l: /loanApplications[ approved ]
end
query FindNotApprovedIdAndAmount
    /loanApplications[ !approved, $id: id, $amount : amount ]
end
rule AllAmounts
when
    accumulate ( $a : /loanApplications ; $sum : sum($a.amount))
then
    allAmounts.add(new AllAmounts($sum));
end
query FindAllApplicationAmounts
    $a : /allAmounts
end

What is the difference between the usage of the variables $l and $a, given that they are both linked to /loanApplications in some way? Are they considered local or instance variables?

I've extracted a couple of rules illustrating what I'm asking about here. What actually goes into $l and $a, given that they're supposed to be different?

rule NotAdultApplication when
    $l: /loanApplications[ applicant.age < 20 ]
then
    modify($l) { setApproved(false) };
end
rule AllAmounts
when
    accumulate ( $a : /loanApplications ; $sum : sum($a.amount))
then
    allAmounts.add(new AllAmounts($sum));
end
maxloo
  • 453
  • 2
  • 12
  • Please include the rules in question in your post itself. Except for globals, which are declared at the top of the file using the `global` keyword, all variables in drools are scoped to the rule, though some can be scoped even more discretely to a function (eg accumulate.) – Roddy of the Frozen Peas Aug 20 '21 at 14:56
  • @RoddyoftheFrozenPeas, thanks, i've included 2 rules as examples. – maxloo Aug 21 '21 at 14:21

1 Answers1

1

Variables in Drools are similar to variables in Java.

For your first rule, NotAdultApplication, the result of /loanApplications is assigned to the variable $l. Its scope is the rule -- both the conditional (when) and consequence (then) clauses.

For your second rule, AllAmounts, there are two different variables: $a and $sum. Similar to $l in the first rule, $sum is scoped to the rule, and you can see that it's being used in the consequence. However $a is scoped to the accumulate function, not to the rule -- you can't access $a from the when clause.

The reason that these two variables are scoped differently is because they serve two different functions; their scope is really due to what they represent as part of the accumulation. If might be easier to understand if I rewrote the rule to look like this:

rule "AllAmounts (alternative version)"
when
  $applications: /loanApplications
  accumulate( $a: LoanApplication() from $applications,
              $sum: sum($a.amount))
then
  allAmounts.add(new AllAmounts($sum));
end

Hopefully this version is more clear: $a is a temporary internal variable that is used to help iterate through the loan applications; it's like the variable in foreach loop -- it gives us a reference to the individual load application. $sum is assigned to the output of the function, which is why you can use it on in the rule consequences.

If you think about it, it wouldn't make sense to try and access $a from outside of the accumulation -- what would it refer to?

This is broadly equivalent to this Java code:

Collection<LoanApplication> $applications = getLoanApplications(); // or whatever the equivalent is for /loanApplications
  
double $sum = 0.0;
for ( LoanApplication $a : $applications ) {
  $sum += $a.amount;
}
// you can't access $a here -- what would it even mean?
Roddy of the Frozen Peas
  • 14,380
  • 9
  • 49
  • 99