Drools与Spring集成 登录测试

来源:互联网 发布:8月外贸数据 编辑:程序博客网 时间:2024/05/21 11:22

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


[html] view plain copy
 print?
  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <beans xmlns="http://www.springframework.org/schema/beans"    
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"    
  4.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"    
  5.     xmlns:p="http://www.springframework.org/schema/p"    
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans       
  7.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd       
  8.         http://www.springframework.org/schema/context       
  9.          http://www.springframework.org/schema/context/spring-context-3.0.xsd       
  10.         http://www.springframework.org/schema/tx       
  11.         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd       
  12.         http://www.springframework.org/schema/aop        
  13.          http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">     
  14.      <import resource="classpath:com/jsptpd/rjy/zyj/drools/beans-drools.xml"/>    
  15. </beans>    


 

beans-drools.xml

[html] view plain copy
 print?
  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <beans xmlns="http://www.springframework.org/schema/beans"    
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  4.        xmlns:drools="http://drools.org/schema/drools-spring"     
  5.        xmlns:camel="http://camel.apache.org/schema/spring"    
  6.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd    
  7.                            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    
  8.                            http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">    
  9.     
  10.   <drools:kbase id="kbase1">    
  11.      <drools:resources>    
  12.           <!--不是<drools:resource type="DRL" source="classpath:com/jsptpd/rjy/zyj/service/Login.drl"/> -->    
  13.          <drools:resource type="DRL" source="classpath:Login.drl"/>    
  14.      </drools:resources>    
  15.   </drools:kbase>    
  16.     
  17.   <drools:ksession id="ksession1" type="stateful" kbase="kbase1"/>    
  18.     
  19.    <bean id="vip" class="com.jsptpd.rjy.zyj.pojo.Vip" />    
  20.    <bean id="loginService" class="com.jsptpd.rjy.zyj.service.LoginServiceImpl" >    
  21.         <property name="vip" ref="vip" />    
  22.    </bean>    
  23. </beans>    


 

LoginTest.Java

[java] view plain copy
 print?
  1. package com.jsptpd.rjy.zyj.junit;    
  2.     
  3. import org.drools.runtime.StatefulKnowledgeSession;    
  4. import org.junit.Test;    
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;    
  6.     
  7. import com.jsptpd.rjy.zyj.service.LoginServiceImpl;    
  8.     
  9. public class LoginTest {    
  10.     @Test    
  11.     public void testLogin(){    
  12.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "beans.xml" );    
  13.         LoginServiceImpl loginServiceImpl= (LoginServiceImpl) context.getBean( "loginService" );    
  14.         StatefulKnowledgeSession kstateless = (StatefulKnowledgeSession) context.getBean( "ksession1" );    
  15.         loginServiceImpl.checkLogin(kstateless);    
  16.         System.out.println("aa");    
  17.     }    
  18. }    



 

LoginServiceImpl.java

[java] view plain copy
 print?
  1. package com.jsptpd.rjy.zyj.service;    
  2.     
  3. import org.drools.runtime.StatefulKnowledgeSession;    
  4. import org.drools.runtime.StatelessKnowledgeSession;    
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;    
  6.     
  7. import com.jsptpd.rjy.zyj.pojo.Vip;    
  8.     
  9. public class LoginServiceImpl {    
  10.         private Vip vip;    
  11.     
  12.         public Vip getVip() {    
  13.             return vip;    
  14.         }    
  15.     
  16.         public void setVip(Vip vip) {    
  17.             this.vip = vip;    
  18.         }    
  19.                
  20.         public void checkLogin(StatefulKnowledgeSession kstateless ){    
  21.             System.out.println("s");    
  22.             kstateless.insert(vip);    
  23.             kstateless.fireAllRules();    
  24.             kstateless.dispose();    
  25.             System.out.println("e");    
  26.         }    
  27.              
  28.         public static boolean checkDB(String name,String password){    
  29.             //实际可以到数据库匹配    
  30.             return name.trim().equals("jack")&&password.trim().equals("123");    
  31.         }    
  32.             
  33. }    

 

Login.drl


[java] view plain copy
 print?
  1. #created on: 2011-10-24    
  2. package com.jsptpd.rjy.zyj.service    
  3.     
  4. #list any import classes here.    
  5. import com.jsptpd.rjy.zyj.pojo.Vip;    
  6. import java.io.Console;    
  7. import java.util.Scanner;    
  8. import com.jsptpd.rjy.zyj.service.LoginServiceImpl    
  9.     
  10. #declare any global variables here    
  11.     
  12.     
  13.     
  14.     
  15. rule "vip初次登录"    
  16.     salience 100    
  17.     when    
  18.         $vip:Vip((name==null||name=="")&&    
  19.                  (password==null||password=="") )    
  20.     then    
  21.         String tempName;    
  22.         String tempPassword;    
  23.         Console console=System.console();    
  24.         Scanner scanner = new Scanner(System.in);    
  25.         System.out.print("请输入用户名: ");       
  26.         tempName=(console!=null?console.readLine():scanner.nextLine());    
  27.         System.out.print("请输入密码: ");    
  28.         tempPassword=(console!=null?new String(console.readPassword()):scanner.nextLine());    
  29.         $vip.setName(tempName.trim());    
  30.         $vip.setPassword(tempPassword.trim());    
  31.         update($vip);    
  32. end    
  33.     
  34. rule "没有输入密码"    
  35.     salience  90    
  36.     when    
  37.        $vip:Vip((name!=null&&name!="")&&    
  38.                  (password==null||password==""),$name:name)    
  39.     then    
  40.         String tempPassword="";    
  41.         Console console=System.console();    
  42.         Scanner scanner = new Scanner(System.in);    
  43.         System.out.print("请输入密码: ");    
  44.         tempPassword=(console!=null?new String(console.readPassword()):scanner.nextLine());    
  45.         $vip.setPassword(tempPassword.trim());    
  46.         update($vip);    
  47.     
  48. end    
  49.     
  50.     
  51. rule "没有输入用户名"    
  52.     salience  90    
  53.     when    
  54.        $vip:Vip((name==null||name=="")&&    
  55.                  (password!=null&&password!=""),$password:password )    
  56.     then    
  57.         String tempName="";    
  58.         Scanner scanner = new Scanner(System.in);    
  59.         System.out.print("请输入用户名: ");       
  60.         tempName=scanner.nextLine();    
  61.         $vip.setName(tempName.trim());    
  62.         update($vip);    
  63.     
  64. end    
  65.     
  66.     
  67. rule "输入正确的用户名和密码"    
  68.     salience  80    
  69.     when    
  70.        $vip:Vip((name!=null&&name!=""),    
  71.                  (password!=null&&password!=""),LoginServiceImpl.checkDB(name,password) )    
  72.     then    
  73.         System.out.print(" 欢迎 !!!"+$vip.getName());     
  74.     
  75. end    
  76.     
  77. rule "输入错误的用户名和密码"    
  78.     salience  80    
  79.     when    
  80.        $vip:Vip((name!=null&&name!=""),    
  81.                  (password!=null&&password!=""),!LoginServiceImpl.checkDB(name,password) )    
  82.     then    
  83.         System.out.print(" 输入错误用户名或密码,请重新输入 !!!\n");        
  84.         $vip.setName("");    
  85.         $vip.setPassword("");    
  86.         update($vip);    
  87. end    

0 0