[rules-users] Average over window

Machiel Groeneveld machielg at gmail.com
Mon Jul 16 12:09:04 EDT 2012


Dear Drools users,

I'm new to Drools but loving it so far. I have a question about accumulate
and window:lengt:

What I want: "Display the average order value considering the last 200
orders for one type of product. I want have at least 10 orders for one
product before drawing any conclusions"

How I solved it now, it's not working as I want it:
I now insert product objects to be able to group the orders per product.
The first problem is that the list of products is not unique but every
insert creates a new product object (even though the product object has
already been inserted for that productId), so the second rule is triggered
way too often. I don't actually want to insert this product object, but I
don't know how else to group on product otherwise. Any help on this part is
greatly appreciated.

The second problem is that the window doesn't seem to use all the orders
matching the product, it just limits the rule to use the last 200 orders,
not the last 200 orders for that product. Is there a way to make this
smarter?

(I can't share our source code, so this might not compile)

declare Product
    productId : String;
end

delcare Order
    @role(event)
    productId: String;
    price: int;
end

rule "Turn purchase into order"
when
    p : Purchase()
then
    insert ( new Product(p.productId) )
    insert ( new Order(p.productId, p.price)

rule "Count average price"
when
    $product : Product ()
    accumulate(
        Order(productId == $product.productId, $pr : price ) over
window:length(200);
        $avg : average( $pr ),
        $count: count($pr);
        $count > 10, $avg < 10)
then
    "The average price for product $product.productId is $avg"
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20120716/28c3d446/attachment.html 


More information about the rules-users mailing list