切记!!! 必须使用jdk1.8 

 

 

工程目录

 

引入依赖包,pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.xiaojf</groupId>
    <artifactId>drools7-demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <drools.version>7.5.0.Final</drools.version>
        <commons-lang3.version>3.7</commons-lang3.version>
        <junit.version>4.12</junit.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>

        <!-- drools -->
        <dependency>
            <groupId>org.kie</groupId>
            <artifactId>kie-api</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-core</artifactId>
            <version>${drools.version}</version>
        </dependency>

        <!-- tools -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>${commons-lang3.version}</version>
        </dependency>


    </dependencies>

</project>

 

META-INF/kmodule.xml , drools 最新api最大的变化之一

<?xml version="1.0" encoding="utf-8" ?>
<kmodule xmlns="http://www.drools.org/xsd/kmodule">
    <kbase name="rules">
        <ksession name="all-rules"></ksession>
    </kbase>
</kmodule>

 

定义规则文件 discount.drl

package cn.xiaojf.rules

import cn.xiaojf.drools7.model.Car;
rule "car-discount>30"
when
    $car : Car(person.age > 30)
    then
        $car.setDiscount(80);
    end

rule "car-discount<=30"
when
    $car : Car(person.age <= 30)
    then
        $car.setDiscount(90);
    end

 

两个业务的实体类

package cn.xiaojf.drools7.model;

public class Person {
    private String name;
    private Integer age;

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}
package cn.xiaojf.drools7.model;

public class Car {
    private int discount = 100;
    private Person person;

    public int getDiscount() {
        return discount;
    }

    public void setDiscount(int discount) {
        this.discount = discount;
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }
}

 

最后是测试类,新的api大量简化了api的使用,主要看注释

package day20180117;

import cn.xiaojf.drools7.model.Car;
import cn.xiaojf.drools7.model.Person;
import org.junit.Test;
import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;

public class Demo {
    @Test
    public void test() {
        KieServices kieServices = KieServices.Factory.get();
        //默认自动加载 META-INF/kmodule.xml
        KieContainer kieContainer = kieServices.getKieClasspathContainer();
        //kmodule.xml 中定义的 ksession name
        KieSession kieSession = kieContainer.newKieSession("all-rules");

        Person p1 = new Person();
        p1.setAge(20);
        Car car = new Car();
        car.setPerson(p1);

        kieSession.insert(car);

        int count = kieSession.fireAllRules();

        System.out.println(count);
        System.out.println(car.getDiscount());

        kieSession.dispose();
    }
}

 

结果

1
90

 

posted on 2018-01-18 00:39  肖建锋  阅读(5952)  评论(0编辑  收藏  举报