Download CLIPS User's Guide 6
Transcript
— 126 — Although the handler could be defined just for DUCK in this case, a handler defined for USER will be called for all subclasses of USER, not just DUCKLING and DUCK. Thus, the handler print-slots will work for all subclasses that we may define of USER. Of course it's possible to get carried away and define all your message handlers as USER handlers. However, it's good style and improves efficiency to define handlers as close as possible to the class or classes for which they are intended. Efficiency is improved because CLIPS does not have to keep searching through a lot of classes to find the applicable handler. Getting Around Let's examine message-handlers in more detail now. We'll define a handler to print out a header when an object receives a message to print itself. The message-handler is defined using a defmessage-handler construct as follows. CLIPS> (defmessage-handler USER print before () (printout t "*** Starting to print ***" crlf)) CLIPS> (send [Dorky_Duck] print) *** Starting to print *** [Dorky_Duck] of DUCK (age 2) (sound quack) CLIPS> The reason that a header is printed rather than a trailer at the end has to do with the handler type. A before type handler is used before the print message. To make a trailer, use the after type handler as shown in the following example. CLIPS> (defmessage-handler USER print after () (printout t "*** Finished printing ***" crlf)) CLIPS> (send [Dorky_Duck] print) *** Starting to print *** [Dorky_Duck] of DUCK (age 2) (sound quack) *** Finished printing *** CLIPS>