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

  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. <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. <drools:ksession id="ksession1" type="stateful" kbase="kbase1"/>

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

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

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

  19. </bean>

  20. </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

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;
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

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;
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

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
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
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

swap空间的增减方法

(1)增大swap空间去激活swap交换区:swapoff v /dev/vg00/lvswap扩展交换lv:lvextend L 10G /dev/vg00/lvswap重新生成swap交换区:mkswap /dev/vg00/lvswap激活新生成的交换区:swapon v /dev/vg00/lvswap

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )