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
"