一:使用maven导入jar包,在maven中央库http://search.maven.org/搜索kie-spring和drools
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-core</artifactId>
<version>6.3.0.Final</version>
</dependency>
<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-spring</artifactId>
<version>6.3.0. Final</version>
</dependency>
二:新建spring-drools.xml配置文件
<kie:kmodule id=”kmodule1″>
<kie:kbase name=”kbase1″ packages=”drools.rules” ><!—父级和子级目录用 . –>
<kie:ksession name=”ksession1″><!—这个kie:ksession可以不写 –>
<kie:consoleLogger/>
</kie:ksession>
</kie:kbase>
</kie:kmodule>
<bean id=”kiePostProcessor” class=”org.kie.spring.KModuleBeanFactoryPostProcessor”/>
注意:其中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
转载请注明来源网站:blog.ytso.com谢谢!
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/14625.html