规则引擎学习-drools

规则引擎学习-drools,第1张

规则引擎学习-drools

规则引擎drools-day01

官网案例

是否年满18岁火警案例

官网案例
    创建maven项目

    org.drools
    drools-compiler
    6.3.0.Final



    org.projectlombok
    lombok
    1.18.12
    provided



    junit
    junit
    4.10

    在resources目录下创建目录meta-INF在meta-INF目录下创建规则引擎默认配置文件kmodule.xml


    至此初始化完成
是否年满18岁
    创建实例
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;
    }
}
    创建规则文件(放在类路径下,比如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
    写测试用例
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());
火警案例
    创建实例
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 {
}
    创建规则文件
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
    测试用例
@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 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();
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5710226.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存