我想我需要一个简单的规则引擎?

发布于 2024-10-05 03:41:41 字数 622 浏览 1 评论 0原文

我需要一些关于解决这个问题的最佳方法的建议。

我研究了 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 技术交流群。

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

发布评论

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

评论(7

×眷恋的温暖 2024-10-12 03:41:41

这不是一个是/否的问题,但我可以分享我的经验,希望它有所帮助。
我在一些项目中非常成功地使用了 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.

不美如何 2024-10-12 03:41:41

Drools 文档讨论了何时使用规则引擎。 http://downloads.jboss .com/drools/docs/5.1.1.34858.FINAL/drools-expert/html_single/index.html#d0e181

从文档中...

对此问题最简短的回答是“当
没有令人满意的传统
编程方法来解决
问题。”。鉴于这个简短的答案,
需要更多解释。这
没有“传统”的原因
方法可能是其中之一
以下:

--这个问题实在是太麻烦了
传统代码。

问题可能并不复杂,但是
你看不到一种不脆弱的方式
为其构建解决方案。

--问题是显而易见的
算法解决方案。

这是一个需要解决的复杂问题,
没有明显的传统
解决方案,或者基本上是问题
还没有完全理解。

-- 逻辑经常变化

逻辑本身可能很简单
但规则经常改变。在
许多组织软件版本
数量少且相距遥远且可插拔
规则可以帮助提供“敏捷性”
这是需要和期望的
相当安全的方式。

-- 领域专家(或业务分析师)
很容易获得,但是
非技术性。

领域专家通常拥有财富
有关业务规则的知识和
流程。他们通常是
非技术性,但可能非常合乎逻辑。
规则可以让他们表达
用他们自己的话来说逻辑。当然,
他们仍然需要批判性地思考
并具有逻辑思维能力。
许多人担任非技术职位
没有受过形式逻辑训练,
所以要小心并与他们合作,因为
通过将业务知识编入
规则,你经常会暴露漏洞
商业规则的方式和
目前已了解流程。

什么时候不可以用...

由于规则引擎是动态的(动态
从某种意义上说,规则可以是
存储、管理和更新为
数据),它们通常被视为
解决部署问题
软件。 (大多数 IT 部门似乎
存在的目的是为了防止
软件正在推出。)如果这是
您希望使用规则的原因
引擎,请注意规则引擎
当你能够写作时,工作效果最好
声明性规则。作为替代方案,
你可以考虑数据驱动的设计
(查找表),或脚本处理
管理脚本的引擎
在数据库中并且能够
即时更新。

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

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.

眼前雾蒙蒙 2024-10-12 03:41:41

我建议你自己的解析器。在这种情况下,为什么不能序列化对象并使用 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.

纸短情长 2024-10-12 03:41:41

我会看一些示例规则引擎接口,看看我喜欢哪些。您可以查看基于网络的电子邮件规则界面以获得一些想法。如果你确实需要一个简单的规则引擎,你只需要创建一个好的接口,然后你可以用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.

迷离° 2024-10-12 03:41:41

可能不会。您需要一个像样的领域模型。您的对象不只是数据占位符。您的用户是否能够理解和使用如此复杂的规则系统,而那些确实更喜欢仅使用 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.

昔日梦未散 2024-10-12 03:41:41

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.

z祗昰~ 2024-10-12 03:41:41

您尝试过 JBehave 吗?

JBehave 是行为驱动开发 (BDD) 的框架。 BDD 是
测试驱动开发 (TDD) 和验收测试的演变
驱动设计,旨在使这些实践更容易实现
对于新手和专家来说都很直观。它改变了词汇
从基于测试到基于行为,并将自己定位为
设计理念。

Have you tried 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.

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