forked from microsoft/RulesEngine
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added returned string from OnFail function; added demo app to solve a…
… github issue microsoft#584
- Loading branch information
Aaron Sulwer
committed
Jun 21, 2024
1 parent
2c69ea8
commit 0c73044
Showing
8 changed files
with
88 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters