Drools - Repeated Events and Temporal Relations

一笑奈何 提交于 2020-06-24 13:43:48

问题


In general I am writing rules for events which equal (by attributes values) events can occur any time in consecutive manner (every second). I want to fire rules for matched events only on an hourly bases.

In more details: I want to fire a rule when an event is inserted for the first time (not exist yet) OR when an event is inserted and if and only if equal events are already inserted to the working memory BUT the newest of them is at least one hour ago old.

What is a reasonable way of writing a rule of that kind, taking events duration will be 24H?


回答1:


rule X
when
    $e : MyEvent() from entry-point "s"
    not( MyEvent( this != $e, id == $e.id, this before[0s,1h] $e ) from entry-point "s" )
then
    // $e arrived and there is no other event with the same
    // id that happened during the last hour
end

Replace "id == $e.id" by whatever constraints you use to decide two events are related to each other.




回答2:


You could create a global queue like this:

global java.util.List eventQueue;

Your also need to access your global queue from java, so just use:

session.getGlobals();
session.setGlobal(name, value);

In this queue save an event and related time. Then check hourly form java code this queue, and execute rules based on timestamp. This is not poor drools approach, but is straightforward.



来源:https://stackoverflow.com/questions/4415806/drools-repeated-events-and-temporal-relations

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!