On 11/10/05, Bouis, Laurent <[EMAIL PROTECTED]> wrote:
>
> 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.


[snip]

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.


Ah, Golf Fishes -- when Salmon swim up the stream to play on the links. :)

Yes, this is entirely possible; this should probably go in the FAQ because
it comes up a fair amount.

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).


Yes, approaches like these are the usual way to do it; there's no specific
support to have Drools do this kind of separation for you. You can use
equality and comparisons to ensure that you're dealing with unique items and
unique combinations. It's true that as you deal with large numbers of
parameters, this gets harder.

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).


Yes, this is true, although there are ways around that, if need be, by
tracking in your object model which items you have already claimed for bulk
discounts and which you haven't.

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..)?


It seems like you've thought through the options well, and AFAIK, there's no
sophisticated Drools mechanism that you're missing. That said, I believe
that one or both of the options you describe above are workable.

- Geoffrey
--
Geoffrey Wiseman

Reply via email to