javadroolsrule-enginedrools-fusion

Drools Complex Rule Advice on Accumulating Facts


I need some advice on how to write a rule for the following case. First, here are my facts:

SessionClock($now : new Date(getCurrentTime()))

ClickEvent( $userId : userId, $productId : productId, $event : "FAVORITE" / "REMOVE_FAVORITE" )

Product($id : id, $endDate : endDate)

Purchase ( $userId : userId, $purchasedProducts : purchasedProducts )

where purchasedProducts is a List of:

PurchasedProduct( $id : id, $price : price)

Now I would like to send a notification everytime at a particular hour:

  1. Today is the endDate of a product and
  2. User has favorited but hasn't unfavorited the product (from ClickEvent) and
  3. User hasn't bought the product (from Purchase) and
  4. Include all such products in one notification (basically I need to collect products)

I appreciate any help on this.

Thanks in advance!


Solution

  • It may be a good idea to do this in steps

    rule "interesting user/product"
    when
      SessionClock( $now: time )
      Purchase( $uid: userId, $purchases: purchasedProducts )
      ClickEvent( userId == $uid, $pid: productId,
                  event == "FAVORITE" )
      not ClickEvent( userId == $uid, productId == $pid,
                      event == "REMOVE_FAVORITE" )     
      Product( id == $pid, $endDate: endDate )
      eval( endDateIsToday( $now, $endDate ) )
    then
    end
    
    rule "make Collection" extends "interesting user/product"
    when
      not Collection( userId == $uid )
    then
      insert( new Collection( $uid ) )
    end
    
    rule "fill Collection" extends "interesting user/product"
    when
      $coll: Collection( userId == $uid, products not contains $pid )
    then
      modify( $coll ){ addProduct( $pid ) }
    end
    

    A third rule, running with reduced salience, can do the notification.

    Edit To clarify, endDateIsToday is a (DRL) function or static method. Collection is a class you need to define with a couple of fields: userId and set of product ids.