Comparing distinct items from collection in drools 6 when clause

447 views Asked by At

I've searched around the internet but could not find an solution for a problem we are currently facing with drools (6.2.0).

Suppose i have a rule like this:

when
    $list: ProductList()
    $product: Product() from $list
    $product2: Product(this != product) from $list
then
    // do something
end

if $list contains 2 products, A and B, this rule will fire for combinations:

  1. A-B
  2. B-A

For some reason I am not able to make the rule fire only once (either ONLY A-B or ONLY B-A)

Does anybody know if there is a standard way to achieve the desired result?

Many Thanks in advance!

Regards,

Kim

1

There are 1 answers

0
laune On

You need to have a Product attribute that is comparable and unique. I'm assuming a product number:

rule comp2prod
when
  $list: ProductList()
  $product: Product( $pn: productNumber ) from $list
  $product2: Product( productNumber > $pn ) from $list
then
  // do something
end