Logical Pattern

Logical patterns match events that arrive in temporal order and correlate them with logical relationships such as and, or and not.

Refer the Siddhi query guide for more information.

define stream RegulatorStateChangeStream(deviceID long,
    roomNo int, tempSet double, action string);

define stream RoomKeyStream(deviceID long, roomNo int,
    action string);

@sink(type='log')
define stream RegulatorActionStream(roomNo int, action string);

from every e1=RegulatorStateChangeStream[ action == 'on' ]
     -> e2=RoomKeyStream
            [ e1.roomNo == roomNo and action == 'removed' ]
        or e3=RegulatorStateChangeStream
            [ e1.roomNo == roomNo and action == 'off']
select e1.roomNo,
    ifThenElse( e2 is null, 'none', 'stop' ) as action
having action != 'none'
insert into RegulatorActionStream;
define stream RegulatorStateChangeStream(deviceID long,
    roomNo int, tempSet double, action string);

Defines RegulatorStateChangeStream having information of regulator state change such as deviceID, roomNo, tempSet and action.

define stream RoomKeyStream(deviceID long, roomNo int,
    action string);

Defines RoomKeyStream which contains the events related to room key usage.

@sink(type='log')
define stream RegulatorActionStream(roomNo int, action string);

Defines RegulatorActionStream which contains the events related to regulator state changes.

from every e1=RegulatorStateChangeStream[ action == 'on' ]
     -> e2=RoomKeyStream
            [ e1.roomNo == roomNo and action == 'removed' ]
        or e3=RegulatorStateChangeStream
            [ e1.roomNo == roomNo and action == 'off']

Sends a stop action on RegulatorActionStream stream, if a removed action is triggered in the RoomKeyStream stream before the regulator state changing to off which is notified RegulatorStateChangeStream stream

select e1.roomNo,
    ifThenElse( e2 is null, 'none', 'stop' ) as action
having action != 'none'

Checks whether pattern triggered due to removal of room key.

insert into RegulatorActionStream;

This application sends a stop action on the regulator if a removed action is triggered in the RoomKeyStream stream.

Input

  • First, below event is sent to RegulatorStateChangeStream,

    [10, 5, 30, on]

  • Then, send below events are sent to RoomKeyStream,

    [10, 5, removed]

Output

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

[5, stop]

Top