Carlos López
Carlos López

Reputation: 759

Checking if an object of a list has some value with drools

I have some problems checking if an object of a list has a value with drools.

My model is this

class Products{
    private List<Approver> approvalPath;
}
class Approver{
    private String employeeName;
}

So, I need to make a rule like this

rule "member"
    when
        //approvalPath has an approver with name "Charles" (for example)
    then
        //do something
end

How can I do it?

Upvotes: 4

Views: 12722

Answers (2)

laune
laune

Reputation: 31290

Here are two versions.

when
  $app: Approver( employeeName == "Charles" )
  Products( approvalPath contains $pp )

And:

when
  Products( $ap: approvalPath )
  Approver( employeeName == "Charles" ) from $ap

Number 1 requires the insertion of Approver objects as facts.

Upvotes: 9

Edson Tirelli
Edson Tirelli

Reputation: 3901

rule member
when
    $p : Products()
    $a : Approver( name == "Charles" ) from $p.approvalPath
then
...
end

The above rule will fire once for each Approver named Charles in the list. If you want to fire the rule just once, no matter how many approvers named Charles are in the list, just wrap the second line in exists( ... ).

Upvotes: 2

Related Questions