<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>Would the following accomplish your goal?<br><br>Earnie!<br><br>import java.util.*;<br>import com.sample.Product;<br>import com.sample.ShippingOrder;<br><br>rule "AddBonusCd"<br>&nbsp;&nbsp;&nbsp; when<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $so : ShippingOrder()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ArrayList(size == 1) from collect( Product(name matches<br>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "cd1") from $so.products)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ArrayList(size == 0) from collect( Product(name matches<br>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "bonus_cd") from $so.products)<br>&nbsp;&nbsp;&nbsp;
 then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("Adding Bonus Cd ");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Set&lt;Product&gt; productSet = new HashSet&lt;Product&gt;($so.getProducts());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Product bonusCd = new Product();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bonusCd.setName("bonus_cd");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; productSet.add(bonusCd);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; modify($so) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; setProducts(productSet);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>end<br></div><div style="font-family: tahoma,new york,times,serif; font-size: 10pt;"><br><div style="font-family: arial,helvetica,sans-serif; font-size: 13px;"><font size="2" face="Tahoma"><hr
 size="1"><b><span style="font-weight: bold;">From:</span></b> javaj &lt;jmilliro@ameritech.net&gt;<br><b><span style="font-weight: bold;">To:</span></b> rules-users@lists.jboss.org<br><b><span style="font-weight: bold;">Sent:</span></b> Tue, July 20, 2010 6:41:25 PM<br><b><span style="font-weight: bold;">Subject:</span></b> [rules-users] collection question<br></font><br>
<br>I'm trying to write a rule to add an object to a collection if the collection<br>already contained a certain object with a specific attribute.<br><br>I developed a small example so maybe that will make more sense:<br><br>Use Case: In a product shipping applicaiton, anytime a product with the name<br>"cd1" is in the shipping order, a bonus product named "bonus_cd" needs to be<br>added to the shipping order.&nbsp; The bonus cd should not be added if it's<br>already in the shipping order.<br><br>Shipping Order:<br><br>public class ShippingOrder {<br><br>&nbsp;&nbsp;&nbsp; private Set&lt;Product&gt; products;<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; public ShippingOrder() {}<br><br>&nbsp;&nbsp;&nbsp; public Set&lt;Product&gt; getProducts() {<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return products;<br>&nbsp;&nbsp;&nbsp; }<br><br>&nbsp;&nbsp;&nbsp; public void setProducts(Set&lt;Product&gt; products) {<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
 this.products = products;<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>}<br><br>Product:<br><br>public class Product {<br><br>&nbsp;&nbsp;&nbsp; private String name;<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; public Product(){}<br><br>&nbsp;&nbsp;&nbsp; public String getName() {<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return name;<br>&nbsp;&nbsp;&nbsp; }<br><br>&nbsp;&nbsp;&nbsp; public void setName(String name) {<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <a target="_blank" href="http://this.name">this.name</a> = name;<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; <br>}<br><br><br>Rule:<br><br>rule "AddBonusCd"<br><br>&nbsp;&nbsp;&nbsp; when<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; $so : ShippingOrder()<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; $productList : ArrayList(size == 1) from collect( Product(name matches<br>"cd1|bonus_cd") from $so.products)<br>&nbsp;&nbsp;&nbsp; then<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
 System.out.println("Adding Bonus Cd");<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Set&lt;Product&gt; productSet = new HashSet&lt;Product&gt;($so.getProducts());<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Product bonusCd = new Product();<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; bonusCd.setName("bonus_cd");<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; productSet.add(bonusCd);<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; modify($so) {<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; setProducts(productSet);<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>end<br><br>The Shipping Order object is inserted as the fact.<br><br>The rule is valid if only cd1 or cd_bonus is in the shipping order.&nbsp; I only<br>want the rule to run when cd1 is in the product set but cd_bonus is not. <br>Makes sense?<br><br>Thanks,<br>J<br>-- <br><span>View
 this message in context: <a target="_blank" href="http://drools-java-rules-engine.46999.n3.nabble.com/collection-question-tp982769p982769.html">http://drools-java-rules-engine.46999.n3.nabble.com/collection-question-tp982769p982769.html</a></span><br>Sent from the Drools - User mailing list archive at <a target="_blank" href="http://Nabble.com">Nabble.com</a>.<br>_______________________________________________<br>rules-users mailing list<br><a ymailto="mailto:rules-users@lists.jboss.org" 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>
</div></body></html>