Conceptual meaning of 'not' keyword; evaluating between objects
-
28-04-2021 - |
Question
I am trying to find a BucketTotal
object which has the smallest total
in a Drools Planner project. I adapted this from example code.
rule "insertMinimumBucketTotal"
when
$b : BucketTotal($total : total)
not BucketTotal(total > $total) // CONFUSED HERE
then
insertLogical(new MinimumBucketTotal($total));
end
As far as my reasoning went, it meant "find BucketTotal
object $b
, such that there doesnt exist another BucketTotal
object whose total
is greater than total
of $b
".
Turns out, it meant the opposite (and I corrected it).
Please explain how Drools reasons that statement to find $b
.
Solution
Indeed your are confusing things. "not" means "not exists". So if you want to find the minimum total you can do:
rule "minimum"
when
BucketTotal( $min : total )
not BucketTotal( total < $min )
then
// do something with $min
end
The above is usually the more performant way of doing it, but you can also use accumulate if you prefer:
rule "minimum"
when
accumulate( BucketTotal( $total : total ),
$min : min( $total ) )
then
// do something with $min
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow