Skip to content

Actions

Abbas Cyclewala edited this page Oct 4, 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 ExecuteActionWorkflowAsync instead of ExecuteRule method of RulesEngine

Differences between ExecuteRule and ExecuteActionWorkflowAsync

ExecuteRule ExecuteActionWorkflowAsync
Takes workflowName as input and executes all rules under it Takes workflowName and ruleName as an input and executes only that rule and any corresponding action associated with it
Does not run actions even if specified in the Rules object Runs the actions specified in the Rules object
Useful when looking for only true or false as result Useful for returning complex results or performing some operation on rule success or failure
Returns List of RuleResultTree for all the rules Returns output of action, List of RuleResultTree of all the rules executed

Inbuilt Actions

RulesEngine provides two actions inbuilt which cover major scenarios related to rule execution

OutputExpression

This action evaluates an expression based on the RuleParameters and returns its value as Output

Usage

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 ExecuteActionWorkflowAsync with the workflowName, ruleName and ruleParameters

   var result = await rulesEngine.ExecuteActionWorkflowAsync("inputWorkflow","GiveDiscount10Percent",ruleParameters);
   Console.WriteLine(result.Output); //result.Output contains the evaluated value of the action

EvaluateRule

This action can be used for Rule chaining.It executes another rule with the help of RulesEngine and its corresponding actions and returns its output

Usage

Define OnSuccess or OnFailure Action for your Rule:

{
  "WorkflowName": "inputWorkflow",
  "Rules": [
    {
        "RuleName": "GiveDiscount20Percent",
        "SuccessEvent": "20",
        "ErrorMessage": "One or more adjust rules failed.",
        "ErrorType": "Error",
        "RuleExpressionType": "LambdaExpression",
        "Expression": "input1.couy == \"india\" AND input1.loyalityFactor <= 5 AND input1.totalPurchasesToDate >= 20000",
        "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.8"
              }
           },
           "OnFailure": { // This will execute if the Rule evaluates to failure
               "Name": "EvaluateRule",
               "Context": {
                   "WorkflowName": "inputWorkflow",
                   "ruleName": "GiveDiscount10Percent"
               }
           }
        }
    },
    {
      "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 ExecuteActionWorkflowAsync with the workflowName, ruleName and ruleParameters

   var result = await rulesEngine.ExecuteActionWorkflowAsync("inputWorkflow","GiveDiscount20Percent",ruleParameters);
   Console.WriteLine(result.Output); //result.Output contains the evaluated value of the action

In the above scenario if GiveDiscount20Percent succeeds, it will return 20 percent discount in output. If it fails, EvaluateRule action will call GiveDiscount10Percent internally and if it succeeds, it will return 10 percent discount in output.