Skip to content

Commit c66bd08

Browse files
add function demo
1 parent 89f9bcd commit c66bd08

File tree

5 files changed

+148
-3
lines changed

5 files changed

+148
-3
lines changed

demo/DemoApp/BasicDemo.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using System;
66
using System.Collections.Generic;
77
using System.Dynamic;
8+
using System.Text.Json;
9+
using System.Text.Json.Serialization;
810
using static RulesEngine.Extensions.ListofRuleResultTreeExtension;
911

1012
namespace DemoApp
@@ -27,12 +29,16 @@ public void Run()
2729
rule.Expression = "count < 3";
2830
rule.RuleExpressionType = RuleExpressionType.LambdaExpression;
2931

32+
//(rule.Actions ??= new RuleActions()).OnSuccess = new ActionInfo() { Name = "123", Context = new Dictionary<string, object>() };
33+
3034
rules.Add(rule);
3135

3236
workflow.Rules = rules;
3337

3438
workflows.Add(workflow);
3539

40+
//var json = JsonSerializer.Serialize(workflow);
41+
3642
var bre = new RulesEngine.RulesEngine(workflows.ToArray(), null);
3743

3844
dynamic datas = new ExpandoObject();

demo/DemoApp/DemoApp.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
<None Update="Workflows\Discount.json">
1616
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
1717
</None>
18+
<None Update="Workflows\FunctionRules.json">
19+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
20+
</None>
1821
<None Update="Workflows\NestedInputDemo.json">
1922
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
2023
</None>

demo/DemoApp/FunctionDemo.cs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using RulesEngine.Models;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.IO;
8+
using System.Linq;
9+
using System.Text;
10+
using System.Text.Json;
11+
using System.Text.RegularExpressions;
12+
using System.Threading.Tasks;
13+
using static FastExpressionCompiler.ExpressionCompiler;
14+
15+
namespace DemoApp
16+
{
17+
// 静态工具类示例
18+
public static class StringUtils
19+
{
20+
// 静态方法:检查字符串是否为邮箱格式
21+
public static bool IsEmail(string input)
22+
{
23+
if (string.IsNullOrEmpty(input))
24+
return false;
25+
return Regex.IsMatch(input, @"^[^@\s]+@[^@\s]+\.[^@\s]+$");
26+
}
27+
28+
// 静态方法:字符串脱敏(隐藏中间字符)
29+
public static string MaskString(string input, int keepStart = 2, int keepEnd = 2)
30+
{
31+
if (string.IsNullOrEmpty(input) || input.Length <= keepStart + keepEnd)
32+
return input;
33+
return input.Substring(0, keepStart) +
34+
new string('*', input.Length - keepStart - keepEnd) +
35+
input.Substring(input.Length - keepEnd);
36+
}
37+
}
38+
public class FunctionDemo
39+
{
40+
public void Run()
41+
{
42+
var files = Directory.GetFiles(Directory.GetCurrentDirectory(), "FunctionRules.json", SearchOption.AllDirectories);
43+
if (files == null || files.Length == 0)
44+
{
45+
throw new Exception("Rules not found.");
46+
}
47+
48+
var fileData = File.ReadAllText(files[0]);
49+
var Workflows = JsonSerializer.Deserialize<List<Workflow>>(fileData);
50+
// 初始化 RulesEngine 配置
51+
var reSettings = new ReSettings {
52+
// 注册类型别名:键为别名,值为类型全名(命名空间+类名)
53+
CustomTypes = new Type[] { typeof(StringUtils) }
54+
};
55+
56+
// 初始化 RulesEngine 引擎
57+
var rulesEngine = new RulesEngine.RulesEngine(Workflows.ToArray(), reSettings);
58+
59+
// 准备输入数据
60+
var input = new UserInput {
61+
Email = "[email protected]",
62+
Phone = "13800138000"
63+
};
64+
65+
// 执行规则
66+
var result = rulesEngine.ExecuteAllRulesAsync(
67+
"UserValidationWorkflow", // 工作流名称
68+
input, // 输入数据
69+
new UserOutput() // 输出对象(可空,默认返回 RuleResultTree)
70+
).Result;
71+
72+
// 处理结果
73+
if (result.All(r => r.IsSuccess))
74+
{
75+
var output = result.First().ActionResult.Output as UserOutput;
76+
Console.WriteLine($"验证结果:{output.IsValid}");
77+
Console.WriteLine($"脱敏手机号:{output.MaskedPhone}"); // 输出:138****8000
78+
}
79+
}
80+
}
81+
82+
// 定义输入模型
83+
public class UserInput
84+
{
85+
public string Email { get; set; }
86+
public string Phone { get; set; }
87+
}
88+
89+
// 定义输出模型
90+
public class UserOutput
91+
{
92+
public bool IsValid { get; set; }
93+
public string Message { get; set; }
94+
public string MaskedPhone { get; set; }
95+
}
96+
}

demo/DemoApp/Program.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ public static class Program
88
public static void Main(string[] args)
99
{
1010
new BasicDemo().Run();
11-
new JSONDemo().Run();
12-
new NestedInputDemo().Run();
13-
new EFDemo().Run();
11+
//new JSONDemo().Run();
12+
//new NestedInputDemo().Run();
13+
//new EFDemo().Run();
14+
new FunctionDemo().Run();
1415
}
1516
}
1617
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
[
2+
{
3+
"WorkflowName": "UserValidationWorkflow",
4+
"Rules": [
5+
{
6+
"RuleName": "CheckEmailFormat",
7+
"ErrorMessage": "邮箱格式无效",
8+
"Expression": "StringUtils.IsEmail(input1.Email)",
9+
"Actions": {
10+
"OnSuccess": {
11+
"Name": "OutputExpression",
12+
"Context": {
13+
"Expression": "input2"
14+
}
15+
}
16+
}
17+
},
18+
{
19+
"RuleName": "MaskUserPhone",
20+
"ErrorMessage": "手机号脱敏失败",
21+
"Expression": "StringUtils.MaskString(input1.Phone, 2, 2) == \"131*******00\"",
22+
"Actions": {
23+
"OnSuccess": {
24+
"Name": "OutputExpression",
25+
"Context": {
26+
"Expression": "input2"
27+
}
28+
},
29+
"OnFailure": {
30+
"Name": "OutputExpression",
31+
"Context": {
32+
"Expression": "input1"
33+
}
34+
}
35+
}
36+
}
37+
]
38+
}
39+
]

0 commit comments

Comments
 (0)