[rules-users] Grouping and Aggregation in Drools

Wolfgang Laun wolfgang.laun at gmail.com
Sat Jan 8 04:39:12 EST 2011


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