Search code examples
droolsmvel

Comparing a field in a List of objects with a List of Strings in Drools


Assume I have a Policy object in Drools, that object contains a List of Cover objects, called covers and a List of String objects, which are called requestedCovers.

The Cover object contains type field, which is a String object.

I want to fire a rule if a requestedCover does not match a type in the Covers list.

rule "Add validation error for policies with requested covers that are not available"
when
    $p: Policy(available == true, $requestedCovers: requestedCovers, $covers: covers)
    $requestedCover: String() from $requestedCovers
    Cover(type not contains $requestedCover) from $covers 
then
    log.error("RHS rule not implemented yet. Found type {}.", $requestedCover);
end

However this seems to trigger every Cover is the covers list.

How to create this rule?


Solution

  • Finally figured it out, thanks to @laune for the help

    when
        $p: Policy(available == true, $requestedCovers: requestedCovers, $covers: covers)
        $requestedCover: String() from $requestedCovers
        $coverTypes: List(!this.isEmpty()) from accumulate(Cover($coverType: type) from $covers; collectList($coverType))
        eval(!$coverTypes.contains($requestedCover))