Search code examples
droolsoptaplanner

accumulate from elements of a set - drools


I want to sum the weight of unique panelist having (at least) one active viewing. A viewing has a panelist, a panelist has a weight (int).

I obtain the set of Panelists having an active Viewing like this:

accumulate(
    Viewing(active==true,
        $p : panelist),
    collectSet($p))

EDIT: Note that I am using a set as a way of getting the set of unique panelists.

I want to sum the weight of every panelist in this set. My attempt (below) is returning

java.util.Collections$UnmodifiableSet cannot be cast to com...domain.Panelist

Basically, a set is not a panelist, duh.

How can I access elements of the set, ideally using idiomatic drools rather than a Java hack?

This is my attempt:

rule "targetLevelReach"
    when
        $actualReach : Number(intValue>10) from accumulate (
            Panelist($weight : weight) from 
                 accumulate(
                     Viewing(
                         active==true, 
                         $p : panelist),
                     collectSet($p)),
            sum($weight))
    then
        ...
end

Solution

  • Building further on laune's idea:

    rule "targetLevelReach"
    when
        $actualReach : Number(intValue>10) from
          accumulate ( $p : Panelist
                       and exists Viewing( active==true, panelist == $p),
                       sum($p.getWeight() )
    then
        ...
    end