<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 <steve.ronderos@ni.com><br><b><span style="font-weight: bold;">To:</span></b> Rules Users List <rules-users@lists.jboss.org><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>
when<br>
$so : ShippingOrder()<br>
Product(name == "cd1") from $so.products<br>
not (Product(name == "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</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>
> From:</font></tt>
<br><tt><font size="2">> <br>
> Wolfgang Laun <wolfgang.laun@gmail.com></font></tt>
<br><tt><font size="2">> <br>
> To:</font></tt>
<br><tt><font size="2">> <br>
> Rules Users List <rules-users@lists.jboss.org></font></tt>
<br><tt><font size="2">> <br>
> Date:</font></tt>
<br><tt><font size="2">> <br>
> 07/21/2010 02:02 AM</font></tt>
<br><tt><font size="2">> <br>
> Subject:</font></tt>
<br><tt><font size="2">> <br>
> Re: [rules-users] collection question</font></tt>
<br><tt><font size="2">> <br>
> Sent by:</font></tt>
<br><tt><font size="2">> <br>
> rules-users-bounces@lists.jboss.org</font></tt>
<br><tt><font size="2">> <br>
> Things would be smpler if you could avoid re-creating Product objects<br>
> because adding an identical Object does not change the Set. (This<br>
> could be implemented with the help of a global Map<String,Product>,)<br>
> <br>
> Second, overriding equals (and hashCode) in Product would also avoid
adding<br>
> an "evil twin" in your rule code - as it is!<br>
> <br>
> Even simpler is a solution where the relevant Product objects are
inserted<br>
> as facts, so that<br>
> $cd1 : Product( name == "cd1" )<br>
> $bonus_cd : Product( name == "bonus_cd" )<br>
> $order : ShippingOrder( products contains $cd1, products
not<br>
> contains $bonus_cd )<br>
> is true (again, with equals and hashCode being overridden).<br>
> <br>
> Given your solution with collect, an additional eval testing for<br>
> "bonus_cd" not being the<br>
> name of the single Product in List $productList might be more<br>
> efficient than iterating<br>
> all products a second time.<br>
> <br>
> -W<br>
> <br>
> On 21 July 2010 00:41, javaj <jmilliro@ameritech.net> wrote:<br>
> ><br>
> > I'm trying to write a rule to add an object to a collection if
thecollection<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>
> > this.name
= 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( <br>
> 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><br>
> ($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>
> > 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>
> 46999.n3.nabble.com/collection-question-tp982769p982769.html<br>
> > Sent from the Drools - User mailing list archive at Nabble.com.<br>
> > _______________________________________________<br>
> > rules-users mailing list<br>
> > rules-users@lists.jboss.org<br>
> > </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>
> ><br>
> <br>
> _______________________________________________<br>
> rules-users mailing list<br>
> rules-users@lists.jboss.org<br>
> </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>