1. 程式人生 > 其它 >drools 規則引擎 java菜鳥demo版

drools 規則引擎 java菜鳥demo版

技術標籤:drools

話不多說,直接上程式碼

專案目錄結構 (沒錯,只有一個類和一個規則字串檔案,為了方便看規則,這裡將規則單獨提成檔案)

pom依賴

    <dependencies>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-core</artifactId>
            <version>6.5.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-compiler</artifactId>
            <version>6.5.0.Final</version>
        </dependency>
    </dependencies>

DroolsDemo:

package com;

import org.apache.commons.io.FileUtils;
import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.KieRepository;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import java.io.File;

/**
 * 規則引擎程式碼
 * 用於對得分進行級別判斷
 * 低於60        不合格
 * [60,80)      有待提升
 * [80,90)      一般
 * [90,95)      良好
 * [90,100)     優秀
 * [100]        爆表
 */
public class DroolsDemo {

    /**
     * 函式入口
     * @param args
     * @throws Exception
     */
    public static final void main(String[] args) throws Exception {
        KieServices ks = KieServices.Factory.get();
        KieRepository kr = ks.getRepository();
        KieFileSystem kfs = ks.newKieFileSystem();
        File file = new File("./tmp.drl");//本地除錯請使用絕對路徑
        String drl = FileUtils.readFileToString(file, "UTF-8");//讀取drools規則語法內容字串
        kfs.write("src/main/resources/" + drl.hashCode() + ".drl", drl);//路徑必須為src/main/resources,不要修改,否則規則不生效,這是虛擬目錄,線上環境不存在這個目錄不會影響結果
        KieBuilder kb = ks.newKieBuilder(kfs);
        kb.buildAll();
        KieContainer kContainer = ks.newKieContainer(kr.getDefaultReleaseId());
        KieSession kieSession = kContainer.newKieSession();
        Unit unit = new Unit(80, null);//初始化實體類,分數80分,級別型別由規則計算
        System.out.println(unit);//列印原來的內容
        kieSession.insert(unit);//傳入實體類用於規則計算
        kieSession.fireAllRules();//執行規則,並更新引數,更新引數在規則檔案中已經寫了,詳情請看tmp.drl檔案
        kieSession.dispose();//清理KieSession的維護狀態
        System.out.println(unit);
    }

    /**
     * 內部實體類
     */
    public static class Unit {
        private int score;//分數
        private String type;//分數對應的級別
        public Unit() {}
        public Unit(int score, String type) {this.score=score;this.type=type; }
        public int getScore() {return score;}
        public String getType() {return type;}
        public void setType(String type) {this.type = type;}
        public void setScore(int score) {this.score = score;}
        @Override
        public String toString() { return "分數: " + score + ",級別:" + type; }
    }
}

tmp.drl: 規則檔案

package rules //隨意寫

import com.DroolsDemo.Unit //匯入實體類用於規則計算賦值

rule "不合格"
    no-loop true
    lock-on-active true
    salience 1
    when
        $s : Unit(score < 60)
    then
        $s.setType("不合格");
        update($s);
end
rule "有待提升"
    no-loop true
    lock-on-active true
    salience 1
    when
        $s : Unit(60 <= score && score < 80)
    then
        $s.setType("有待提升");
        update($s);
end
rule "一般"
    no-loop true
    lock-on-active true
    salience 1
    when
        $s : Unit(80 <= score && score < 90)
    then
        $s.setType("良好");
        update($s);
end
rule "良好"
    no-loop true
    lock-on-active true
    salience 1
    when
        $s : Unit(90 <= score && score < 95)
    then
        $s.setType("良好");
        update($s);
end
rule "優秀"
    no-loop true
    lock-on-active true
    salience 1
    when
        $s : Unit(95 <= score && score < 100)
    then
        $s.setType("優秀");
        update($s);
end

rule "爆表!"
    no-loop true
    lock-on-active true
    salience 1
    when
        $s : Unit(100 <= score)
    then
        $s.setType("爆表");
        update($s);
end

執行結果: 80分,規則計算為良好