[rules-users] Grouping and Aggregation in Drools
Manav
manav7574 at yahoo.com
Mon Jan 10 10:41:25 EST 2011
Thanks for the quick response Wolfgang. Appreciate it.
I have a question about this line in the rule you specified below
not Category( $sup == name, cart == $cart )
What should this name be ? How will this be determined at runtime ?
Also would you mind how these rules work . I tried to implement it but could not
make much progress.
On the other hand i tried with a slightly different approach. My use-case is the
Customer will have
an actual cart and a suggested cart when they are made available in the working
memory. Here's the rule : -
rule "Test 1"
when
Customer ($actual_cart : actual_cart, $reference_cart : reference_cart)
$item : CartItem () from $actual_cart
$category_limit : Category (category == $item.category, $limit : categoryLimit)
from $reference_cart
// This will accumulate as many times as the category exists in the actual_cart
$total : Double()
from accumulate ( CartItem (category == $item.category, $value :
actual_value) from $actual_cart,
sum ($value)
)
//if there's no row in the reference_Cart for specified category then $limit
should be set to zero.
eval ($total > $limit)
then
//record the category where the excess items has been purchased
end
There are some obvious issues with the approach that i have outlined above.
Wonder if there's some way to get around them.
Regards,
Manav
----- Original Message ----
From: Wolfgang Laun <wolfgang.laun at gmail.com>
To: Manav <manav7574 at yahoo.com>; Rules Users List <rules-users at lists.jboss.org>
Sent: Sat, January 8, 2011 3:09:12 PM
Subject: Re: [rules-users] Grouping and Aggregation in Drools
A Category combines a name, a reference to the cart, a sum of article
values and a percentage.
Any change in the cart triggers an update of all associated Category
facts. The rules are quite simple. The update of the cart (not shown
here) must be run with higher salience.
rule "first super"
$cart. Cart( $tot: total )
$art: Article( $cart: cart, $val: value, $sup. super, $sub: sub )
not Category( $sup == name, cart == $cart )
then
insert( new Category( $sup, $car, $val, $val/$tot ) );
end
rule "next super"
$cart. Cart( $tot: total )
$art: Article( $cart: cart, $val: value, $sup. super, $sub: sub )
$cat: Category( $sup == name, cart == $cart )
then
Double newVal =$cat.getValue() + $val;
modify( $cat ){
setValue( newVal ),
setPercent( newVal / tot )
}
end
Almost identical rules handle sub-categories.
For removal from the cart, you can use the same rule if the article
value is negative.
Make sure to avoid division by 0 if the last item is removed from the cart.
The reference cart is similar in structure, and the comparison rule is
generic for all categories.
-W
On 08/01/2011, Manav <manav7574 at yahoo.com> wrote:
> Hi,
> I have a slightly tricky problem on my hand that i am trying to solve using
> rules.
>
> I am using version 5.1 of Drools.Appreciate if there are any suggestions /
> pointers .
>
>
> I have a customer who will have a shopping cart with various products. For
> example
>
> the shopping cart will be a list of products that belong to a certain
> sub-category and
>
> super category like
>
> product-id= 'Noodles', sub-category='PackagedFood',
> super-category='FMCG-Food',
> %value of total=3%
> product-id= 'Cookies',
> sub-category='PackagedFood',super-category='FMCG-Food',
> %value of total=6%
> product-id= 'Pizza', sub-category='ReadyToEat',super-category='FMCG-Food',
> %value of total=5%
> product-id= 'Burgers', sub-category='ReadyToEat',super-category='FMCG-Food',
> %value of total=7%
> product-id= 'Shampoo',
> sub-category='Toiletaries',super-category='FMCG-NonFood',
> %value of total=4%
> product-id= 'Conditioner',
> sub-category='Toiletaries',super-category='FMCG-NonFood', %value of total=8%
>
> The customer will also have a suggested cart that serves as a reference at
> the
> sub-category and supercategory level. The reference will tell what level of
> his
> total purchase he can spend on the sub-category and super-category. The
> reference will look something like
>
>
> super-category='FMCG-Food', %value-suggested=20%
> super-category='FMCG-NonFood', %value-suggested=15%
> sub-category='PackagedFood',%value-suggested=10%
> sub-category='ReadyToEat',%value-suggested=10%
> sub-category='Toiletaries',%value-suggested=6%
>
> Looking at this example the customer is going overboard in the FMCG-Food
> super
> category as the suggested
>
> limit is 20% and the actual grouped at the supsercategory is 21% . Also he
> is
> crossing the limit at the
>
> sub-category level for ReadyToEat (suggested is 10% vs actual is 12%) and
> Toiletaries (suggested is 6% vs actual 12%)
>
> My problem is how do i group the actual data at various supercategory
> level,
> sum the %value and then compare it with the suggested in the when clause.
>
>
> If they cross the limit i will then have some action taken in the then
> clause.
> Similarly i will have to do the same at the sub-category level as well.
> Regards,
> Manav
>
>
>
>
>
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>
More information about the rules-users
mailing list