-
Notifications
You must be signed in to change notification settings - Fork 562
Actions
Abbas Cyclewala edited this page Oct 24, 2020
·
6 revisions
As a part of v3, Actions have been introduced to allow custom code execution on rule result. This can be achieved by calling ExecuteAllRulesAsync
method of RulesEngine
RulesEngine provides two actions inbuilt which cover major scenarios related to rule execution
This action evaluates an expression based on the RuleParameters and returns its value as Output
Define OnSuccess or OnFailure Action for your Rule:
{
"WorkflowName": "inputWorkflow",
"Rules": [
{
"RuleName": "GiveDiscount10Percent",
"SuccessEvent": "10",
"ErrorMessage": "One or more adjust rules failed.",
"ErrorType": "Error",
"RuleExpressionType": "LambdaExpression",
"Expression": "input1.couy == \"india\" AND input1.loyalityFactor <= 2 AND input1.totalPurchasesToDate >= 5000 AND input2.totalOrders > 2 AND input2.noOfVisitsPerMonth > 2",
"Actions": {
"OnSuccess": {
"Name": "OutputExpression", //Name of action you want to call
"Context": { //This is passed to the action as action context
"Expression": "input1.TotalBilled * 0.9"
}
}
}
}
]
}
Call ExecuteAllRulesAsync
with the workflowName, ruleName and ruleParameters
var ruleResultList = await rulesEngine.ExecuteAllRulesAsync("inputWorkflow",ruleParameters);
foreach(var ruleResult in ruleResultList){
if(ruleResult.ActionResult != null){
Console.WriteLine(ruleResult.ActionResult.Output); //ActionResult.Output contains the evaluated value of the action
}
}
RulesEngine allows registering custom actions which can be used in the rules workflow.
- Create a class which extends
ActionBase
class and implement the run method