java development kie_drools6的kie-spring集成使用详解

fff6c98081bf8904098fb5e1bd59ff4b.gif

一:使用maven导入jar包,在maven中央库http://search.maven.org/搜索kie-spring和drools

org.drools

drools-core

6.3.0.Final

org.kie

kie-spring

6.3.0. Final

二:新建spring-drools.xml配置文件

注意:其中KModuleBeanFactoryPostProcessor这个bean一定得注解,

详细介绍请看https://docs.jboss.org/drools/release/6.1.0.Final/drools-docs/html/ch.kie.spring.html

三:写rules规则文件

package net.itxm

import net.itxm.DroolsTest.Message;

rule"Hello World"

when

m : Message( status == Message.HELLO, myMessage : message )

then

System.out.println( myMessage );

m.setMessage( "Goodbye cruel world" );

m.setStatus( Message.GOODBYE );

update( m );

end

rule"GoodBye"

when

Message( status == Message.GOODBYE, myMessage : message )

then

System.out.println( myMessage );

end

因为不能贴项目代码,所以用helloworld,并且假设你们知道rules规则,

不知道的请看http://docs.jboss.org/drools/release/6.3.0.Final/drools-docs/html/index.html

四:Junit4测试

在类名前加上注解

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration("classpath:drools/spring-drools.xml")

注解该属性

@Autowired

private KieBase kbase;

@Test

publicvoid test(){

KieSession kSession = kbase.newKieSession();

//Object obj;一个赋值的java对象

kSession.insert(obj);

kSession.fireAllRules();

}

Main方法

@SuppressWarnings("resource")

publicstaticvoid main(String[] args) {

ClassPathXmlApplicationContext context =

new ClassPathXmlApplicationContext( "drools/spring-drools.xml" );

//Object obj;一个赋值的java对象

StatefulKnowledgeSession kSession = (StatefulKnowledgeSession) context.getBean( "ksession1" );

kSession.insert(obj);

kSession.fireAllRules();

}

五:补充

rule"TOKEN A"

salience 99

agenda-group"TOKEN A"

auto-focustrue

when

BrokerageBean( typeList.size() == 1 )

then

kcontext.getKnowledgeRuntime().getAgenda().getAgendaGroup("TOKEN A1").setFocus();//①

kcontext.getKnowledgeRuntime().getAgenda().getAgendaGroup("TOKEN A2").setFocus();//②

end

rule"TOKEN A1"

salience 99

agenda-group"TOKEN A1"

when

$BB : BrokerageBean( $typeList : typeList )

$TIB : TypesOfInsuranceBean( typeId == 1) from $typeList

InsuranceBean( id == 3 ) from $TIB.insuranceList

//exists ( $TIB : TypesOfInsuranceBean( typeId == 1) from $typeList

//and InsuranceBean( id == 3 ) from $TIB.insuranceList )

then

DefaultDroolsRules.log.info("执行TOKEN A1");

//$BB.setInsuranceRate("32","0","0");

DefaultDroolsRules.setBb($BB,32,0);

end

rule"TOKEN A2"

salience 99

agenda-group"TOKEN A2"

when

$BB : BrokerageBean( $typeList : typeList )

TypesOfInsuranceBean( typeId == 1) from $typeList

//exists ( TypesOfInsuranceBean( typeId == 1) from $typeList )

then

DefaultDroolsRules.log.info("执行TOKEN A2");

//$BB.setInsuranceRate("24","0","0");

DefaultDroolsRules.setBb($BB,24,0);

End

fff6c98081bf8904098fb5e1bd59ff4b.gif

转载请注明来源网站:www.itxm.cn谢谢!

分享到:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值