哪种模式/模式最适合开发规则/决策引擎

时间:2022-01-16 11:59:53

I am working on a decision engine / rule evaluation engine. For example:

我正在研究决策引擎/规则评估引擎。例如:

Input: Customer and all the offences done by the customer

输入:客户和客户完成的所有违规行为

Output: Consequences of the offences

产出:违法行为的后果

A sample case would be:

一个示例案例是:

Input: Customer(Jhonny Chimpo, 999-00-1111), Offences ( Broke window, slapped boss, kicked team lead in his groin)

输入:客户(Jhonny Chimpo,999-00-1111),犯罪(打破了窗口,打了一个老板,在他的腹股沟踢了团队领导)

Output: Gets pink slip

输出:获得粉红色滑动

So the piece of code I want to write evaluates different offences based on rules for each offence and combined offence. The current code is just a maze of if and else statements. I am sure such business problems are common. What design/enterprise pattern is usually used to solve a problem like this?

因此,我想写的代码片段根据每个攻击和组合攻击的规则评估不同的攻击。当前代码只是if和else语句的迷宫。我确信这些业务问题很常见。通常使用什么设计/企业模式来解决这样的问题?

Is it the specification pattern? I want the code to be open for extension, clean and flexible.

这是规格模式吗?我希望代码是开放的扩展,干净和灵活。

6 个解决方案

#1


3  

Basically business rules look like

基本上业务规则看起来像

forall rules:
  if <condition> then doAction();

What about categorizing all offences by severity using scores, perhaps extra bonus for frequent "evil-doers", some offences may become time-barred and whatever required.

如何根据严重程度使用分数对所有犯罪进行分类,或许对频繁的“邪恶行为者”额外奖励,某些犯罪可能会成为时间限制,无论需要什么。

Then a rough draft of an algorithm could be:

然后算法的草稿可以是:

  • Sum of all scores of a customer (weighted)
  • 客户所有分数的总和(加权)
  • compare to maximum
  • 比较最大值

This would be straight forward using data structures instead of many (possibly deeply nested) if..then..else things.

这将是直接使用数据结构而不是许多(可能深度嵌套)if..then..else的东西。

#2


3  

I can suggest you a tool we used to solve a similar problem.

我可以建议你一个我们用来解决类似问题的工具。

Take a look at JBoss Drools: http://www.jboss.org/drools/

看看JBoss Drools:http://www.jboss.org/drools/

It's a BRMS: Business Rules Management System

这是一个BRMS:业务规则管理系统

Here it is an introductory video: http://www.jboss.com/products/platforms/brms/

这是一个介绍性的视频:http://www.jboss.com/products/platforms/brms/

#3


1  

I'm not sure any of the answers above have been that helpful.

我不确定上面的任何答案是否有帮助。

I have written similar components using expression trees. You can build lambda expressions that represent predicates, compile and execute them, all dynamically, and then trigger some action in response. This approach is powerful, flexible and strips out all the if/else horror (which is definitely not the way to go).

我使用表达式树编写了类似的组件。您可以构建表示谓词的lambda表达式,动态地编译和执行它们,然后触发一些响应操作。这种方法功能强大,灵活,并删除所有if / else恐怖(绝对不是这样)。

However, what you are really talking about is logic programming. There are numerous implementations of Prolog over .NET. Prolog is a logic based language, used alot for AI applications, that starts to become seriously powerful once you have your head around its paradigm.

但是,你真正在谈论的是逻辑编程。 Prolog over .NET有很多实现。 Prolog是一种基于逻辑的语言,很多用于AI应用程序,一旦你了解它的范例,它就开始变得非常强大。

Have a look at some of these ..

看看其中的一些......

#4


0  

You could try something similar to this "event based" rules engine

您可以尝试类似于“基于事件”的规则引擎

#5


0  

I think any RETE Algorithm based Rule Engine would work for your case. You can try drools.

我认为任何基于RETE算法的规则引擎都适用于您的情况。你可以尝试流口水。

#6


-1  

I think you're trying to develop an expert system. You can check the term and then check the appropriate programming languages as prolog etc.

我想你正在努力开发一个专家系统。您可以检查术语,然后检查相应的编程语言作为prolog等。

#1


3  

Basically business rules look like

基本上业务规则看起来像

forall rules:
  if <condition> then doAction();

What about categorizing all offences by severity using scores, perhaps extra bonus for frequent "evil-doers", some offences may become time-barred and whatever required.

如何根据严重程度使用分数对所有犯罪进行分类,或许对频繁的“邪恶行为者”额外奖励,某些犯罪可能会成为时间限制,无论需要什么。

Then a rough draft of an algorithm could be:

然后算法的草稿可以是:

  • Sum of all scores of a customer (weighted)
  • 客户所有分数的总和(加权)
  • compare to maximum
  • 比较最大值

This would be straight forward using data structures instead of many (possibly deeply nested) if..then..else things.

这将是直接使用数据结构而不是许多(可能深度嵌套)if..then..else的东西。

#2


3  

I can suggest you a tool we used to solve a similar problem.

我可以建议你一个我们用来解决类似问题的工具。

Take a look at JBoss Drools: http://www.jboss.org/drools/

看看JBoss Drools:http://www.jboss.org/drools/

It's a BRMS: Business Rules Management System

这是一个BRMS:业务规则管理系统

Here it is an introductory video: http://www.jboss.com/products/platforms/brms/

这是一个介绍性的视频:http://www.jboss.com/products/platforms/brms/

#3


1  

I'm not sure any of the answers above have been that helpful.

我不确定上面的任何答案是否有帮助。

I have written similar components using expression trees. You can build lambda expressions that represent predicates, compile and execute them, all dynamically, and then trigger some action in response. This approach is powerful, flexible and strips out all the if/else horror (which is definitely not the way to go).

我使用表达式树编写了类似的组件。您可以构建表示谓词的lambda表达式,动态地编译和执行它们,然后触发一些响应操作。这种方法功能强大,灵活,并删除所有if / else恐怖(绝对不是这样)。

However, what you are really talking about is logic programming. There are numerous implementations of Prolog over .NET. Prolog is a logic based language, used alot for AI applications, that starts to become seriously powerful once you have your head around its paradigm.

但是,你真正在谈论的是逻辑编程。 Prolog over .NET有很多实现。 Prolog是一种基于逻辑的语言,很多用于AI应用程序,一旦你了解它的范例,它就开始变得非常强大。

Have a look at some of these ..

看看其中的一些......

#4


0  

You could try something similar to this "event based" rules engine

您可以尝试类似于“基于事件”的规则引擎

#5


0  

I think any RETE Algorithm based Rule Engine would work for your case. You can try drools.

我认为任何基于RETE算法的规则引擎都适用于您的情况。你可以尝试流口水。

#6


-1  

I think you're trying to develop an expert system. You can check the term and then check the appropriate programming languages as prolog etc.

我想你正在努力开发一个专家系统。您可以检查术语,然后检查相应的编程语言作为prolog等。