Drools与Spring集成 登录测试

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>  


 

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:resources>  
          <!--不是<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>  


 

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 {  
    @Test  
    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");  
    }  
}  



 

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");  
        }  
          
}  

 

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  



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值