Math & Logical Operation

Provides examples on performing math or logical operations on events.

To see all complex math operations that can be performed please see Siddhi Execution Math Docs

define stream TemperatureStream
        (sensorId string, temperature double);

@infor(name = 'celciusTemperature')
from TemperatureStream
select sensorId, 
                (temperature * 9 / 5) + 32 as temperature
insert into FahrenheitTemperatureStream;

@info(name = 'Overall-analysis')
from FahrenheitTemperatureStream
select sensorId, 
                math:floor(temperature) as approximateTemp 
insert all events into OverallTemperatureStream;

@info(name = 'RangeFilter') 
from OverallTemperatureStream
              [ approximateTemp > -2 and approximateTemp < 40]
select *
insert into NormalTemperatureStream;
define stream TemperatureStream
        (sensorId string, temperature double);
@infor(name = 'celciusTemperature')
from TemperatureStream
select sensorId, 
                (temperature * 9 / 5) + 32 as temperature

Converts Celsius value into Fahrenheit.

insert into FahrenheitTemperatureStream;
@info(name = 'Overall-analysis')
from FahrenheitTemperatureStream
select sensorId, 
                math:floor(temperature) as approximateTemp 

Calculate approximated temperature to the first digit

insert all events into OverallTemperatureStream;
@info(name = 'RangeFilter') 
from OverallTemperatureStream
              [ approximateTemp > -2 and approximateTemp < 40]

Filter out events where -2 < approximateTemp < 40

select *
insert into NormalTemperatureStream;

Input

Below event is sent to TemperatureStream,

['SensorId', -17]

Output

After processing, the following events will be arriving at each stream:

  • FahrenheitTemperatureStream:
    ['SensorId', 1.4]
  • OverallTemperatureStream:
    ['SensorId', 1.0]
  • NormalTemperatureStream:
    ['SensorId', 1.0]
Top