Skip to content

Commit

Permalink
added returned string from OnFail function; added demo app to solve a…
Browse files Browse the repository at this point in the history
… github issue microsoft#584
  • Loading branch information
Aaron Sulwer committed Jun 21, 2024
1 parent 2c69ea8 commit 0c73044
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 10 deletions.
2 changes: 1 addition & 1 deletion demo/DemoApp/Demos/Basic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public async Task Run(CancellationToken ct = default)
outcome = true;
});

resultList.OnFail(() => {
resultList.OnFail((eventName) => {
outcome = false;
});

Expand Down
73 changes: 73 additions & 0 deletions demo/DemoApp/Demos/BasicWithCustomTypes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using RulesEngine.Models;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using static RulesEngine.Extensions.ListofRuleResultTreeExtension;

namespace DemoApp.Demos
{
public class BasicWithCustomTypes
{
internal static class Utils
{
public static bool CheckContains(string check, string valList)
{
if (String.IsNullOrEmpty(check) || String.IsNullOrEmpty(valList))
return false;

var list = valList.Split(',').ToList();
return list.Contains(check);
}
}
public async Task Run(CancellationToken ct = default)
{
Console.WriteLine($"Running {nameof(BasicWithCustomTypes)}....");
var workflows = new List<Workflow>();
var workflow = new Workflow();
workflow.WorkflowName = "Test Workflow Rule 1";

var rules = new List<Rule> {
new Rule() {
RuleName = "Test Rule",
SuccessMessage = "doSomething ran successfully",
ErrorMessage = "doSomething failed",
Expression = "Utils.CheckContains(string1, \"bye,seeya,hello\") == true",
RuleExpressionType = RuleExpressionType.LambdaExpression
}
};

workflow.Rules = rules;
workflows.Add(workflow);

var reSettings = new ReSettings {
CustomTypes = [ typeof(Utils) ]
};

var bre = new RulesEngine.RulesEngine(workflows.ToArray(), reSettings);

var string1 = new RuleParameter("string1", "hello");

var ruleResultTree = await bre.ExecuteAllRulesAsync("Test Workflow Rule 1", [string1], ct);
ruleResultTree.OnSuccess((eventName) => {
Console.WriteLine($"Result '{eventName}' is as expected.");
});
ruleResultTree.OnFail((eventName) => {
Console.WriteLine($"Test outcome: false");
});

var actionRuleResult = await bre.ExecuteActionWorkflowAsync("Test Workflow Rule 1", "Test Rule", [string1], ct);
actionRuleResult.Results.OnSuccess((eventName) => {
Console.WriteLine($"Result '{eventName}' is as expected.");
});
actionRuleResult.Results.OnFail((eventName) => {
Console.WriteLine($"Test outcome: false");
});
}
}
}
2 changes: 1 addition & 1 deletion demo/DemoApp/Demos/EF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public async Task Run(CancellationToken ct = default)
discountOffered = $"Discount offered is {eventName} % over MRP.";
});

resultList.OnFail(() => {
resultList.OnFail((eventName) => {
discountOffered = "The user is not eligible for any discount.";
});

Expand Down
2 changes: 1 addition & 1 deletion demo/DemoApp/Demos/JSON.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public async Task Run(CancellationToken ct = default)
discountOffered = $"Discount offered is {eventName} % over MRP.";
});

resultList.OnFail(() => {
resultList.OnFail((eventName) => {
discountOffered = "The user is not eligible for any discount.";
});

Expand Down
2 changes: 1 addition & 1 deletion demo/DemoApp/Demos/NestedInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public async Task Run(CancellationToken ct = default)

resultList.OnSuccess((eventName) => {
Console.WriteLine($"{workflow.WorkflowName} evaluation resulted in success - {eventName}");
}).OnFail(() => {
}).OnFail((eventName) => {
Console.WriteLine($"{workflow.WorkflowName} evaluation resulted in failure");
});
}
Expand Down
1 change: 1 addition & 0 deletions demo/DemoApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public static async Task Main(string[] args)
//cts.CancelAfter(TimeSpan.FromMilliseconds(94));

await new Demos.Basic().Run(cts.Token);
await new Demos.BasicWithCustomTypes().Run(cts.Token);
await new Demos.JSON().Run(cts.Token);
await new Demos.NestedInput().Run(cts.Token);
await new Demos.EF().Run(cts.Token);
Expand Down
12 changes: 8 additions & 4 deletions src/RulesEngine/Extensions/ListofRuleResultTreeExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace RulesEngine.Extensions
public static class ListofRuleResultTreeExtension
{
public delegate void OnSuccessFunc(string eventName);
public delegate void OnFailureFunc();
public delegate void OnFailureFunc(string eventName);


/// <summary>
Expand Down Expand Up @@ -40,9 +40,13 @@ public static List<RuleResultTree> OnSuccess(this List<RuleResultTree> ruleResul
/// <returns></returns>
public static List<RuleResultTree> OnFail(this List<RuleResultTree> ruleResultTrees, OnFailureFunc onFailureFunc)
{
bool allFailure = ruleResultTrees.All(ruleResult => ruleResult.IsSuccess == false);
if (allFailure)
onFailureFunc();
var allFailure = ruleResultTrees.FirstOrDefault(ruleResult => ruleResult.IsSuccess == false);
if (allFailure != null)
{
var eventName = allFailure.Rule.ErrorMessage ?? allFailure.Rule.RuleName;
onFailureFunc(eventName);
}

return ruleResultTrees;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public void OnFailWithSuccessTest()

var successEventName = true;

rulesResultTree.OnFail(() => {
rulesResultTree.OnFail((eventName) => {
successEventName = false;
});

Expand Down Expand Up @@ -203,7 +203,7 @@ public void OnFailWithoutSuccessTest()

var successEventName = true;

rulesResultTree.OnFail(() => {
rulesResultTree.OnFail((eventName) => {
successEventName = false;
});

Expand Down

0 comments on commit 0c73044

Please sign in to comment.