drools7 (一、最简单的例子)

drools7 (一、最简单的例子)

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

drools7 (一、最简单的例子)_第1张图片

 

 

工程目录

drools7 (一、最简单的例子)_第2张图片

 

引入依赖包,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.0modelVersion>

    <groupId>cn.xiaojfgroupId>
    <artifactId>drools7-demoartifactId>
    <version>1.0-SNAPSHOTversion>

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

    <dependencies>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>${junit.version}version>
            <scope>testscope>
        dependency>

        
        <dependency>
            <groupId>org.kiegroupId>
            <artifactId>kie-apiartifactId>
            <version>${drools.version}version>
        dependency>

        <dependency>
            <groupId>org.droolsgroupId>
            <artifactId>drools-compilerartifactId>
            <version>${drools.version}version>
        dependency>

        <dependency>
            <groupId>org.droolsgroupId>
            <artifactId>drools-coreartifactId>
            <version>${drools.version}version>
        dependency>

        
        <dependency>
            <groupId>org.apache.commonsgroupId>
            <artifactId>commons-lang3artifactId>
            <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 @ 2018-01-18 00:39 Xiao.jf 阅读( ...) 评论( ...) 编辑 收藏

你可能感兴趣的:(drools7 (一、最简单的例子))