spring和Drools规则引擎的使用

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. <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>
<?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. <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. <drools:kbase id="kbase1">

  9. drools:resources\

  10. <drools:resource type="DRL" source="classpath:Login.drl"/>

  11. </drools:resources>

  12. </drools:kbase>

  13. <drools:ksession id="ksession1" type="stateful" kbase="kbase1"/>

  14. <bean id="vip" class="com.jsptpd.rjy.zyj.pojo.Vip" />

  15. <bean id="loginService" class="com.jsptpd.rjy.zyj.service.LoginServiceImpl" >

  16. <property name="vip" ref="vip" />

  17. </bean>

  18. </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. import org.drools.runtime.StatefulKnowledgeSession;

  3. import org.junit.Test;

  4. import org.springframework.context.support.ClassPathXmlApplicationContext;

  5. import com.jsptpd.rjy.zyj.service.LoginServiceImpl;

  6. public class LoginTest {

  7. @Test

  8. public void testLogin(){

  9. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "beans.xml" );

  10. LoginServiceImpl loginServiceImpl= (LoginServiceImpl) context.getBean( "loginService" );

  11. StatefulKnowledgeSession kstateless = (StatefulKnowledgeSession) context.getBean( "ksession1" );

  12. loginServiceImpl.checkLogin(kstateless);

  13. System.out.println("aa");

  14. }

  15. }

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. import org.drools.runtime.StatefulKnowledgeSession;

  3. import org.drools.runtime.StatelessKnowledgeSession;

  4. import org.springframework.context.support.ClassPathXmlApplicationContext;

  5. import com.jsptpd.rjy.zyj.pojo.Vip;

  6. public class LoginServiceImpl {

  7. private Vip vip;

  8. public Vip getVip() {

  9. return vip;

  10. }

  11. public void setVip(Vip vip) {

  12. this.vip = vip;

  13. }

  14. public void checkLogin(StatefulKnowledgeSession kstateless ){

  15. System.out.println("s");

  16. kstateless.insert(vip);

  17. kstateless.fireAllRules();

  18. kstateless.dispose();

  19. System.out.println("e");

  20. }

  21. public static boolean checkDB(String name,String password){

  22. //实际可以到数据库匹配

  23. return name.trim().equals("jack")&&password.trim().equals("123");

  24. }

  25. }

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. #list any import classes here.

  4. import com.jsptpd.rjy.zyj.pojo.Vip;

  5. import java.io.Console;

  6. import java.util.Scanner;

  7. import com.jsptpd.rjy.zyj.service.LoginServiceImpl

  8. #declare any global variables here

  9. rule "vip初次登录"

  10. salience 100

  11. when

  12. $vip:Vip((name==null||name=="")&&

  13. (password==null||password=="") )

  14. then

  15. String tempName;

  16. String tempPassword;

  17. Console console=System.console();

  18. Scanner scanner = new Scanner(System.in);

  19. System.out.print("请输入用户名: ");

  20. tempName=(console!=null?console.readLine():scanner.nextLine());

  21. System.out.print("请输入密码: ");

  22. tempPassword=(console!=null?new String(console.readPassword()):scanner.nextLine());

  23. $vip.setName(tempName.trim());

  24. $vip.setPassword(tempPassword.trim());

  25. update($vip);

  26. end

  27. rule "没有输入密码"

  28. salience 90

  29. when

  30. $vip:Vip((name!=null&&name!="")&&

  31. (password==null||password==""),$name:name)

  32. then

  33. String tempPassword="";

  34. Console console=System.console();

  35. Scanner scanner = new Scanner(System.in);

  36. System.out.print("请输入密码: ");

  37. tempPassword=(console!=null?new String(console.readPassword()):scanner.nextLine());

  38. $vip.setPassword(tempPassword.trim());

  39. update($vip);

  40. end

  41. rule "没有输入用户名"

  42. salience 90

  43. when

  44. $vip:Vip((name==null||name=="")&&

  45. (password!=null&&password!=""),$password:password )

  46. then

  47. String tempName="";

  48. Scanner scanner = new Scanner(System.in);

  49. System.out.print("请输入用户名: ");

  50. tempName=scanner.nextLine();

  51. $vip.setName(tempName.trim());

  52. update($vip);

  53. end

  54. rule "输入正确的用户名和密码"

  55. salience 80

  56. when

  57. $vip:Vip((name!=null&&name!=""),

  58. (password!=null&&password!=""),LoginServiceImpl.checkDB(name,password) )

  59. then

  60. System.out.print(" 欢迎 !!!"+$vip.getName());

  61. end

  62. rule "输入错误的用户名和密码"

  63. salience 80

  64. when

  65. $vip:Vip((name!=null&&name!=""),

  66. (password!=null&&password!=""),!LoginServiceImpl.checkDB(name,password) )

  67. then

  68. System.out.print(" 输入错误用户名或密码,请重新输入 !!!\n");

  69. $vip.setName("");

  70. $vip.setPassword("");

  71. update($vip);

  72. 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

评论

暂无评论