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

  1. Create a class which extends ActionBase class and implement the run method
 public class MyCustomAction: ActionBase
    {
     
        public MyCustomAction(SomeInput someInput)
        {
            ....
        }

        public override ValueTask<object> Run(ActionContext context, RuleParameter[] ruleParameters)
        {
            var customInput = context.GetContext<string>("customContextInput");
            //Add your custom logic here and return a ValueTask
        }

Actions can have async code as well

 public class MyCustomAction: ActionBase
    {
     
        public MyCustomAction(SomeInput someInput)
        {
            ....
        }

        public override async ValueTask<object> Run(ActionContext context, RuleParameter[] ruleParameters)
        {
            var customInput = context.GetContext<string>("customContextInput");
            //Add your custom logic here
            return await MyCustomLogicAsync();
        }
  1. Register them in ReSettings and pass it to RulesEngine
   var reSettings = new ReSettings{
                        CustomActions = new Dictionary<string, Func<ActionBase>>{
                                             {"MyCustomAction", () => new MyCustomAction(someInput) }
                                         }
                     };

   var re = new RulesEngine(workflowRules,logger,reSettings);
  1. You can now use the name you registered in the Rules json in success or failure actions
{
  "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": "MyCustomAction",  //Name context
            "Context": {  //This is passed to the action as action context
               "customContextInput": "input1.TotalBilled * 0.9"
            }
         }
      }
    }
  ]
}