Java 数据驱动规则引擎 - Drools
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18359590/
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
Data Driven Rules Engine - Drools
提问by Jasper
I have been evaluating Drools as a Rules Engine for use in our Business Web Application.
我一直在评估 Drools 作为在我们的业务 Web 应用程序中使用的规则引擎。
My use case is a Order Management Application.
And the rules are of following kind:
- If User Type is "SPECIAL" give an extra 5% discount.
- If User has made 10+ Purchases already, give an extra 3% discount.
- If Product Category is "OLD", give a Gift Hamper to the user worth $5.
- If Product Category is "NEW", give a Gift Hamper to the user worth $1
- If User has made purchases of over $1000 in the past, Shipping is Free
我的用例是订单管理应用程序。
规则如下:
- 如果用户类型为“特殊”,则给予额外 5% 的折扣。
- 如果用户已经进行了 10 次以上的购买,则给予额外 3% 的折扣。
- 如果产品类别为“旧”,则向用户赠送价值 5 美元的礼品篮。
- 如果产品类别为“新”,则向用户赠送价值 1 美元的礼品篮
- 如果用户过去购买超过 1000 美元,则免运费
The immediate challenge i see is that:
- There is no meaningful UI that i can offer to the end users to modify the rules.
- Guvnor UI or any Editor to modify drl files is just not acceptable from end user point of view
- Most of these Rules will operate on often huge data available in db
我看到的直接挑战是:
- 没有有意义的 UI 可以提供给最终用户来修改规则。
- 从最终用户的角度来看,Guvnor UI 或任何用于修改 drl 文件的编辑器是不可接受的 - 这些规则中的大多数将操作 db 中通常可用的大量数据
So,
- I want a way for Admin users to specify these Rule from within my Web App UI.
- Could i store these "Rules" in database, and then operate on them via Drools - at least that allows me to "modify" these Rules via my "own" UI. So this is something like a Decision Table in DB.
- What is the best way to go about this?
所以,
- 我想要一种让管理员用户从我的 Web 应用程序 UI 中指定这些规则的方法。
- 我可以将这些“规则”存储在数据库中,然后通过 Drools 对它们进行操作 - 至少这允许我通过我的“自己的”用户界面“修改”这些规则。所以这有点像数据库中的决策表。
- 最好的方法是什么?
回答by kevinpeterson
Generally, I've found it is easier to work at a more abstract level, such as a Domain Model, and have some sort of programmatic conversion from that to Drools rules, instead of dealing with Drools rules directly. That way, you can store your Domain Model however you like, and you can build UIs around it, etc, and still have the option to generate Drools rules on demand. Then challenge with this is creating a programmatic transformation from your model to Drools rules, but templating tools will help here. I've used Groovy templating for this, and it has worked well.
通常,我发现在更抽象的级别(例如域模型)上工作更容易,并且从该级别进行某种编程转换为 Drools 规则,而不是直接处理 Drools 规则。这样,您可以随心所欲地存储域模型,并且可以围绕它构建 UI,等等,并且仍然可以选择按需生成 Drools 规则。然后面临的挑战是创建从模型到 Drools 规则的程序化转换,但模板工具将在这里有所帮助。我为此使用了 Groovy 模板,并且效果很好。
回答by Bill Karwin
You asked me to give an answer to your question, given my answer to Data driven business rules. My answer to that question was that SQL is a bad solution to executebusiness rules stored in the database. The person who asked that question wanted to generate SQL expressions from their stored business rules, and I cautioned against doing that, because it would lead to problems in security, testability, performance, and maintenance.
鉴于我对数据驱动业务规则的回答,您让我回答您的问题。我对这个问题的回答是 SQL 是执行存储在数据库中的业务规则的糟糕解决方案。提出这个问题的人想从他们存储的业务规则中生成 SQL 表达式,我警告不要这样做,因为这会导致安全性、可测试性、性能和维护方面的问题。
I have not used Drools, but I gather from documentation that it includes Guvnor, a business rules manager that supports using an RDBMS as a repositoryfor user-defined rules.
我没有使用过 Drools,但我从文档中了解到它包含 Guvnor,这是一个支持使用 RDBMS 作为用户定义规则存储库的业务规则管理器。
[Drools] Guvnor uses the JCR standard for storing assets such as rules. The default implementation is Apache Hymanrabbit, http://Hymanrabbit.apache.org. This includes an out of the box storage engine/database, which you can use as is, or configure to use an existing RDBMS if needed. (http://docs.jboss.org/drools/release/5.2.0.Final/drools-guvnor-docs/html/chap-database_configuration.html)
[Drools] Guvnor 使用 JCR 标准来存储规则等资产。默认实现是 Apache Hymanrabbit,http://Hymanrabbit.apache.org。这包括一个开箱即用的存储引擎/数据库,您可以按原样使用,或者在需要时配置为使用现有的 RDBMS。( http://docs.jboss.org/drools/release/5.2.0.Final/drools-guvnor-docs/html/chap-database_configuration.html)
Apache Hymanrabbit is not an RDBMS, it is "a content repository is a hierarchical content store with support for structured and unstructured content, full text search, versioning, transactions, observation, and more." This seems like a more appropriate repository for Drools.
Apache Hymanrabbit 不是 RDBMS,它是“内容存储库是一种分层内容存储,支持结构化和非结构化内容、全文搜索、版本控制、事务、观察等。” 这似乎是 Drools 更合适的存储库。
But Drools doesn't say it tries to use SQL to execute those business rules. It has a separate component, Drools Expert (Rules Engine)to do that.
但是 Drools 并没有说它尝试使用 SQL 来执行这些业务规则。它有一个单独的组件Drools Expert(规则引擎)来做到这一点。
Drools Expert is a declarative, rule based, coding environment. This allows you to focus on "what it is you want to do", and not the "how to do this". (http://www.jboss.org/drools/drools-expert.html)
Drools Expert 是一个声明式的、基于规则的编码环境。这使您可以专注于“您想做什么”,而不是“如何做”。( http://www.jboss.org/drools/drools-expert.html)
SQL is also a declarative programming language, but it's designed to perform relational operations on table-structured data. A language to implement a rules engine has different goals, and can probably do things that SQL can't (and vice-versa).
SQL 也是一种声明式编程语言,但它旨在对表结构数据执行关系操作。一种实现规则引擎的语言有不同的目标,并且可能可以做 SQL 不能做的事情(反之亦然)。
So I would suggest if you use Drools, feel free to use an RDBMS as a repository as they document (use their JCR-compliant implementation of content repository, do not try to design your own). Then use their Drools Expert as a specialized language designed for executing rules.
所以我建议如果你使用 Drools,可以随意使用 RDBMS 作为他们记录的存储库(使用他们的 JCR 兼容的内容存储库实现,不要尝试设计你自己的)。然后使用他们的 Drools Expert 作为专门为执行规则而设计的语言。
回答by Steve
- There is no meaningful UI that i can offer to the end users to modify the rules.
- 我无法向最终用户提供用于修改规则的有意义的 UI。
Out of the box, Guvnor provides web based decision tables(and Excel if you prefer), as you say you would like to provide. It provides guided editorsfor more complex rules, but your rules would appear to be very simple.
开箱即用,Guvnor 提供基于 Web 的决策表(如果您愿意,还可以提供Excel),正如您所说的那样。它为更复杂的规则提供了指导编辑器,但您的规则看起来非常简单。
- Guvnor UI or any Editor to modify drl files is just not acceptable from end user point of view
- 从最终用户的角度来看,Guvnor UI 或任何修改 drl 文件的编辑器是不可接受的
As mentioned, Guvnor supports decision tables. If you don't like the layout of the Guvnor web application, then you can just embed the Guvnor editorsinto your own web application.
如前所述,Guvnor 支持决策表。如果您不喜欢 Guvnor Web 应用程序的布局,那么您可以将 Guvnor 编辑器嵌入到您自己的 Web 应用程序中。
- Most of these Rules will operate on often huge data available in db
- 这些规则中的大多数将在 db 中可用的大量数据上运行
The size of your database is irrelevant to the use of Guvnor. Guvnor is for editing rules, not runtime evaluation. Drools Expert is the runtime rules engine. It's fast. It can deal with very large volumes of data and very large volumes of rules. All you need to do is write database queries to get relevant chunks of that data into the rules engine at runtime. You need to do that, whatever solution you try to implement.
数据库的大小与 Guvnor 的使用无关。Guvnor 用于编辑规则,而不是运行时评估。Drools Expert 是运行时规则引擎。它很快。它可以处理非常大量的数据和非常大量的规则。您需要做的就是编写数据库查询,以便在运行时将该数据的相关块获取到规则引擎中。无论您尝试实施什么解决方案,您都需要这样做。
On a side-note, if what you're really after is an explanation of when rules engines are good (and bad) solutions to a problem, then I would recommend reading the Why use a Rule engine?section of the Drools Expert manual.
附带说明一下,如果您真正想要的是解释规则引擎何时是问题的好(和坏)解决方案,那么我建议您阅读为什么使用规则引擎?Drools 专家手册的部分。