<div>Dear Drools users,</div><div><br></div><div>I&#39;m new to Drools but loving it so far. I have a question about accumulate and window:lengt:</div><div><br></div><div>What I want: &quot;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&quot;</div>
<div><br></div><div>How I solved it now, it&#39;s not working as I want it:</div><div>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&#39;t actually want to insert this product object, but I don&#39;t know how else to group on product otherwise. Any help on this part is greatly appreciated.</div>
<div><br></div><div>The second problem is that the window doesn&#39;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?</div>
<div><br></div><div>(I can&#39;t share our source code, so this might not compile)</div><div><br></div><div>declare Product</div><div>    productId : String;</div><div>end</div><div><br></div><div>delcare Order</div><div>
    @role(event)</div><div>    productId: String;</div><div>    price: int;</div><div>end</div><div><br></div><div>rule &quot;Turn purchase into order&quot;</div><div>when</div><div>    p : Purchase()</div><div>then</div>
<div>    insert ( new Product(p.productId) )</div><div>    insert ( new Order(p.productId, p.price)</div><div><br></div><div>rule &quot;Count average price&quot;</div><div>when</div><div>    $product : Product ()</div><div>
    accumulate(</div><div>        <span class="Apple-tab-span" style="white-space:pre">        </span>Order(productId == $product.productId, $pr : price ) over window:length(200);</div><div>        <span class="Apple-tab-span" style="white-space:pre">        </span>$avg : average( $pr ),</div>
<div>        <span class="Apple-tab-span" style="white-space:pre">        </span>$count: count($pr);</div><div>        <span class="Apple-tab-span" style="white-space:pre">        </span>$count &gt; 10, $avg &lt; 10)</div><div>then</div>
<div>    &quot;The average price for product $product.productId is $avg&quot;</div><div><br></div>