Search code examples
javarule-enginecomplex-event-processingesper

Esper rule language: fire rule only after condition is fulfilled for a certain time period


I want to write a rule that fires when the heart rate is above 160 for 5 minutes. The rule I came up with is the following:

 EPAdministrator cepRule3 = cep.getEPAdministrator();
        EPStatement cepStatementRule3 = cepRule3.createEPL("select * from "
                + "HeartRate.win:time(5 min) "
                + "group by macAddress "
                + "having min(heartrate) > 160");
        cepStatementRule3.addListener(new rule3Listener());

My HeartRate class has the following fields:

int heartrate;
String heartratesTimestamp;
String macAddress;

The problem i'm facing is that this rule fires every time there is a heart rate above 160. Instead I want it to only fire when the heart rate remains above 160 for 5 minutes. How can I adjust this rule?


Solution

  • EPL: every(HeartRate(heartrate>160) -> (timer:interval(5 min) and not HeartRate(heartrate<=160))

    or simply with and in Java code: EPStatement cepStatementRule3 = cepRule3.createEPL("every(HeartRate(heartrate>160) -> (timer:interval(5 min) and not HeartRate(heartrate<=160))"); cepStatementRule3.addListener(new rule3Listener());

    Look up the EPL pattern syntax in the Esper documentation.