next up previous

9.6.2 Reinitializing Existing Instances

The initialize-instance function provides the ability to reinitialize an existing instance with class defaults and new slot-overrides. The return value of initialize-instance is the name of the instance on success or the symbol FALSE on failure. The evaluation of <instance-name-expression> can either be an instance-name, instance-address or a symbol. initialize-instance automatically delays all object pattern-matching activities for rules until all slot overrides have been processed. The function active-initialize-instance can be used if delayed pattern-matching is not desired.

Syntax

(initialize-instance <instance-name-expression>
    <slot-override>*)

initialize-instance performs the following steps in order:

1) 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>*).

2) The 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.5.1). This handler calls the init-slotsfunction (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 no slot-override or class default specifies the value of a slot, that value will remain the same. Empty class default values allow initialize-instance to clear a slot.

If an error occurs, the instance will not be deleted, but the slot values may be in an inconsistent state.

Example

CLIPS> (clear)
CLIPS>
(defclass A (is-a USER)
  (role concrete)
    (slot x (default 34)
          (create-accessor write))
    (slot y (default ?NONE)
          (create-accessor write))
    (slot z (create-accessor write)))
CLIPS> (make-instance a of A (y 100))
[a]
CLIPS> (send [a] print)
[a] of A
(x 34)
(y 100)
(z nil)
CLIPS> (send [a] put-x 65)
65
CLIPS> (send [a] put-y abc)
abc
CLIPS> (send [a] put-z "Hello world.")
ìHello world.î
CLIPS> (send [a] print)
[a] of A
(x 65)
(y abc)
(z "Hello world.")
CLIPS> (initialize-instance a)
[a]
CLIPS> (send [a] print)
a of A
(x 34)
(y abc)
(z nil)
CLIPS>


next up previous