1

I want to accumulate the results of a collection of accumulate results. The problem is as follows :

  • I have a collection of objects (SwapAllocation) that can be classified in different groups (StockGroup).
  • A group is considered "large" if the cumulative value of its elements is above a given threshold.
  • The rule must be fired when the sum of the values of the "large" groups is itself above a given limit.

which should translate as something like (sorry it is in latex I do not have sufficient rep to post image)

\sum _{G \in \Gamma} \left(  \left\{\begin{matrix} &\text{if } \sum _{S \in G} S.nominal > 5 \text{ then } 1
\\ 
&\text{else } 0
\end{matrix}\right \right ) \leq 40

Is there a "drools" way to do that?

My attempt at the drools rule looks like :

rule "maximalCumulativeWeightLargeWeightStockPerSwap"
  when
    $sw : Swap($nominal: nominal,
               $largeWeightStockGroupThresh: largeWeightStockGroupThresh,
                $maxCumWeightLargeWeightStockGroup:
                     maxCumWeightLargeWeightStockGroup)
    $reqNominal : Number(doubleValue > $maxCumWeightLargeWeightStockGroup*$nominal ) from accumulate(
        $sg : StockGroup()
        $reqNominalSG : Number( doubleValue > $largeWeightStockGroupThresh*$nominal) from accumulate(
            $sa : SwapAllocation(swap == $sw, stock.getStockGroup() == $sg)
            sum($sa.nominal())
        )
        sum($reqNominalSG.doubleValue())
      )
  then
    scoreHolder.addHardConstraintMatch(kcontext, (int) (100 * ($maxCumWeightLargeWeightStockGroup - $reqNominal.doubleValue()/$nominal)));
end

which yields

text=[ERR 102] Line 73:27 mismatched input ':' in rule "maximalCumWeightLargeWeightStockPerSwap"]

because it appears that I a cannot define multiple elements in the accumulate function like I would in the when section of a rule.

laune
  • 31,114
  • 3
  • 29
  • 42
Bruno
  • 15
  • 4

1 Answers1

0

As far as I have understood this:

"A group is considered "large" if the cumulative value of its elements is above a given threshold."

I'd recommend to write a rule that does an insertLogical (see "Truth Maintenance") of an auxiliary fact that marks that group as "large".

This should simplify the accumulation of the "large" groups.

(Sorry, I edited to reduce the name monsters to readability.)

laune
  • 31,114
  • 3
  • 29
  • 42