Drools5.2.0.Final与Spring3集成测试
在drools5.2,有一个jar包:drools-spring-5.2.0.Final.jar,其中定义了在spring中应用的drools的扩展。通过这些扩展,可以直接在spring的配置文件中,配置knowledgebase、session等bean,从而在spring配置的程序中直接应用。
drools-spring-5.2.0.Final.jar在droolsjbpm-integration-distribution-5.2.0.Final\binaries文件夹下。
登录例子部分代码:

beans.xml
- <?xml version\="1.0" encoding\="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"\>
- <import resource="classpath:com/jsptpd/rjy/zyj/drools/beans-drools.xml"/>
- </beans>
<?xml version="1.0" encoding="UTF-8"?>
1<beans xmlns="http://www.springframework.org/schema/beans" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 3 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 4 xmlns:p="http://www.springframework.org/schema/p" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context-3.0.xsd 9 http://www.springframework.org/schema/tx 10 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 11 http://www.springframework.org/schema/aop 12 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> 13 <import resource="classpath:com/jsptpd/rjy/zyj/drools/beans-drools.xml"/> 14</beans>
beans-drools.xml
- <?xml version\="1.0" encoding\="UTF-8"?>
-
<beans xmlns="http://www.springframework.org/schema/beans"
-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-
xmlns:drools="http://drools.org/schema/drools-spring"
-
xmlns:camel="http://camel.apache.org/schema/spring"
-
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
-
http://drools.org/schema/drools-spring http://anonsvn.jboss.org/repos/labs/labs/jbossrules/trunk/drools-container/drools-spring/src/main/resources/org/drools/container/spring/drools-spring-1.0.0.xsd
-
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"\>
-
<drools:kbase id="kbase1">
- <!--不是<drools:resource type="DRL" source="classpath:com/jsptpd/rjy/zyj/service/Login.drl"/> -->
-
<drools:resource type="DRL" source="classpath:Login.drl"/>
-
</drools:resources>
-
</drools:kbase>
-
<drools:ksession id="ksession1" type="stateful" kbase="kbase1"/>
-
<bean id="vip" class="com.jsptpd.rjy.zyj.pojo.Vip" />
-
<bean id="loginService" class="com.jsptpd.rjy.zyj.service.LoginServiceImpl" >
-
<property name="vip" ref="vip" />
-
</bean>
-
</beans>
<?xml version="1.0" encoding="UTF-8"?>
1<beans xmlns="http://www.springframework.org/schema/beans" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns:drools="http://drools.org/schema/drools-spring" 4 xmlns:camel="http://camel.apache.org/schema/spring" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd 6 http://drools.org/schema/drools-spring http://anonsvn.jboss.org/repos/labs/labs/jbossrules/trunk/drools-container/drools-spring/src/main/resources/org/drools/container/spring/drools-spring-1.0.0.xsd 7 http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"> 8 9 <drools:kbase id="kbase1"> 10 <drools:resources> 11 <!--不是<drools:resource type="DRL" source="classpath:com/jsptpd/rjy/zyj/service/Login.drl"/> --> 12 <drools:resource type="DRL" source="classpath:Login.drl"/> 13 </drools:resources> 14 </drools:kbase> 15 16 <drools:ksession id="ksession1" type="stateful" kbase="kbase1"/> 17 18 <bean id="vip" class="com.jsptpd.rjy.zyj.pojo.Vip" /> 19 <bean id="loginService" class="com.jsptpd.rjy.zyj.service.LoginServiceImpl" > 20 <property name="vip" ref="vip" /> 21 </bean> 22</beans>
LoginTest.java
-
package com.jsptpd.rjy.zyj.junit;
-
import org.drools.runtime.StatefulKnowledgeSession;
-
import org.junit.Test;
-
import org.springframework.context.support.ClassPathXmlApplicationContext;
-
import com.jsptpd.rjy.zyj.service.LoginServiceImpl;
-
public class LoginTest {
-
public void testLogin(){
-
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "beans.xml" );
-
LoginServiceImpl loginServiceImpl= (LoginServiceImpl) context.getBean( "loginService" );
-
StatefulKnowledgeSession kstateless = (StatefulKnowledgeSession) context.getBean( "ksession1" );
-
loginServiceImpl.checkLogin(kstateless);
-
System.out.println("aa");
-
}
-
}
package com.jsptpd.rjy.zyj.junit;
1import org.drools.runtime.StatefulKnowledgeSession; 2import org.junit.Test; 3import org.springframework.context.support.ClassPathXmlApplicationContext; 4 5import com.jsptpd.rjy.zyj.service.LoginServiceImpl; 6 7public class LoginTest { 8 @Test 9 public void testLogin(){ 10 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "beans.xml" ); 11 LoginServiceImpl loginServiceImpl= (LoginServiceImpl) context.getBean( "loginService" ); 12 StatefulKnowledgeSession kstateless = (StatefulKnowledgeSession) context.getBean( "ksession1" ); 13 loginServiceImpl.checkLogin(kstateless); 14 System.out.println("aa"); 15 } 16}
LoginServiceImpl.java
-
package com.jsptpd.rjy.zyj.service;
-
import org.drools.runtime.StatefulKnowledgeSession;
-
import org.drools.runtime.StatelessKnowledgeSession;
-
import org.springframework.context.support.ClassPathXmlApplicationContext;
-
import com.jsptpd.rjy.zyj.pojo.Vip;
-
public class LoginServiceImpl {
-
private Vip vip;
-
public Vip getVip() {
-
return vip;
-
}
-
public void setVip(Vip vip) {
-
this.vip = vip;
-
}
-
public void checkLogin(StatefulKnowledgeSession kstateless ){
-
System.out.println("s");
-
kstateless.insert(vip);
-
kstateless.fireAllRules();
-
kstateless.dispose();
-
System.out.println("e");
-
}
-
public static boolean checkDB(String name,String password){
-
//实际可以到数据库匹配
-
return name.trim().equals("jack")&&password.trim().equals("123");
-
}
-
}
package com.jsptpd.rjy.zyj.service;
1import org.drools.runtime.StatefulKnowledgeSession; 2import org.drools.runtime.StatelessKnowledgeSession; 3import org.springframework.context.support.ClassPathXmlApplicationContext; 4 5import com.jsptpd.rjy.zyj.pojo.Vip; 6 7public class LoginServiceImpl { 8 private Vip vip; 9 10 public Vip getVip() { 11 return vip; 12 } 13 14 public void setVip(Vip vip) { 15 this.vip = vip; 16 } 17 18 public void checkLogin(StatefulKnowledgeSession kstateless ){ 19 System.out.println("s"); 20 kstateless.insert(vip); 21 kstateless.fireAllRules(); 22 kstateless.dispose(); 23 System.out.println("e"); 24 } 25 26 public static boolean checkDB(String name,String password){ 27 //实际可以到数据库匹配 28 return name.trim().equals("jack")&&password.trim().equals("123"); 29 } 30 31}
Login.drl
-
#created on: 2011-10-24
-
package com.jsptpd.rjy.zyj.service
-
#list any import classes here.
-
import com.jsptpd.rjy.zyj.pojo.Vip;
-
import java.io.Console;
-
import java.util.Scanner;
-
import com.jsptpd.rjy.zyj.service.LoginServiceImpl
-
#declare any global variables here
-
rule "vip初次登录"
-
salience 100
-
when
-
$vip:Vip((name==null||name=="")&&
-
(password==null||password=="") )
-
then
-
String tempName;
-
String tempPassword;
-
Console console=System.console();
-
Scanner scanner = new Scanner(System.in);
-
System.out.print("请输入用户名: ");
-
tempName=(console!=null?console.readLine():scanner.nextLine());
-
System.out.print("请输入密码: ");
-
tempPassword=(console!=null?new String(console.readPassword()):scanner.nextLine());
-
$vip.setName(tempName.trim());
-
$vip.setPassword(tempPassword.trim());
-
update($vip);
-
end
-
rule "没有输入密码"
-
salience 90
-
when
-
$vip:Vip((name!=null&&name!="")&&
-
(password==null||password==""),$name:name)
-
then
-
String tempPassword="";
-
Console console=System.console();
-
Scanner scanner = new Scanner(System.in);
-
System.out.print("请输入密码: ");
-
tempPassword=(console!=null?new String(console.readPassword()):scanner.nextLine());
-
$vip.setPassword(tempPassword.trim());
-
update($vip);
-
end
-
rule "没有输入用户名"
-
salience 90
-
when
-
$vip:Vip((name==null||name=="")&&
-
(password!=null&&password!=""),$password:password )
-
then
-
String tempName="";
-
Scanner scanner = new Scanner(System.in);
-
System.out.print("请输入用户名: ");
-
tempName=scanner.nextLine();
-
$vip.setName(tempName.trim());
-
update($vip);
-
end
-
rule "输入正确的用户名和密码"
-
salience 80
-
when
-
$vip:Vip((name!=null&&name!=""),
-
(password!=null&&password!=""),LoginServiceImpl.checkDB(name,password) )
-
then
-
System.out.print(" 欢迎 !!!"+$vip.getName());
-
end
-
rule "输入错误的用户名和密码"
-
salience 80
-
when
-
$vip:Vip((name!=null&&name!=""),
-
(password!=null&&password!=""),!LoginServiceImpl.checkDB(name,password) )
-
then
-
System.out.print(" 输入错误用户名或密码,请重新输入 !!!\n");
-
$vip.setName("");
-
$vip.setPassword("");
-
update($vip);
-
end
#created on: 2011-10-24
1package com.jsptpd.rjy.zyj.service 2 3#list any import classes here. 4import com.jsptpd.rjy.zyj.pojo.Vip; 5import java.io.Console; 6import java.util.Scanner; 7import com.jsptpd.rjy.zyj.service.LoginServiceImpl 8 9#declare any global variables here 10 11 12 13 14rule "vip初次登录" 15 salience 100 16 when 17 $vip:Vip((name==null||name=="")&& 18 (password==null||password=="") ) 19 then 20 String tempName; 21 String tempPassword; 22 Console console=System.console(); 23 Scanner scanner = new Scanner(System.in); 24 System.out.print("请输入用户名: "); 25 tempName=(console!=null?console.readLine():scanner.nextLine()); 26 System.out.print("请输入密码: "); 27 tempPassword=(console!=null?new String(console.readPassword()):scanner.nextLine()); 28 $vip.setName(tempName.trim()); 29 $vip.setPassword(tempPassword.trim()); 30 update($vip); 31end 32 33rule "没有输入密码" 34 salience 90 35 when 36 $vip:Vip((name!=null&&name!="")&& 37 (password==null||password==""),$name:name) 38 then 39 String tempPassword=""; 40 Console console=System.console(); 41 Scanner scanner = new Scanner(System.in); 42 System.out.print("请输入密码: "); 43 tempPassword=(console!=null?new String(console.readPassword()):scanner.nextLine()); 44 $vip.setPassword(tempPassword.trim()); 45 update($vip); 46 47end 48 49 50rule "没有输入用户名" 51 salience 90 52 when 53 $vip:Vip((name==null||name=="")&& 54 (password!=null&&password!=""),$password:password ) 55 then 56 String tempName=""; 57 Scanner scanner = new Scanner(System.in); 58 System.out.print("请输入用户名: "); 59 tempName=scanner.nextLine(); 60 $vip.setName(tempName.trim()); 61 update($vip); 62 63end 64 65 66rule "输入正确的用户名和密码" 67 salience 80 68 when 69 $vip:Vip((name!=null&&name!=""), 70 (password!=null&&password!=""),LoginServiceImpl.checkDB(name,password) ) 71 then 72 System.out.print(" 欢迎 !!!"+$vip.getName()); 73 74end 75 76rule "输入错误的用户名和密码" 77 salience 80 78 when 79 $vip:Vip((name!=null&&name!=""), 80 (password!=null&&password!=""),!LoginServiceImpl.checkDB(name,password) ) 81 then 82 System.out.print(" 输入错误用户名或密码,请重新输入 !!!\n"); 83 $vip.setName(""); 84 $vip.setPassword(""); 85 update($vip); 86end
