1

我正在使用 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 流模式。

4

1 回答 1

1

@timestamp 和“超过窗口:时间”之间有什么关系?一个长度为 d 的窗口选择一个包含时间戳 x if 的事件now.d < x <= now

如果我需要在工作内存中插入未排序的事件(按时间戳)会发生什么?除非引擎处于“云”模式,否则您不应该这样做。时间戳基本上只是一个长值,并且可以同样进行评估。但是,您应该在执行此操作之前仔细考虑,因为这可能会产生与以正确顺序完成插入的执行不同的结果。

我可以使用我的事件表示的时间戳而不是插入时间表示的时间戳吗?由于@timestamp(date)在 DRL 声明语句中,您似乎正在这样做。

于 2015-03-25T18:42:41.873 回答