RulesEngine icon indicating copy to clipboard operation
RulesEngine copied to clipboard

Using the output from a rule as input to the expression for other rule

Open naheedmk opened this issue 1 year ago • 1 comments

Is it possible to use the output of Rule1 as input to the next Rule ? Looking for something below. Accessing "Expression": "context.TotalAmount >= 100000", . The TotalAmount is conditionally calculated in the previous rule, and it needs to be passed to the next Rule.

{ "WorkflowName": "EvaluateRuleActionTest", "GlobalParams": [ { "Name": "OrderCount", "Expression": "1 + 1" } ], "Rules": [ { "RuleName": "TotalAmount5W", "Expression": "input1 + input2 >= 50000", // Expression to evaluate "Actions": { "OnSuccess": [ { "Name": "OutputExpression", // Action to output TotalAmount "Context": { "TotalAmount": "input1 + input2" // Setting TotalAmount conditionally } } ] } }, { "RuleName": "TotalAmount10W", "Expression": "context.TotalAmount >= 100000", // Accessing the TotalAmount from context "Actions": { "OnSuccess": [ { "Name": "EvaluateRule", "Context": { "workflowName": "EvaluateRuleActionTest", "ruleName": "TotalAmount10W" } } ] } } ] }

naheedmk avatar Oct 18 '24 13:10 naheedmk

@naheedmk, Please look into below example to work with chained rules

[
  {
    "WorkflowName": "EvaluateRuleActionTest",
    "Rules": [
      {
        "RuleName": "TotalAmount5W",
        "Expression": "input1 + input2 >= 50000",
        "ErrorMessage": "The game doesn't meet the criteria for 5% discount",
        "ErrorType": "Error",
        "Actions": {
          "OnSuccess": {
            "Name": "EvaluateRule",
            "Context": {
              "workflowName": "EvaluateRuleActionTest",
              "ruleName": "TotalAmount10W",
              "additionalInputs": [
                { "Name": "TotalAmount", "Expression": "input1 + input2" }
              ]
            }
          }
        }
      },
      {
        "RuleName": "TotalAmount10W",
        "Expression": "TotalAmount >= 100000",
        "Actions": {
          "OnSuccess": {
            "Name": "OutputExpression",
            "Context": {
              "Expression": "TotalAmount * 0.10"
            }
          }
        }
      }
    ]
  }
]

// See https://aka.ms/new-console-template for more information

using RE = RulesEngine;

namespace MsRulesEngine;

public class ChainedRuleExample
{

    public static async Task Run()
    {
        string filePath = Path.Combine(Environment.CurrentDirectory, "RulesFiles", "ChainedRuleExample.json");
        Console.WriteLine(filePath);
        string jsonString = File.ReadAllText(filePath);
        var jsonDocument = JsonDocument.Parse(jsonString);

        var workflows = JsonSerializer.Deserialize<Workflow[]>(jsonString);

        var d1 = DateTime.Now;
        var d2 = DateTime.Now.AddMinutes(20);

        var ruleParameters = new RuleParameter[] {
        new ("input1", 50000),
        new ("input2", 50000)
      };

        RE.RulesEngine rulesEngine = new(workflows);

        var result = await rulesEngine.ExecuteActionWorkflowAsync("EvaluateRuleActionTest", "TotalAmount5W", ruleParameters);


            Console.WriteLine($"Rule: {result.Output}");
        }
}

vamsidogiparthi avatar Feb 04 '25 22:02 vamsidogiparthi