Drools 规则仅对插入的事件触发

Drools rule fire only for inserted event

我有多种用途,每个传感器都可以发送计步器数据。我有一个基于 macAddress 的规则文件,触发规则:

declare Steps
    @role(event)
end

declare User
    @role(fact)
end

rule "MAC"
when
  User( $mac: macAddress ) from entry-point "entrySteps"
then end

rule "ACC STEPS RULE" extends "MAC"
when
    accumulate( Steps( $s : steps , macAddress == $mac )
            over window:time( 1h ) from entry-point "entrySteps"; 
        $fst: min( $s ), $lst: max( $s );
        $lst - $fst < 50 )
then
    System.out.println($lst + "   " + $fst);
    Action.handleAction($mac,"STEPS RULE: get moving!");
end

我的用户只有一个字段 macAddress,步骤事件有以下字段:

double steps;
Date timeStamp;
String macAddress;

现在,当我为每个 macAddress 插入一个事件时,如果具有该 macAddress 的用户在过去一小时内的步数少于 50,则规则将被触发。因此,如果满足此条件,则规则将为每个 macAddress 触发。 但我希望该规则只能针对插入的 Step 事件的 macAddress 触发。我该如何调整我的规则?

奇怪的是,当只有一个用户并且没有该用户的步骤时,也会触发,即 window 为空。输出包含最小和最大双精度值 - 不确定这是否是 Drools 中的错误。

作为解决方法,添加对累积计数的测试,可能大于 0 或大于 1。

accumulate( Steps( $s : steps , macAddress == $mac )
        over window:time( 1h ) from entry-point "entrySteps";
    $fst: min( $s ), $lst: max( $s ), $cnt: count( $s );
    $cnt > 0, $lst - $fst < 50 )