"Group by" is implicit in rules, just by writing patterns in order. So, if you write a rule that starts with Customer(), it will automatically do a "group by" customer:
when
Customer(...)
...
then
"Distinct" is a bit problematic, as we have no explicit distinct functionality (I have an open ticket for that:
https://jira.jboss.org/browse/JBRULES-2640) but in some cases you can use collectSet for similar results.
In your case, you can go with Wolfgang's suggestion, or using accumulate:
declare ReportForDate
custNo : String
date : Date
end
rule "insert report dates"
salience 10
dialect "mvel"
when
Customer( $pd : purchaseDate, $custNo : custNo )
not( ReportForDate( custNo == $custNo, date == $pd ) )
then
with( $r = new ReportDate() ) {
custNo = $custNo,
date == $pd
}
insert( $r );
end
rule "customers that purchased more than 100 on a given date"
when
ReportDate( $pd: date, $cn : custNo )
Number( $total : doubleValue > 100 ) from accumulate(
Customer( $custNo == $cn, purchaseDate == $pd, $am : amount ),
sum( $am ) )
then
// do something as the customer $cn purchased $total on $pd
end
Edson
--
Edson Tirelli
JBoss Drools Core Development
JBoss by Red Hat @
www.jboss.com