zoukankan      html  css  js  c++  java
  • drools规则引擎与kie-wb和kie-server远程执行规则(7.18.0.Final)

    最近研究了一下规则引擎drools。

    这篇博客带你搭建并运行一个可在线编辑,在线打包,远程执行的规则引擎(drools)

    本篇博客同时参考https://blog.csdn.net/chinrui/article/details/79018351 博客内容

    1、搭建Workbench (首先保证主机联网,有docker环境)

      docker run -p 8080:8080 -p 8001:8001 -d --name drools-wb jboss/drools-workbench-showcase:latest

      容器启动完之后就可以访问如下链接(可能docker镜像下载有点慢。可以切换阿里云镜像仓库)

      http://IP:8080/business-central/kie-wb.jsp?locale=zh_CN

    界面如下:

      用户名:admin  密码:admin

      登录进去之后

      点击创建工程

     

     

       点击创建Add Asset

     

      创建完对象点击保存

      保存之后回到工程界面

      继续创建rule,向导性规则

     

     

    package com.myspace.mytest1;
    
    rule "myrule1"
        dialect "java"
        when
            $info : mytestmodel1( sex == 0 )
        then
            $info.setName( "李雷" );
    end

    创建测试场景

     编辑如下测试规则

    然后返回点击build

    然后点击

    2、创建 kie-server

      docker run -p 8180:8080 -d --name kie-server --link drools-wb:kie_wb jboss/kie-server-showcase:latest

      运行如上docker容器

      

      容器运行起来之后

      访问如下链接http://IP:8180/kie-server/services/rest/server/

      

      在访问链接 http://IP:8180/kie-server/services/rest/server/containers

     

      这里发现已经有了我们刚才创建的kie-server服务列表,这是我们需要创建一个运行容器,返回我们的mytest1rule

     

     

      发布成功之后

      然后在访问链接http://IP:8180/kie-server/services/rest/server/containers

    接下来上代码开发我们的测试程序

    maven项目引用依赖

    <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
            <drools.version>7.18.0.Final</drools.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.drools</groupId>
                <artifactId>drools-core</artifactId>
                <version>${drools.version}</version>
            </dependency>
            <dependency>
                <groupId>org.drools</groupId>
                <artifactId>drools-compiler</artifactId>
                <version>${drools.version}</version>
            </dependency>
            <dependency>
                <groupId>org.drools</groupId>
                <artifactId>drools-decisiontables</artifactId>
                <version>${drools.version}</version>
            </dependency>
            <dependency>
                <groupId>org.drools</groupId>
                <artifactId>drools-templates</artifactId>
                <version>${drools.version}</version>
            </dependency>
            <dependency>
                <groupId>org.kie</groupId>
                <artifactId>kie-api</artifactId>
                <version>${drools.version}</version>
            </dependency>
            
            <dependency>
                <groupId>org.kie.server</groupId>
                <artifactId>kie-server-client</artifactId>
                <version>${drools.version}</version>
            </dependency>
    
        </dependencies>

    实体类

    package com.myspace.mytest1;
    
    /**
     * This class was automatically generated by the data modeler tool.
     */
    
    public class mytestmodel1 implements java.io.Serializable {
    
        static final long serialVersionUID = 1L;
    
        private java.lang.Integer id;
        private java.lang.String name;
        private java.lang.Integer sex;
    
        public mytestmodel1() {
        }
    
        public java.lang.Integer getId() {
            return this.id;
        }
    
        public void setId(java.lang.Integer id) {
            this.id = id;
        }
    
        public java.lang.String getName() {
            return this.name;
        }
    
        public void setName(java.lang.String name) {
            this.name = name;
        }
    
        public java.lang.Integer getSex() {
            return this.sex;
        }
    
        public void setSex(java.lang.Integer sex) {
            this.sex = sex;
        }
    
        public mytestmodel1(java.lang.Integer id, java.lang.String name,
                java.lang.Integer sex) {
            this.id = id;
            this.name = name;
            this.sex = sex;
        }
    
        @Override
        public String toString() {
            return "mytestmodel1 [id=" + id + ", name=" + name + ", sex=" + sex + "]";
        }
    
    }

    控制台执行代码

    package com.myspace.mytest1;
    
    
    import java.io.IOException;
    import java.util.LinkedList;
    import java.util.List;
    
    import org.kie.api.KieServices;
    import org.kie.api.command.Command;
    import org.kie.api.command.KieCommands;
    import org.kie.api.runtime.ExecutionResults;
    import org.kie.server.api.marshalling.MarshallingFormat;
    import org.kie.server.api.model.ServiceResponse;
    import org.kie.server.client.KieServicesClient;
    import org.kie.server.client.KieServicesConfiguration;
    import org.kie.server.client.KieServicesFactory;
    import org.kie.server.client.RuleServicesClient;
    
    
    public class droolstest {
    
        public static final String SERVER_URL = "http://ip:8180/kie-server/services/rest/server";
        public static final String PASSWORD = "kieserver1!";
        public static final String USERNAME = "kieserver";
        public static final String KIE_CONTAINER_ID = "mytest1_1.0.0-SNAPSHOT";
        public static final String KIE_SESSION_ID = "mytest1";
    
        public static void main(String[] args) throws IOException {
    
            
            // KisService 配置信息设置
            KieServicesConfiguration kieServicesConfiguration =
                    KieServicesFactory.newRestConfiguration(SERVER_URL, USERNAME, PASSWORD, 10000L);
            kieServicesConfiguration.setMarshallingFormat(MarshallingFormat.JSON);
    
            // 创建规则服务客户端
            KieServicesClient kieServicesClient = KieServicesFactory.newKieServicesClient(kieServicesConfiguration);
            RuleServicesClient ruleServicesClient = kieServicesClient.getServicesClient(RuleServicesClient.class);
    
            // 规则输入条件
            mytestmodel1 mytest = new mytestmodel1();
            mytest.setSex(1);
            
            // 命令定义,包含插入数据,执行规则
            KieCommands kieCommands = KieServices.Factory.get().getCommands();
            List<Command<?>> commands = new LinkedList<>();
            commands.add(kieCommands.newInsert(mytest, "mytestmodel1"));
            commands.add(kieCommands.newFireAllRules());
            ServiceResponse<ExecutionResults> results = ruleServicesClient.executeCommandsWithResults(KIE_CONTAINER_ID,
                    kieCommands.newBatchExecution(commands, KIE_SESSION_ID));
    
            // 返回值读取
            mytestmodel1 value = (mytestmodel1) results.getResult().getValue("mytestmodel1");
            System.out.println(value.toString());
        }
        
    
        
    }

    执行结果

    当sex为0时有如下输出

     当sex等于1的时候

     这里就完成了整个流程的跑通

    其中注意链接的 KIE_CONTAINER_ID和KIE_SESSION_ID的对应关系

     

  • 相关阅读:
    java unicode转中文
    java常用
    Intellij IDEA常用快捷键——Mac版
    mac 快捷键
    thrift 学习
    ubuntu上的翻译软件,看论文神器
    linux中jupyter notebook中切换虚拟环境
    02_opencv_python_图像处理进阶
    01_opencv_python_基本图像处理
    python刷剑指offer(21-40)(一刷)
  • 原文地址:https://www.cnblogs.com/zhyg/p/10711055.html
Copyright © 2011-2022 走看看