Skip to content

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

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 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
      }
   }
   

Custom Actions

RulesEngine allows registering custom actions which can be used in the rules workflow.

Steps to use a custom Action

  • Create a class which extends ActionBase class and implement the run method