GroomedGorilla
GroomedGorilla

Reputation: 1002

Matching object to list member in Drools

I've currently got a series of objects of class A and an object which is a List of objects of Class B in Drools Working Memory. Both classes have an ID attribute.

I need to run a rule to check if there is an object of Class A in memory that matches an ID from the list of Class B. (In essence, running a search for each element of the list to check if its ID matches an object of Class A)

To make things clearer, I've got something of the sort in memory:

class A{
    ID : String
}

List[B]  where:

Class B{
    ID: String
}

I've been trying to structure a rule query around this to no avail. Ideas? (Still relatively new to Drools)

Upvotes: 0

Views: 1201

Answers (1)

laune
laune

Reputation: 31300

Perhaps like this:

when
  $list: List()
  B($id: ID) from $list
  A(ID == $id)
then

Using a container as a fact is usually considered (at least by me) as being an antipattern. Things are easier if the B's are facts.

A($id: ID)
B(ID == $id)

You can still insert the List if you think this will ever be useful.

Upvotes: 3

Related Questions