有没有人有动态 NRules 的工作示例?
Does anyone have a working example of dynamic NRules?
我正在寻找动态 NRules 的工作示例。其实我想把规则写在记事本文件里,想在运行时读取。
过去 4 天我一直在互联网上搜索它,但没有找到任何东西。
任何帮助都是可观的。
NRules 主要定位为规则引擎,其中规则在 C# 中编写,并编译成程序集。
有一个配套项目 https://github.com/NRules/NRules.Language 定义了用于表达规则的文本 DSL(称为 Rule#)。它的功能不如 C# DSL 完整,但可能是您正在寻找的东西。
您仍然会有一个 C# 项目,它从文件系统或数据库加载文本规则,并驱动规则引擎。
您将使用 https://www.nuget.org/packages/NRules.RuleSharp package for parsing the textual rules into a rule model, and https://www.nuget.org/packages/NRules.Runtime 将规则模型编译为可执行形式,并使用 运行 规则。
给定一个域模型:
namespace Domain
{
public class Customer
{
public string Name { get; set; }
public string Email { get; set; }
}
}
并给定一个文本文件,其中包含名为 MyRuleFile.txt
的规则:
using Domain;
rule "Empty Customer Email"
when
var customer = Customer(x => string.IsNullOrEmpty(x.Email));
then
Console.WriteLine("Customer email is empty. Customer={0}", customer.Name);
下面是规则驱动程序代码的示例:
var repository = new RuleRepository();
repository.AddNamespace("System");
//Add references to any assembly that the rules are using, e.g. the assembly with the domain model
repository.AddReference(typeof(Console).Assembly);
repository.AddReference(typeof(Customer).Assembly);
//Load rule files
repository.Load(@"MyRuleFile.txt");
//Compile rules
var factory = repository.Compile();
//Create a rules session
var session = factory.CreateSession();
//Insert facts into the session
session.Insert(customer);
//Fire rules
session.Fire();
输出:
Customer email is empty. Customer=John Do
我正在寻找动态 NRules 的工作示例。其实我想把规则写在记事本文件里,想在运行时读取。
过去 4 天我一直在互联网上搜索它,但没有找到任何东西。
任何帮助都是可观的。
NRules 主要定位为规则引擎,其中规则在 C# 中编写,并编译成程序集。 有一个配套项目 https://github.com/NRules/NRules.Language 定义了用于表达规则的文本 DSL(称为 Rule#)。它的功能不如 C# DSL 完整,但可能是您正在寻找的东西。
您仍然会有一个 C# 项目,它从文件系统或数据库加载文本规则,并驱动规则引擎。 您将使用 https://www.nuget.org/packages/NRules.RuleSharp package for parsing the textual rules into a rule model, and https://www.nuget.org/packages/NRules.Runtime 将规则模型编译为可执行形式,并使用 运行 规则。
给定一个域模型:
namespace Domain
{
public class Customer
{
public string Name { get; set; }
public string Email { get; set; }
}
}
并给定一个文本文件,其中包含名为 MyRuleFile.txt
的规则:
using Domain;
rule "Empty Customer Email"
when
var customer = Customer(x => string.IsNullOrEmpty(x.Email));
then
Console.WriteLine("Customer email is empty. Customer={0}", customer.Name);
下面是规则驱动程序代码的示例:
var repository = new RuleRepository();
repository.AddNamespace("System");
//Add references to any assembly that the rules are using, e.g. the assembly with the domain model
repository.AddReference(typeof(Console).Assembly);
repository.AddReference(typeof(Customer).Assembly);
//Load rule files
repository.Load(@"MyRuleFile.txt");
//Compile rules
var factory = repository.Compile();
//Create a rules session
var session = factory.CreateSession();
//Insert facts into the session
session.Insert(customer);
//Fire rules
session.Fire();
输出:
Customer email is empty. Customer=John Do