next up previous

3.1 SLOT DEFAULT VALUES

The <defaultattribute> specifies the value to be used for unspecified slots of a template fact when an assert action is performed. One of two types of default selections can be chosen: default or dynamicdefault.

The attribute specifies a static default value. The specified expressions are evaluated once when the deftemplate is defined and the result is stored with the deftemplate. The result is assigned to the appropriate slot when a new template fact is asserted. If the keyword ?DERIVE is used for the default value, then a default value is derived from the constraints for the slot (see section 11.5 for more details). By default, the default attribute for a slot is (default ?DERIVE). If the keyword ?NONE is used for the default value, then a value must explicitly be assigned for a slot when an assert is performed. It is an error to assert a template fact without specifying the values for the (default ?NONE) slots.

The defaultdynamic attribute is a dynamic default. The specified expressions are evaluated every time a template facts is asserted, and the result is assigned to the appropriate slot.

A singlefield slot may only have a single value for its default. Any number of values may be specified as the default for a multifield slot (as long as the number of values satisfies the cardinality attribute for the slot).

Example

CLIPS> (clear)
CLIPS>
(deftemplate foo
   (slot w (default ?NONE))
   (slot x (default ?DERIVE))
   (slot y (default (gensym*)))
   (slot z (default-dynamic (gensym*))))
CLIPS> (assert (foo))
[TMPLTRHS1] Slot w requires a value because of its (default ?NONE) attribute.
CLIPS> (assert (foo (w 3)))
<Fact-0>
CLIPS> (assert (foo (w 4)))
<Fact-1>
CLIPS> (facts)
f-0     (foo (w 3) (x nil) (y gen1) (z gen2))
f-1     (foo (w 4) (x nil) (y gen1) (z gen3))
For a total of 2 facts.
CLIPS>


next up previous