Drools:获取3个最新事件 [英] Drools: get the 3 most recent Events
问题描述
我正在研究一个小的Drools项目,因为我想了解更多有关使用规则引擎的信息。我有一个名为 Event
的类,该类具有以下字段:
I'm working on a small Drools project, because I want to learn more about using rule engines. I have a class called Event
that has the following fields:
-
String标签;
可以是任何字符串的标签。 -
long millis;
时间戳记。 (实际上,这是从同样在Event
中的JodaTimeLocalDate
字段转换的。) -
int值;
我要说明的值。
String tag;
A tag which can be any string.long millis;
A timestamp. (Actually, this is converted from a JodaTimeLocalDate
field which is also inEvent
.)int value;
A value that I want reason about.
我将数百个 Event
实例插入我的知识库中,现在我想获取3个最新的带有标记的事件
。我想出了下面的代码,它可以正常工作:
I insert several hundreds of Event
instances into my knowledge base, and now I want to get the 3 most recent events that are tagged with "OK"
. I came up with the following code, which works:
rule "Three most recent events tagged with 'OK'"
when
$e1 : Event( tag == "OK",
$millis1 : millis )
$e2 : Event( tag == "OK",
millis < $millis1, $millis2 : millis )
$e3 : Event( tag == "OK",
millis < $millis2, $millis3 : millis )
not Event( tag == "OK",
millis > $millis1 )
not Event( tag == "OK",
millis > $millis2 && millis < $millis1 )
not Event( tag == "OK",
millis > $millis3 && millis < $millis2 )
then
# Do something with $e1.value, $e2.value and $e3.value
end
但是我有一种感觉应该是一种更好的方法。这非常冗长,而且不容易重复使用:如果我想获得 value>的五个最新事件怎么办?例如10
?我最终会粘贴很多代码,但我不想这样做:)。
另外,代码对我来说看起来也不是很美丽。我真的不喜欢重复的 not Event ...
约束,而且我也不想一遍又一遍地重复相同的标记条件。 (此示例是我的真实应用程序的高度简化版本,其中的条件实际上要复杂得多。)
But I have a feeling there should be a better way to do this. This is quite verbose and not easily re-used: what if I want to get the five most recent events with value > 10
, for example? I would end up copy-pasting a lot of code, and I don't want to do that :).
Also, the code doesn't look very 'beautiful' to me. I don't really like the repeated not Event...
constraints, and I also don't like having to repeat the same tag condition over and over. (This example is a heavily simplified version of my real app, in which the condition is actually a lot more complex.)
如何改进此代码?
推荐答案
假设您使用的是STREAM事件处理模式,并且事件在流中排序:
Assuming you are using the STREAM event processing mode and your events are ordered in the stream:
rule "3 most recent events"
when
accumulate( $e : Event( tag == "OK" ) over window:length(3),
$events : collectList( $e ) )
then
// $events is a list that contains your 3 most recent
// events by insertion order
end
=====编辑====
===== edit ====
根据下面的评论,这是在Drools 5.4+中实现所需功能的方法:
Based on your comment bellow, here is how to achieve what you want in Drools 5.4+:
declare window LastEvents
Event() over window:length(3)
end
rule "OK events among the last 3 events"
when
accumulate( $e : Event( tag == "OK" ) from window LastEvents,
$events : collectList( $e ) )
then
// $events is a list that contains the OK events among the last 3
// events by insertion order
end
请仔细检查语法,因为我会很认真地执行此操作,但是应该很接近
Just double check the syntax as I am doing this by heart, but it should be close to this.
这篇关于Drools:获取3个最新事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!