简体   繁体   中英

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 .

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 .

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.

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

I fixed it .

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

and it worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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