How to use collect() in drools in this scenario

1.7k views Asked by At

I am trying to do something like below to collect a ecrtain type of objects fullfilling certain conditions.

rule "collect_other"
$lineItem2 : A( $iVal : iValue, $qd2 : quantity) 
B($bVal : bId)
$rowLowerOther : C(iValue == $iVal, bId == $bVal,
                   $lsequence : sequence, $jValue : jAvlDate)
$rowHigherOther : List() 
    from collect(C(iValue == $iVal, bId == $bVal,
                   sequence == $lsequence-1, jAvlDate != $jValue))

then
    //do something
end

Right Now I am getting one object only even though I am having numbers of object fullfilliong the condition.

Note: I want to collect all items fullfilling conditions by taking the value from first object instance of "C"

Please help me.

1

There are 1 answers

3
laune On BEST ANSWER

Objects of class C are:

C(101, "BU1", 1,"2014-11-23");
C(101, "BU1", 2,"2014-11-24");
C(101, "BU1", 3,"2014-11-25");
C(101, "BU1", 4,"2014-11-26")

Let's assume that we have

A(101,...)
B("BU1")

Then the pattern

C(iValue == $iVal, bId == $bVal,$lsequence : sequence,...)

matches any of these C objects, binding $lsequence to 1, 2, 3 and so on. For the first binding, the rule doesn't fire, because no C fact matches sequence == 1-1

For each of the values 2, 3, 4 and so on, the rule will match and collect exactly on C fact into the list, i.e., the one with seuqnce == 1, 2, 3,... respectively.

Perhaps this does what you want - but I'm only guessing.

rule "collect_other"
when
  $lineItem2: A( $iVal: iValue, $qd2 : quantity ) 
              B( $bVal : bId )
  $rowLowerOther: C(iValue == $iVal, bId == $bVal,
                    $lsequence : sequence, $jValue: jAvlDate)
  not C( iValue == $iVal, bId == $bVal, sequence < $lsequence )
  $rowHigherOther : List() 
     from collect(C(iValue == $iVal, bId == $bVal, jAvlDate != $jValue))
then
  //...
end