|
| 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 | + |
| 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 | +} |
0 commit comments