<br> All,<br><br> Unfortunately the problem of dealing with a clock in a rules engine is a lot more complex than it seems at first.<br><br> 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.<br>
<br> 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.<br><br> 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.<br>
<br> Michael's idea of using collect/accumulate for counting days is creative, but unfortunately too heavy for practical uses I am afraid.<br><br> Good luck,<br> Edson<br><br> <br><br><div class="gmail_quote">
2009/1/29 Anstis, Michael (M.) <span dir="ltr"><<a href="mailto:manstis1@ford.com">manstis1@ford.com</a>></span><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div>
<div dir="ltr" align="left"><span><font size="2" color="#0000ff" face="Arial">You got it.</font></span></div>
<div dir="ltr" align="left"><span><font size="2" color="#0000ff" face="Arial"></font></span> </div>
<div dir="ltr" align="left"><span><font size="2" color="#0000ff" face="Arial">You won't find this proposal in any pattern books!!
;-)</font></span></div>
<div dir="ltr" align="left"><span><font size="2" color="#0000ff" face="Arial"></font></span> </div>
<div dir="ltr" align="left"><span><font size="2" color="#0000ff" face="Arial">At least you understood the syntax, so you can now
officially consider yourself not a noob.</font></span></div><br>
<blockquote style="margin-right: 0px;">
<div dir="ltr" align="left" lang="en-us">
<hr>
<font size="2" face="Tahoma"><div class="Ih2E3d"><b>From:</b> <a href="mailto:rules-users-bounces@lists.jboss.org" target="_blank">rules-users-bounces@lists.jboss.org</a>
[mailto:<a href="mailto:rules-users-bounces@lists.jboss.org" target="_blank">rules-users-bounces@lists.jboss.org</a>] <b>On Behalf Of </b>David
Siefert<br></div><b>Sent:</b> 29 January 2009 12:59<div><div></div><div class="Wj3C7c"><br><b>To:</b> Rules Users
List<br><b>Subject:</b> Re: [rules-users] Working with dates in
rules<br></div></div></font><br></div><div><div></div><div class="Wj3C7c">
<div></div>
<div>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.</div>
<div> </div>
<div>My bad!</div>
<div> </div>
<div>Thanks,</div>
<div>David<br><br></div>
<div class="gmail_quote">On Thu, Jan 29, 2009 at 6:53 AM, David Siefert <span dir="ltr"><<a href="mailto:siefert.david.mailinglist@gmail.com" target="_blank">siefert.david.mailinglist@gmail.com</a>></span>
wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0px 0px 0px 0.8ex; padding-left: 1ex;">
<div>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).</div>
<div> </div>
<div>Thanks,</div>
<div>David<font color="#888888"><br><br></font></div>
<div>
<div></div>
<div>
<div class="gmail_quote">On Thu, Jan 29, 2009 at 5:57 AM, Anstis, Michael (M.)
<span dir="ltr"><<a href="mailto:manstis1@ford.com" target="_blank">manstis1@ford.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0px 0px 0px 0.8ex; padding-left: 1ex;">
<div>
<div dir="ltr" align="left"><span><font size="2" color="#0000ff" face="Arial">Hi
David,</font></span></div>
<div dir="ltr" align="left"><span><font size="2" color="#0000ff" face="Arial"></font></span> </div>
<div dir="ltr" align="left"><span><font size="2" color="#0000ff" face="Arial">Sorry
it was of little help.</font></span></div>
<div dir="ltr" align="left"><span><font size="2" color="#0000ff" face="Arial"></font></span> </div>
<div dir="ltr" align="left"><span><font size="2" color="#0000ff" face="Arial">A rule
of thumb is to stay away from "eval" as it kills performance (the patterns
can't be indexed).</font></span></div>
<div dir="ltr" align="left"><span><font size="2" color="#0000ff" face="Arial"></font></span> </div>
<div dir="ltr" align="left"><span><font size="2" color="#0000ff" face="Arial">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).</font></span></div>
<div dir="ltr" align="left"><span><font size="2" color="#0000ff" face="Arial"></font></span> </div>
<div dir="ltr" align="left"><span><font size="2" color="#0000ff" face="Arial">I
guess you could look at something like this (gees, this is going to show
how "green" I am with Drools now):-</font></span></div>
<div dir="ltr" align="left"><span><font size="2" color="#0000ff" face="Arial"></font></span> </div>
<div dir="ltr" align="left"><span><font size="2" color="#0000ff" face="Courier New">rule "yuck"</font></span></div>
<div dir="ltr" align="left"><span><font face="Courier New"><font size="2" color="#0000ff"> when</font></font></span></div>
<div dir="ltr" align="left"><span><font face="Courier New"><font size="2" color="#0000ff"> DateFactory($today :
today )</font></font></span></div>
<div dir="ltr" align="left"><span><font face="Courier New"><font size="2" color="#0000ff"> $c : CreditCard(
$expires : expirationDate )</font></font></span></div>
<div dir="ltr" align="left"><span><font size="2" color="#0000ff" face="Courier New"> ArrayList( size >= 0
) from collect( Day( date >= $today, date <= $expires
))<br> then</font></span></div>
<div dir="ltr" align="left"><span><font face="Courier New"><font size="2" color="#0000ff"> //Difference between
"today" and "expiry date" is more than zero
days</font></font></span></div>
<div dir="ltr" align="left"><font size="2"><font color="#0000ff"><font face="Courier New"><span>end</span><span></span></font></font></font></div>
<div dir="ltr" align="left"><span><font size="2" color="#0000ff" face="Arial"></font></span> </div>
<div dir="ltr" align="left"><span><font size="2" color="#0000ff" face="Arial">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. </font></span></div>
<div dir="ltr" align="left"><span><font size="2" color="#0000ff" face="Arial"></font></span> </div>
<div dir="ltr" align="left"><span><font size="2" color="#0000ff" face="Arial">Good
luck with your adventures with Drools.</font></span></div>
<div dir="ltr" align="left"><span><font size="2" color="#0000ff" face="Arial"></font></span> </div>
<div dir="ltr" align="left"><span><font size="2" color="#0000ff" face="Arial">Mike</font></span></div><br>
<blockquote style="margin-right: 0px;">
<div dir="ltr" align="left" lang="en-us">
<hr>
<font size="2" face="Tahoma">
<div><b>From:</b> <a href="mailto:rules-users-bounces@lists.jboss.org" target="_blank">rules-users-bounces@lists.jboss.org</a> [mailto:<a href="mailto:rules-users-bounces@lists.jboss.org" target="_blank">rules-users-bounces@lists.jboss.org</a>] <b>On Behalf Of
</b>David Siefert<br></div><b>Sent:</b> 29 January 2009
11:31<br><b>To:</b> Rules Users List<br><b>Subject:</b> Re:
[rules-users] Working with dates in rules<br></font><br></div>
<div>
<div></div>
<div>
<div></div>
<div>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.</div>
<div> </div>
<div>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.</div>
<div> </div>
<div>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"):</div>
<div> </div>
<div><font face="courier new,monospace">rule "Fires when expiration less
than today"</font></div>
<div><font face="courier new,monospace"> when</font></div>
<div><font face="courier new,monospace"> $c:
CreditCard($expires : expirationDate)</font></div>
<div><font face="courier new,monospace"> eval(new
TemporalReasoner(Calendar.getInstance()).lessThan($expires))</font></div>
<div><font face="courier new,monospace"> then</font></div>
<div><font face="courier new,monospace"> // do
something for when credit card is still valid</font></div>
<div><font face="courier new,monospace">end</font></div>
<div> </div>
<div>Now, I'm still generally a noob with BRs, so I'm not sure how this
will affect performance. Any advice?</div>
<div> </div>
<div>Thanks,</div>
<div>David<br><br></div>
<div class="gmail_quote">On Thu, Jan 29, 2009 at 3:50 AM, Anstis, Michael
(M.) <span dir="ltr"><<a href="mailto:manstis1@ford.com" target="_blank">manstis1@ford.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0px 0px 0px 0.8ex; padding-left: 1ex;">
<div>
<div dir="ltr" align="left"><span><font size="2" color="#0000ff" face="Arial">Hi,</font></span></div>
<div dir="ltr" align="left"><span><font size="2" color="#0000ff" face="Arial"></font></span> </div>
<div dir="ltr" align="left"><span><font size="2" color="#0000ff" face="Arial">I
wonder whether Drools 5 "Complex Event Processing Support (Temporal
Reasoning)" might be what you're looking for?</font></span></div>
<div dir="ltr" align="left"><span><font size="2" color="#0000ff" face="Arial"></font></span> </div>
<div dir="ltr" align="left"><span><font size="2" color="#0000ff" face="Arial"><a href="http://blog.athico.com/2008/07/drools-50-m1-new-and-noteworthy.html" target="_blank">http://blog.athico.com/2008/07/drools-50-m1-new-and-noteworthy.html</a></font></span></div>
<div dir="ltr" align="left"><span><font size="2" color="#0000ff" face="Arial"></font></span> </div>
<div dir="ltr" align="left"><span><font size="2" color="#0000ff" face="Arial">With kind regards,</font></span></div>
<div dir="ltr" align="left"><span><font size="2" color="#0000ff" face="Arial"></font></span> </div>
<div dir="ltr" align="left"><span><font size="2" color="#0000ff" face="Arial">Mike</font></span></div><br>
<blockquote dir="ltr" style="margin-right: 0px;">
<div dir="ltr" align="left" lang="en-us">
<hr>
<font size="2" face="Tahoma"><b>From:</b> <a href="mailto:rules-users-bounces@lists.jboss.org" target="_blank">rules-users-bounces@lists.jboss.org</a> [mailto:<a href="mailto:rules-users-bounces@lists.jboss.org" target="_blank">rules-users-bounces@lists.jboss.org</a>] <b>On Behalf
Of </b>David Siefert<br><b>Sent:</b> 29 January 2009
09:40<br><b>To:</b> Rules Users List<br><b>Subject:</b>
[rules-users] Working with dates in rules<br></font><br></div>
<div>
<div></div>
<div>
<div></div>
<div>Hi-</div>
<div> </div>
<div>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 <i>x</i> days
from the current date, <i>n</i> 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?</div>
<div> </div>
<div>Big thanks,</div>
<div>David</div></div></div></blockquote></div><br>_______________________________________________<br>rules-users
mailing list<br><a href="mailto:rules-users@lists.jboss.org" target="_blank">rules-users@lists.jboss.org</a><br><a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
<br></blockquote></div><br></div></div></blockquote></div><br>_______________________________________________<br>rules-users
mailing list<br><a href="mailto:rules-users@lists.jboss.org" target="_blank">rules-users@lists.jboss.org</a><br><a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
<br></blockquote></div><br></div></div></blockquote></div><br></div></div></blockquote></div>
<br>_______________________________________________<br>
rules-users mailing list<br>
<a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
<br></blockquote></div><br><br clear="all"><br>-- <br> Edson Tirelli<br> JBoss Drools Core Development<br> JBoss, a division of Red Hat @ <a href="http://www.jboss.com">www.jboss.com</a><br>