我想我需要一个简单的规则引擎?
我需要一些关于解决这个问题的最佳方法的建议。
我研究了 DROOLS、Java 规则引擎和其他一些引擎。所有这些都很强大,并且都有好处。我不知道哪一个(如果有的话)对我来说是最好的选择。
我有一个商业对象。 (为了演示而简化)
Person
firstName:String
lastName:String
departMent:String
hireDate:Date
我需要在 Web 应用程序中编写一个编辑器,该编辑器将允许围绕这些字段构建复杂的规则。我需要支持复杂的嵌套 AND/OR 逻辑。我只需要基本运算符,并且规则应该简单地评估为 true 或 false。如果规则评估为 true 或 false,则将分别发生一个操作。
例如,
firstName 包含“值”并且 (姓氏等于“输入”或部门 包含“输入”)
我曾想,也许我应该编写自己的解析器并评估自己代码中的逻辑。我不知道该怎么做,任何建议或阅读内容的链接将不胜感激。有我可以研究的特定设计模式吗?
你会如何解决这个问题?对规则引擎的一个保留意见是,对于一个简单的问题来说,它们可能太复杂了?
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.
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.
For example,
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这不是一个是/否的问题,但我可以分享我的经验,希望它有所帮助。
我在一些项目中非常成功地使用了 DROOLS。除了某些情况(另一个团队在重负载下遇到 DROOLS 问题)之外,DROOLS 是一个非常有用的库。
我构建了一个应用程序:
1.从源读取输入
2. 根据一组可用操作的输入选择下一个操作
尽管看起来很简单,但它需要非常灵活:
1. 输入是一组变量名称-值对,名称未预先确定。
2.值,某些名称/值的存在/不存在(基于事件的发生/不存在),触发不同的操作。
3. 业务规则可以在应用程序运行时发生变化。
也许有更好的解决方案,但无论好坏,我最终使用了 DROOLS。我开发了一个 BPEL,其中的决策是由 DROOLS 组件做出的。 DROOLS 组件在内部从 Microsoft Excel 电子表格读取决策规则。如果文件发生更改,它会重建规则。
现在,领域专家会在需要时更改此电子表格,并且我们不会经历痛苦的部署!
如果您想要复杂的 UI,DROOLS Guvnor 是一个随时可用的 Web 应用程序(具有丰富的 UI),它将帮助您的领域/主题专家构建规则并将其存储在数据库中。
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.
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
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.
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!
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.
Drools 文档讨论了何时使用规则引擎。 http://downloads.jboss .com/drools/docs/5.1.1.34858.FINAL/drools-expert/html_single/index.html#d0e181
从文档中...
什么时候不可以用...
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
From the docs...
When not to use...
我建议你自己的解析器。在这种情况下,为什么不能序列化对象并使用 AJAX 在后端验证它?然后,它将验证逻辑与 UI 分开。
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.
我会看一些示例规则引擎接口,看看我喜欢哪些。您可以查看基于网络的电子邮件规则界面以获得一些想法。如果你确实需要一个简单的规则引擎,你只需要创建一个好的接口,然后你可以用javascript将规则发送到服务器。
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.
可能不会。您需要一个像样的领域模型。您的对象不只是数据占位符。您的用户是否能够理解和使用如此复杂的规则系统,而那些确实更喜欢仅使用 Java 进行编程的用户会不会因为他们有适当的封装和重构支持?规则系统仅适用于受限域上的简单规则,您可以在其中向未受过程序员培训的人员解释如何构建它们。并且不要忘记规则构建只是编程,因此您仍然需要版本控制、测试,并且不需要全局变量。
Probably not. You need a decent domain model. Not one where your objects are 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.
Jython 没有用吗?
每个表达式/复杂规则都可以是函数的主体。
因此,用户提供主体,并且您的代码将函数规范放在其周围,然后执行它。
您还可以将您自己的任何 Java 对象/变量放入 jython 上下文中,以便在脚本/函数体中使用。
然后您就可以轻松获得一种标准化的、可扩展的广泛使用的语言。
但我认为 Jython 编辑器可能是一个挑战。
Wouldn't Jython be usefull?
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.
Then you have a standardized, extendable widely used language at your fingertips.
But i think the Jython editor could be a challenge.
您尝试过 JBehave 吗?
Have you tried JBehave?