[rules-users] Working with dates in rules

Edson Tirelli tirelli at post.com
Thu Jan 29 09:05:04 EST 2009


    All,

    Unfortunately the problem of dealing with a clock in a rules engine is a
lot more complex than it seems at first.

    David, your best bet is to create an instance of a clock class, insert
as a fact and update this fact from time to time when you want your rules to
be reevaluated. Do not use any Calendar.getInstance() or
System.currentTimeMillis() in the LHS of your rules, because you will get
inconsistent behavior. Also, once you have the clock as a fact, I back the
suggestion of using a library like JodaTime for date arithmetics.

    If you want to go the extra mile, you can implement your own operators
in drools, but in Drools 4 this is still not as simple as in Drools 5.

    Just to reinforce, the problem is not handling dates themselves. The
problem is constraining your rules against the concept of "current time"
because that is mutable.

    Michael's idea of using collect/accumulate for counting days is
creative, but unfortunately too heavy for practical uses I am afraid.

    Good luck,
       Edson



2009/1/29 Anstis, Michael (M.) <manstis1 at ford.com>

>  You got it.
>
> You won't find this proposal in any pattern books!! ;-)
>
> At least you understood the syntax, so you can now officially consider
> yourself not a noob.
>
>  ------------------------------
> *From:* rules-users-bounces at lists.jboss.org [mailto:
> rules-users-bounces at lists.jboss.org] *On Behalf Of *David Siefert
> *Sent:* 29 January 2009 12:59
>
> *To:* Rules Users List
> *Subject:* Re: [rules-users] Working with dates in rules
>
>  Ah! Sorry, I believe you were saying Day is a fact that I would have to
> create for each likely day....  The collect operation is what gathers all
> those (Day) facts into a list that is counted. so the rule fires if there is
> more than 0 day facts.
>
> My bad!
>
> Thanks,
> David
>
> On Thu, Jan 29, 2009 at 6:53 AM, David Siefert <
> siefert.david.mailinglist at gmail.com> wrote:
>
>> Hey, that was quite clever.  Where can I find more about Day()?  I did
>> some research about DateFactory which I can see how dates get parsed
>> according to the documentation, as well as compared with the conditional
>> operators (<, >, <=, >=, etc).
>>
>> Thanks,
>> David
>>
>>   On Thu, Jan 29, 2009 at 5:57 AM, Anstis, Michael (M.) <
>> manstis1 at ford.com> wrote:
>>
>>>  Hi David,
>>>
>>> Sorry it was of little help.
>>>
>>> A rule of thumb is to stay away from "eval" as it kills performance (the
>>> patterns can't be indexed).
>>>
>>> I'm not sure if Drools can handle Date variables (the docs state it
>>> handles Data literals formatted as "dd-mmm-yyyy" so Cheese( date ==
>>> "27-Oct-2008") works).
>>>
>>> I guess you could look at something like this (gees, this is going to
>>> show how "green" I am with Drools now):-
>>>
>>> rule "yuck"
>>>     when
>>>         DateFactory($today : today )
>>>         $c : CreditCard( $expires : expirationDate )
>>>         ArrayList( size >= 0 ) from collect( Day( date >= $today, date <=
>>> $expires ))
>>>     then
>>>         //Difference between "today" and "expiry date" is more than zero
>>> days
>>> end
>>>
>>> This does require the insertion of (possibly) huge amounts of Day facts.
>>> Yuck. There must be a better way (in fact your eval looks soooo much
>>> cleaner). I'm just throwing out ideas.
>>>
>>> Good luck with your adventures with Drools.
>>>
>>> Mike
>>>
>>>  ------------------------------
>>>  *From:* rules-users-bounces at lists.jboss.org [mailto:
>>> rules-users-bounces at lists.jboss.org] *On Behalf Of *David Siefert
>>> *Sent:* 29 January 2009 11:31
>>> *To:* Rules Users List
>>> *Subject:* Re: [rules-users] Working with dates in rules
>>>
>>>   Not sure--Drools 5 temporal reasoning looks like it would be the
>>> answer.  Only problem is that I need a solution now, so I am stuck with
>>> Drools 4.
>>>
>>> This did give me some ideas though.  I created myself a class called
>>> TemporalReasoner.  This has a few methods, namely to roll a date (so I can
>>> tell it x amount of time from now), and then compare it to another date.
>>>
>>> So in my rule, lets say I have a fact CreditCard with property
>>> expiration:Date (speaking uml-ishly for "a property named 'expiration' of
>>> type Date"):
>>>
>>> rule "Fires when expiration less than today"
>>>   when
>>>     $c: CreditCard($expires : expirationDate)
>>>     eval(new TemporalReasoner(Calendar.getInstance()).lessThan($expires))
>>>   then
>>>     // do something for when credit card is still valid
>>> end
>>>
>>> Now, I'm still generally a noob with BRs, so I'm not sure how this will
>>> affect performance.  Any advice?
>>>
>>> Thanks,
>>> David
>>>
>>> On Thu, Jan 29, 2009 at 3:50 AM, Anstis, Michael (M.) <manstis1 at ford.com
>>> > wrote:
>>>
>>>>  Hi,
>>>>
>>>> I wonder whether Drools 5 "Complex Event Processing Support (Temporal
>>>> Reasoning)" might be what you're looking for?
>>>>
>>>> http://blog.athico.com/2008/07/drools-50-m1-new-and-noteworthy.html
>>>>
>>>> With kind regards,
>>>>
>>>> Mike
>>>>
>>>>  ------------------------------
>>>> *From:* rules-users-bounces at lists.jboss.org [mailto:
>>>> rules-users-bounces at lists.jboss.org] *On Behalf Of *David Siefert
>>>> *Sent:* 29 January 2009 09:40
>>>> *To:* Rules Users List
>>>> *Subject:* [rules-users] Working with dates in rules
>>>>
>>>>   Hi-
>>>>
>>>> I'm wondering how it would be possible to compare a part of a date in
>>>> the lhs condition of a rule in a Drool.  In other words, I need to compare a
>>>> date to see if it is *x* days from the current date, *n* hours from
>>>> current time, etc.  Would anyone be kind enough to provide an example?  I'm
>>>> thinking I may have to implement something of my own to offer that
>>>> functionality?
>>>>
>>>> Big thanks,
>>>> David
>>>>
>>>>
>>>> _______________________________________________
>>>> rules-users mailing list
>>>> rules-users at lists.jboss.org
>>>> https://lists.jboss.org/mailman/listinfo/rules-users
>>>>
>>>>
>>>
>>> _______________________________________________
>>> rules-users mailing list
>>> rules-users at lists.jboss.org
>>> https://lists.jboss.org/mailman/listinfo/rules-users
>>>
>>>
>>
>
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>
>


-- 
 Edson Tirelli
 JBoss Drools Core Development
 JBoss, a division of Red Hat @ www.jboss.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20090129/c1e47086/attachment.html 


More information about the rules-users mailing list