Comcast / rulio

Rulio
Apache License 2.0
336 stars 59 forks source link

Rule with dynamic parameter #48

Open roneetshaw opened 5 years ago

roneetshaw commented 5 years ago

This is my rule

{
    "location": "here",
    "rule": {
        "action": {
            "code": "var msg ='Name :' + who + ' age : '+x; console.log(msg); msg;"
        },
        "condition": {
            "and": [
                {
                    "pattern": {
                        "age": "?x",
                        "name": "?who"
                    }
                },
                {
                    "code": "x >6"
                }
            ]
        },
        "when": {
            "pattern": {
                "age": "?x",
                "name": "?who"
            }
        }
    }
}

In the above JSON, I am checking with constant 6 where age> 6 in my condition, but is there a way where I can pass a variable instead of fix values from the uri: /api/loc/events/ingest For example uri: /api/loc/events/ingest request

{
    "location": "here",
    "event": {
        "age": "8",
        "name": "sonu"
    }
}

The age passed here i.e. 8 should be used in the rule condition and the result should be according to that?

rakesh-roshan commented 5 years ago

Do you want to pass the the value of age and the threshold both in the event digest? You can change the pattern then, to capture that value in a variable. But in the sample even digest, you are passing only the value of age. So 8 will be compared to which value? I think you want to pass both the threshold and the value in the event. e.g.

{
    "location": "here",
    "event": {
        "age": "8",
       "age_threshold": "6",
        "name": "sonu"
    }
}

Then your rule can be,

{
    "location": "here",
    "rule": {
        "action": {
            "code": "var msg ='Name :' + who + ' age : '+x; console.log(msg); msg;"
        },
        "condition": {
            "and": [
                {
                    "pattern": {
                        "age": "?x",
                       "age_threshold": "?t",
                        "name": "?who"
                    }
                },
                {
                    "code": "x > t"
                }
            ]
        },
        "when": {
            "pattern": {
                "age": "?x",
                "age_threshold": "?t",
                "name": "?who"
            }
        }
    }
}

One thing, in the ingest you are passing the value as string so the comparison will be string comparison. I think in the code you should convert the value and threshold to numeric explicitly for numeric comparison.

roneetshaw commented 5 years ago

Thanks @rakesh-roshan for the suggestion. I will try it out and let you know about the result. Thank you so much.

roneetshaw commented 5 years ago

Hi Rakesh, Your solution worked great. Thanks for sharing. However, can we perform comparisons (>, <, etc) at the "pattern" level? and not on "code" level