欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

drools规则引擎与kie-wb和kie-server远程执行规则(7.18.0.Final)

程序员文章站 2022-03-30 22:15:28
最近研究了一下规则引擎drools。 这篇博客带你搭建并运行一个可在线编辑,在线打包,远程执行的规则引擎(drools) 本篇博客同时参考https://blog.csdn.net/chinrui/article/details/79018351 博客内容 1、搭建Workbench (首先保证主机 ......

 

 

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

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

本篇博客同时参考 博客内容

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

界面如下:

drools规则引擎与kie-wb和kie-server远程执行规则(7.18.0.Final)

  用户名:admin  密码:admin

  登录进去之后

drools规则引擎与kie-wb和kie-server远程执行规则(7.18.0.Final)

  点击创建工程

drools规则引擎与kie-wb和kie-server远程执行规则(7.18.0.Final)

 

 drools规则引擎与kie-wb和kie-server远程执行规则(7.18.0.Final)

 drools规则引擎与kie-wb和kie-server远程执行规则(7.18.0.Final)

 

   点击创建add asset

drools规则引擎与kie-wb和kie-server远程执行规则(7.18.0.Final)

 

drools规则引擎与kie-wb和kie-server远程执行规则(7.18.0.Final)

drools规则引擎与kie-wb和kie-server远程执行规则(7.18.0.Final)

 

 drools规则引擎与kie-wb和kie-server远程执行规则(7.18.0.Final)

  创建完对象点击保存

 

drools规则引擎与kie-wb和kie-server远程执行规则(7.18.0.Final)

drools规则引擎与kie-wb和kie-server远程执行规则(7.18.0.Final)

  保存之后回到工程界面

  继续创建rule,向导性规则

drools规则引擎与kie-wb和kie-server远程执行规则(7.18.0.Final)

drools规则引擎与kie-wb和kie-server远程执行规则(7.18.0.Final)

 

 drools规则引擎与kie-wb和kie-server远程执行规则(7.18.0.Final)

 

 drools规则引擎与kie-wb和kie-server远程执行规则(7.18.0.Final)

package com.myspace.mytest1;

rule "myrule1"
    dialect "java"
    when
        $info : mytestmodel1( sex == 0 )
    then
        $info.setname( "李雷" );
end

 

创建测试场景

drools规则引擎与kie-wb和kie-server远程执行规则(7.18.0.Final)

 

 编辑如下测试规则

drools规则引擎与kie-wb和kie-server远程执行规则(7.18.0.Final)

然后返回点击build

drools规则引擎与kie-wb和kie-server远程执行规则(7.18.0.Final)

然后点击

drools规则引擎与kie-wb和kie-server远程执行规则(7.18.0.Final)

drools规则引擎与kie-wb和kie-server远程执行规则(7.18.0.Final)

2、创建 kie-server

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

  运行如上docker容器

  drools规则引擎与kie-wb和kie-server远程执行规则(7.18.0.Final)

  容器运行起来之后

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

  drools规则引擎与kie-wb和kie-server远程执行规则(7.18.0.Final)

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

drools规则引擎与kie-wb和kie-server远程执行规则(7.18.0.Final)

 

drools规则引擎与kie-wb和kie-server远程执行规则(7.18.0.Final)

 

 

 drools规则引擎与kie-wb和kie-server远程执行规则(7.18.0.Final)

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

 drools规则引擎与kie-wb和kie-server远程执行规则(7.18.0.Final)

drools规则引擎与kie-wb和kie-server远程执行规则(7.18.0.Final)

 

 drools规则引擎与kie-wb和kie-server远程执行规则(7.18.0.Final)

  发布成功之后

drools规则引擎与kie-wb和kie-server远程执行规则(7.18.0.Final)

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

drools规则引擎与kie-wb和kie-server远程执行规则(7.18.0.Final)

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

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时有如下输出

drools规则引擎与kie-wb和kie-server远程执行规则(7.18.0.Final)

 

 当sex等于1的时候

 

drools规则引擎与kie-wb和kie-server远程执行规则(7.18.0.Final)

 

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

其中注意链接的 kie_container_id和kie_session_id的对应关系

drools规则引擎与kie-wb和kie-server远程执行规则(7.18.0.Final)

 drools规则引擎与kie-wb和kie-server远程执行规则(7.18.0.Final)