其他分享
首页 > 其他分享> > 规则引擎学习-drools

规则引擎学习-drools

作者:互联网

规则引擎drools-day01

官网案例

  1. 创建maven项目
<dependency>
    <groupId>org.drools</groupId>
    <artifactId>drools-compiler</artifactId>
    <version>6.3.0.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.12</version>
    <scope>provided</scope>
</dependency>

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.10</version>
</dependency>
  1. 在resources目录下创建目录META-INF
  2. 在META-INF目录下创建规则引擎默认配置文件kmodule.xml
<?xml version="1.0" encoding="UTF-8"?>
<kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule"/>
  1. 至此初始化完成

是否年满18岁

  1. 创建实例
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Applicant {
    private String name;
    private int age;
    private boolean valid = true;

    public Applicant(String name, int age) {
        this.name = name;
        this.age = age;
    }
}
  1. 创建规则文件(放在类路径下,比如resources/rules/rules.drl)
//类似名称空间的作用,目前不用考虑
package rules;
dialect  "mvel"
//此处引入对应实例
import com.drools.test.entity.*

rule "Is of valid age"
    when
        $a:Applicant(age < 18);
    then
        $a.setValid(false);
end
  1. 写测试用例
KieServices kieServices = KieServices.Factory.get();
KieContainer kieContainer = kieServices.getKieClasspathContainer();
StatelessKieSession statelessKieSession = kieContainer.newStatelessKieSession();
Applicant applicant = new Applicant("Mr John Smith", 16);
assertTrue(applicant.isValid());
statelessKieSession.execute(applicant);
assertFalse(applicant.isValid());

火警案例

  1. 创建实例
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Room {
    private String name;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Fire {
    private Room room;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Sprinkler {
    private Room room;
    private boolean on;
    public Sprinkler(Room room) {
        this.room = room;
    }
}
public class Alarm {
}
  1. 创建规则文件
rule "When there is a fire turn on the sprinkle"
    salience 1
    when
        Fire($room : room)
        $sprinkler:Sprinkler($room == room, on == false);
    then
        modify($sprinkler){setOn(true)}
        System.out.println("room 【" + $room.getName() + "】 is firing!!!");
        System.out.println("Turn on the sprinkler from room 【" + $room.getName() + "】.");
end

rule "When the fire is gone trun off the srinkle"
    when
        $room:Room();
        $sprinkler:Sprinkler($room == room, on == true);
        not Fire($room  == room);
    then
        modify($sprinkler){setOn(false)}
        System.out.println("Room 【" + $room.getName() + "】 fire is gone.");
        System.out.println("Turn off the sprinkler from room 【" + $room.getName() + "】.");
end

rule "Raise the alarm when have one or more fires"
	//先报警,在开喷头
    salience 2
    when
        exists Fire();
    then
        insert(new Alarm())
        System.out.println("Raise the alarm.");
end

rule "Cancel the alarm when all the fires have gone"
    when
        not Fire();
        $alarm:Alarm()
    then
        delete($alarm);
        System.out.println("Cancel the alarm.");
end

rule "Status output when things are ok"
    when
        not Alarm();
        not Sprinkler(on == true);
    then
        System.out.println("Everything is ok.");
end
  1. 测试用例
@Test
public void test02(){
    KieServices kieServices = KieServices.Factory.get();
    KieContainer kieClasspathContainer = kieServices.getKieClasspathContainer();
    KieSession kieSession = kieClasspathContainer.newKieSession();
    String[] roomNames = new String[]{"kitchen", "bedroom", "office", "living-room"};
    List<Room> rooms = new ArrayList<>();
    for (int i = 0; i < 4; i++) {
        Room room = new Room(roomNames[i]);
        rooms.add(room);
        Sprinkler sprinkler = new Sprinkler(room);
        kieSession.insert(room);
        kieSession.insert(sprinkler);
    }
    //一切ok
    kieSession.fireAllRules();
    FactHandle bedroomHandle = kieSession.insert(new Fire(rooms.get(1)));
    //卧室发生火灾
    kieSession.fireAllRules();
    //扑灭卧室火灾
    kieSession.delete(bedroomHandle);
    kieSession.fireAllRules();
    kieSession.dispose();
}

标签:drools,room,kieSession,Room,when,引擎,规则,public,sprinkler
来源: https://blog.csdn.net/qq_36055114/article/details/122617053