<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> when<br> $so : ShippingOrder()<br> ArrayList(size == 1) from collect( Product(name matches<br> "cd1") from $so.products)<br> ArrayList(size == 0) from collect( Product(name matches<br> "bonus_cd") from $so.products)<br>
then<br> System.out.println("Adding Bonus Cd ");<br> <br> Set<Product> productSet = new HashSet<Product>($so.getProducts());<br> <br> Product bonusCd = new Product();<br> bonusCd.setName("bonus_cd");<br> productSet.add(bonusCd);<br> <br> modify($so) {<br> setProducts(productSet);<br> }<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 <jmilliro@ameritech.net><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. 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> private Set<Product> products;<br> <br> public ShippingOrder() {}<br><br> public Set<Product> getProducts() {<br> return products;<br> }<br><br> public void setProducts(Set<Product> products) {<br>
this.products = products;<br> }<br> <br>}<br><br>Product:<br><br>public class Product {<br><br> private String name;<br> <br> public Product(){}<br><br> public String getName() {<br> return name;<br> }<br><br> public void setName(String name) {<br> <a target="_blank" href="http://this.name">this.name</a> = name;<br> }<br> <br>}<br><br><br>Rule:<br><br>rule "AddBonusCd"<br><br> when<br> $so : ShippingOrder()<br> $productList : ArrayList(size == 1) from collect( Product(name matches<br>"cd1|bonus_cd") from $so.products)<br> then<br>
System.out.println("Adding Bonus Cd");<br> <br> Set<Product> productSet = new HashSet<Product>($so.getProducts());<br> <br> Product bonusCd = new Product();<br> bonusCd.setName("bonus_cd");<br> productSet.add(bonusCd);<br> <br> modify($so) {<br> setProducts(productSet);<br> }<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. 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>