next up previous

3 Deftemplate Construct

Os encode information positionally. To access that information, a user must know not only what data is stored in a fact but which field contains the data. N (or deftemplate) facts provide the user with the ability to abstract the structure of a fact by assigning names to each field found within the fact. The deftemplate construct is used to create a template which can then be used by nonordered facts to access fields of the fact by name. The deftemplate construct is analogous to a record or structure definition in programming languages such as Pascal and C.

The syntax of the deftemplate construct is:

Syntax

(deftemplate <deftemplate-name> [<comment>]
   <slot-definition>*)
<slot-definition> ::= <single-slot-definition> |
                      <multislot-definition>
<single-slot-definition> 
               ::= (slot <slot-name> 
                         <template-attribute>*)
<multislot-definition>   
               ::= (multislot <slot-name> 
                              <template-attribute>*)
<template-attribute> ::= <default-attribute> |
                         <constraint-attribute>
<default-attribute>  
               ::= (default ?DERIVE | ?NONE | <expression>*) |
                   (default-dynamic <expression>*)

Redefining a deftemplate will result in the previous definition being discarded. A deftemplate can not be redefined while it is being used (for example, by a fact or pattern in a rule). A deftemplate can have any number of single or multifeld slots. CLIPS always enforces the single and multifield definitions of the deftemplate. For example, it is an error to store (or match) multiple values in a single-field slot.

Example

(deftemplate object
   (slot name)
   (slot location)
   (slot on-top-of)
   (slot weight)
   (multislot contents))

3.1 SLOT DEFAULT VALUES

3.2 SLOT DEFAULT CONSTRAINTS FOR PATTERNMATCHING

3.3 SLOT VALUE CONSTRAINT ATTRIBUTES

3.4 IMPLIED DEFTEMPLATES



next up previous