简体   繁体   English

使用状态会话,Drools BRMS火灾和警报规则无法按预期工作

[英]Drools BRMS Fire and Alarm rule not working as expected using statefulsession

I am trying to execute the fire,sprinker and alarm rule from drools documentation . 我正在尝试执行Drools文档中的火灾,洒水和警报规则。

I am using drools 6.5 runtime and when I execute this example using statefull session (KIE Session) I can see the below output which differs with output mentioned in the documentation . 我正在使用drools 6.5运行时,当我使用statefull会话(KIE会话)执行此示例时,我可以看到以下输出,该输出与文档中提到的输出不同。

Below is the output I see when I Insert sprinkler and Fire in working memory output that I see is : 以下是我在工作内存输出中插入自动喷水灭火时看到的输出:

Everything is ok Raise the alarm 一切都很好发出警报

Actual output as per the documentation is : 根据文档的实际输出为:

Everything is ok Raise the alarm Turn on the sprinkler for room kitchen Turn on the sprinkler for room office 一切正常发出警报开启房间厨房的洒水装置开启房间办公室的洒水装置

Appreciate any help to figure out why I other two rules are not getting fired. 感谢任何帮助,以找出为什么我没有其他两个规则被解雇的原因。

Attaching the source code: 附加源代码:

package com.company.license

import com.sample.dto.*

rule "When there is a fire turn on the sprinkler"
when
    Fire($room : room)
    $sprinkler : Sprinkler( room == $room, on == false )
then
    modify( $sprinkler ) { setOn( true ) };
    System.out.println( "Turn on the sprinkler for room " + $room.getName() );
end

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

rule "Raise the alarm when we have one or more fires"
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
    retract( $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



package com.sample.dto;

public class Alarm {

}

package com.sample.dto;

public class Fire {
    private Room room;

    public Fire(Room room2) {
        // TODO Auto-generated constructor stub
    }

    public Room getRoom() {
        return room;
    }

    public void setRoom(Room room) {
        this.room = room;
    }
}

package com.sample.dto;

public class Room {
    private String name;


    public Room(String name){
        this.name = name;
    }

    public String getName() {
        return name;
    }

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

package com.sample.dto;

public class Sprinkler {
    private Room room;
    private boolean on;

    public Sprinkler(Room room) {
        // TODO Auto-generated constructor stub
        this.room = room;
    }
    public Room getRoom() {
        return room;
    }
    public void setRoom(Room room) {
        this.room = room;
    }
    public boolean isOn() {
        return on;
    }
    public void setOn(boolean on) {
        this.on = on;
    }
}

<?xml version="1.0" encoding="UTF-8"?>
<kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule">
    <kbase name="rules" packages="rules">
        <ksession name="ksession-rules"/>
        <ksession name="statelesssession" type="stateless" default="true"/>
    </kbase>
    <kbase name="dtables" packages="dtables">
        <ksession name="ksession-dtables"/>
    </kbase>
    <kbase name="process" packages="process">
        <ksession name="ksession-process"/>
    </kbase>
</kmodule>

public class FireAlarmStateful {

    public static final void main(String[] args) {
        try {
            // load up the knowledge base
            KieServices ks = KieServices.Factory.get();
            KieContainer kContainer = ks.getKieClasspathContainer();
            KieSession ksession = kContainer.newKieSession("ksession-rules");

            String[] names = new String[]{"kitchen", "bedroom", "office", "livingroom"};
            Map<String,Room> name2room = new HashMap<String,Room>();
            Room kitchen =  new Room("kitchen");
            Room office =  new Room("office");
            ksession.insert( kitchen );
            ksession.insert( office );
            Sprinkler sprinkler1 = new Sprinkler( kitchen);
            Sprinkler sprinkler2 = new Sprinkler( office);
            ksession.insert( sprinkler1 );
            ksession.insert( sprinkler2 );
            ksession.fireAllRules();

            Fire kitchenFire = new Fire( kitchen );
            Fire officeFire = new Fire( office );
            FactHandle kitchenFireHandle = ksession.insert( kitchenFire );
            FactHandle officeFireHandle = ksession.insert( officeFire );
            ksession.fireAllRules();

            ksession.dispose();

        } catch (Throwable t) {
            t.printStackTrace();
        }
    }
}


Output I get is : 
Everything is ok
Raise the alarm

Output mentioned in the Drools Documentation:
Everything is ok 
Raise the alarm 
Turn on the sprinkler for room kitchen 
Turn on the sprinkler for room office

I've got a fix. 我已经解决了。 The constructor for the Fire object was empty. Fire对象的构造函数为空。

public Fire(Room room2) {
    // TODO Auto-generated constructor stub
}

I fixed it . 我修好了它 。

public Fire(Room room2) {
    this.room =  room2 ;
}

and it worked. 而且有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM