规则引擎drools Part5

规则引擎drools Part5

Drools Workbench

  1. Drools Workbench是可视化的规则编辑器,用来授权和管理业务规则。workbench的war包下载地址,安装到tomcat中就可以运行了。使用workbench可以在浏览器中创建数据对象、规则文件、测试场景并把规则部署到maven仓库方便其他应用使用。
  2. workbench部署步骤
  • 需要安装配置jdk、maven、tomcat

  • 把下载的kie-drools-wb-7.10.0.Final-tomcat8.war改名为kie-drools-wb.war

  • 在tomcat/bin目录下创建setenv.bat文件,添加配置

shell 复制代码
# 其中配置CATALINA_HOME为tomcat的安装目录
CATALINA_OPTS="-Xmx512M \
    -Djava.security.auth.login.config=$CATALINA_HOME/webapps/kie-drools-wb/WEB-INF/classes/login.config \
    -Dorg.jboss.logging.provider=jdk"
  • 修改tomcat/conf/tomcat-users.xml文件,添加用户kie
xml 复制代码
<?xml version='1.0' encoding='utf-8'?>
<tomcat-users xmlns="http://tomcat.apache.org/xml"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"
              version="1.0">
  <!--定义admin角色-->
  <role rolename="admin"/>
  <!--定义一个用户,用户名为kie,密码为kie,对应的角色为admin角色-->
  <user username="kie" password="kie" roles="admin"/>
</tomcat-users>
  • 在tomcat/conf/server.xml文件添加配置
xml 复制代码
<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <!-- <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" /> -->
		<!-- 新增配置 -->
		<Value className="org.kie.integration.tomcat.JACCValve"/>
</Host>    
  • 下载以下Jar包并添加到tomcat/lib目录下
shell 复制代码
kie-tomcat-integration-7.10.0.Final.jar
jakarta.security.jacc-api-1.6.1.jar
slf4j-api-1.7.25.jar
  • 启动tomcat,访问workbench首页,使用kie/kie登录
shell 复制代码
http://localhost:8080/kie-drools-wb/kie-drools-wb.jsp
  1. Drools Workbench的使用

使用WorkBench时,首先创建空间或者使用默认的空间,然后在空间中创建项目,最后在项目中创建数据对象、规则文件、测试场景等。创建项目时会使用到maven下载依赖。

(1)创建数据对象,指定名称和软件包,添加字段后自动生成对应的java源码类Student.java

java 复制代码
package com.jzy.demo;

/**
 * This class was automatically generated by the data modeler tool.
 */

public class Student implements java.io.Serializable {

	static final long serialVersionUID = 1L;

	private Integer id;
	private java.lang.String name;
	private int age;
	private java.lang.String address;

	public Student() {
	}

	public java.lang.String getName() {
		return this.name;
	}

	public void setName(java.lang.String name) {
		this.name = name;
	}

	public int getAge() {
		return this.age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public java.lang.String getAddress() {
		return this.address;
	}

	public void setAddress(java.lang.String address) {
		this.address = address;
	}

	public java.lang.Integer getId() {
		return this.id;
	}

	public void setId(java.lang.Integer id) {
		this.id = id;
	}

	public Student(java.lang.Integer id, java.lang.String name, int age,
			java.lang.String address) {
		this.id = id;
		this.name = name;
		this.age = age;
		this.address = address;
	}

}

(2)创建规则文件student.drl

shell 复制代码
package com.jzy.demo;
import com.jzy.demo.Student

rule "rule_1"
    when
        com.jzy.demo.Student(age < 20 && name != null)
    then
        System.out.println("rule1 activated.");    
end

(3)在项目视图点击设置,添加Kie bases

(4)在项目视图点击build构建,使用deploy发布后在本地maven库生成一个jar包

(5)在maven项目中使用workbench生成的包

  • 创建一个Student类,与workbench中创建的包名类名完全相同,属性也要对应
  • 通过远程加载maven仓库中的jar包完成规则调用
java 复制代码
public static void main(String[] args) throws Exception{
    //通过此URL可以访问到maven仓库中的jar包
    //URL地址构成:http://ip地址:Tomcat端口号/WorkBench工程名/maven2/坐标/版本号/xxx.jar
    String url = "http://localhost:8080/kie-drools-wb/maven2/com/jzy/demo/1.0.0/demo-1.0.0.jar";    
    KieServices kieServices = KieServices.Factory.get();
    //通过Resource资源对象加载jar包
    UrlResource resource = (UrlResource) kieServices.getResources().newUrlResource(url);
    //通过Workbench提供的服务来访问maven仓库中的jar包资源,需要先进行Workbench的认证
    resource.setUsername("kie");
    resource.setPassword("kie");
    resource.setBasicAuthentication("enabled");    
    //将资源转换为输入流,通过此输入流可以读取jar包数据
    InputStream inputStream = resource.getInputStream(); 
    //创建仓库对象,仓库对象中保存Drools的规则信息
    KieRepository repository = kieServices.getRepository();
    //通过输入流读取maven仓库中的jar包数据,包装成KieModule模块添加到仓库中
    KieModule kieModule = repository.addKieModule(
        kieServices.getResources().newInputStreamResource(inputStream));    
    //基于KieModule模块创建容器对象,从容器中可以获取session会话
    KieContainer kieContainer = kieServices
        .newKieContainer(kieModule.getReleaseId());
    KieSession session = kieContainer.newKieSession();
    Student stu = new Student();
    stu.setAge(12);
    session.insert(stu);
    session.fireAllRules();
    session.dispose();
}
相关推荐
ylfhpy2 分钟前
Java面试黄金宝典22
java·开发语言·算法·面试·职场和发展
风象南35 分钟前
Spring Boot 实现文件秒传功能
java·spring boot·后端
橘猫云计算机设计36 分钟前
基于django优秀少儿图书推荐网(源码+lw+部署文档+讲解),源码可白嫖!
java·spring boot·后端·python·小程序·django·毕业设计
黑猫Teng39 分钟前
Spring Boot拦截器(Interceptor)与过滤器(Filter)深度解析:区别、实现与实战指南
java·spring boot·后端
星河浪人1 小时前
Spring Boot启动流程及源码实现深度解析
java·spring boot·后端
佩奇的技术笔记1 小时前
中级:Maven面试题精讲
java·面试·maven
Lizhihao_1 小时前
JAVA-堆 和 堆排序
java·开发语言
极客先躯1 小时前
高级java每日一道面试题-2025年3月21日-微服务篇[Nacos篇]-什么是Nacos?
java·开发语言·微服务
工业互联网专业1 小时前
基于springboot+vue的动漫交流与推荐平台
java·vue.js·spring boot·毕业设计·源码·课程设计·动漫交流与推荐平台
雷渊1 小时前
深入分析Spring的事务隔离级别及实现原理
java·后端·面试