Deftemplate
- Deftemplate is similar to a struct definition in C. That is, the deftemplate defines a group of related fields in a pattern similar to the way in which a C struct is a group of related data.
- A deftemplate is a list of named fields called slots. Deftemplate allows access by name rather than by specifying the order of fields.
- Deftemplate contributes to good style in expert systems programs and is a valuable tool of software engineering.
Single-slot
- A slot is a named single-slot or multislot. A single-slot or simply slot contains exactly one field while a multislot contains zero or more fields.
- Any number of single or multislot may be used in a deftemplate. To write a slot, give the field name (attribute) followed by the field value.
- Note that a multislot slot with one value is strictly not the same as a single-slot. As an analogy, think of a cupboard (the multislot) that may contain dishes.
- A cupboard with one dish is not the same as a dish (single-slot.)
- However, the value of a single-slot slot (or variable) may match a multislot slot (or multislot variable) that has one field.
- As an example of a deftemplate relation, consider the attributes of a business who might be considered good for prospect.
name "Dopey Wonderful"
assets rich
age 99
(deftemplate prospect ; name of deftemplate relation
"vital information" ; optional comment in quates
(slot name ; name of field
(type STRING) ; default value of field name
(default ?DERIVE) ) ; default value of field name
(slot assets ; name of field
(type SYMBOL) ; type of field
(default rich)) ; default value of field assets
(slot age ; name of field
(type NUMBER) ; type. NUMBER can be INTEGER or FLOT
(default 80))) ; default value of field age
In this example, the components of deftemplate are structured as:
- A deftemplate relation name
- Attributes called fields
- The field type, which can be any one of the allowed types: SYMBOL, STRING, NUMBER, and others.
- The default for the field value
- (assert (prospect))
- As you see, CLIPS has inserted the default value of the null string, ''', for the name field since that is the default for a STRING. Likewise, the asserts and age defaults were also inserted by CLIPS. Different types have different default symbols such as the null string, " ", for STRING; the integer 0 for INTEGER; the float 0.0 for FLOT and so on.
- The ?DRIVE keyword select the appropriate type of constraints for that slot, e.g., the null string, " ", for a slot of type STRING.
(prospect (name ?name) (assets ?net_worth)
(age ?months) )
=>
(printout t "Prospect: " ?name crlf
?net_worth crlf ?months " months old" crlf) )
No comments:
Post a Comment