Counting Pattern

Counting patterns allow to match multiple events that may have been received for the same matching condition. The number of events matched per condition can be limited via condition postfixes.

Refer the Siddhi query guide for more information.

define stream TemperatureStream (sensorID long, roomNo int,
    temp double);

define stream RegulatorStream (deviceID long, roomNo int,
    tempSet double, isOn bool);

@sink(type = 'log')
define stream TemperatureDiffStream(roomNo int,
    tempDiff double);

from every( e1 = RegulatorStream)
    -> e2 = TemperatureStream[e1.roomNo == roomNo] < 1: >
    -> e3 = RegulatorStream[e1.roomNo == roomNo]
select e1.roomNo, e2[0].temp - e2[last].temp as tempDiff
insert into TemperatureDiffStream;
define stream TemperatureStream (sensorID long, roomNo int,
    temp double);

Defines TemperatureStream having information on room temperature such as sensorID, roomNo and temp.

define stream RegulatorStream (deviceID long, roomNo int,
    tempSet double, isOn bool);

Defines RegulatorStream which contains the events from regulator with attributes deviceID, roomNo, tempSet, and isOn.

@sink(type = 'log')
define stream TemperatureDiffStream(roomNo int,
    tempDiff double);

Defines TemperatureDiffStream which contains the events related to temperature difference.

from every( e1 = RegulatorStream)
    -> e2 = TemperatureStream[e1.roomNo == roomNo] < 1: >
    -> e3 = RegulatorStream[e1.roomNo == roomNo]

Calculates the temperature difference between two regulator events. Here, when at least one TemperatureStream event needs to arrive between two RegulatorStream events.

select e1.roomNo, e2[0].temp - e2[last].temp as tempDiff

Finds the temperature difference between the first and last temperature event.

insert into TemperatureDiffStream;

This application calculates the temperature difference between two regulator events. Here, when at least one TemperatureStream event occurs between two RegulatorStream events the pattern is valid and logs can be seen.

Input

  • First, below event is sent to RegulatorStream,

    [21, 2, 25, true]

  • Below events are sent to TemperatureStream,

    [21, 2, 29]

    [21, 2, 26]

  • Finally, below event is sent again to RegulatorStream,

    [21, 2, 30, true]

Output

After processing the above input events, the event arriving at TemperatureDiffStream will be as follows:

[2, 3.0]

Top