[rules-users] collection question

Earnest Dyke earniedyke at yahoo.com
Tue Jul 20 22:43:47 EDT 2010


Would the following accomplish your goal?

Earnie!

import java.util.*;
import com.sample.Product;
import com.sample.ShippingOrder;

rule "AddBonusCd"
    when
        $so : ShippingOrder()
        ArrayList(size == 1) from collect( Product(name matches
                   "cd1") from $so.products)
        ArrayList(size == 0) from collect( Product(name matches
                   "bonus_cd") from $so.products)
    then
        System.out.println("Adding Bonus Cd ");
       
        Set<Product> productSet = new HashSet<Product>($so.getProducts());
       
        Product bonusCd = new Product();
        bonusCd.setName("bonus_cd");
        productSet.add(bonusCd);
       
        modify($so) {
            setProducts(productSet);
        }
end




________________________________
From: javaj <jmilliro at ameritech.net>
To: rules-users at lists.jboss.org
Sent: Tue, July 20, 2010 6:41:25 PM
Subject: [rules-users] collection question


I'm trying to write a rule to add an object to a collection if the collection
already contained a certain object with a specific attribute.

I developed a small example so maybe that will make more sense:

Use Case: In a product shipping applicaiton, anytime a product with the name
"cd1" is in the shipping order, a bonus product named "bonus_cd" needs to be
added to the shipping order.  The bonus cd should not be added if it's
already in the shipping order.

Shipping Order:

public class ShippingOrder {

    private Set<Product> products;
    
    public ShippingOrder() {}

    public Set<Product> getProducts() {
        return products;
    }

    public void setProducts(Set<Product> products) {
        this.products = products;
    }
        
}

Product:

public class Product {

    private String name;
    
    public Product(){}

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    
}


Rule:

rule "AddBonusCd"

    when
        $so : ShippingOrder()
        $productList : ArrayList(size == 1) from collect( Product(name matches
"cd1|bonus_cd") from $so.products)
    then
        System.out.println("Adding Bonus Cd");
        
        Set<Product> productSet = new HashSet<Product>($so.getProducts());
        
        Product bonusCd = new Product();
        bonusCd.setName("bonus_cd");
        productSet.add(bonusCd);
        
        modify($so) {
            setProducts(productSet);
        }
end

The Shipping Order object is inserted as the fact.

The rule is valid if only cd1 or cd_bonus is in the shipping order.  I only
want the rule to run when cd1 is in the product set but cd_bonus is not. 
Makes sense?

Thanks,
J
-- 
View this message in context: 
http://drools-java-rules-engine.46999.n3.nabble.com/collection-question-tp982769p982769.html

Sent from the Drools - User mailing list archive at Nabble.com.
_______________________________________________
rules-users mailing list
rules-users at lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20100720/3e29e552/attachment.html 


More information about the rules-users mailing list