<font size=2 face="sans-serif">Hi J,</font>
<br>
<br><font size=2 face="sans-serif">In your example I don't think that the
collect is necessary.</font>
<br>
<br><font size=2 face="sans-serif">I believe the following will work:</font>
<br>
<br><tt><font size=2>rule &quot;AddBonusCd&quot;<br>
<br>
 &nbsp;when<br>
 &nbsp; &nbsp;$so : ShippingOrder()<br>
 &nbsp; &nbsp;Product(name == &quot;cd1&quot;) from $so.products<br>
 &nbsp; &nbsp;not (Product(name == &quot;bonus_cd&quot;) from $so.products)<br>
 &nbsp;then<br>
 &nbsp; &nbsp;System.out.println(&quot;Adding Bonus Cd&quot;);<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>
 &nbsp; &nbsp;Set&lt;Product&gt; productSet = new HashSet&lt;Product&gt;($so.getProducts());<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>
 &nbsp; &nbsp;Product bonusCd = new Product();<br>
 &nbsp; &nbsp;bonusCd.setName(&quot;bonus_cd&quot;);<br>
 &nbsp; &nbsp;productSet.add(bonusCd);<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>
 &nbsp; &nbsp;modify($so) {<br>
 &nbsp; &nbsp; &nbsp;setProducts(productSet);<br>
 &nbsp; &nbsp;}<br>
end</font></tt>
<br>
<br><font size=2 face="sans-serif">You don't need the collects because
you are just checking for existence/non-existence of facts within the products
collection.</font>
<br>
<br><font size=2 face="sans-serif">Good luck!</font>
<br><font size=2 face="sans-serif">Steve</font>
<br>
<br>
<br><tt><font size=2>rules-users-bounces@lists.jboss.org wrote on 07/21/2010
01:59:40 AM:<br>
<br>
&gt; From:</font></tt>
<br><tt><font size=2>&gt; <br>
&gt; Wolfgang Laun &lt;wolfgang.laun@gmail.com&gt;</font></tt>
<br><tt><font size=2>&gt; <br>
&gt; To:</font></tt>
<br><tt><font size=2>&gt; <br>
&gt; Rules Users List &lt;rules-users@lists.jboss.org&gt;</font></tt>
<br><tt><font size=2>&gt; <br>
&gt; Date:</font></tt>
<br><tt><font size=2>&gt; <br>
&gt; 07/21/2010 02:02 AM</font></tt>
<br><tt><font size=2>&gt; <br>
&gt; Subject:</font></tt>
<br><tt><font size=2>&gt; <br>
&gt; Re: [rules-users] collection question</font></tt>
<br><tt><font size=2>&gt; <br>
&gt; Sent by:</font></tt>
<br><tt><font size=2>&gt; <br>
&gt; rules-users-bounces@lists.jboss.org</font></tt>
<br><tt><font size=2>&gt; <br>
&gt; Things would be smpler if you could avoid re-creating Product objects<br>
&gt; because adding an identical Object does not change the Set. (This<br>
&gt; could be implemented with the help of a global Map&lt;String,Product&gt;,)<br>
&gt; <br>
&gt; Second, overriding equals (and hashCode) in Product would also avoid
adding<br>
&gt; an &quot;evil twin&quot; in your rule code - as it is!<br>
&gt; <br>
&gt; Even simpler is a solution where the relevant Product objects are
inserted<br>
&gt; as facts, so that<br>
&gt; &nbsp; &nbsp;$cd1 : Product( name == &quot;cd1&quot; )<br>
&gt; &nbsp; &nbsp;$bonus_cd : Product( name == &quot;bonus_cd&quot; )<br>
&gt; &nbsp; &nbsp;$order : ShippingOrder( products contains $cd1, products
not<br>
&gt; contains $bonus_cd )<br>
&gt; is true (again, with equals and hashCode being overridden).<br>
&gt; <br>
&gt; Given your solution with collect, an additional eval testing for<br>
&gt; &quot;bonus_cd&quot; not being the<br>
&gt; name of the single Product in List $productList might be more<br>
&gt; efficient than iterating<br>
&gt; all products a second time.<br>
&gt; <br>
&gt; -W<br>
&gt; <br>
&gt; On 21 July 2010 00:41, javaj &lt;jmilliro@ameritech.net&gt; wrote:<br>
&gt; &gt;<br>
&gt; &gt; I'm trying to write a rule to add an object to a collection if
thecollection<br>
&gt; &gt; already contained a certain object with a specific attribute.<br>
&gt; &gt;<br>
&gt; &gt; I developed a small example so maybe that will make more sense:<br>
&gt; &gt;<br>
&gt; &gt; Use Case: In a product shipping applicaiton, anytime a product
with the name<br>
&gt; &gt; &quot;cd1&quot; is in the shipping order, a bonus product named
&quot;bonus_cd&quot; needs to be<br>
&gt; &gt; added to the shipping order. &nbsp;The bonus cd should not be
added if it's<br>
&gt; &gt; already in the shipping order.<br>
&gt; &gt;<br>
&gt; &gt; Shipping Order:<br>
&gt; &gt;<br>
&gt; &gt; public class ShippingOrder {<br>
&gt; &gt;<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp;private Set&lt;Product&gt; products;<br>
&gt; &gt;<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp;public ShippingOrder() {}<br>
&gt; &gt;<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp;public Set&lt;Product&gt; getProducts()
{<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return
products;<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp;}<br>
&gt; &gt;<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp;public void setProducts(Set&lt;Product&gt;
products) {<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this.products
= products;<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp;}<br>
&gt; &gt;<br>
&gt; &gt; }<br>
&gt; &gt;<br>
&gt; &gt; Product:<br>
&gt; &gt;<br>
&gt; &gt; public class Product {<br>
&gt; &gt;<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp;private String name;<br>
&gt; &gt;<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp;public Product(){}<br>
&gt; &gt;<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp;public String getName() {<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return
name;<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp;}<br>
&gt; &gt;<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp;public void setName(String name) {<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this.name
= name;<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp;}<br>
&gt; &gt;<br>
&gt; &gt; }<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt; Rule:<br>
&gt; &gt;<br>
&gt; &gt; rule &quot;AddBonusCd&quot;<br>
&gt; &gt;<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp;when<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$so :
ShippingOrder()<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$productList
: ArrayList(size == 1) from collect( <br>
&gt; Product(name matches<br>
&gt; &gt; &quot;cd1|bonus_cd&quot;) from $so.products)<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp;then<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(&quot;Adding
Bonus Cd&quot;);<br>
&gt; &gt;<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Set&lt;Product&gt;
productSet = new HashSet&lt;Product&gt;<br>
&gt; ($so.getProducts());<br>
&gt; &gt;<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Product
bonusCd = new Product();<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;bonusCd.setName(&quot;bonus_cd&quot;);<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;productSet.add(bonusCd);<br>
&gt; &gt;<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;modify($so)
{<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp;setProducts(productSet);<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br>
&gt; &gt; end<br>
&gt; &gt;<br>
&gt; &gt; The Shipping Order object is inserted as the fact.<br>
&gt; &gt;<br>
&gt; &gt; The rule is valid if only cd1 or cd_bonus is in the shipping
order. &nbsp;I only<br>
&gt; &gt; want the rule to run when cd1 is in the product set but cd_bonus
is not.<br>
&gt; &gt; Makes sense?<br>
&gt; &gt;<br>
&gt; &gt; Thanks,<br>
&gt; &gt; J<br>
&gt; &gt; --<br>
&gt; &gt; View this message in context: </font></tt><a href="http://drools-java-rules-engine/"><tt><font size=2>http://drools-java-rules-engine</font></tt></a><tt><font size=2>.<br>
&gt; 46999.n3.nabble.com/collection-question-tp982769p982769.html<br>
&gt; &gt; Sent from the Drools - User mailing list archive at Nabble.com.<br>
&gt; &gt; _______________________________________________<br>
&gt; &gt; rules-users mailing list<br>
&gt; &gt; rules-users@lists.jboss.org<br>
&gt; &gt; </font></tt><a href="https://lists.jboss.org/mailman/listinfo/rules-users"><tt><font size=2>https://lists.jboss.org/mailman/listinfo/rules-users</font></tt></a><tt><font size=2><br>
&gt; &gt;<br>
&gt; <br>
&gt; _______________________________________________<br>
&gt; rules-users mailing list<br>
&gt; rules-users@lists.jboss.org<br>
&gt; </font></tt><a href="https://lists.jboss.org/mailman/listinfo/rules-users"><tt><font size=2>https://lists.jboss.org/mailman/listinfo/rules-users</font></tt></a><tt><font size=2><br>
</font></tt>