drools7 简单helloworld示例

导入jar包

	<dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-core</artifactId>
            <version>${drools.version}</version>
        </dependency>
	<properties>
        <java.version>1.8</java.version>
        <drools.version>7.7.0.Final</drools.version>
    </properties>
        <!-- https://mvnrepository.com/artifact/org.drools/drools-compiler -->
        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-compiler</artifactId>
            <version>${drools.version}</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.drools/drools-templates -->
        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-templates</artifactId>
            <version>${drools.version}</version>
        </dependency>

注意事项: jdk必须在1.8以上

配置Kmodule.xml

<?xml version="1.0" encoding="UTF-8"?>
<kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule">

    <kbase name="rules">
        <ksession name="all-rules"/>
    </kbase>
</kmodule>

编写实体类

public class Person {
    private int age;

    public int getAge() {
        return age;
    }

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

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;
    }
}

编写规则

package com.rules

import com.yxdtyut.spring.security.drools.model.Car;

rule "person by car age than 60"

when
    $car : Car(person.age > 60)
then
    $car.setDiscount(80);
    System.out.println("age than 60:" + $car.getPerson().getAge());
 end

 rule "person by car age less 60"

 when
     $car : Car(person.age <= 60)
 then
     $car.setDiscount(90);
     System.out.println("age less 60:" + $car.getPerson().getAge());
  end

编写测试代码

public class PersonByCarDemo extends BaseKieSession  {
    public static void main(String[] args) {
        KieSession kieSession = getKieSession();

        Person p1 = new Person();
        p1.setAge(80);
        Car c1 = new Car();
        c1.setPerson(p1);

        Person p2 = new Person();
        p2.setAge(28);
        Car c2 = new Car();
        c2.setPerson(p2);

        kieSession.insert(c1);
        kieSession.insert(c2);

        int i = kieSession.fireAllRules();
		kieSession.dispose();
        System.out.println("fire " + i+ "com/rules");
        System.out.println("the discount of c1 is " + c1.getDiscount());
        System.out.println("the discount of c2 is " + c2.getDiscount());
    }
}

public class BaseKieSession {
    public static KieSession getKieSession(){
        KieServices kieServices = KieServices.get();
        KieContainer kieContainer = kieServices.getKieClasspathContainer();
        KieSession kieSession = kieContainer.newKieSession("all-rules");
        return kieSession;
    }

    public static KieSession getKieSession(String groupName){
        KieSession kieSession = getKieSession();
        kieSession.getAgenda().getAgendaGroup(groupName).setFocus();
        return kieSession;
    }
}

你可能感兴趣的:(drools7 简单helloworld示例)