Example 1 of drools reasoning-Room Fire Fighting

Source: Internet
Author: User

1. a stateful session must be used.

 
Knowledgebuilder kbuilder = knowledgebuilderfactory. newknowledgebuilder (); kbuilder. add (resourcefactory. newclasspathresource ("firealarm. DRL ", getclass (), resourcetype. DRL); If (kbuilder. haserrors () {system. err. println (builder. geterrors (). tostring ();} kbase. addknowledgepackages (kbuilder. getknowledgepackages (); statefulknowledgesession ksession = kbase. newstatefulknowledgesession ();

2. Basic classes

    • Room
    • Sprinkler Fire extinguisher
    • Alarm alarm
    • Fire
3. Rule writing

Note:

A. the modify statement is used to modify an object. Unlike the set method that directly calls an object, after modify is called, the modified object will re-check whether a rule is matched in the rule engine to generate reasoning. Therefore, reasoning must use stateful sessions.

B. "Not" Statement, which can check whether an object does not exist

// When the fire in the room is off, open the fire extinguisher <br/> rule "when there is a fire turn on the Sprinkler" <br/> when <br/> fire ($ room: room) <br/> $ sprinkler: sprinkler (room = $ room, on = false) <br/> then <br/> modify ($ sprinkler) {Seton (true) }; <br/> system. out. println ("turn on the sprinkler for room" + $ room. getname (); <br/> end <br/> // when no fire occurs in the room, if the fire extinguisher is still on, disable the fire extinguisher <br/> rule "when the fire is gone turn off the Sprinkler" <br/> when <br/> $ room: Room () <br/> $ sprinkler: sprinkler (room = $ room, on = true) <br/> not fire (room = $ Room) <br/> then <br/> modify ($ sprinkler) {Seton (false)}; <br/> system. out. println ("turn off the sprinkler for room" + $ room. getname (); <br/> end

Turn on the alarm as long as any room is under fire

Note that the exists statement is used to check whether the object exists.

Rule "raise the alarm when we have one or more fires" <br/> when <br/> exists fire () <br/> then <br/> insert (new alarm (); <br/> system. out. println ("raise the alarm"); <br/> end

Turn off the alarm when no fire is off in all rooms

Rule "cancel the alarm when all the fires have gone" <br/> when <br/> not fire () <br/> $ alarm: Alarm () <br/> then <br/> retract ($ alarm); <br/> system. out. println ("cancel the alarm"); <br/> end <br/>

Report if there is no fire or alarm!

Rule "status output when things are OK" <br/> when <br/> not alarm () <br/> not sprinkler (on = true) <br/> then <br/> system. out. println ("everything is OK"); <br/> end

4. Rule Execution Process

A. Prepare four rooms and trigger the execution first.

String [] names = new string [] {"Kitchen", "bedroom", "office", "livingroom" }; <br/> Map <string, room> name2room = new hashmap <string, room> (); <br/> for (string name: names) {<br/> room = new room (name ); <br/> name2room. put (name, room); <br/> ksession. insert (room); // when the insert operation is complete, the rule is matched. <br/> sprinkler = new sprinkler (room); <br/> ksession. insert (sprinkler); <br/>}< br/> ksession. fireallrules (); // trigger rule execution <br/>

The system reports "Everything is OK ".
!"

B. Two rooms are under fire

Fire kitchenfire = new fire (name2room. get ("Kitchen"); <br/> fire officefire = new fire (name2room. get ("Office"); <br/> facthandle kitchenfirehandle = ksession. insert (kitchenfire); <br/> facthandle officefirehandle = ksession. insert (officefire); <br/> ksession. fireallrules ();

The system reports "raise the alarm
"" Turn on the sprinkler for room kitchen
"" Turn on the sprinkler for room office
"

C. Remove the fire object.

Ksession. Retract (kitchenfirehandle); <br/> ksession. Retract (officefirehandle); <br/> ksession. fireallrules ();

The system reports "cancel the alarm
"" Everything is OK
"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.