Download CLIPS User's Guide

Transcript
— 123 —
CLIPS> (describe-class INTEGER)
================================================================
****************************************************************
Abstract: direct instances of this class cannot be created.
Direct Superclasses: NUMBER
Inheritance Precedence: INTEGER NUMBER PRIMITIVE OBJECT
Direct Subclasses:
****************************************************************
================================================================
CLIPS>
The problem is that the INTEGER class has no handler for "+". In fact, it has no
handlers at all since none are listed. As you recall, the USER class and its subclasses
always have print, delete, and other handlers automatically defined by CLIPS. If we
want to send print messages to an object like 1 of the INTEGER class, we'll have to
make our own print handler.
Before writing this handler, let's answer a question that you may have concerning
how INTEGER can have instances. Since INTEGER is an abstract class, you may
wonder how it can have instances such as 1, 2, 3, etc. Although you cannot make direct
instances of an abstract class, you can make use of existing instances. For the case of
the predefined system class INTEGER, all the integers up to the maximum allowed are
available as objects. Likewise, strings and symbols are available for the system defined
abstract classes STRING and SYMBOL, and so forth for the other predefined classes.
Shown following is the definition of a handler for the NUMBER class that will handle
addition by messages. The handler is defined for NUMBER rather than INTEGER
because we would also like to handle FLOAT objects too. Instead of defining the same
handler for FLOAT and for INTEGER, it's easier to just define a handler for the
superclass NUMBER. If a handler for a message is not defined in the class of the
object, CLIPS tries all the handlers in the inheritance precedence list. Since "+" is not
defined for INTEGER, CLIPS tries NUMBER next, finds the applicable handler, and
returns the result of 3.
CLIPS>
(defmessage-handler NUMBER + (?arg) ; Argument of handler
(+ ?self ?arg))
; Function addition of handler
CLIPS> (send 1 + 2)
3
CLIPS> (send 2.5 + 3)