Expression Evaluation - if then else¶
In this rule users can provide “if then else” expression, it will be evaluated and the result will be populated in the output filtered JSON format against a new JSON tag “expressionResult”. This can be very useful for data transformation for a single sensor on the fly. Example - A temperature sensor providing raw data in terms of Fahrenheit, which has to be transformed to Centigrade. This equation can be written in “if then else” condition.
Simple if then else¶
Below is the create rule json for if then else filtering:-
{
"RuleName": "Demo rule expression eval",
"RuleDescription": "Demo rule for user guide",
"input": {
"DirectExchange": "Direct.Exchange",
"RoutingKey": "e325f4fe-a0e3-4252-82af-4c24deb8c0cf"
},
"params": [
{
"name": "feature",
"value": "room1"
},
{
"name": "sensorID",
"value": "NEXUS1"
},
{
"name": "time",
"value": {
"starttime": "",
"endtime": ""
}
},
{
"name": "expression",
"value": {
"if": "systolic>80",
"then": "return \"high\";",
"else": "return \"low\";"
}
},
{
"name": "",
"type": "rectangle/circles/polygon",
"geoFencing": "YES/NO",
"value": {
"points": [
{
"x": "",
"y": ""
},
{
"x": "",
"y": ""
}
]
}
},
{
"name": "ObservedProperties",
"value": [
{
"name": "",
"type": "Text/quantity/count",
"value": "",
"operator": ""
}
]
}
],
"output": {
"TopicExchange": "DemoExchange",
"Topic": "demo6"
}
}
As we can see in the above rule json under the parameter expression if then else condition is defined. The observed property systolic on input json will be checked and depending on the condition it will give the output. Output json will get added with another output parameter as:
Note
If-then-else conditions variables can be observed properties names, sensor, feature, starttime, endtime of a sensor. Also dynamically values can be retrieved using vars.get(“property name”) where “property name” is a value derived from a variable.
Now we will create the above rule in the following 2 ways:
Using MR API swagger
Using MR portal
Using MR API swagger¶
Click on POST /routingRules api in swagger, use the above json template and hit
Try it out!
button. It will create a new MR rule with expression parameter value as shown in the figure below:

As we can see in the above figure MR rule created with rule ID - 1124 will be used to start this rule. We can refer to start rule api to start rule.
Using MR portal¶
Click on
Create Rule
button in MR rule, fill in all the details as mentioned in the figure below and clickCreate Rule
button. It will create a new MR rule with expression as shown in the figure below:

After clicking
Create Rule
button the below message will be dispalyed at the upper right corner with rule ID. Here in our case rule ID is 1125.

As we can see in the above figure MR rule created with rule ID - 1125 which will be used to start this rule. We can refer to the start rule section through MR portal to start rule.
Send Request operation for systolic value greater than MR rule value
After the rule is created it needs to be started as mentioned above and then input json can be posted to MR rule using send request API. For this we have to configure and start SSE viewer page as it is mentioned in send request section.
Below is the input SOS observation json
{
"version": "1.0.1",
"observations": [
{
"sensor": "NEXUS1",
"feature": "room1",
"record": [
{
"starttime": "15-JAN-2016 15:30:03 IST",
"output": [
{
"name": "OutsideSensorTemp",
"value": "46.0",
"type": "decimal"
},
{
"name": "systolic",
"value": "99",
"type": "quantity"
}
]
}
],
"meta-data": []
}
]
}
Here in input json we can see that sensor name is NEXUS1 ,feature is room1 and systolic is 99 which is in greater than given if condition of MR rule value as 80. Therefore the coming output has the expressionResult value as low. We can now use send request API to verify this rule as shown in the figures below:

After the data is sent to MR rule we can see that output of MR rule is received in SSE viewer as shown in the figure below. Here we can see that an addtion parameter has got added in the output of MR rule as expressionResult.
{
"version": "1.0.1",
"observations": [
{
"sensor": "NEXUS1",
"feature": "room1",
"record": [
{
"starttime": "15-JAN-2016 15:30:03 IST",
"output": [
{
"name": "OutsideSensorTemp",
"value": "46.0",
"type": "decimal"
},
{
"name": "systolic",
"value": "99",
"type": "quantity"
},
{
"name": "expressionResult",
"value": "high"
}
]
}
],
"meta-data": []
}
]
}

For systolic value less than the given MR rule below is the input for MR :-
{
"version": "1.0.1",
"observations": [
{
"sensor": "NEXUS1",
"feature": "room1",
"record": [
{
"starttime": "15-JAN-2016 15:30:03 IST",
"output": [
{
"name": "OutsideSensorTemp",
"value": "46.0",
"type": "decimal"
},
{
"name": "systolic",
"value": "77",
"type": "quantity"
}
]
}
],
"meta-data": []
}
]
}
Similarly for systolic value lesser than the given MR rule value the output data will have expression result value as “low”:
{
"version": "1.0.1",
"observations": [
{
"sensor": "NEXUS1",
"feature": "room1",
"record": [
{
"starttime": "15-JAN-2016 15:30:03 IST",
"output": [
{
"name": "OutsideSensorTemp",
"value": "46.0",
"type": "decimal"
},
{
"name": "systolic",
"value": "77",
"type": "quantity"
},
{
"name": "expressionResult",
"value": "low"
}
]
}
],
"meta-data": []
}
]
}
Nested if then else¶
In nested if then else condition we can have mutiple if then else clause with different conditions and their outputs. Output will always be the value under parameter “expressionResult” as stated above.
Below is the create rule json portion for nested if then else filtering:
{
"name": "expression",
"value": {
"if": "systolic>80",
"then": "return \"high\";",
"else": "if ( systolic<= 80 && systolic > 50 ) {return \"medium\";} else {if (systolic <= 50){return \"low\";}}"
}
}
Now we will create the above rule in the following 2 ways:
Using MR API swagger
Using MR portal
Using MR API swagger¶
Click on POST /routingRules api in swagger, use the above json template and hit
Try it out!
button, it will create a new MR rule with expression parameter value as shown in the figure below:

As we can see in the above figure MR rule created with rule ID - 1128 will be used to start this rule. We can refer to start rule api to start rule.
Using MR portal¶
Click on
Create Rule
button in MR rule, fill in all the details as mentioned in the figure below and clickCreate Rule
button, it will create a new MR rule with expression as shown in the figure below:

After clicking
Create Rule
button below message will be dispalyed at the upper right corner with rule ID. Here in our case rule ID is 1129.

As we can see in the above figure MR rule created with rule ID - 1129 will be used to start this rule. We can refer to start rule section through MR portal to start rule.
Send Request operation for systolic value medium to 50 and 80 of MR rule value.
After the rule is created it needs to be started as mentioned above and then input json can be posted to MR rule using send request API. For this we have to configure and start SSE viewer page as it is mentioned in send request section.
Below is the input SOS observation json
{
"version": "1.0.1",
"observations": [
{
"sensor": "NEXUS1",
"feature": "room1",
"record": [
{
"starttime": "15-JAN-2016 15:30:03 IST",
"output": [
{
"name": "OutsideSensorTemp",
"value": "46.0",
"type": "decimal"
},
{
"name": "systolic",
"value": "51",
"type": "quantity"
}
]
}
],
"meta-data": []
}
]
}
Here in input json we can see that sensor name is NEXUS1, feature is room1 and systolic is 51 which is in medium to MR value of 50 and 80 in if condition of rule. Therefore the output which came is having expressionResult value as medium. We can now use send request API to verify this rule as shown in the figures below:

After the data is sent to MR rule we can see that output of MR rule is received in SSE viewer as shown in the figure below. Here we can see that an addtional parameter has got added in the output of MR rule as expressionResult.
{
"version": "1.0.1",
"observations": [
{
"sensor": "NEXUS1",
"feature": "room1",
"record": [
{
"starttime": "15-JAN-2016 15:30:03 IST",
"output": [
{
"name": "OutsideSensorTemp",
"value": "46.0",
"type": "decimal"
},
{
"name": "systolic",
"value": "51",
"type": "quantity"
},
{
"name": "expressionResult",
"value": "medium"
}
]
}
],
"meta-data": []
}
]
}

Send Request operation for systolic value less than 50 where the output result will be low, as shown below:
Below is the input SOS observation json:
{
"version": "1.0.1",
"observations": [
{
"sensor": "NEXUS1",
"feature": "room1",
"record": [
{
"starttime": "15-JAN-2016 15:30:03 IST",
"output": [
{
"name": "OutsideSensorTemp",
"value": "46.0",
"type": "decimal"
},
{
"name": "systolic",
"value": "45",
"type": "quantity"
}
]
}
],
"meta-data": []
}
]
}

Below is the ouput SOS observation json:
{
"version": "1.0.1",
"observations": [
{
"sensor": "NEXUS1",
"feature": "room1",
"record": [
{
"starttime": "15-JAN-2016 15:30:03 IST",
"output": [
{
"name": "OutsideSensorTemp",
"value": "46.0",
"type": "decimal"
},
{
"name": "systolic",
"value": "45",
"type": "quantity"
} ,
{
"name": "expressionResult",
"value": "low"
}
]
}
],
"meta-data": []
}
]
}

Complex if then else¶
In complex if then else condition we can do mathematical operation like add, subract, multiply and divide. Also we can add a new variabe and assign the resultant value after mathematical operation. Output will always be the value under parameter “expressionResult” as stated above.
Below is the create rule json portion for complex if then else filtering:
{
"name": "expression",
"value": {
"if": "temp>80",
"then": "Fahrenheit= (temp/5)*9+32; return \"Value in Fahrenheit :: \"+Fahrenheit ",
"else": "Kelvin= (java.lang.Integer)temp + 273 ; return\"Value in Kelvin:: \"+Kelvin"
}
}
Here in the above json we can see that the conversion of temperature to fahrenheit and kelvin is done based upon the condition provided in ‘if’ clause. We have taken Fahrenheit and kelvin variable and the resultant is assigned to them.
Note
In order to perform add operation on integer value you have to type cast the property to Integer as (java.lang.Integer)temp + 273 or multiply the property by 1.0 before + operator, else it will concat them as two string types.
Now we will create the above rule in the following 2 ways:
Using MR API swagger
Using MR portal
Using MR API swagger¶
Click on POST /routingRules api in swagger, use the above json template and hit
Try it out!
button , it will create a new MR rule with expression parameter value as shown in the figure below:

As we can see in the above figure MR rule created with rule ID - 1132 which will be used to start this rule. We can refer to start rule api to start rule.
Using MR portal¶
Click on
Create Rule
button in MR rule, fill in all the details as mentioned in the figure below and clickCreate Rule
button. It will create a new MR rule with expression as shown in the figure below:

After clicking
Create Rule
button below message will be dispalyed at the upper right corner with rule ID. Here in our case rule ID is 1133.

As we can see in the above figure MR rule created with rule ID - 1133 which will be used to start this rule. We can refer to start rule section through MR portal to start rule.
Send Request operation for temp value less than 80 where the output result will be temperature in kelvin, as shown below:
Below is the input SOS observation json :-
{
"version": "1.0.1",
"observations": [
{
"sensor": "NEXUS1",
"feature": "room1",
"record": [
{
"starttime": "15-JAN-2016 15:30:03 IST",
"output": [
{
"name": "OutsideSensorTemp",
"value": "46.0",
"type": "decimal"
},
{
"name": "temp",
"value": "31",
"type": "quantity"
}
]
}
],
"meta-data": []
}
]
}

Below is the ouput SOS observation json :-
{
"version": "1.0.1",
"observations": [
{
"sensor": "NEXUS1",
"feature": "room1",
"record": [
{
"starttime": "15-JAN-2016 15:30:03 IST",
"output": [
{
"name": "OutsideSensorTemp",
"value": "46.0",
"type": "decimal"
},
{
"name": "temp",
"value": "31",
"type": "quantity"
},
{
"name": "expressionResult",
"value": "Value in Kelvin:: 304"
}
]
}
],
"meta-data": []
}
]
}

Send Request operation for temp value greater than 80 where the output result will be temperature in Fahrenheit, as shown below:
Below is the input SOS observation json:
{
"version": "1.0.1",
"observations": [
{
"sensor": "NEXUS1",
"feature": "room1",
"record": [
{
"starttime": "15-JAN-2016 15:30:03 IST",
"output": [
{
"name": "OutsideSensorTemp",
"value": "46.0",
"type": "decimal"
},
{
"name": "temp",
"value": "85",
"type": "quantity"
}
]
}
],
"meta-data": []
}
]
}

Below is the ouput SOS observation json:
{
"version": "1.0.1",
"observations": [
{
"sensor": "NEXUS1",
"feature": "room1",
"record": [
{
"starttime": "15-JAN-2016 15:30:03 IST",
"output": [
{
"name": "OutsideSensorTemp",
"value": "46.0",
"type": "decimal"
},
{
"name": "temp",
"value": "85",
"type": "quantity"
},
{
"name": "expressionResult",
"value": "Value in Fahrenheit :: 185"
}
]
}
],
"meta-data": []
}
]
}

Multiple prints with custom key¶
In this rule users can provide “if then else” expression, it will be evaluated and result will be populated in the output filtered JSON format against a new JSON tag “expressionResult”. This can be very useful for data transformation for a single sensor on the fly. Example - temperature sensor providing raw data in terms of Fahrenheit, which has to be be transformed to Centigrade. This equation can be written in “if then else” condition.
Below is the create rule json:
{
"RuleName": "MultiplePrint",
"RuleDescription": "Multiple expression result print",
"parallelismFactor": 1,
"input": {
"DirectExchange": "Direct.Exchange",
"RoutingKey": "uKK6WjaZZnnYBj1G6rONQgNAIuc="
},
"params": [
{
"name": "feature",
"value": ""
},
{
"name": "sensorID",
"value": "blood-sensor-22"
},
{
"name": "time",
"value": {
"starttime": "",
"endtime": ""
}
},
{
"name": "expression",
"value": {
"if": "systolic> 120 ",
"then": "print(\"evaluation\",\"high\");print(\"type\",\"blood pressure\"); ",
"else": ""
}
},
{
"name": "ObservedProperties",
"value": []
}
],
"output": {
"TopicExchange": "testExchange",
"Topic": "multiprint.*"
}
}
Note
Multiple print() function can be written as shown above. print() function accept key and value pair. No need to write “return” if print() function is used. If you write “return” keyword its value will only be returned and will be appended in the JSON as a JSON tag “expressionResult”.
Route to multiple topics¶
If the rule condition is satisfied then the data is published to a topic as defined in the rule output section.
In case rule needs to support MVEL based filters. Some example filters are:
Route to specific topic based on comparison of input date with a specific MVEL
Route to specific topic based on comparison of observed properties with a specific MVEL
Topic can be set in the if-then-else condition using setTopic() function as shown below.
Below is the create rule json:
{
"Status": "Running",
"RuleID": 761,
"RuleName": "MultiplePrint1",
"RuleDescription": "Multiple expression result print",
"parallelismFactor": 1,
"input": {
"DirectExchange": "Direct.Exchange",
"RoutingKey": "uKK6WjaZZnnYBj1G6rONQgNAIuc="
},
"params": [
{
"name": "feature",
"value": ""
},
{
"name": "sensorID",
"value": "blood-sensor-22"
},
{
"name": "time",
"value": {
"starttime": "",
"endtime": ""
}
},
{
"name": "expression",
"value": {
"if": "systolic> 120",
"then": "setTopic(\"topic1\");print(\"evaluation\",\"high\");print(\"type\",\"blood pressure\");",
"else": "print(\"evaluation\",\"not high\");"
}
},
{
"name": "ObservedProperties",
"value": []
}
],
"output": {
"TopicExchange": "testExchange",
"Topic": "multiprint.*"
}
}
Note
setTopic() function can be written as shown above. setTopic() function accepts only topic name. Exchange name will be as defined in the output section of the rule. setTopic() function can be invoked from all the three if-else-then expressions. In case setTopic() function is not defined then default topic name will be selected as defined in the rule.
Logical AND with rest of conditions¶
User can call setLogic(“AND”) function in the if-then-else condition provided return value 0 or 1 or positive, then setLogic() function can do the AND operation with the rest of the condition.
Below is the create rule json:
{
"Status": "Running",
"RuleID": 781,
"RuleName": "MultiplePrint2",
"RuleDescription": "Multiple expression result print",
"parallelismFactor": 1,
"input": {
"DirectExchange": "Direct.Exchange",
"RoutingKey": "uKK6WjaZZnnYBj1G6rONQgNAIuc="
},
"params": [
{
"name": "feature",
"value": ""
},
{
"name": "sensorID",
"value": "blood-sensor-23"
},
{
"name": "time",
"value": {
"starttime": "",
"endtime": ""
}
},
{
"name": "expression",
"value": {
"if": "setLogic(\"AND\");systolic> 120",
"then": "return 1;",
"else": "return 0;"
}
},
{
"name": "ObservedProperties",
"value": []
}
],
"output": {
"TopicExchange": "testExchange",
"Topic": "multiprint2.*"
}
}
Note
If setLogic(“AND”) is set and return value is 0 or null then no event output will be generated.