-
Notifications
You must be signed in to change notification settings - Fork 504
Referencing another Rule #263
-
I don't see how we can reference an already created rule/decision in a new decision so that we are not recreating the decision from scratch.
It would be nice if a condition can reference a pre existing condition. Is this possible now or could this be added.
A fact could allow picking another condition (decision) and automatically the possible values are True or False as it would be a type boolean.
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 2 comments 2 replies
-
Unless I'm misunderstanding, I think you should be able to achieve this by assigning the condition to a variable and using that variable when creating each rule.
Beta Was this translation helpful? Give feedback.
All reactions
-
Can I get an example of how this can be achieved using a json construct please?
Beta Was this translation helpful? Give feedback.
All reactions
-
Was thinking more on the lines of referencing the evaluation of a previously defined condition/event outcome in another rule so as to not restate the facts (Again idea being to having minimal js coding for the rules). See below example :
"decisions": [
{
"conditions": {
"any": [
{
"fact": "name",
"operator": "equal",
"value": "David"
},
{
"fact": "age",
"operator": "greaterThan",
"value": "21"
}
]
},
"event": {
"type": "DavidCanDrink",
}
},
{
"conditions": {
"all": [
{
"conditionRef": "DavidCanDrink",
"operator": "equal",
"value": "True"
},
{
"fact": "day",
"operator": "equal",
"value": "Monday"
}
]
},
"event": {
"type": "DavidMayDrink",
"params": {
"drinkName": "Martini"
}
}
}]
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 2
-
@CacheControl Would it make sense to allow the creation of "facts" that used the condition format something like:
engine.addFact({ "name": "DavidCanDrink", "conditions": { "all": [ { "fact": "name", "operator":"equal", "value": "David" }, { "fact": "age", "operator": "greaterThanInclusive", "value": 21 } ] } }); engine.addRule({ "conditions": { "all": [ { "fact": "DavidCanDrink", "operator": "equal", "value": true }, { "fact": "day", "operator": "equal", "value": "Monday", ] }, "event": { "type": "DavidMayDrink", "params": { "drinkName": "Martini" } } });
This could allow Facts to be defined by users and loaded into the system at run-time. The requirement would be that the fact would have to be a boolean value.
I'm thinking you could even introduce a syntax similar to the { "fact": "name" } one used in the value field where you could reference a parameter. Something like: { "param": "name" } which would then require the new fact to have a name field in the params.
Rewriting the above rule you could do something like:
engine.addFact({ "name": "CanDrink", "conditions": { "all": [ { "fact": "name", "operator":"equal", "value": { "param": "name" } }, { "fact": "age", "operator": "greaterThanInclusive", "value": 21 } ] } }); engine.addRule({ "conditions": { "all": [ { "fact": "CanDrink", "operator": "equal", "value": true, "params": { "name": "David" } }, { "fact": "day", "operator": "equal", "value": "Monday", ] }, "event": { "type": "DavidMayDrink", "params": { "drinkName": "Martini" } } });
Beta Was this translation helpful? Give feedback.