我正在使用 Drools 6.2.0.Final,我需要使用 window:time 处理一组事件。每个事件都有一个日期字段。
public class Event {
private Long id;
private Date date;
...
在我的 drl 中:
declare org.drools.examples.broker.events.Event
@role( event )
@timestamp (date)
end
rule "test"
when
$count: Number() from accumulate (
$e: Event() over window:time(40s) from entry-point "stream" ,
count($e))
then
System.out.println("Count:" + $count);
end
- e1 (2015-01-01 00:00:00)
- e2 (2015-01-01 00:00:20)
- e3 (2015-01-01 00:00:40)
- e4 (2015-01-01 00:01:00)
场景 1:使用实时并同时插入一组事件。
session.getEntryPoint("stream").insert(e1);
session.fireAllRules();
session.getEntryPoint("stream").insert(e2);
session.fireAllRules();
session.getEntryPoint("stream").insert(e3);
session.fireAllRules();
session.getEntryPoint("stream").insert(e4);
session.fireAllRules();
场景2:使用伪,同时插入一组事件并添加时钟事件的偏移量。
session.getEntryPoint("stream").insert(e1);
session.fireAllRules();
clock.advanceTime(20, TimeUnit.SECONDS);
session.getEntryPoint("stream").insert(e2);
session.fireAllRules();
clock.advanceTime(40, TimeUnit.SECONDS);
session.getEntryPoint("stream").insert(e3);
session.fireAllRules();
clock.advanceTime(60, TimeUnit.SECONDS);
session.getEntryPoint("stream").insert(e4);
session.fireAllRules();
第二种情况运行良好。但我有一些问题:
- @timestamp 和“超过窗口:时间”之间有什么关系?
- 如果需要在工作内存中插入未排序的事件(按时间戳)会发生什么?
- 我可以使用我的事件表示的时间戳而不是插入时间表示的时间戳吗?
谢谢。
更新 1
@timestamp、@duration 等仅用于将事件关联在一起(例如,A 在 B 之前,A 遇到 B,等等),并且它们不会将事件与时钟相关联。但是“over window:time”是基于 Drools 的时钟。窗口时间使用事件插入工作内存的时刻来匹配规则。您需要使用 Drools 流模式。