Skip to content

Commit

Permalink
Merge 0bfc9a0 into 5f4372e
Browse files Browse the repository at this point in the history
  • Loading branch information
cblanco committed Jan 31, 2019
2 parents 5f4372e + 0bfc9a0 commit 2c0e4ec
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
* sinon-chai
* grunt and grunt related module
* closure-linter-wrapper
- Add 'ruleName' as variable automatically in rule text field (EPL) on rule creation time (#208)
2 changes: 1 addition & 1 deletion documentation/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ A simplified format in JSON can be used to represent rules. The former visual ru
```json
{
"name" : "prueba-test",
"text" : "select *,\"prueba-test\" as ruleName from pattern [every ev=iotEvent((cast(id?, String) regexp \"asd\"))]",
"text" : "select * from pattern [every ev=iotEvent((cast(id?, String) regexp \"asd\"))]",
"action" : {
"type" : "email",
"template" : "DCA message",
Expand Down
2 changes: 1 addition & 1 deletion documentation/models.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Example:
},
"subservice" : "/",
"service" : "unknownt",
"text" : "select *,\"ReglaId\" as ruleName from pattern [every ev=iotEvent((cast(`id`?, String) regexp \"^value.*\"))]"
"text" : "select * from pattern [every ev=iotEvent((cast(`id`?, String) regexp \"^value.*\"))]"
}

```
Expand Down
17 changes: 8 additions & 9 deletions documentation/plain_rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ The “anatomy” of a rule is as follows
```json
{
"name":"blood_rule_update",
"text":"select *,\"blood_rule_update\" as ruleName, *, ev.BloodPressure? as Pressure, ev.id? as Meter from pattern [every ev=iotEvent(cast(cast(BloodPressure?,String),float)>1.5 and type=\"BloodMeter\")]",
"text":"select *, *, ev.BloodPressure? as Pressure, ev.id? as Meter from pattern [every ev=iotEvent(cast(cast(BloodPressure?,String),float)>1.5 and type=\"BloodMeter\")]",
"action":{
"type":"update",
"parameters":{
Expand Down Expand Up @@ -36,16 +36,15 @@ EPL is documented in [Esper website](http://www.espertech.com/esper/esper-docume
A EPL statement to use with perseo could be:

```
select *, "blood_rule_update" as ruleName,
ev.BloodPressure? as Pressure, ev.id? as Meter
select *, ev.BloodPressure? as Pressure, ev.id? as Meter
from pattern
[every ev=iotEvent(cast(cast(BloodPressure?,String),float)>1.5 and type="BloodMeter")]
[every ev=iotEvent(cast(cast(BloodPressure?,String),float)>1.5 and type="BloodMeter")]
```


* The rule name must be present with **ruleName** alias. It must be equal to the ‘name’ field of the rule object
* The *from* pattern must name the event as **ev** and the event stream from which take events must be **iotEvent**
* A *type=* condition must be concatenated for avoiding mixing different kinds of entities
* The name of the rule is not mandatory, but for backward compatibility, it can be present as ** ruleName ** alias (`e.g: select *, "blood_rule_update" as ruleName...`) in the select clause. If present, it must be equal to the ‘name’ field of the rule object. If it is not present, you can still use the variable 'ruleName' in the actions, because it will be included automatically.


The used entity's attributes must be cast to `float` in case of being numeric (like in the example). Alphanumeric
values must be cast to `String`. Nested cast to string and to float is something we are analyzing, and could be
Expand Down Expand Up @@ -335,7 +334,7 @@ could be used by a rule so
```json
{
"name": "blood_rule_email_md",
"text": "select *,\"blood_rule_email_md\" as ruleName, *,ev.BloodPressure? as Pression, ev.id? as Meter from pattern [every ev=iotEvent(cast(BloodPressure__metadata__crs__system?,String)=\"WGS84\" and type=\"BloodMeter\")]",
"text": "select *, *,ev.BloodPressure? as Pression, ev.id? as Meter from pattern [every ev=iotEvent(cast(BloodPressure__metadata__crs__system?,String)=\"WGS84\" and type=\"BloodMeter\")]",
"action": {
"type": "email",
"template": "Meter ${Meter} has pression ${Pression} (GEN RULE) and system is ${BloodPressure__metadata__crs__system}",
Expand Down Expand Up @@ -474,7 +473,7 @@ An example of rule taking advantage of these derived attributes could be:
```json
{
"name": "rule_distance",
"text": "select *, \"rule_distance\" as ruleName from pattern [every ev=iotEvent(Math.pow((cast(cast(position__x?,String),float) - 618618.8286057833), 2) + Math.pow((cast(cast(position__y?,String),float) - 9764160.736945232), 2) < Math.pow(5e3,2))]",
"text": "select *, from pattern [every ev=iotEvent(Math.pow((cast(cast(position__x?,String),float) - 618618.8286057833), 2) + Math.pow((cast(cast(position__y?,String),float) - 9764160.736945232), 2) < Math.pow(5e3,2))]",
"action": {
"type": "email",
"template": "${id} (${type}) is at ${position__lat}, ${position__lon} (${position__x}, ${position__y})",
Expand Down Expand Up @@ -654,7 +653,7 @@ A rule that will check if the employee has been hired in the last half hour, cou
```json
{
"name": "rule_time",
"text": "select *, \"rule_time\" as ruleName from pattern [every ev=iotEvent(cast(cast(hire__ts?,String),float) > current_timestamp - 30*60*1000)]",
"text": "select *, from pattern [every ev=iotEvent(cast(cast(hire__ts?,String),float) > current_timestamp - 30*60*1000)]",
"action": {
"type": "email",
"template": "So glad with our new ${role}, ${id}!",
Expand Down
27 changes: 27 additions & 0 deletions lib/models/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*
* For those usages not covered by the GNU Affero General Public License
* please contact with::[contacto@tid.es]
*
* Modified by: Carlos Blanco - Future Internet Consulting and Development Solutions (FICODES)
*/
'use strict';

Expand Down Expand Up @@ -132,6 +134,23 @@ function putR2core(rules, callback) {
myutils.requestHelperWOMetrics('put', {url: config.perseoCore.rulesURL, json: rulesAndContexts}, callback);
}

/**
* Add ruleName if necessary in rule text field
*
* @param rule The rule object
*/
function normalizeRuleName(rule) {

var newAs = '"' + rule.name + '" as ruleName';
// Add "name as ruleName" if not exist
if (rule.text && rule.text.indexOf(' as ruleName') === -1) {
var idx = rule.text.toLowerCase().indexOf('select') + 7;
rule.text = rule.text = rule.text.slice(0, idx) + newAs + ', ' + rule.text.slice(idx);
}

return rule;
}

module.exports = {
FindAll: function(service, subservice, callback) {
rulesStore.FindAll(service, subservice, function(err, data) {
Expand All @@ -150,6 +169,10 @@ module.exports = {
myutils.logErrorIf(localError);
return callback(localError);
}

// Normalize the rule text
rule = normalizeRuleName(rule);

async.series(
[
function(localCallback) {
Expand Down Expand Up @@ -208,6 +231,10 @@ module.exports = {
myutils.logErrorIf(localError);
return callback(localError);
}

// Normalize the rule text
rule = normalizeRuleName(rule);

async.series(
[
delR2core.bind(null, rule),
Expand Down
81 changes: 81 additions & 0 deletions test/component/rules_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*
* For those usages not covered by the GNU Affero General Public License
* please contact with iot_support at tid dot es
*
* Modified by: Carlos Blanco - Future Internet Consulting and Development Solutions (FICODES)
*/

'use strict';
Expand Down Expand Up @@ -87,6 +89,85 @@ describe('Rules', function() {
}
], done);
});
it('should add ruleName automatically when the rule text does not include "rule as ruleName"', function(done) {
var rule = utilsT.loadExample('./test/data/good_rules/no_ruleName_in_text_rule_post.json');
async.series([
function(callback) {
clients.PostRule(rule, function(error, data) {
should.not.exist(error);
data.should.have.property('statusCode', 200);
return callback(null);
});
},
function(callback) {
clients.GetRule(rule.name, function(error, data) {
should.not.exist(error);
data.should.have.property('statusCode', 200);
data.should.have.property('body');
data.body.should.have.property('data');
data.body.data.should.have.property('text',
'select "x_post_auto" as ruleName, *, ev.xPressure? as Pression, ev.id? as Meter from ' +
'pattern [every ev=iotEvent(cast(cast(xPressure?,String),float)>1.5 and type="xMeter")]');
data.body.data.should.have.property('name', rule.name);

return callback();
});
}
], done);
});
it('should preserve user ruleName', function(done) {
var rule = utilsT.loadExample('./test/data/good_rules/blood_rule_post.json');
async.series([
function(callback) {
clients.PostRule(rule, function(error, data) {
should.not.exist(error);
data.should.have.property('statusCode', 200);
return callback(null);
});
},
function(callback) {
clients.GetRule(rule.name, function(error, data) {
should.not.exist(error);
data.should.have.property('statusCode', 200);
data.should.have.property('body');
data.body.should.have.property('data');
data.body.data.should.have.property('text',
'select *,\"blood_post\" as ruleName,ev.BloodPressure? as Pression, ev.id? as Meter from' +
' pattern [every ev=iotEvent(cast(cast(BloodPressure?,String),float)>1.5 and ' +
'type=\"BloodMeter\")]');
data.body.data.should.have.property('name', rule.name);

return callback();
});
}
], done);
});
it('should preserve user incorrect ruleName', function(done) {
var rule = utilsT.loadExample('./test/data/bad_rules/rule_bad_ruleName_text_rule_post.json');
async.series([
function(callback) {
clients.PostRule(rule, function(error, data) {
should.not.exist(error);
data.should.have.property('statusCode', 200);
return callback(null);
});
},
function(callback) {
clients.GetRule(rule.name, function(error, data) {
should.not.exist(error);
data.should.have.property('statusCode', 200);
data.should.have.property('body');
data.body.should.have.property('data');
data.body.data.should.have.property('text',
'select *, "badRuleleName" as ruleName, ev.xPressure? as Pression, ev.id? as Meter from' +
' pattern [every ev=iotEvent(cast(cast(xPressure?,String),float)>1.5 and type="xMeter")]');
data.body.data.should.have.property('name', rule.name);

return callback();
});
}
], done);
});
it('should return an error if core endpoint is not working', function(done) {
var cases = utilsT.loadDirExamples('./test/data/good_rules');
utilsT.setServerCode(400);
Expand Down
11 changes: 11 additions & 0 deletions test/data/bad_rules/rule_bad_ruleName_text_rule_post.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "theCorrectName",
"text": "select *, \"badRuleleName\" as ruleName, ev.xPressure? as Pression, ev.id? as Meter from pattern [every ev=iotEvent(cast(cast(xPressure?,String),float)>1.5 and type=\"xMeter\")]",
"action": {
"type": "post",
"template": "Meter ${Meter} has pression ${Pression}.",
"parameters": {
"url": "localhost:1111"
}
}
}
11 changes: 11 additions & 0 deletions test/data/good_rules/no_ruleName_in_text_rule_post.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "x_post_auto",
"text": "select *, ev.xPressure? as Pression, ev.id? as Meter from pattern [every ev=iotEvent(cast(cast(xPressure?,String),float)>1.5 and type=\"xMeter\")]",
"action": {
"type": "post",
"template": "Meter ${Meter} has pression ${Pression}.",
"parameters": {
"url": "localhost:1111"
}
}
}

0 comments on commit 2c0e4ec

Please sign in to comment.