Drools规则引擎
通过学习rete算法之后,这篇博客来介绍一下一个rete算法实现的规则引擎的框架Drools。Drools是Jboss公司旗下一款开源的规则引擎,有如下特点;完整的实现了Rete算法;提供了强大的Eclipse Plugin开发支持;通过使用其中的DSL(Domain Specific Language),可以实现用自然语言方式来描述业务规则,使得业务分析人员也可以看懂业务规则代码;提供了基于WE
·
通过学习rete算法之后,这篇博客来介绍一下一个rete算法实现的规则引擎的框架Drools。
Drools是Jboss公司旗下一款开源的规则引擎,有如下特点;
- 完整的实现了Rete算法;
- 提供了强大的Eclipse Plugin开发支持;
- 通过使用其中的DSL(Domain Specific Language),可以实现用自然语言方式来描述业务规则,使得业务分析人员也可以看懂业务规则代码;
- 提供了基于WEB的BRMS——Guvnor,Guvnor提供了规则管理的知识库,通过它可以实现规则的版本控制,及规则的在线修改与编译,使得开发人员和系统管理人员可以在线管理业务规则。
Drools 是业务逻辑集成平台,被分为4个项目:
- Drools Guvnor (BRMS/BPMS):业务规则管理系统
- Drools Expert (rule engine):规则引擎,drools的核心部分
- Drools Flow (process/workflow):工作流引擎
- Drools Fusion (cep/temporal reasoning):事件处理
Drools规则引擎的原理(原理解析见rete算法篇)
Drools 实例:
实体类:
package com.test;
public class Order {
private String name = "";
private Integer sumprice =0;
private Integer DiscountPercent=0;;
public Integer getSumprice() {
return sumprice;
}
public void setSumprice(Integer sumprice) {
this.sumprice = sumprice;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getDiscountPercent() {
return DiscountPercent;
}
public void setDiscountPercent(Integer DiscountPercent) {
this.DiscountPercent = DiscountPercent;
}
}
规则文件:
#created on: 2009-11-11
package com.test
#list any import classes here.
import com.test.Order;
#declare any global variables here
rule "First Rule"
when
#conditions
order:Order(sumprice>30,sumprice<=50);
then
#actions
order.setDiscountPercent(98);
end
rule "Second Rule"
#include attributes such as "salience" here...
when
#conditions
order:Order(sumprice>50,sumprice<=100);
then
#actions
order.setDiscountPercent(95);
end
rule "third Rule"
#include attributes such as "salience" here...
when
#conditions
order:Order(sumprice>100);
then
#actions
order.setDiscountPercent(90);
end
测试类:
package com.test;
import java.util.Arrays;
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.runtime.StatelessKnowledgeSession;
public class ruleTest {
public static final void main(String[] args) throws Exception {
ruleTest launcher = new ruleTest();
launcher.executeExample();
}
public int executeExample() throws Exception {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add( ResourceFactory.newClassPathResource( "discountrule.drl",
getClass() ),
ResourceType.DRL);
if ( kbuilder.hasErrors() ) {
System.err.print( kbuilder.getErrors() );
return -1;
}
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages( kbuilder.getKnowledgePackages() );
StatelessKnowledgeSession ksession = kbase.newStatelessKnowledgeSession();
Order order = new Order();
order.setSumprice(159);
ksession.execute( Arrays.asList( new Object[]{order} ) );
System.out.println( "DISCOUNT IS: " + order.getDiscountPercent() );
return order.getDiscountPercent();
}
}
测试结果:
通过理论和demo应该对drools规则引擎有一定的了解,后面还会对drools规则引擎再介绍一篇博客,针对它的用法详细介绍一下。
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
已为社区贡献2条内容
所有评论(0)