java 我需要一个简单的规则引擎,我想?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4329858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
I need a simple rules engine, I think?
提问by Andy
I need some advice on the best approach to solving this problem.
我需要一些有关解决此问题的最佳方法的建议。
I've researched DROOLS, Java Rule Engine and a few others. All of these are powerful, and have good things about them. I don't know which (if any) will be the best choice for me.
我研究了 DROOLS、Java Rule Engine 和其他一些。所有这些都是强大的,并且有关于它们的优点。我不知道哪个(如果有的话)将是我的最佳选择。
I have one business object. (simplified for demo)
我有一个业务对象。(简化演示)
Person
firstName:String
lastName:String
departMent:String
hireDate:Date
I need to write an editor in a web application that will allow for complex rules to be built around these fields. I need to support complex nested AND/OR logic. I just need basic operators, and the rule should simply evaluate to true or false. If the rule evaluates to true or false, one action will occur respectively.
我需要在 Web 应用程序中编写一个编辑器,以允许围绕这些字段构建复杂的规则。我需要支持复杂的嵌套 AND/OR 逻辑。我只需要基本的运算符,规则应该简单地评估为真或假。如果规则评估为真或假,将分别发生一个动作。
For example,
例如,
firstName CONTAINS "value" AND (lastName EQUALS "input" OR department CONTAINS "input")
firstName CONTAINS "value" AND (lastName EQUALS "input" OR department CONTAINS "input")
I had thought, maybe I should just write my own parser and evaluate the logic in my own code. I don't know what to do, any advice or links to something to read would be greatly appreciated. Is there a specific design pattern I could research?
我曾想,也许我应该编写自己的解析器并在自己的代码中评估逻辑。我不知道该怎么办,任何建议或指向阅读内容的链接将不胜感激。有我可以研究的特定设计模式吗?
How would you solve this problem? One reservations about the rules engines is that maybe they are too complex for just a simple problem?
你会如何解决这个问题?对规则引擎的一个保留意见是,对于一个简单的问题,它们可能太复杂了?
采纳答案by CMR
This is not an yes/no question, but I can probably share my experiences, and hope it helps. I have used DROOLS quite successfully in a few projects. Apart from some cases (another team had issues with DROOLS under heavy load,) DROOLS is quite an useful library.
这不是一个是/否的问题,但我可能可以分享我的经验,并希望它有所帮助。我在一些项目中非常成功地使用了 DROOLS。除了某些情况(另一个团队在重负载下遇到 DROOLS 问题)之外,DROOLS 是一个非常有用的库。
I built an application which:
1. read input from a source
2. chose the next action based on the input from a set of available operations
我构建了一个应用程序,它:
1. 从源读取输入
2. 根据一组可用操作的输入选择下一个操作
As trivial as it looks, it needed to be very flexible:
1. The input was a variable set of name-value pairs, names not predetermined.
2. values, presence/absence of certain name/values (based on occurrence/absence of events), trigger different actions.
3. The business rules can change while the application is running.
尽管看起来微不足道,但它需要非常灵活:
1. 输入是一组可变的名称-值对,名称不是预先确定的。
2.值,某些名称/值的存在/不存在(基于事件的发生/不存在),触发不同的动作。
3. 业务规则可以在应用程序运行时改变。
Maybe there are better solutions, but for better or worse, I ended up using DROOLS. I developed a BPEL in which the decisions are made by the DROOLS component. The DROOLS component internally reads the decision making rules from a Microsoft Excel spreadsheet. It rebuilds the rules if there is a change in the file. Now the domain experts change this spreadsheet when required, and we do not go through painful deployments!
也许有更好的解决方案,但无论好坏,我最终使用了 DROOLS。我开发了一个 BPEL,其中决策由 DROOLS 组件做出。DROOLS 组件在内部从 Microsoft Excel 电子表格中读取决策制定规则。如果文件发生更改,它会重建规则。现在,领域专家会在需要时更改此电子表格,我们无需经历痛苦的部署!
If you want a sophisticated UI, DROOLS Guvnor is a readily available web-application (with rich UI,) which would help your domain/subject-matter experts to build rules and store them in a database.
如果您想要一个复杂的 UI,DROOLS Guvnor 是一个随时可用的 Web 应用程序(具有丰富的 UI),它将帮助您的领域/主题专家构建规则并将它们存储在数据库中。
回答by Bill
The Drools documentation talks about when to use a rules engine. http://downloads.jboss.com/drools/docs/5.1.1.34858.FINAL/drools-expert/html_single/index.html#d0e181
Drools 文档讨论了何时使用规则引擎。 http://downloads.jboss.com/drools/docs/5.1.1.34858.FINAL/drools-expert/html_single/index.html#d0e181
From the docs...
从文档...
The shortest answer to this is "when there is no satisfactory traditional programming approach to solve the problem.". Given that short answer, some more explanation is required. The reason why there is no "traditional" approach is possibly one of the following:
-- The problem is just too fiddle for traditional code.
The problem may not be complex, but you can't see a non-fragile way of building a solution for it.
-- The problem is beyond any obvious algorithmic solution.
It is a complex problem to solve, there are no obvious traditional solutions, or basically the problem isn't fully understood.
-- The logic changes often
The logic itself may even be simple but the rules change quite often. In many organizations software releases are few and far between and pluggable rules can help provide the "agility" that is needed and expected in a reasonably safe way.
-- Domain experts (or business analysts) are readily available, but are nontechnical.
Domain experts often possess a wealth of knowledge about business rules and processes. They typically are nontechnical, but can be very logical. Rules can allow them to express the logic in their own terms. Of course, they still have to think critically and be capable of logical thinking. Many people in nontechnical positions do not have training in formal logic, so be careful and work with them, as by codifying business knowledge in rules, you will often expose holes in the way the business rules and processes are currently understood.
对此的最短回答是“当没有令人满意的传统编程方法来解决问题时。”。鉴于这个简短的答案,需要更多的解释。没有“传统”方法的原因可能是以下之一:
-- 这个问题对于传统代码来说太麻烦了。
问题可能并不复杂,但您看不到为它构建解决方案的非脆弱方法。
-- 这个问题超出了任何明显的算法解决方案。
这是一个需要解决的复杂问题,没有明显的传统解决方案,或者基本上没有完全理解问题。
-- 逻辑经常变化
逻辑本身甚至可能很简单,但规则经常变化。在许多组织中,软件版本很少,而且可插拔规则可以帮助以合理安全的方式提供所需和预期的“敏捷性”。
-- 领域专家(或业务分析师)随时可用,但不是技术人员。
领域专家通常拥有丰富的业务规则和流程知识。它们通常是非技术性的,但可能非常合乎逻辑。规则可以让他们用自己的术语来表达逻辑。当然,他们仍然需要批判性地思考,并具有逻辑思维能力。许多非技术职位的人没有接受过形式逻辑方面的培训,所以要小心并与他们一起工作,因为通过将业务知识编入规则,您经常会暴露当前理解业务规则和流程的方式中的漏洞。
When not to use...
什么时候不用...
As rule engines are dynamic (dynamic in the sense that the rules can be stored and managed and updated as data), they are often looked at as a solution to the problem of deploying software. (Most IT departments seem to exist for the purpose of preventing software being rolled out.) If this is the reason you wish to use a rule engine, be aware that rule engines work best when you are able to write declarative rules. As an alternative, you can consider data-driven designs (lookup tables), or script processing engines where the scripts are managed in a database and are able to be updated on the fly.
由于规则引擎是动态的(在规则可以作为数据存储、管理和更新的意义上是动态的),它们通常被视为部署软件问题的解决方案。(大多数 IT 部门的存在似乎是为了防止软件推出。)如果这就是您希望使用规则引擎的原因,请注意,当您能够编写声明性规则时,规则引擎的工作效果最佳。作为替代方案,您可以考虑数据驱动设计(查找表)或脚本处理引擎,其中脚本在数据库中进行管理并且能够即时更新。
回答by Houtman
Wouldn't Jython be usefull?
Jython 不会有用吗?
Each expression/complex-rule could be the body of a function. So the user provides the body, and your code puts the function spec around it, and then execute it.
每个表达式/复杂规则都可以是一个函数体。所以用户提供主体,你的代码将函数规范放在它周围,然后执行它。
You can also put any Java objects / variables of your own into the jython context to be used in your script/ function body.
您还可以将您自己的任何 Java 对象/变量放入 jython 上下文中,以便在您的脚本/函数体中使用。
Then you have a standardized, extendable widely used language at your fingertips. But i think the Jython editor could be a challenge.
然后,您就拥有了一种触手可及的标准化、可扩展的广泛使用的语言。但我认为 Jython 编辑器可能是一个挑战。
回答by Dasith Wijes
I would suggest your own parser. In this context why can't you serialize the object and use AJAX to validate it in the back end? It then seperates the validation logic from the UI.
我会建议你自己的解析器。在这种情况下,为什么不能序列化对象并在后端使用 AJAX 对其进行验证?然后它将验证逻辑与 UI 分开。
回答by Jon Snyder
I would take a look at some sample rules engine interfaces and see which ones I like. You can look at web based email rules interfaces to get some ideas. If you really need a simple rules engine, you just need to create a good interface, and then you can send the rules to the server with javascript.
我会看看一些示例规则引擎接口,看看我喜欢哪些。您可以查看基于 Web 的电子邮件规则界面以获得一些想法。如果你真的需要一个简单的规则引擎,你只需要创建一个好的界面,然后你就可以用javascript将规则发送到服务器。
回答by Stephan Eggermont
Probably not. You need a decent domain model. Not one where your objectsare just data placeholders. Are your users likely to be able to understand and use such a complex rules system, and wouldn't those that do prefer just programming in java, where they have proper encapsulation and refactoring support? Rules systems only work with simple rules on a restricted domain, where you can explain people not trained as programmers how to build them. And don't forget rules building is just programming, so you still need version control, tests, and don't want globals.
可能不是。您需要一个体面的域模型。不是您的对象只是数据占位符的地方。您的用户是否可能能够理解和使用如此复杂的规则系统,而那些确实更喜欢用 Java 编程的人,在那里他们有适当的封装和重构支持吗?规则系统仅适用于受限域中的简单规则,您可以在其中解释未受过程序员培训的人如何构建它们。并且不要忘记规则构建只是编程,因此您仍然需要版本控制、测试,并且不需要全局变量。
回答by Steven
Have you tried JBehave?
你试过JBehave吗?
JBehave is a framework for Behaviour-Driven Development (BDD). BDD is an evolution of test-driven development (TDD) and acceptance-test driven design, and is intended to make these practices more accessible and intuitive to newcomers and experts alike. It shifts the vocabulary from being test-based to behaviour-based, and positions itself as a design philosophy.
JBehave 是行为驱动开发 (BDD) 的框架。BDD 是测试驱动开发 (TDD) 和验收测试驱动设计的演变,旨在使这些实践对新手和专家都更容易理解和直观。它将词汇从基于测试转变为基于行为,并将自己定位为一种设计哲学。