<div>Hi guys, </div><div><br></div><div>I&#39;m new to drools, and i&#39;m trying to build a small prototype. </div><div>I have 2 entities in my model: Offers and Bids. Both have a size (and a price). I need a rule that matches a single Bid with multiple Offers adding up in size to the Bid size. Matched Offers have to be sorted by (increasing) insertion time . </div>

<meta http-equiv="content-type" content="text/html; charset=utf-8"><div>I have managed to write a rule using &#39;accumulate&#39;, but i feel it&#39;s not too efficient, since i have to match every Offer in the working memory with the right price, sort them and then pick the first n ones. </div>

<div><br></div><div>Here&#39;s the rule:</div><div><br></div><div>rule &quot;Match Bids&quot;</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>when</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>$b : Bid($s:size, $p:price)</div>

<div><span class="Apple-tab-span" style="white-space:pre">                </span>$i : OfferAccumulator(acumSize &gt;= $s) from accumulate ( $o:Offer(price&lt;=$p) , </div><div><span class="Apple-tab-span" style="white-space:pre">                        </span>init(OfferAccumulator accum = new OfferAccumulator($s); ),</div>

<div><span class="Apple-tab-span" style="white-space:pre">                        </span>action( accum.add($o);),</div><div><span class="Apple-tab-span" style="white-space:pre">                        </span>reverse ( accum.remove($o);),</div><div><span class="Apple-tab-span" style="white-space:pre">                        </span>result( accum ))</div>

<div><span class="Apple-tab-span" style="white-space:pre">                </span></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>then</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>System.out.println( &quot;Matched Bid:&quot; + $b + &quot;,&quot; + $i.getOffers());</div>

<div>end</div><div><br></div><div>and here&#39;s OfferAccumulator:</div><div><br></div><div>public class OfferAccumulator {</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>private int sizeLimit=0;</div>

<div><span class="Apple-tab-span" style="white-space:pre">        </span>private int accumSize=0;</div><div><span class="Apple-tab-span" style="white-space:pre">        </span></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>Set&lt;Offer&gt; offers = new TreeSet&lt;Offer&gt;();</div>

<div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>public OfferAccumulator(int sizeLimit) {</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>this.sizeLimit = sizeLimit;</div>

<div><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>public void add(Offer op) {</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>accumSize+=op.getSize();</div>

<div><span class="Apple-tab-span" style="white-space:pre">                </span>offers.add(op);</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>public void remove(Offer op) {</div>

<div><span class="Apple-tab-span" style="white-space:pre">                </span>if (offers.remove(op)) {</div><div><span class="Apple-tab-span" style="white-space:pre">                        </span>accumSize-=op.getSize();</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>}</div>

<div><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>public List&lt;Offer&gt; getOffers() {</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>List&lt;Offer&gt; ret = new ArrayList&lt;Offer&gt;();</div>

<div><span class="Apple-tab-span" style="white-space:pre">                </span>int accum=0;</div><div><span class="Apple-tab-span" style="white-space:pre">                </span></div><div><span class="Apple-tab-span" style="white-space:pre">                </span>Iterator&lt;Offer&gt; it = offers.iterator();</div>

<div><span class="Apple-tab-span" style="white-space:pre">                </span>while (it.hasNext() &amp;&amp; acum&lt;sizeLimit) {</div><div><span class="Apple-tab-span" style="white-space:pre">                        </span>Offer offer = it.next();</div>

<div><span class="Apple-tab-span" style="white-space:pre">                        </span>accum+=offer.getSize();</div><div><span class="Apple-tab-span" style="white-space:pre">                        </span>ret.add(offer);</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>}</div>

<div><span class="Apple-tab-span" style="white-space:pre">                </span>return ret;</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div>}</div><div><br></div><div>Can you think of a more efficient way of achieving the same result? Is there a way of controlling the order in which facts are feeded to the accumulator?</div>

<div>Thanks in advance!</div><div>     Andres</div>