Laurent,

This could help....

=========
If you wanna test the parameters of the rules are different instances
of the same ObjectType you can use this:

If you have an Object:

public class MyObject{
       private String name;

       public MyObject(String name){
              this.name=name;
       }

       public String getName(){
              return this.name;
       }
}

In your code you can have:

assertObject(new MyObject("o1");
assertObject(new MyObject("o2");
assertObject(new MyObject("o3");
assertObject(new MyObject("o4");
assertObject(new MyObject("o5");

In the DRL file you can have:

<java:functions>
                public boolean diffInstances(Object obj1, Object obj2){
                        if (System.identityHashCode(obj1) > 
System.identityHashCode(obj2)){
                                return true;
                        }
                        return false;
                }
                
                public boolean diffInstances(Object [] objects){
                        for (int i=0; i &lt; objects.length-1;i++){
                                if (!diffInstances(objects[i],objects[i+1])){
                                        return false;
                                }
                        }
                        return true;
                }
</java:functions>

Then in your rule you can have something like this:

<rule name="test-different-instances" salience="1" no-loop="true">
        <parameter identifier="o1">
                <class>MyObject</class>
        </parameter>
        <parameter identifier="o2">
                <class>MyObject</class>
        </parameter>
        <parameter identifier="o3">
                <class>MyObject</class>
        </parameter>
        <parameter identifier="o4">
                <class>MyObject</class>
        </parameter>
        <parameter identifier="o5">
                <class>MyObject</class>
        </parameter>
                

        <java:condition>diffInstances(new 
Object[]{o1,o2,o3,o4,o5})</java:condition>
                
        <java:consequence>
                System.out.println("o1:"+o1.getName());
                System.out.println("o2:"+o2.getName());
                System.out.println("o3:"+o3.getName());
                System.out.println("o4:"+o4.getName());
                System.out.println("o5:"+o5.getName());
        </java:consequence>
</rule>


----

This will activate the rule just in 1 case when the instances are all
diferents.


========

Thursday, November 10, 2005, 7:50:18 PM, you wrote:

> Hi,

 

> I am new to drools, trying to see if I could use it to implement a
> discount engine.

 

> I am playing with the petstore example (shopping cart), and I was
> wondering if Drools has a pattern matching feature or not, if rules can
> be fired based on combinations rather than permutations.

 

> For example I want to implement a buy 2 Gold Fishes get 1 free discount.

 

> If I define my rule like:

>   <java:condition>item1.getName().equals( "Gold Fish" )</java:condition>

>   <java:condition>item2.getName().equals( "Gold Fish" )</java:condition>

 

>   <java:consequence>

>       //gives 100% discount to one item

>       item2.setDiscount(100);

>     </java:consequence>

 

> If the user puts 2 Golf Fishes in his cart (A, B), the rule will fire 4
> times (for permutation (A,A), (A,B), (B,B), (B, A), and gives 100%
> discount to all items...That does not work.

 

> I can add a condition to make sure the items are different and the rules
> fires only once per combo such as 

> <java:condition>item1.getId()>item2.getId()</java:condition>

 

> That works in this case, but it is not scalable at all (if I want to do
> 4 gold fishes get 1 free...it gets more complicated).

 

> I also want the rule to fire more than 1 time if the user has let's say
> 8 gold fishes (he should get 4 free), so having a condition on the cart
> itself such as cart.hasXitems(4, "GolfFish") would probably not work I
> suppose (such rule would be fired only once if I understand how the
> engine works).

 

 

> So how would this (buy X items, get 1 free; applicable more than 1 time
> per cart) be implemented in a scalable and elegant way with drools? 

> Is there some kind of pattern engine in drools (not sure if this is the
> proper term for this, but basically I want the rule to be fired only
> once per combination of facts that match some conditions, not per
> permutation..)?

 

 

> Thanks.

 

 

 

> Laurent

 

 



--------------------------
Felipe Piccolini
[EMAIL PROTECTED]

Reply via email to