问题
I have a multiple uses with each a sensor that sends pedometer data. I have a rule file that based on the macAddress, fires the rule:
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
My User has just a field macAddress
and Steps events have the following fields:
double steps;
Date timeStamp;
String macAddress;
Now when I insert an event then for each macAddress, the rule will fire if the number of steps of a user with that macAddress in the last hour is less then 50. So the rule will fire for every macAddress if this condition is fulfilled. But I want that the rule can only fire for the macAddress of the inserted Step event. How can I adjust my rule?
回答1:
Strangely enough, there's also a firing when there is just a User and no Steps for that User, i.e., the window is empty. The output contains minimum and maximum double values - not sure whether this is a bug in Drools.
As a workaround, add a test of the accumulated count, perhaps greater 0 or greater 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 )
来源:https://stackoverflow.com/questions/36008719/drools-rule-fire-only-for-inserted-event