Kur*_*rtz 67 .net c# logic rule-engine .net-core
有谁知道一个好的.NET库规则库(理想情况下是开源的)?我需要的东西,可以做嵌套的逻辑表达式,如(A和B)和(B或C或d).我需要对对象属性进行比较,例如A.P1和B.P1.(理想情况下,我可以比较任何属性 - A.P1和B.P2).
它应该将规则存储在数据库中(我有很多简单的可配置逻辑).它应该有一个规则创建/管理API.管理工具必须检查实例以确定哪些属性可用以及存在哪些约束.
谢谢!
哦,还有一件事.作为规则引擎,我需要包含Actions(Commands)的概念.这些是表达式返回时执行的操作:
If (expression.Evaluation) { actions.Execute(); }
Run Code Online (Sandbox Code Playgroud)
所以我看到一条规则如下:
class Rule
{
Expression Exp;
Actions[] Actions;
Run()
{
if(Exp.Evaluate())
{
foreach(action in Actions)
{
action.Execute();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
Nic*_*nov 46
我同意将使用工作流引擎系列中的某些东西,尽管不是工作流程.检查System.Workflow.Activities.Rules命名空间 - 它在.Net 3中得到支持,并内置到.Net3.5中.你提供的所有东西都可以免费使用:
条件的RuleCondition,动作的RuleAction
用于描述元代码的标准化格式(CodeDom - CodeExpressions)
你可以通过TypeProviders插入任何类型的复杂性(除了Linq和lambdas以及某种类型的扩展方法之外说实话)
有一个用intellisense进行规则编辑的内置编辑器
由于规则是可序列化的,因此可以很容易地保留
对于入门者: 使用工作流程之外的规则
Ps.:我们广泛使用它,并且命名空间中的内容比你想象的要多得多 - >一个完整的元算法语言
而最重要的是:它很容易使用 - 真的
Sha*_*owe 18
这是我过去使用的课程.它就像eval()在Javascript中一样评估字符串.
String result = ExpressionEvaluator.EvaluateToString("(2+5) < 8");
Run Code Online (Sandbox Code Playgroud)
您需要做的就是构造一个要从业务对象中评估的字符串,这将处理所有复杂的嵌套逻辑等.
using System;
using System.CodeDom.Compiler;
using System.Globalization;
using System.Reflection;
using Microsoft.JScript;
namespace Common.Rule
{
internal static class ExpressionEvaluator
{
#region static members
private static object _evaluator = GetEvaluator();
private static Type _evaluatorType;
private const string _evaluatorSourceCode =
@"package Evaluator
{
class Evaluator
{
public function Eval(expr : String) : String
{
return eval(expr);
}
}
}";
#endregion
#region static methods
private static object GetEvaluator()
{
CompilerParameters parameters;
parameters = new CompilerParameters();
parameters.GenerateInMemory = true;
JScriptCodeProvider jp = new JScriptCodeProvider();
CompilerResults results = jp.CompileAssemblyFromSource(parameters, _evaluatorSourceCode);
Assembly assembly = results.CompiledAssembly;
_evaluatorType = assembly.GetType("Evaluator.Evaluator");
return Activator.CreateInstance(_evaluatorType);
}
/// <summary>
/// Executes the passed JScript Statement and returns the string representation of the result
/// </summary>
/// <param name="statement">A JScript statement to execute</param>
/// <returns>The string representation of the result of evaluating the passed statement</returns>
public static string EvaluateToString(string statement)
{
object o = EvaluateToObject(statement);
return o.ToString();
}
/// <summary>
/// Executes the passed JScript Statement and returns the result
/// </summary>
/// <param name="statement">A JScript statement to execute</param>
/// <returns>The result of evaluating the passed statement</returns>
public static object EvaluateToObject(string statement)
{
lock (_evaluator)
{
return _evaluatorType.InvokeMember(
"Eval",
BindingFlags.InvokeMethod,
null,
_evaluator,
new object[] { statement },
CultureInfo.CurrentCulture
);
}
}
#endregion
}
}
Run Code Online (Sandbox Code Playgroud)
开源的.NET规则引擎都没有支持在数据库中存储规则.唯一存储规则的数据库是商业的.我已经为运行数据库的自定义规则引擎创建了一些UI,但实现起来并非易事.这通常是您不会免费看到该功能的主要原因.
据我所知,他们都没有达到你的所有标准,但这里列出了我所知道的:
最简单的是SRE
http://sourceforge.net/projects/sdsre/
一个有规则管理UI的是NxBRE
http://www.agilepartner.net/oss/nxbre/
Drools.NET使用JBOSS规则
http://droolsdotnet.codehaus.org/
我个人没有使用它们中的任何一个,因为我工作过的所有项目都不想使用内部构建的东西.大多数企业认为这很容易做到,但最终会浪费太多时间编写和实现它.这是Not Invented Here Syndrome(NIH)规则的那些领域之一.
好吧,因为逻辑表达式只是数学表达式的一个子集,所以您可能想在CodePlex上尝试使用NCalc - Mathematical Expressions Evaluator for .NET over.
| 归档时间: |
|
| 查看次数: |
44457 次 |
| 最近记录: |