Search code examples
droolsrule-enginerule

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)


Solution

  • 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.