next up previous

9.6.1 Creating Instances

Like facts, instances of user-defined classes must be explicitly created by the user. Likewise, all instances are deleted during the reset command, and they can be loaded and saved similarly to facts. All operations involving instances require message-passing using the send function except for creation, since the object does not yet exist. A function called make-instance is used to create and initialize a new instance. This function implicitly sends an initialization message to the new object after allocation, and the user can customize instance initialization with daemons. make-instance also allows slot-overrides to change any predefined initialization for a particular instance. make-instance automatically delays all object pattern-matching activities for rules until all slot overrides have been processed. The function active-make-instance can be used if delayed pattern-matching is not desired. active-make-instance remembers the current state of delayed pattern-matching, explicitly turns delay on, and then restores it to its previous state once all slot overrides have been processed.

Syntax

(make-instance <instance-definition>)
(active-make-instance <instance-definition>)
<instance-definition> ::= [<instance-name-expression>] of
                        <class-name-expression>
                        <slot-override>*
<slot-override>       ::= (<slot-name-expression>                               <expression>*)

The return value of make-instance is the name of the new instance on success or the symbol FALSE on failure. The evaluation of <instance-name-expression> can either be an instance-name or a symbol. If <instance-name-expression> is not specified, then the function gensym* will be called to generate the instance-name.

make-instance performs the following steps in order:

1) If an instance of the specified name already exists, that instance receives a message, e.g. (send <instance-name> delete). If this fails for any reason, the new instance creation is aborted. Normally, the handler attached to class USER will respond to this message (see section 9.4.5.2).

2) A new and uninitialized instance of the specified class is created with the specified name.

3) All slot-overrides are immediately evaluated and placed via put- messages (see section 9.3.3.10), e.g. (send <instance-name> put-<slot-name> <expression>*). If there are any errors, the new instance is deleted.

4) The new instance receives the init message, e.g. (send <instance-name> init). Normally, the handler attached to class USER will respond to this message (see section 9.4.4.1). This handler calls the init-slots function (see section 12.16.4.1). This function uses defaults from the class definition (if any) for any slots which do not have slot-overrides. The class defaults are placed directly without the use of messages. If there are any errors, the new instance is deleted.

Example

CLIPS> (clear)
CLIPS>
(defclass A (is-a USER)
  (role concrete)
    (slot x (default 34)
          (create-accessor write))
    (slot y (default abc)))
CLIPS>
(defmessage-handler A put-x before (?value)
    (printout t "Slot x set with message." crlf))
CLIPS>
(defmessage-handler A delete after ()
    (printout t "Old instance deleted." crlf))
CLIPS> (make-instance a of A)
[a]
CLIPS> (send [a] print)
[a] of A
(x 34)
(y abc)
CLIPS> (make-instance [a] of A (x 65))
Old instance deleted.
Slot x set with message.
[a]
CLIPS> (send [a] print)
a of A
(x 65)
(y abc)
CLIPS> (send [a] delete)
Old instance deleted.
TRUE
CLIPS>

9.6.1.1 Definstances Construct



next up previous