1. 程式人生 > >drools 7.x 決策錶轉drl

drools 7.x 決策錶轉drl

1. 決策表內容

位置:/Users/lcc/IdeaProjects/drools_test/src/main/resources/com/drools/excel/table/test1/personAge.xlsx

內容 在這裡插入圖片描述

RuleSet	com.drools.excel.table.test1				
Import	entity.PersonForExcel				
Functions	" function void printName(String name,String desc) {
            System.out.println(""name:""+name+"" desc:""+ desc);
        }"				
					
	RuleTable   ageTest				
	PRIORITY	CONDITION	CONDITION	ACTION	ACTION
		person:PersonForExcel			
		age>$1	age<=$1	"   person.setDesc($1);
retract($2);"	printName($1,$2);
LHS	salience	age			
boy	1		12	"少年",person	person.getName(),person.getDesc()
youth	2	12	24	"青年",person	person.getName(),person.getDesc()
midlife	3	24	65	"中年",person	person.getName(),person.getDesc()
old	4	65		"老年",person	person.getName(),person.getDesc()

2.maven重點

 <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <drools.version>7.11.0.Final</drools.version>
        <lombok.version>1.18.0</lombok.version>
    </properties>
    <dependencies>

        <
dependency
>
<groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>org.drools</groupId> <artifactId>
drools-compiler</artifactId> <version>${drools.version}</version> </dependency> <!-- https://mvnrepository.com/artifact/org.drools/drools-core --> <dependency> <groupId>org.drools</groupId> <artifactId>drools-core</artifactId> <version>${drools.version}</version> </dependency> 這個一定要加上,這個是決策表需要的 <dependency> <groupId>org.drools</groupId> <artifactId>drools-decisiontables</artifactId> <version>${drools.version}</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.kie</groupId> <artifactId>kie-maven-plugin</artifactId> <version>${drools.version}</version> <extensions>true</extensions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build>

3.事實

package entity;

/**
 * Created by lcc on 2018/10/9.
 */
public class PersonForExcel {
    private String name;
    private int age;
    private String desc;

    public PersonForExcel() {
       super();
    }

    public PersonForExcel(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public String toString() {
        return "[name=" + name + ",age=" + age + ",desc=" + desc + "]";
    }
}

4.配置

<?xml version="1.0" encoding="UTF-8" ?>
<kmodule xmlns="http://www.drools.org/xsd/kmodule">
    <!--excel 決策表使用 -->
    <kbase name="exceltest" packages="com.drools.excel.table.test1">
        <ksession name="exceltest"/>
    </kbase>
</kmodule>

5.測試

/**
     * 測試點:決策表 先翻譯成drl 後執行
     */
    @Test
    public  void excel1() {
//        //把excel翻譯成drl檔案
        SpreadsheetCompiler compiler = new SpreadsheetCompiler();
        String drl = compiler.compile(ResourceFactory.newClassPathResource("com/drools/excel/table/test1/personAge.xlsx", "UTF-8"), "rule-table");
        System.out.println(drl);
        Long start = System.currentTimeMillis();        //執行決策表
        try {
            // load up the knowledge base
            //
              KieServices ks = KieServices.Factory.get();
              KieContainer kContainer = ks.getKieClasspathContainer();
              KieSession kSession = kContainer.newKieSession("exceltest");
              PersonForExcel person = new PersonForExcel();
              person.setName("Tony");
              kSession.insert(person);
              kSession.fireAllRules();
              kSession.dispose();
        } catch (Throwable t) {
            t.printStackTrace();
        }
        System.out.println("COST:"+String.valueOf(System.currentTimeMillis()-start));
    }

執行結果

package com.drools.excel.table.test1;
//generated from Decision Table
import entity.PersonForExcel;
 function void printName(String name,String desc) {
            System.out.println("name:"+name+" desc:"+ desc);
        }
// rule values at B10, header at B5
rule "ageTest rule-table_10"
	salience 1
	when
		person:PersonForExcel(age<=12)
	then
		person.setDesc("少年");
retract(person);
		printName(person.getName(),person.getDesc());
end

// rule values at B11, header at B5
rule "ageTest rule-table_11"
	salience 2
	when
		person:PersonForExcel(age>12, age<=24)
	then
		person.setDesc("青年");
retract(person);
		printName(person.getName(),person.getDesc());
end

// rule values at B12, header at B5
rule "ageTest rule-table_12"
	salience 3
	when
		person:PersonForExcel(age>24, age<=65)
	then
		person.setDesc("中年");
retract(person);
		printName(person.getName(),person.getDesc());
end

// rule values at B13, header at B5
rule "ageTest rule-table_13"
	salience 4
	when
		person:PersonForExcel(age>65)
	then
		person.setDesc("老年");
retract(person);
		printName(person.getName(),person.getDesc());
end


name:Tony desc:少年
COST:21999
Disconnected from the target VM, address: '127.0.0.1:50629', transport: 'socket'

可以看到使用SpreadsheetCompiler可以將決策錶轉化成drl