Hi,

 

I ran into the situation when I need the functionality similar to ‘distinct’ keyword from SQL, ie I have a set of facts:

 

Record (f1 = 1, f2 = 1, value = 4)

Record (f1 = 1, f2 = 2, value = 1)

Record (f1 = 1, f2 = 13, value = 2)

Record (f1 = 1, f2 = 14, value = 5)

Record (f1 = 2, f2 = 5, value = 7)

Record (f1 = 2, f2 = 6, value = 3)

Record (f1 = 2, f2 = 15, value = 8)

Record (f1 = 2, f2 = 16, value = 9)

.. etc

 

And I need to calculate two sums _within the same ‘f1’ field_ for each unique ‘f1’ field and compare them:

-          $sum1: f2 <   10

-          $sum2: f2 >= 10

-          If $sum1 > sum2 then do something

 

One of the ways to do that is to iterate through all unique f1 fields and calculate two sums. Here is where I thought ‘distinct’ keyword might have helped. Otherwise I need to assert another set of ‘unique f1 fields’ facts to iterate through:

 

Rule XXX

            When

                        Distinct Record ( $f1 : f1 )

                        Integer ( intValue > 0) from accumulate (

                                    Record ( f1 == $f1, f2 < 10, $value1 : value )

                                    Record ( f1 == $f1, f2 >= 10, $value2 : value )

                                    Init ( int sum = 0; ),

                                    Action ( sum += $value1 - $value2 ),

                                    Result ( new Integer (sum) )

                        )

            Then

                        System.out.println(“HURRAH!”);

End

 

I realize that one would not be able to bind any other variable from ‘distincted’ record other than those in the constraint fields, but this is not really required….. The only other solution for the problem above I managed to come up with is to accumulate the sums in a helper object, which has the buckets for each unique f1 value and do the eval after ‘accumulate on testing if any of the buckets is a positive number. In other words, not to iterate through each unique ‘f1’, but iterate through all records and put the sum to the correct bucket: Helper.getBucket($f1).addValues($value1, value2).

 

The other thing is what Edson mentioned some time ago: DROOLS does not allow internally for the same fact to be selected twice in a tuple. Surprisingly one of the recent tests I ran against 3.1M did actually select the same fact in the tuple (see the post from last Friday, March 02: “confused again.... - why it selects all possible combinations”). Not sure if it’s a bug or an extended functionality… but this kind of thing should be helpful in here….

 

Are there any other approaches I missed?... What’s the best one in terms of performance? Thanks,

 

Vlad