Search code examples
indexingpattern-matchingcomplex-event-processingesper

How to index events in patterns with Esper EPL (CEP)


I am not able to refer to a particular event with an index or sth. alike of the generated data tuple by the window defined (as I have understand the logic behind Esper CEP Engine). Without the possibility of a reference I can not implement the logic of a new CEP pattern within Esper EPL.

Already tried:
1) previous function, but not allowed within the pattern [e.g. from Esper Doc.: select prev(2, price) from Trade.win:length(10)]
2) indexing the tuple like in Python or Java [e.g. dataTuple[1] for second entry]
3) creating several parts (a, b, c...) and referencing them [e.g. a=EventA -> b=EventA < a.EventA-1]

Incoming events:
EventStream={id=1, Temp1Event=35.5, Temp2Event=37.2, PressureEvent=896.99}
t=t.plus(2 seconds)
EventStream={id=2, Temp1Event=37.4, Temp2Event=39.1, PressureEvent=869.5}
t=t.plus(2 seconds)
EventStream={id=3, Temp1Event=48.3, Temp2Event=51.9, PressureEvent=908.56}
t=t.plus(2 seconds)
EventStream={id=4, Temp1Event=55.7, Temp2Event=56.9, PressureEvent=928.82}
t=t.plus(2 seconds)
EventStream={id=5, Temp1Event=66.5, Temp2Event=39.48, PressureEvent=0}
t=t.plus(2 seconds)
EventStream={id=6, Temp1Event=52.3745930271536, Temp2Event=31.1136121636059, PressureEvent=0}
t=t.plus(2 seconds)
EventStream={id=7, Temp1Event=41.9102341671244, Temp2Event=24.9156396131152, PressureEvent=0}

The rule's logic:
Give a warning of an open pressure chamber door (=rule fires) if the pressure becomes '0' and due to this, the temperature is sinking rapidly.

My statement:
SELECT * FROM pattern [every (a=EventStream(PressureEvent=0) -> b=EventStream(Temp1Event[0] > 0.7*Temp1Event[2]))].win:length(3);

I expect the rule to fire if a) the pressure event becomes 0 and b) afterwards the temperature is sinking rapidly, proofed by the value from first incoming event against value of second incoming event.

I know that there are functions "lastEvent" and "firstEvent". Maybe they are a possible solution. Despite that, I hope to get a possibility similar to an index "DataTuple[x]" as I guess this is more flexible.

Thanks a lot in advance! :)


Solution

  • You could do this.

    SELECT * FROM pattern [every a=EventStream(PressureEvent=0) 
      -> b=EventStream
      -> c=EventStream(b.temp > 0.7*temp)]
    

    Within a pattern the "tag=" assigns a name to an event such as "b". The tag can then be used in expressions i.e. "b.temp".

    There are other solutions. The "prev" function or enumeration (i.e. take).