在 .NET 中寻找简单的规则引擎库
有谁知道一个好的.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
同意我会说使用工作流引擎系列中的某些东西,尽管不是工作流。
检查 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
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
这是我过去使用过的一个类。 它评估字符串就像 JavaScript 中的 eval() 一样。
您需要做的就是构造一个要从业务对象进行评估的字符串,这将处理所有复杂的嵌套逻辑等。
Here is a class I have used in the past. It evaluates strings just like eval() does in Javascript.
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.
开源 .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.
好吧,由于逻辑表达式只是数学表达式的子集,您可能需要尝试 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.
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).
我用过这个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.
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.
您也可以在 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.
我会看看 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.
也许查看SmartRules。 它不是免费的,但界面看起来很简单。
只知道它是因为我之前使用过那里的 SmartCode codegen 实用程序。
以下是网站上的规则示例:
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:
您可以使用 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
试用
http://rulesengine.codeplex.com/
它是一个与表达式树一起使用的 C# 开源规则引擎。
Try out
http://rulesengine.codeplex.com/
It's a C# Open-Source rules engine that works with Expression trees.
查看 Logician:CodeProject
项目上的 教程/概述:page/source
Have a look at Logician: tutorial/overview on CodeProject
Project: page/source on SourceForge
根据您尝试执行的操作,使用 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.
它不是免费的,因为您无法轻松地将其从 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.)