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"?>
<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. <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"?>
<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

时间: 2024-08-25 17:35:44

spring和Drools规则引擎的使用的相关文章

Spring Boot+Drools规则引擎整合

目的 官方的Drools范例大都是基于纯Java项目或Maven项目,而基于Spring Boot项目的很少. 本文介绍如何在Spring Boot项目上加上Drools规则引擎. POM依赖 POM文件如下: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=&q

drools规则引擎因为内存泄露导致的内存溢出

进入这个问题之前,先了解一下drools: 在很多行业应用中比如银行.保险领域,业务规则往往非常复杂,并且规则处于不断更新变化中,而现有很多系统做法基本上都是将业务规则绑定在程序代码中. 主要存在的问题有以下几个方面: 1) 当业务规则变更时,对应的代码也得跟着更改,每次即使是小的变更都需要经历开发.测试验证上线等过程,变更成本比较大. 2) 长时间系统变得越来越难以维护. 3) 开发团队一般是由一个熟悉业务的BA(业务分析人员)和若干个熟悉技术的开发人员组成,开发人员对业务规则的把握能力远不及

Drools规则引擎初窥---drl和决策表实现[实例]

因项目需要,看了下drools规则引擎.写了一个比较简单的drools的drl规则和决策表实现的例子. 规则说明: 网络商城要举办活动(奖励额外积分), 订单原价金额在 100以下, 不加分 100-500 加100分 500-1000 加500分 1000 以上 加1000分 1.建立最基本的Drools项目结构并引入必须的类库.(这里采用junit来执行单元测试).创建一个Java Project,建立maven形式的源码包. 2.定义实体类Order.java 1 import java.

drools规则引擎初探

1.drools是什么 Drools是为Java量身定制的基于Charles  Forgy的RETE算法的规则引擎的实现.具有了OO接口的RETE,使得商业规则有了更自然的表达. Rule是什么呢? 一条规则是对商业知识的编码.一条规则有 attributes ,一个 Left Hand Side ( LHS )和一个 Right Hand Side ( RHS ).Drools 允许下列几种 attributes : salience , agenda-group , no-loop , au

Drools 规则引擎环境搭建

一.关于 drools 规则引擎 前面写过一篇 Drools 规则引擎相关的文章,这篇文章主要记录一下规则引擎的环境搭建和简单示例.不熟悉 drools 的朋友可以看看这篇文章: 自己写个 Drools 文件语法检查工具--栈的应用之编译器检测语法错误 介绍的内容: Drools 规则引擎的使用场景 Drools 规则引擎的优点 Drools的基本工作工程(Fact对象.Drl文件内容.Drools的基础语法) drools 文件的形式 Drools 文件语法初步检查 二 .Drools 的环境

Drools规则引擎-如果判断某个对象中的集合是否包含指定的值

规则引擎集合相关处理 在实际生产过程中,有很多关于集合的处理场景,比如一个Fact对象中包含有一个集合,而需要判断该集合是否包含某个值.而Drools规则引擎也提供了多种处理方式,比如通过from.contains.exists等进行操作,比较. 当然也可以通过function函数来做相应的比较,在个在其他章节讲到过,就不在此赘述.下面重点以几个实例才进行讲解,在具体实践中根据具体情况来进行运用. 实例 省略掉基本的配置,直接看调用代码和规则代码. 测试调用代码: public class Co

SpringBoot2 整合 Drools规则引擎,实现高效的业务规则

本文源码:GitHub·点这里 || GitEE·点这里 一.Drools引擎简介 1.基础简介 Drools是一个基于java的规则引擎,开源的,可以将复杂多变的规则从硬编码中解放出来,以规则脚本的形式存放在文件中,使得规则的变更不需要修正代码重启机器就可以立即在线上环境生效.具有易于访问企业策略.易于调整以及易于管理的特点,作为开源业务规则引擎,符合业内标准,速度快.效率高. 2.规则语法 (1).演示drl文件格式 package droolRule ; import org.slf4j.

Drools 规则引擎 BRMS

安装说明 | Drools中文网 http://www.drools.org.cn/category/install Drools规则引擎-在微服务中的应用 - 简书 https://www.jianshu.com/p/887ff9021eb3 Drools规则引擎-在微服务中的应用 - 简书 https://www.jianshu.com/p/887ff9021eb3 微服务组件-规则引擎 - Fx_demon - OSCHINA https://my.oschina.net/fxdemon/

drools规则引擎中易混淆语法分析_循环触发导致死循环分析

整理了下最近在项目中使用drools出现的问题,幸好都在开发与测试阶段解决了,未波及到prod. 首先看这样两条规则 /** * 规则1_set默认利率 */ rule "rate_default" no-loop true when $request:AmountRateRequest(calculateEnum == CalculateEnum.INTEREST || calculateEnum == CalculateEnum.AMOUNT_INTEREST) $response