Drools 使用时态规则时,插入/计算会逐渐变慢
我有一个规则,为同一实体查找两个连续事件。为了对其进行压力测试,我插入了10K个连续事件。我在插入每个事件后调用fireAllRules。我在每100个事件后打印时间戳。我注意到,随着事件的增加,插入/评估的速度越来越慢。规则如下:Drools 使用时态规则时,插入/计算会逐渐变慢,drools,drools-fusion,Drools,Drools Fusion,我有一个规则,为同一实体查找两个连续事件。为了对其进行压力测试,我插入了10K个连续事件。我在插入每个事件后调用fireAllRules。我在每100个事件后打印时间戳。我注意到,随着事件的增加,插入/评估的速度越来越慢。规则如下: rule "Consecutive events" when $latest : Event($id : id) // newest event not Event(id == $id, this after $latest) // no event
rule "Consecutive events"
when
$latest : Event($id : id) // newest event
not Event(id == $id, this after $latest) // no events after the newest
$previous : Event(id == $id, this before $latest) // event before the newest
not Event(id == $id, this before $latest, this after $previous) // no events between $latest and $previous
then
//System.out.println($latest.toString());
end
我的理解是,上述规则应该只匹配最新的2个事件,并且自动内存管理应该删除较旧的事件。如果是这样,为什么插入会逐渐变慢?有趣的是,ksession.getObjects返回所有插入的事件,而不仅仅是最近的两个事件。我对规则的理解有误吗?这条规则是否以某种方式迫使所有事件留在记忆中?我使用的是v6.0.1.Final。您的规则没有定义自动收回的时间限制,因为之前和之后都没有限制 有几种方法可以将事件数保持在较低的水平,这就是插入速度逐渐变慢的原因。下面是一个简单的技巧:
declare Pair
one : Event
two : Event
id : String
end
rule "create Pair"
when
$e: Event( $id: id )
not Pair( id == id )
then
insert( new Pair( null, $e, $id ) );
end
rule "another Pair"
when
$e2: Event( $id: id )
$p: Pair( $e0: one, $e1: two != $e2, id == $id )
then
modify( $p ){
setOne( $e1 ),
setTwo( $e2 ) }
retract( $e0 ); // optional
// process pair $e1, $e2
end
谢谢经过一些小的修改后,你的技巧很有魅力——主要是在另一对规则中使用了无循环。