drools規則引擎與kie-wb和kie-server遠程執行規則(7.18.0.Final)

 

 

最近研究了一下規則引擎drools。java

這篇博客帶你搭建並運行一個可在線編輯,在線打包,遠程執行的規則引擎(drools)docker

本篇博客同時參考http://www.javashuo.com/article/p-vzykyijj-s.html 博客內容api

一、搭建Workbench (首先保證主機聯網,有docker環境)jsp

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

  容器啓動完以後就能夠訪問以下連接(可能docker鏡像下載有點慢。能夠切換阿里雲鏡像倉庫)ide

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

界面以下:ui

  用戶名:admin  密碼:adminthis

  登陸進去以後阿里雲

  點擊建立工程

 

 

 

 

   點擊建立Add Asset

 

 

 

  建立完對象點擊保存

 

  保存以後回到工程界面

  繼續建立rule,嚮導性規則

 

 

 

 

package com.myspace.mytest1;

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

 

建立測試場景

 

 編輯以下測試規則

而後返回點擊build

而後點擊

二、建立 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的對應關係

 

相關文章
相關標籤/搜索