java 开发小型规则引擎的简单设计模式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4275773/
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
A simple design pattern to develop a small Rule-Engine
提问by Vijay Veeraraghavan
I have a requirement which take lots of rules validated against the java value objects and produces results. (we cant use any rule engine apps in our company, lots of formalities and lots of questions to be answered). So, rather than implementing the rules as ifs' in java code I suggested to implement a small rule engine, Simple and Extensible. Which design pattern to follow?
我有一个需求,它需要针对 java 值对象验证大量规则并产生结果。(我们公司不能使用任何规则引擎应用程序,很多手续和很多问题需要回答)。因此,我建议实现一个小的规则引擎,简单和可扩展,而不是像在 java 代码中那样实现规则。遵循哪种设计模式?
I have added below a rough xml structure of the rules defined.
我在下面添加了定义规则的粗略 xml 结构。
<rule-set>
<name>Example1</name>
<description>Example rules defined</description>
<beans>
<bean class="com.example.Customer" alias="cust"/>
<bean class="com.example.Account" alias="acnt"/>
<bean class="com.example.Transaction" alias="trans"/>
</beans>
<rule name="CustomerInfo" description="This rule validates if all the customer values are present">
<if lhs="cust.getFirstName" rhs="null" operator="!="/>
<if lhs="cust.getLastName" rhs="null" operator="!=" logicaloperator="&&"/>
<if lhs="cust.getCountry" rhs="null" operator="!=" logicaloperator="||"/>
<if lhs="cust.getCity" rhs="null" operator="!=" logicaloperator="&&"/>
<if lhs="cust.getPhone" rhs="null" operator="!=" logicaloperator="&&"/>
<if lhs="cust.getEmail" rhs="null" operator="!=" logicaloperator="&&"/>
<then do="cust.completeFlag" arg1="true"/>
</rule>
<rule name="Transaction" description="Transfer the money from one ac to another">
<if lhs="trans.fromAccount" operator="!=" rhs="null"/>
<if lhs="trans.toAccount" operator="!=" rhs="null"/>
<if lhs="trans.fromAccount.balance" operator=">" rhs="trans.getTransaferAmount"/>
<then do="trans.fromAccount.debit" arg1="trans.getTransaferAmount"/>
<then do="trans.toAccount.credit" arg1="trans.getTransaferAmount"/>
</rule>
</rule-set>
回答by Sriram
It really depends on the complexity of the rules you are trying to implement. The key idea in declarative programming is that rules are treated as data. So, the simplest way to start is to see if all your rules can be represented as data in a table. For instance, if your rules are of the type if a=10, then b = 7, then you can represent the same in a table and write a generic method that can handle all your cases.
这实际上取决于您尝试实施的规则的复杂性。声明式编程的关键思想是将规则视为数据。因此,最简单的开始方法是查看您的所有规则是否都可以表示为表中的数据。例如,如果您的规则属于 if a=10, then b = 7 类型,那么您可以在表中表示相同的内容并编写一个可以处理所有情况的通用方法。
On the other hand, if your rules allow multiple conditions (and/or clauses as well as comparison operators), a table based design will not help.
另一方面,如果您的规则允许多个条件(和/或子句以及比较运算符),则基于表的设计将无济于事。
In that case, you need to specify the grammar for your rules, generate a lexer and a parser. The parser would parse your actual rules into a abstract syntax tree. Once you get to that stage, you can either take that tree and target it to an existing rule engine or your own rule engine that knows how to execute the tree.
在这种情况下,您需要为规则指定语法,生成词法分析器和解析器。解析器会将您的实际规则解析为抽象语法树。到达该阶段后,您可以使用该树并将其定位到现有规则引擎或您自己的知道如何执行树的规则引擎。
回答by p.marino
Not exactly what you asked (i.e. pattern) but maybe you can find the following approach useful if you want to roll your own:
不完全是你问的(即模式),但如果你想推出自己的方法,也许你会发现以下方法很有用:
http://mcqueeney.com/blog/creating-a-simple-rules-engine-using-the-java-scripting-api/
http://mcqueeney.com/blog/creating-a-simple-rules-engine-using-the-java-scripting-api/
回答by Buhake Sindi
I would recommend a Factory Method Pattern. Each rule will have a Factory that can create those rules. I would then encapsulate all these Rule Factories into an Abstract Factory.
我会推荐工厂方法模式。每个规则都有一个可以创建这些规则的工厂。然后我会将所有这些规则工厂封装到一个抽象工厂中。
Alternatively, you can create a Rule Engine as a Builder (using a Builder Pattern) that allows passing rule sets and construct a rule.
或者,您可以创建一个规则引擎作为构建器(使用构建器模式),它允许传递规则集并构建规则。
That's what I would do best, but since I don't know the exact requirements of your rule engine that what popped into mind.
这就是我会做的最好的,但因为我不知道你的规则引擎的确切要求,所以我想到了什么。
Perhaps a Strategy Patterncould help you?
也许策略模式可以帮助您?