在规则的LHS中使用java.util.Date [英] use java.util.Date in a rule's LHS
问题描述
我是流口水的新秀,学习流口水并工作了大约10天。
我遇到了一个问题,即要在规则的LHS部分中比较Date()对象。
I'm a rookie of drools, learn about and work on drools for about 10 days. I was faced a problem that a Date() object is to be compared in a rule's LHS part.
// in Java
SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss");
session.setGlobal("currentDate", format.format(new Date()));
# in drl file
global java.lang.String currentDate ;
#global java.util.Date currentDate; both tested
when
$united : LotteryUnitedDO(lotteryTypeId == LotteryTypeEnum.SSQ,
totalFee >= 1000,
shareCnt >= (0.1 * totalShare),
#unitedEndTime is a java.util.Date Object
unitedEndTime > currentDate
)
currentDate的值是当前日期,我看过一些示例和文章,但是他们只使用了日期的 yyyy-mm-dd部分。但是我必须比较 hh:mm:ss部分。像这样:
the value of currentDate is the current date, I've seen some examples and articles doing this but they used only the "yyyy-mm-dd" part of the date. Yet I have to compare the "hh:mm:ss" part. like this:
$dateInputBox : DateInputBox(verifyDate > "12-Oct-2005")
我试图更改 drools.dateformat属性并格式化插入知识库的日期,只是为了获得各种编译错误。我该如何处理,或者流口水无法处理LHS中日期的小时部分?
I tried to change the "drools.dateformat" property and format the date inserted into knowledge base only to get various of compilation error.So How can I deal with this or drools is just unable to deal with the hour part of a date in LHS?
在此先感谢您的建议。
推荐答案
要比较日期,您可以简单地使用Drools Fusion的时间运算符(之后,之前等):
To compare dates, you can simply use Drools Fusion's temporal operators (after, before, etc.):
when
MyClass( myDate after $someOtherDate )
then
...
但是,您的方法还有另一个问题:您不应在某种情况下反对全局变量。请参阅专家手册部分关于全局变量。
However, there is another problem with your approach: you should not reason against a global in a condition. See the Expert manual section on globals.
在Drools中,现在的概念有些问题。实际上,有问题可能不是正确的词;让我们来谈谈棘手。您如何表示它在很大程度上取决于您的用例。我将尝试总结一下:
The notion of "now" is a bit problematic in Drools. Actually, maybe "problematic" isn't the right word; let's go with "tricky". How you represent it depends heavily on your use case. I'll try to summarize:
- 如果您在无状态会话中执行,那么您的方法将与Fusion运算符结合使用。但是,仍然不建议这样做。更好的方法是定义一个事实,将其称为
Now
,其中包含一个Date
字段。初始化并与其他事实一起插入它,然后针对它而不是全局原因进行反对。 - 如果您有状态的会话,它将变得更加棘手,因为即使会话处于空闲,这意味着您的
Now
事实越来越过时了。解决问题的方法是使用WorkingMemoryEventListener
。我们使用objectInserted
,objectRetracted
和objectUpdated
方法这个侦听器可以使我们的Now
事实保持最新(我们不在乎精确度不到一分钟,因此我们检查从上次更新起是否经过一分钟以避免不必要的开销)。规则不会评估工作内存是否没有更改,因此使用此侦听器足以确保在需要时更新Now
(除非您有依赖于查询的查询)关于当前值Now
,但这是另一个主题。) - 您还可以考虑在STREAM模式下使用Drools Fusion,具有现在的概念,但还提出了一些其他要求。有关更多信息,请参见 Fusion docs 信息。
- If you are executing within a stateless session, then your approach will work in combination with the Fusion operators. It is still not a recommended practice, however. A better approach is to define a fact, call it
Now
, which contains a singleDate
field. Initialize and insert it along with your other facts then reason against it instead of a global. - If you have a stateful session, it gets more tricky because real time is passing even while the session is idle, meaning your
Now
fact is getting more and more out of date. The way we have solved this is via the use of aWorkingMemoryEventListener
. We use theobjectInserted
,objectRetracted
, andobjectUpdated
methods of this listener to keep ourNow
fact current (we do not care about precision less than one minute so we check whether a minute has passed from the last update to avoid unnecessary overhead). Rules won't evaluate if working memory isn't changing, so using this listener is sufficient to ensure thatNow
is updated when it needs to be (unless you have queries that depend on the current value ofNow
, but that's another topic). - You could also look into using Drools Fusion in STREAM mode, which has a notion of "now", but also imposes a few other requirements. See the Fusion docs for more information.
这篇关于在规则的LHS中使用java.util.Date的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!