<html><head><style type="text/css"><!-- DIV {margin:0px;} --></style></head><body><div style="font-family:tahoma,new york,times,serif;font-size:10pt"><div>Very clean.<br><br>Earnie!<br></div><div style="font-family: tahoma,new york,times,serif; font-size: 10pt;"><br><div style="font-family: times new roman,new york,times,serif; font-size: 12pt;"><font size="2" face="Tahoma"><hr size="1"><b><span style="font-weight: bold;">From:</span></b> Steve Ronderos &lt;steve.ronderos@ni.com&gt;<br><b><span style="font-weight: bold;">To:</span></b> Rules Users List &lt;rules-users@lists.jboss.org&gt;<br><b><span style="font-weight: bold;">Sent:</span></b> Wed, July 21, 2010 9:56:56 AM<br><b><span style="font-weight: bold;">Subject:</span></b> Re: [rules-users] collection question<br></font><br><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 "AddBonusCd"<br>
<br>
 &nbsp;when<br>
 &nbsp; &nbsp;$so : ShippingOrder()<br>
 &nbsp; &nbsp;Product(name == "cd1") from $so.products<br>
 &nbsp; &nbsp;not (Product(name == "bonus_cd") from $so.products)<br>
 &nbsp;then<br>
 &nbsp; &nbsp;System.out.println("Adding Bonus Cd");<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("bonus_cd");<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 "evil twin" 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 == "cd1" )<br>
&gt; &nbsp; &nbsp;$bonus_cd : Product( name == "bonus_cd" )<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; "bonus_cd" 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; "cd1" is in the shipping order, a bonus product named
"bonus_cd" 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 "AddBonusCd"<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; "cd1|bonus_cd") 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("Adding
Bonus Cd");<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("bonus_cd");<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 rel="nofollow" target="_blank" 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 rel="nofollow" target="_blank" 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 rel="nofollow" target="_blank" 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></div></div>
</div></body></html>