0

I'm a newbie at Kogito and Drools. 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 does /loanApplications and /allAmounts mean or refer to? Are they linked to other files? /loanApplications appears in many parts of the code, I've extracted 2 particular parts as examples. One of them uses /loanApplications without attaching it to a variable. How does that work?

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
maxloo
  • 453
  • 2
  • 12

1 Answers1

2

/loanApplications and /allAmounts are using OOPath notation to reference the rule units of the equivalent names.

The Drools documentation describes OOPath notation as follows:

OOPath is an object-oriented syntax extension of XPath that is designed for browsing graphs of objects in DRL rule condition constraints. OOPath uses the compact notation from XPath for navigating through related elements while handling collections and filtering constraints, and is specifically useful for graphs of objects.

More information about Rule Units can be found in the official Drools documentation, which I always recommend because it's extremely well written and generally very clear. However I'll give a brief explanation here about how it specifically works with the rule you've provided as an example.

Notice, first, that at the top of the linked DRL file there is a RuleUnit declaration immediately under the package declaration:

unit LoanUnit;

This tells us that there is a rule unit involved here, which likely contains methods that look something like this:

DataSource<LoanApplication> getLoanApplications()
DataSource<AllAmounts> getAllAmounts()

If we poke through the repository, we can find a defined LoanUnit.java under /src/main/java/org/kie/kogito/queries/. Looking at the source it turns out my wild guess for what those functions look like is entirely correct. That's a good thing too -- I shouldn't have to know the intricacies of how the rule unit is defined in order to make use of the data and functionality it provides.

Anyway, the rule writer decided to use OOPath notation for their rules in your example DRL (some people find it "simpler"; I personally find it confusing. But as long as you pick a syntax and stick to it, I suppose it doesn't matter in the long term.) /loanApplications[ !approved, $id: id, $amount : amount ] is functionally equivalent to this traditional DRL notation:

LoanApplication( !approved, $id: id, $amount: amount ) from LoanApplications

The from LoanApplications maps to the getLoanApplications() method in the RuleUnit, using Drools' usual methodology of mapping to getters based on Java bean naming conventions.

Since OOPath notation is just an alternative way of expressing the traditional DRL notation, you can still do all the things you would have done in a normal rule, including assigning variables. In the example above, we don't actually need the LoanApplication instance itself, just the id and the amount; but if we actually do want to haul around the instance, we could just as easily assign the variable in either of these two notations:

$myLoan: /loanApplications[ !approved, $id: id, $amount : amount ]
$myLoan: LoanApplication( !approved, $id: id, $amount: amount ) from LoanApplications

The two statements are identical, functionally.


If you want to learn more about OOPath notation ... well I can't help you there. Googling seems to indicate it's a JBoss/Drools thing, but it's apparently very poorly documented. Most of the syntax I've figured out has been by working backwards from the provided examples. I'm sure it's very nice if you like xpath notation (who does?), but the choice of using it is up to you, of course.

Roddy of the Frozen Peas
  • 14,380
  • 9
  • 49
  • 99