wolfgang, <br>I didnt get it, so does that mean that all CE will be re-evaluated on every change of workingmemory no matter which rule fired? even when executing RHS. I didnt test it with copy of list yet. I will do it and see.<br>
<br><div class="gmail_quote">On Sun, Sep 11, 2011 at 11:28 AM, Wolfgang Laun <span dir="ltr">&lt;<a href="mailto:wolfgang.laun@gmail.com">wolfgang.laun@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
The result of the collect CE is changed due to the modify. You have to<br>
copy the list and use the copy in the loop.<br>
-W<br>
<br>
2011/9/9 Sandeep Bandela &lt;<a href="mailto:gibsosmat@gmail.com">gibsosmat@gmail.com</a>&gt;:<br>
<div><div></div><div class="h5">&gt; Thanks wolfgang, for the info and that workaround.<br>
&gt; I think there is some off by one error in for loop, or may be some mistake<br>
&gt; that I didnt notice in my code. the following rule with 3 objects gives<br>
&gt; index out of bound error. I am attaching java &amp; drl file (package<br>
&gt; com.example.drools.testing). I am using 5.2.0 final release.<br>
&gt;<br>
&gt; rule &quot;Buy X units of Product A and Get Y units of Product B Free&quot;<br>
&gt;     when<br>
&gt;         $itemsA : ArrayList( $a : size &gt;= 2 ) from collect( CartItem(<br>
&gt; productId == &quot;A&quot;, processed == false) )<br>
&gt;         $itemsB : ArrayList( $b : size &gt;= 1 ) from collect( CartItem(<br>
&gt; productId == &quot;B&quot;, processed == false) )<br>
&gt;         $m : RuleMessage()<br>
&gt;  then<br>
&gt;          $m.addMessage(&quot;sizes- A=&quot;+$a + &quot; B=&quot;+$b);<br>
&gt;          int $x = 2;<br>
&gt;         int $y = 1;<br>
&gt;         $m.addMessage(&quot;sizes- X=&quot;+$x + &quot; Y=&quot;+$y);<br>
&gt;         for(int i=0 ; i &lt; $x ; i++){<br>
&gt;              CartItem $ci = (CartItem) $itemsA.get(i);<br>
&gt;              $m.addMessage( $ci.getId()+&quot;)Found item: &quot;+$ci.getProductId());<br>
&gt;              modify((CartItem)$ci){<br>
&gt;                 setProcessed(true),<br>
&gt;                 setPromoItemDiscount(0D),<br>
&gt;                 setPromoId(&quot;2&quot;),<br>
&gt;                 setBundleId(bundler.getId()),<br>
&gt;                 setAggregatorId(1)<br>
&gt;             };<br>
&gt;          };<br>
&gt;          for(int i=0 ; i &lt; $y  ; i++){<br>
&gt;          CartItem $ci = (CartItem) $itemsB.get(i);<br>
&gt;              $m.addMessage( $ci.getId()+&quot;)Found item: &quot;+$ci.getProductId());<br>
&gt;              modify((CartItem)$ci){<br>
&gt;                 setProcessed(true),<br>
&gt;                 setPromoItemDiscount($ci.getPrice()),<br>
&gt;                 setPromoId(&quot;2&quot;),<br>
&gt;                 setBundleId(bundler.getId()),<br>
&gt;                 setAggregatorId(2)<br>
&gt;             };<br>
&gt;          };<br>
&gt;         bundler.increment();<br>
&gt; end<br>
&gt;<br>
&gt;<br>
&gt; error<br>
&gt; Exception in thread &quot;main&quot; org.drools.runtime.rule.ConsequenceException:<br>
&gt; rule: Buy X units of Product A and Get Y units of Product B Free<br>
&gt;<br>
&gt;     at<br>
&gt; org.drools.runtime.rule.impl.DefaultConsequenceExceptionHandler.handleException(DefaultConsequenceExceptionHandler.java:39)<br>
&gt;     at<br>
&gt; org.drools.common.DefaultAgenda.fireActivation(DefaultAgenda.java:916)<br>
&gt;     at org.drools.common.DefaultAgenda.fireNextItem(DefaultAgenda.java:845)<br>
&gt;     at org.drools.common.DefaultAgenda.fireAllRules(DefaultAgenda.java:1056)<br>
&gt;     at<br>
&gt; org.drools.common.AbstractWorkingMemory.fireAllRules(AbstractWorkingMemory.java:733)<br>
&gt;     at<br>
&gt; org.drools.common.AbstractWorkingMemory.fireAllRules(AbstractWorkingMemory.java:699)<br>
&gt;     at<br>
&gt; org.drools.impl.StatefulKnowledgeSessionImpl.fireAllRules(StatefulKnowledgeSessionImpl.java:218)<br>
&gt;     at com.example.drools.testing.Main.main(Main.java:52)<br>
&gt; Caused by: java.lang.IndexOutOfBoundsException: Index: 1, Size: 1<br>
&gt;     at java.util.ArrayList.RangeCheck(ArrayList.java:547)<br>
&gt;     at java.util.ArrayList.get(ArrayList.java:322)<br>
&gt;     at<br>
&gt; com.example.drools.testing.Rule_Buy_X_units_of_Product_A_and_Get_Y_units_of_Product_B_Free_0.defaultConsequence(Rule_Buy_X_units_of_Product_A_and_Get_Y_units_of_Product_B_Free_0.java:12)<br>
&gt;     at<br>
&gt; com.example.drools.testing.Rule_Buy_X_units_of_Product_A_and_Get_Y_units_of_Product_B_Free_0DefaultConsequenceInvoker.evaluate(Rule_Buy_X_units_of_Product_A_and_Get_Y_units_of_Product_B_Free_0DefaultConsequenceInvoker.java:44)<br>

&gt;     at<br>
&gt; org.drools.common.DefaultAgenda.fireActivation(DefaultAgenda.java:906)<br>
&gt;     ... 6 more<br>
&gt;<br>
&gt;<br>
&gt; 2011/9/9 Wolfgang Laun &lt;<a href="mailto:wolfgang.laun@gmail.com">wolfgang.laun@gmail.com</a>&gt;<br>
&gt;&gt;<br>
&gt;&gt; If the declared type of productId and getProductId() is String, it should<br>
&gt;&gt; be compared to a string literal. (A conversion might be made, but don&#39;t rely<br>
&gt;&gt; on that.)<br>
&gt;&gt;<br>
&gt;&gt; You cannot access components of a global in RHS code using the simplified<br>
&gt;&gt; notation (not unless you use &quot;mvel&quot; - but don&#39;t do that.) Here, all accesses<br>
&gt;&gt; should be written the usual Java way, with getter and setter calls.<br>
&gt;&gt;<br>
&gt;&gt; The problem you have with modify(ci) is a bug (fixed for 5.3.0). As a<br>
&gt;&gt; workaround,use<br>
&gt;&gt;<br>
&gt;&gt; modify( (CartItem)ci ){ ... }<br>
&gt;&gt;<br>
&gt;&gt; -W<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; 2011/9/9 Sandeep Bandela &lt;<a href="mailto:gibsosmat@gmail.com">gibsosmat@gmail.com</a>&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; some more info for the modify() problem, the CartItem is an interface and<br>
&gt;&gt;&gt; the implementation is CartItemImpl. if this info leads to some clues.<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; On Fri, Sep 9, 2011 at 1:28 PM, Sandeep Bandela &lt;<a href="mailto:gibsosmat@gmail.com">gibsosmat@gmail.com</a>&gt;<br>
&gt;&gt;&gt; wrote:<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; Hi here is the same thing converted to mvl<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; // this Bundler.java has integer member that keeps incrementing<br>
&gt;&gt;&gt;&gt; global Bundler bundler<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; rule &quot;Apply 10% discount if you purchase 2 items&quot;<br>
&gt;&gt;&gt;&gt;     when<br>
&gt;&gt;&gt;&gt;         $s1 : CartItem( productId == 219759, processed == false)<br>
&gt;&gt;&gt;&gt;         $s2 : CartItem( this != $s1, productId == $s1.productId,<br>
&gt;&gt;&gt;&gt; processed == false)<br>
&gt;&gt;&gt;&gt;         $m : RuleMessage()<br>
&gt;&gt;&gt;&gt;  then<br>
&gt;&gt;&gt;&gt;         $m.addMessage(&quot;Found item1: &quot; + $s1.getName() + &quot; id:<br>
&gt;&gt;&gt;&gt; &quot;+$s1.getId());<br>
&gt;&gt;&gt;&gt;         $m.addMessage(&quot;Found item2: &quot; + $s2.getName() + &quot; id:<br>
&gt;&gt;&gt;&gt; &quot;+$s2.getId());<br>
&gt;&gt;&gt;&gt;         modify($s1){<br>
&gt;&gt;&gt;&gt;         setProcessed(true),<br>
&gt;&gt;&gt;&gt;         setPromoItemDiscount($s1.getPrice() * 0.1),<br>
&gt;&gt;&gt;&gt;         setPromoId(&quot;1&quot;),<br>
&gt;&gt;&gt;&gt;         setBundleId(bundler.getId()),<br>
&gt;&gt;&gt;&gt;         setAggrigatorId(1)<br>
&gt;&gt;&gt;&gt;         };<br>
&gt;&gt;&gt;&gt;         modify($s2){<br>
&gt;&gt;&gt;&gt;         setProcessed(true),<br>
&gt;&gt;&gt;&gt;         setPromoItemDiscount($s2.getPrice() * 0.1),<br>
&gt;&gt;&gt;&gt;         setPromoId(&quot;1&quot;),<br>
&gt;&gt;&gt;&gt;         setBundleId(bundler.getId()),<br>
&gt;&gt;&gt;&gt;         setAggrigatorId(1)<br>
&gt;&gt;&gt;&gt;         };<br>
&gt;&gt;&gt;&gt;         $m.addMessage(&quot;bundled [&quot;+bundler.getId()+&quot;]<br>
&gt;&gt;&gt;&gt; &quot;+$s1.getId()+&quot;,&quot;+$s2.getId());<br>
&gt;&gt;&gt;&gt;         bundler.increment();<br>
&gt;&gt;&gt;&gt; end<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; the above rule dosent fire, but if I change the highlighter to<br>
&gt;&gt;&gt;&gt; getProductId() == 219759, or use productId == &quot;219759&quot;, may be this is some<br>
&gt;&gt;&gt;&gt; edge case but it took long time for me to figure it out productId is String.<br>
&gt;&gt;&gt;&gt; now consider the bundler.getId(), if I use <a href="http://bundler.id" target="_blank">bundler.id</a> I get compilation<br>
&gt;&gt;&gt;&gt; error.<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; Rule Compilation error : [Rule name=&#39;Apply 10% discount if you purchase<br>
&gt;&gt;&gt;&gt; 2 items&#39;]<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; com/example/promotions/promotionEngine/Rule_Apply_10__discount_if_you_purchase_2_items_0.java<br>
&gt;&gt;&gt;&gt; (23:1592) : The field Bundler.id is not visible<br>
&gt;&gt;&gt;&gt; 2011-09-09 13:18:39,863  INFO [STDOUT] - 2011-09-09 13:18:39,863  INFO<br>
&gt;&gt;&gt;&gt; [PromotionsManager] - ksession initialized<br>
&gt;&gt;&gt;&gt; 2011-09-09 13:18:39,863  INFO [STDOUT] - 2011-09-09 13:18:39,863  INFO<br>
&gt;&gt;&gt;&gt; [PromotionsManager] - Exploding items and inserting cart<br>
&gt;&gt;&gt;&gt; 2011-09-09 13:18:39,864  INFO [STDOUT] - 2011-09-09 13:18:39,863 ERROR<br>
&gt;&gt;&gt;&gt; [CheckOutBasedUtilities] - Failed to apply promo offers<br>
&gt;&gt;&gt;&gt; java.lang.RuntimeException: Unexpected global [bundler]<br>
&gt;&gt;&gt;&gt;     at<br>
&gt;&gt;&gt;&gt; org.drools.common.AbstractWorkingMemory.setGlobal(AbstractWorkingMemory.java:613)<br>
&gt;&gt;&gt;&gt;     at<br>
&gt;&gt;&gt;&gt; org.drools.impl.StatefulKnowledgeSessionImpl.setGlobal(StatefulKnowledgeSessionImpl.java:332)<br>
&gt;&gt;&gt;&gt;     at<br>
&gt;&gt;&gt;&gt; com.example.promotions.promotionEngine.PromotionsManager.applyOffers(PromotionsManager.java:154)<br>
&gt;&gt;&gt;&gt;     at<br>
&gt;&gt;&gt;&gt; com.example.promotions.promotionEngine.PromotionsManager.shuffleForOffers(PromotionsManager.java:61)`<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; lastly my actual problem that I am looking for a solution lets say we<br>
&gt;&gt;&gt;&gt; have &quot;Buy X items of A and get Y Items of B&quot;. X=5 &amp; Y=4 from the original<br>
&gt;&gt;&gt;&gt; problem I want to template the rule and load the values of X,A,Y,B from<br>
&gt;&gt;&gt;&gt; database or excel etc. if I use modify the error is as follows<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; my rule<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; rule &quot;Buy X units of Product A and Get Y units of Product B Free&quot;<br>
&gt;&gt;&gt;&gt;     when<br>
&gt;&gt;&gt;&gt;         $itemsX : ArrayList( size == 2 ) from collect( CartItem(<br>
&gt;&gt;&gt;&gt; getProductId() == 24257, !isProcessed()))<br>
&gt;&gt;&gt;&gt;         $itemsY : ArrayList( size == 1) from collect(CartItem(<br>
&gt;&gt;&gt;&gt; getProductId() == 24260, !isProcessed()))<br>
&gt;&gt;&gt;&gt;         $m : RuleMessage()<br>
&gt;&gt;&gt;&gt;  then<br>
&gt;&gt;&gt;&gt;          for(Object item : $itemsX){<br>
&gt;&gt;&gt;&gt;              CartItem ci = (CartItem) item;<br>
&gt;&gt;&gt;&gt;              $m.addMessage( ci.getId()+&quot;)Found item: &quot;+ci.getItemId()+&quot;<br>
&gt;&gt;&gt;&gt; name: &quot;+ci.getName());<br>
&gt;&gt;&gt;&gt;              modify(ci){<br>
&gt;&gt;&gt;&gt;                 setProcessed(true),<br>
&gt;&gt;&gt;&gt;                 setPromoItemDiscount(0),<br>
&gt;&gt;&gt;&gt;                 setPromoId(&quot;2&quot;),<br>
&gt;&gt;&gt;&gt;                 setBundleId(bundler.getId()),<br>
&gt;&gt;&gt;&gt;                 setAggrigatorId(1)<br>
&gt;&gt;&gt;&gt;             };<br>
&gt;&gt;&gt;&gt;          }<br>
&gt;&gt;&gt;&gt;          for(Object item : $itemsY){<br>
&gt;&gt;&gt;&gt;          CartItem ci = (CartItem) item;<br>
&gt;&gt;&gt;&gt;              $m.addMessage( ci.getId()+&quot;)Found item: &quot;+ci.getItemId()+&quot;<br>
&gt;&gt;&gt;&gt; name: &quot;+ci.getName());<br>
&gt;&gt;&gt;&gt;              modify(ci){<br>
&gt;&gt;&gt;&gt;                 setProcessed(true),<br>
&gt;&gt;&gt;&gt;                 setPromoItemDiscount(ci.getPrice()),<br>
&gt;&gt;&gt;&gt;                 setPromoId(&quot;2&quot;),<br>
&gt;&gt;&gt;&gt;                 setBundleId(bundler.getId()),<br>
&gt;&gt;&gt;&gt;                 setAggrigatorId(2)<br>
&gt;&gt;&gt;&gt;             };<br>
&gt;&gt;&gt;&gt;          }<br>
&gt;&gt;&gt;&gt;         bundler.increment();<br>
&gt;&gt;&gt;&gt; end<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; Rule Compilation error : [Rule name=&#39;Buy X units of Product A and Get Y<br>
&gt;&gt;&gt;&gt; units of Product B Free&#39;]<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; com/example/promotions/promotionEngine/Rule_Buy_X_units_of_Product_A_and_Get_Y_units_of_Product_B_Free_0.java<br>
&gt;&gt;&gt;&gt; (11:1236) : The method setProcessed(boolean) is undefined for the type<br>
&gt;&gt;&gt;&gt; Object<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; com/example/promotions/promotionEngine/Rule_Buy_X_units_of_Product_A_and_Get_Y_units_of_Product_B_Free_0.java<br>
&gt;&gt;&gt;&gt; (12:1265) : The method setPromoItemDiscount(int) is undefined for the type<br>
&gt;&gt;&gt;&gt; Object<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; com/example/promotions/promotionEngine/Rule_Buy_X_units_of_Product_A_and_Get_Y_units_of_Product_B_Free_0.java<br>
&gt;&gt;&gt;&gt; (13:1299) : The method setPromoId(String) is undefined for the type Object<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; com/example/promotions/promotionEngine/Rule_Buy_X_units_of_Product_A_and_Get_Y_units_of_Product_B_Free_0.java<br>
&gt;&gt;&gt;&gt; (14:1325) : The method setBundleId(Integer) is undefined for the type Object<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; com/example/promotions/promotionEngine/Rule_Buy_X_units_of_Product_A_and_Get_Y_units_of_Product_B_Free_0.java<br>
&gt;&gt;&gt;&gt; (15:1364) : The method setAggrigatorId(int) is undefined for the type Object<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; com/example/promotions/promotionEngine/Rule_Buy_X_units_of_Product_A_and_Get_Y_units_of_Product_B_Free_0.java<br>
&gt;&gt;&gt;&gt; (22:1702) : The method setProcessed(boolean) is undefined for the type<br>
&gt;&gt;&gt;&gt; Object<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; com/example/promotions/promotionEngine/Rule_Buy_X_units_of_Product_A_and_Get_Y_units_of_Product_B_Free_0.java<br>
&gt;&gt;&gt;&gt; (23:1731) : The method setPromoItemDiscount(Double) is undefined for the<br>
&gt;&gt;&gt;&gt; type Object<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; com/example/promotions/promotionEngine/Rule_Buy_X_units_of_Product_A_and_Get_Y_units_of_Product_B_Free_0.java<br>
&gt;&gt;&gt;&gt; (24:1777) : The method setPromoId(String) is undefined for the type Object<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; com/example/promotions/promotionEngine/Rule_Buy_X_units_of_Product_A_and_Get_Y_units_of_Product_B_Free_0.java<br>
&gt;&gt;&gt;&gt; (25:1803) : The method setBundleId(Integer) is undefined for the type Object<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; com/example/promotions/promotionEngine/Rule_Buy_X_units_of_Product_A_and_Get_Y_units_of_Product_B_Free_0.java<br>
&gt;&gt;&gt;&gt; (26:1842) : The method setAggrigatorId(int) is undefined for the type Object<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; Rule Compilation error : [Rule name=&#39;Apply 10% discount if you purchase<br>
&gt;&gt;&gt;&gt; 2 items&#39;]<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; com/example/promotions/promotionEngine/Rule_Apply_10__discount_if_you_purchase_2_items_0.java<br>
&gt;&gt;&gt;&gt; (23:1592) : The field Bundler.id is not visible<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; On Fri, Sep 9, 2011 at 12:40 PM, Sandeep Bandela &lt;<a href="mailto:gibsosmat@gmail.com">gibsosmat@gmail.com</a>&gt;<br>
&gt;&gt;&gt;&gt; wrote:<br>
&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt; Hi Wolfgang,<br>
&gt;&gt;&gt;&gt;&gt; This is what I did initially but for product &gt; 2 I didnt find a generic<br>
&gt;&gt;&gt;&gt;&gt; way to do this. following is my first rule. I didnt use field names because<br>
&gt;&gt;&gt;&gt;&gt; it was giving error that member is not accessible. I will attach the errors<br>
&gt;&gt;&gt;&gt;&gt; of each case in a few mins.<br>
&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt; rule &quot;Apply 10% discount if you purchase 2 items&quot;<br>
&gt;&gt;&gt;&gt;&gt;     dialect &quot;java&quot;<br>
&gt;&gt;&gt;&gt;&gt;     when<br>
&gt;&gt;&gt;&gt;&gt;         $s1 : CartItem( getProductId() == 236061, !isProcessed())<br>
&gt;&gt;&gt;&gt;&gt;         $s2 : CartItem( getProductId() == 236061, getId() !=<br>
&gt;&gt;&gt;&gt;&gt; $s1.getId(), !isProcessed())<br>
&gt;&gt;&gt;&gt;&gt;         $m : RuleMessage()<br>
&gt;&gt;&gt;&gt;&gt;  then<br>
&gt;&gt;&gt;&gt;&gt;         $m.addMessage(&quot;Found item1: &quot; + $s1.getName() + &quot; id:<br>
&gt;&gt;&gt;&gt;&gt; &quot;+$s1.getId());<br>
&gt;&gt;&gt;&gt;&gt;         $m.addMessage(&quot;Found item2: &quot; + $s2.getName() + &quot; id:<br>
&gt;&gt;&gt;&gt;&gt; &quot;+$s2.getId());<br>
&gt;&gt;&gt;&gt;&gt;         modify($s1){<br>
&gt;&gt;&gt;&gt;&gt;         setProcessed(true),<br>
&gt;&gt;&gt;&gt;&gt;         setPromoItemDiscount($s1.getPrice() * 0.1),<br>
&gt;&gt;&gt;&gt;&gt;         setPromoId(&quot;1&quot;),<br>
&gt;&gt;&gt;&gt;&gt;         setBundleId(bundler.getId()),<br>
&gt;&gt;&gt;&gt;&gt;         setAggrigatorId(1)<br>
&gt;&gt;&gt;&gt;&gt;         };<br>
&gt;&gt;&gt;&gt;&gt;         modify($s2){<br>
&gt;&gt;&gt;&gt;&gt;         setProcessed(true),<br>
&gt;&gt;&gt;&gt;&gt;         setPromoItemDiscount($s2.getPrice() * 0.1),<br>
&gt;&gt;&gt;&gt;&gt;         setPromoId(&quot;1&quot;),<br>
&gt;&gt;&gt;&gt;&gt;         setBundleId(bundler.getId()),<br>
&gt;&gt;&gt;&gt;&gt;         setAggrigatorId(1)<br>
&gt;&gt;&gt;&gt;&gt;         };<br>
&gt;&gt;&gt;&gt;&gt;         bundler.increment();<br>
&gt;&gt;&gt;&gt;&gt; end<br>
&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt; 2011/9/9 Wolfgang Laun &lt;<a href="mailto:wolfgang.laun@gmail.com">wolfgang.laun@gmail.com</a>&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt; Your rule exhibits procedural thinking. (For an aspiring rule<br>
&gt;&gt;&gt;&gt;&gt;&gt; programmer his is typical teething troubles.)<br>
&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt; There&#39;s no need to collect all A&#39;s and B&#39;s in order to determine their<br>
&gt;&gt;&gt;&gt;&gt;&gt; number being greater than one or two - this is achieved automatically by a<br>
&gt;&gt;&gt;&gt;&gt;&gt; simple triple match rule. Consider<br>
&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt; rule &quot;2A - 1B&quot;<br>
&gt;&gt;&gt;&gt;&gt;&gt; when<br>
&gt;&gt;&gt;&gt;&gt;&gt;    $a1: CartItem( productId == &quot;A&quot;, processed == false )<br>
&gt;&gt;&gt;&gt;&gt;&gt;    $a2: CartItem( this != $a1, productId == &quot;A&quot;, processed == false )<br>
&gt;&gt;&gt;&gt;&gt;&gt;    $b: CartItem( productId == &quot;B&quot;, processed == false )<br>
&gt;&gt;&gt;&gt;&gt;&gt; then<br>
&gt;&gt;&gt;&gt;&gt;&gt;    modify( $a1 ){...}<br>
&gt;&gt;&gt;&gt;&gt;&gt;    modify( $a2 ){...}<br>
&gt;&gt;&gt;&gt;&gt;&gt;    modify( $b ){...}<br>
&gt;&gt;&gt;&gt;&gt;&gt; end<br>
&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt; Note:<br>
&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt; The modify is essential. (When you say &quot;doesn&#39;t work&quot;, always provide<br>
&gt;&gt;&gt;&gt;&gt;&gt; details.)<br>
&gt;&gt;&gt;&gt;&gt;&gt; Use the field name rather than the getter call - it improves<br>
&gt;&gt;&gt;&gt;&gt;&gt; readability.<br>
&gt;&gt;&gt;&gt;&gt;&gt; I&#39;m not sure whether 5.2.0 final handles boolean fields correctly -<br>
&gt;&gt;&gt;&gt;&gt;&gt; IIRC, there were some issues. It&#39;s possible that CartItem(..., !processed)<br>
&gt;&gt;&gt;&gt;&gt;&gt; works.<br>
&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt; -W<br>
&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt; 2011/9/8 Sandeep Bandela &lt;<a href="mailto:gibsosmat@gmail.com">gibsosmat@gmail.com</a>&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; Hi,<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; I am new to rules engine, after going through the examples shipped<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; (shopping.drl, petstore.drl)<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; I am trying to implement promotional discounting using drools 5.2.0<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; final, stateful session. assume cart has all the items that customer wants<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; to buy,<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; I am trying to bundle them together with the existing offers<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; rule &quot;Buy X units of Product A and Get Y units of Product B Free&quot;<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;     dialect &quot;java&quot;<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; when<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;     $itemsA : ArrayList( size &gt;= 2) from collect( CartItem(<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; getProductId() == &quot;A&quot;, !isProcessed()))<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;     $itemsB : ArrayList( size &gt;= 1) from collect( CartItem(<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; getProductId() == &quot;B&quot;, !isProcessed()))<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; then<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;     // current scenario buy 2*A and get 1*B<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;     int x = 2;<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;     int y = 1;<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;     for(int i=0 ; i &lt; x ; i++){<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;         CartItem $ci = (CartItem) $itemsA.get(i);<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;         // modify($ci){setProcessed(true) ... } dosent work<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;         $ci.setProcessed(true);<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;         $ci.setItemDiscount(0.0);<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;         $ci.setBundleId(bundler.getId());<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;     }<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;     for(int i=0 ; i &lt; y  ; i++){<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;         CartItem $ci = (CartItem) $itemsB.get(i);<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;         $ci.setProcessed(true);<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;         $ci.setItemDiscount($ci.getPrice());<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;         $ci.setBundleId(bundler.getId());<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;     }<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;     // global counter to identify bundled items<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;     bundler.increment();<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; end<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; the above rule calculates only for 1 set of offer i.e first 2*A &amp; 1*B<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; are considered, rest in the cart are not bundled.<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; if customer buys 4*A + 2*B it dosent consider it. am I missing<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; anything? is this the right way to do it?<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; any feedback is appreciated.<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; --<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; Regards,<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; Sandeep Bandela.<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; _______________________________________________<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; rules-users mailing list<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; <a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; <a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt; _______________________________________________<br>
&gt;&gt;&gt;&gt;&gt;&gt; rules-users mailing list<br>
&gt;&gt;&gt;&gt;&gt;&gt; <a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
&gt;&gt;&gt;&gt;&gt;&gt; <a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt; --<br>
&gt;&gt;&gt;&gt;&gt; Regards,<br>
&gt;&gt;&gt;&gt;&gt; Sandeep Bandela.<br>
&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; --<br>
&gt;&gt;&gt;&gt; Regards,<br>
&gt;&gt;&gt;&gt; Sandeep Bandela.<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; --<br>
&gt;&gt;&gt; Regards,<br>
&gt;&gt;&gt; Sandeep Bandela.<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; _______________________________________________<br>
&gt;&gt;&gt; rules-users mailing list<br>
&gt;&gt;&gt; <a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
&gt;&gt;&gt; <a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
&gt;&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; _______________________________________________<br>
&gt;&gt; rules-users mailing list<br>
&gt;&gt; <a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
&gt;&gt; <a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
&gt;&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; --<br>
&gt; Regards,<br>
&gt; Sandeep Bandela.<br>
&gt;<br>
&gt;<br>
&gt; _______________________________________________<br>
&gt; rules-users mailing list<br>
&gt; <a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
&gt; <a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
&gt;<br>
&gt;<br>
<br>
_______________________________________________<br>
rules-users mailing list<br>
<a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br>Regards,<br>Sandeep Bandela.<br><br>