spring和Drools规则引擎的使用

2014/12/04 17:39
阅读数 6.2K

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代码 复制代码  收藏代码
  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>  
<?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代码 复制代码  收藏代码
  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>  
<?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

Java代码 复制代码  收藏代码
  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. }  
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

Java代码 复制代码  收藏代码
  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. }  
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

Java代码 复制代码  收藏代码
  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  
#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
14 收藏
分享
加载中
更多评论
打赏
0 评论
14 收藏
0
分享
返回顶部
顶部