Search code examples
droolsdrools-fusion

What is the relationship between @timestamp and "over window:time"?


I'm using Drools 6.2.0.Final and I need to process a set of event using window:time. Each event has a date's field.

public class Event {

    private Long id;

    private Date date;
        ...

And in my 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)

Scenario 1: Using realtime and inserting a set a event simultaneously.

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();

Scenario 2: Using pseudo, inserting a set a event simultaneously and adding to clock the event's offset.

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();

The second scenario runs fine. But I have some questions:

  • What is the relationship between @timestamp and "over window:time"?
  • What happends if a need to insert unsorted events (by timestamp) in Working memory?
  • Can I use the timestamp denoted by my event instead of the timestamp denoted by the insert's time?

Thanks.

UPDATE 1

@timestamp, @duration etc. are only used to relate events together (e.g A before B, A meets B, and so on), and that they do not relate the event to the clock. But "over window:time" is based on Drools's clock. The window's time uses the moment that the event was inserted in working memory to match the rule. You need to use Drools stream mode.


Solution

  • What is the relationship between @timestamp and "over window:time"? A window with length d selects an event containing the timestamp x if now.d < x <= now.

    What happens if I need to insert unsorted events (by timestamp) in Working memory? You shouldn't do that unless the Engine is in "cloud" mode. The timestamp is basically nothing but a long value, and can be evaluated all the same. However, you should think carefully before doing this, as this may produce results that differ from an execution where inserts are done in the proper order.

    Can I use the timestamp denoted by my event instead of the timestamp denoted by the insert's time? It appears that you are doing just that, due to @timestamp(date) in the DRL declare statement.