在 .NET 中寻找简单的规则引擎库

发布于 2024-07-07 05:15:08 字数 677 浏览 6 评论 0原文

有谁知道一个好的.NET 库规则库(最好是开源的)? 我需要一些可以执行嵌套逻辑表达式的东西,例如(A AND B)AND(B OR C OR D)。 我需要比较对象属性,例如 A.P1 和 B.P1。 (理想情况下,我可以比较任何属性 - A.P1 和 B.P2)。

它应该将规则存储在数据库中(我有很多简单的可配置逻辑)。 它应该有一个规则创建/管理 API。 管理工具必须检查实例以确定哪些属性可用以及存在哪些约束。

谢谢!


哦,还有一件事。 作为规则引擎,我需要包含操作(命令)的概念。 这些是表达式返回时执行的内容:

If (expression.Evaluation) { actions.Execute(); }

所以我认为规则如下:

class Rule
{
    Expression Exp;
    Actions[] Actions;
    Run() 
    { 
        if(Exp.Evaluate()) 
        { 
            foreach(action in Actions) 
            { 
                action.Execute(); 
            }
        } 
    }
}

Does anyone know of a good .NET library rules library (ideally open-source)? I need something that can do nested logic expressions, e.g., (A AND B) AND (B OR C OR D). I need to do comparisons of object properties, e.g., A.P1 AND B.P1. (Ideally, I could compare any property -- A.P1 AND B.P2).

It should store the rules in a database (I've got a lot of simple configurable logic). And it should have a rule creation/management API. The management tool would have to inspect the instances to determine which properties are available and which constraints exist.

Thanks!


Oh, one more thing. As a rules-engine, I need to include the concept of Actions (Commands). These are what execute when the expression returns:

If (expression.Evaluation) { actions.Execute(); }

So I see a rule as something like:

class Rule
{
    Expression Exp;
    Actions[] Actions;
    Run() 
    { 
        if(Exp.Evaluate()) 
        { 
            foreach(action in Actions) 
            { 
                action.Execute(); 
            }
        } 
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

濫情▎り 2024-07-14 05:15:08

同意我会说使用工作流引擎系列中的某些东西,尽管不是工作流。
检查 System.Workflow.Activities.Rules 命名空间一点点 - 它在 .Net 3 中受支持,并内置于 .Net3.5 中。 您拥有一切,可以免费使用,就像您提到的那样:

  • 用于条件的 RuleCondition,用于操作的 RuleAction

  • 用于描述的标准化格式
    元代码(CodeDom - CodeExpressions)

  • 您可以插入任何类型的复杂性
    进入那个(说实话,除了
    Linq 和 lambda 等扩展
    某种方法)通过
    TypeProviders

  • 有一个内置的规则编辑器
    使用智能感知进行编辑

  • 因为规则是可序列化的,所以可以
    容易持久

  • 如果您打算在一段时间内使用这些规则,那么
    数据库方案然后通过 typeprovider
    它也可以实现

对于初学者来说, :
在工作流程之外使用规则

Ps.:我们广泛使用它,并且该命名空间中的内容比您想象的要多得多 -> 一个完整的元算法语言

最重要的是:它很容易使用 - 真的

Agreeing with will I would say use something from the workflow engine family although not workflow.
Examine System.Workflow.Activities.Rules Namespace a little bit - it's supported in .Net 3, and built into .Net3.5. You have everything in hand for free to use like you mentioned :

  • RuleCondition for conditions , RuleAction for actions

  • standardized format for describing
    metacode (CodeDom - CodeExpressions)

  • you can plugin any kind of complexity
    into that (to tell the truth except
    Linq and lambdas and so extension
    methods of some kind) via
    TypeProviders

  • there's a builtin editor for rule
    editing with intellisense

  • as the rule is serializable it can be
    easily persisted

  • if you meant to use the rules over a
    database scheme then via typeprovider
    it can be implemented too

For a starter :
Using rules outside of a workflow

Ps.: we're using it extensively and there're much more in that namespace than you ever imagine -> a complete meta algorithm language

And the most important : it's easy to use - really

梦断已成空 2024-07-14 05:15:08

这是我过去使用过的一个类。 它评估字符串就像 JavaScript 中的 eval() 一样。

String result = ExpressionEvaluator.EvaluateToString("(2+5) < 8");

您需要做的就是构造一个要从业务对象进行评估的字符串,这将处理所有复杂的嵌套逻辑等。

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
  }    
}

Here is a class I have used in the past. It evaluates strings just like eval() does in Javascript.

String result = ExpressionEvaluator.EvaluateToString("(2+5) < 8");

All you need to do is construct a string to be evaluated from your business objects and this will take care of all the complicated nested logic etc.

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
  }    
}
顾冷 2024-07-14 05:15:08

开源 .NET 规则引擎都不支持在数据库中存储规则。 唯一将规则存储在数据库中的都是商业性的。 我已经为运行数据库的自定义规则引擎创建了一些 UI,但这实现起来并不简单。 这通常是您无法免费看到该功能的主要原因。

据我所知,没有一个能够满足您的所有标准,但这里列出了我所知道的:

最简单的一个是 SRE
http://sourceforge.net/projects/sdsre/

NxBRE 是一款具有规则管理 UI 的
http://www.agilepartner.net/oss/nxbre/

Drools.NET 使用 JBOSS 规则
http://droolsdotnet.codehaus.org/

我个人没有使用过其中任何一个,因为所有我参与的项目从来不想使用内部构建的东西。 大多数企业认为这很容易做到,但最终却浪费了太多的编码和实现时间。 这是非发明综合症 (NIH) 所管辖的领域之一。

None of the open sourced .NET rules-engine have support for storing rules in the database. The only ones that stored the rules in a database are commercial. I've created some UIs for custom rule engines that run off the database, but this can be non-trivial to implement. That's usually the main reason you won't see that feature for free.

As far as I know, none of them will meet all of your criteria, but here is a list of the ones I know of:

Simplest one is SRE
http://sourceforge.net/projects/sdsre/

One with rule management UI is NxBRE
http://www.agilepartner.net/oss/nxbre/

Drools.NET uses JBOSS rules
http://droolsdotnet.codehaus.org/

I personally haven't used any of them, because all of the projects I worked with never wanted to use something built in-house. Most business think that this is pretty easy to do, but end up wasting too much time coding and implementing it. This is one of those areas that the Not Invented Here Syndrome (NIH) rules.

爱人如己 2024-07-14 05:15:08

好吧,由于逻辑表达式只是数学表达式的子集,您可能需要尝试 NCalc - .NET 数学表达式评估器< /a> 在 CodePlex 上。

Well, since logical expression are just a subset of mathematical expression, you may want to try NCalc - Mathematical Expressions Evaluator for .NET over on CodePlex.

裸钻 2024-07-14 05:15:08

MS 的官方解决方案是 Windows Workflow。 尽管我不会称其为“简单”,但它满足您的所有规范(无论如何,这需要一个广泛的框架来满足)。

The official MS solution for this is Windows Workflow. Although I wouldn't call it "simple", it meets all of your specifications (which would require an extensive framework to meet, anyhow).

像你 2024-07-14 05:15:08

我用过这个http://www.codeproject.com/KB/recipes/Flee。 aspx 过去曾取得成功。 试一试。

I've used this http://www.codeproject.com/KB/recipes/Flee.aspx with success in the past. Give it a try.

各空 2024-07-14 05:15:08

Windows Workflow Foundation 确实为您提供了一个免费的前向链接推理引擎。 您可以在没有工作流程部分的情况下使用它。 创建和编辑规则对于开发人员来说是可以的。

如果您想让非程序员编辑和维护规则,您可以尝试规则管理器

规则管理器将为您生成一个有效的视觉工作室解决方案。 这应该可以让你很快上手。 只需单击“文件\导出”并选择 WFRules 格式。

Windows Workflow Foundation does give you a free forward chaining inference engine. And you can use it without the workflow part. Creating and Editing rules is ok for developers.

If you want to have non-programmers edit and maintain the rules you can try out the Rule Manager.

The Rule Manager will generate a working visual studio solution for you. That should get you started rather quickly. Just click on File \ Export and selecte the WFRules format.

燕归巢 2024-07-14 05:15:08

您也可以在 http://www.FlexRule.com 上查看我们的产品

FlexRule 是一项业务规则引擎框架,支持三种引擎; 程序引擎、推理引擎和规则流引擎。 其推理引擎是前向链接推理,使用 Rete 算法的增强实现。

You can take a look at our product as well at http://www.FlexRule.com

FlexRule is a Business Rule Engine framework with support for three engines; Procedural engine, Inference engine and RuleFlow engine. Its inference engine is a forward chaining inference that uses enhanced implementation of Rete Algorithm.

何必那么矫情 2024-07-14 05:15:08

我会看看 Windows 工作流程。 规则引擎和工作流程往往开始时很简单,然后逐渐变得更加复杂。 像 Windows Workflow Foundation 这样的东西入门并不太难,并且提供了增长空间。 这里有一篇文章表明,让一个简单的工作流引擎运行起来并不是太困难。

I would look at Windows Workflow. Rules engines and workflow tend to start simple and get progressively more complex. Something like Windows Workflow Foundation is not too difficult to start with and provides room for growth. Here is a post that shows it's not too difficult to get a simple workflow engine going.

风和你 2024-07-14 05:15:08

也许查看SmartRules。 它不是免费的,但界面看起来很简单。

只知道它是因为我之前使用过那里的 SmartCode codegen 实用程序。

以下是网站上的规则示例:

BUSINESS RULES IN NATURAL LANGUAGE      

Before
If (Customer.Age > 50 && Customer.Status == Status.Active) {
policy.SetDiscount(true, 10%);
}

After (with Smart Rules)
If Customer is older than 50 and
the Customer Status is Active Then
Apply 10 % of Discount

Maybe check out SmartRules. Its not free, but the interface looks simple enough.

Only know about it because I've used the SmartCode codegen utility from there before.

Here is an example rule from the Website:

BUSINESS RULES IN NATURAL LANGUAGE      

Before
If (Customer.Age > 50 && Customer.Status == Status.Active) {
policy.SetDiscount(true, 10%);
}

After (with Smart Rules)
If Customer is older than 50 and
the Customer Status is Active Then
Apply 10 % of Discount
日记撕了你也走了 2024-07-14 05:15:08

您可以使用 RuEn,这是我创建的一个简单的基于开源属性的规则引擎:

http://ruen.codeplex.com< /a>

You can use a RuEn, an simple open source attribute based Rule Engine created by me:

http://ruen.codeplex.com

水晶透心 2024-07-14 05:15:08

试用
http://rulesengine.codeplex.com/

它是一个与表达式树一起使用的 C# 开源规则引擎。

Try out
http://rulesengine.codeplex.com/

It's a C# Open-Source rules engine that works with Expression trees.

岁月静好 2024-07-14 05:15:08

查看 Logician:CodeProject

项目上的 教程/概述page/source

Have a look at Logician: tutorial/overview on CodeProject

Project: page/source on SourceForge

时光与爱终年不遇 2024-07-14 05:15:08

根据您尝试执行的操作,使用 Lambda 表达式(和表达式树)可以实现此概念。 本质上,您提供一个字符串形式的表达式,然后将其动态编译为 lambda 表达式/表达式树,然后您可以执行(评估)该表达式。 一开始理解它并不容易,但是一旦你理解了它就会非常强大并且设置起来相当简单。

Depending on what you are trying to do using Lambda expressions (and expression trees) can work for this concept. Essentially, you provide an expression as a string that is then compiled on the fly into a lambda expression/expression tree, which you can then execute (evaluate). It's not simple to understand at first, but once you do it's extremely powerful and fairly simple to set up.

虐人心 2024-07-14 05:15:08

它不是免费的,因为您无法轻松地将其从 BizTalk 血统中分离出来,但 BizTalk 的业务规则引擎组件是独立于核心 BizTalk 引擎本身的独立实体,并且包含一个非常强大的规则引擎,其中包括基于规则/策略的规则图形用户界面。 如果有一个免费版本,它将满足您的要求(仅仅为了 BRE 购买 BizTalk 并不能真正用于商业用途。)

It's not free, as you can't easily untangle it from its BizTalk parentage, but the Business Rules Engine components of BizTalk are a separate entity from the core BizTalk engine itself, and comprise a very powerful rules engine that includes a rules / policy based GUI. If there was a free version of this it would fit your requirements (buying BizTalk just for the BRE wouldn't really work commercially.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文