COOL provides a useful query system for determining and performing actions on sets of instances of user-defined classes that satisfy user-defined queries. The instance query system in COOL provides six functions, each of which operate on instance-sets determined by user-defined criteria:
Function Purpose any-instancep Determines if one or more instance-sets satisfy a query find-instance Returns the first instance-set that satisfies a query find-all-instances Groups and returns all instance-sets which satisfy a query do-for-instance Performs an action for the first instance-set which satisfies a query do-for-all-instances Performs an action for every instance-set which satisfies a query as they are found delayed-do-for-all-instances Groups all instance-sets which satisfy a query and then iterates an action over this group
Explanations on how to form instance-set templates, queries and actions immediately follow, for these definitions are common to all of the query functions. The specific details of each query function will then be given. The following is a complete example of an instance-set query function:
Example
CLIPS> (do-for-all-instances ((?car1 MASERATI BMW) (?car2 ROLLS-ROYCE)) ;Instance-set template (> ?car1:price (* 1.5 ?car2:price) ;Instance-set query (printout t ?car1 crlf)) ;Instance-set distributed action [Albert-Maserati] CLIPS>
For all of the examples in this section, assume that the commands below have already been entered:
Example
CLIPS> (defclass PERSON (is-a USER) (role abstract) (slot sex (access read-only) (storage shared)) (slot age (type NUMBER) (visibility public))) CLIPS> (defmessage-handler PERSON put-age (?value) (dynamic-put age ?value)) CLIPS> (defclass FEMALE (is-a PERSON) (role abstract) (slot sex (source composite) (default female))) CLIPS> (defclass MALE (is-a PERSON) (role abstract) (slot sex (source composite) (default male))) CLIPS> (defclass GIRL (is-a FEMALE) (role concrete) (slot age (source composite) (default 4) (range 0.0 17.9))) CLIPS> (defclass WOMAN (is-a FEMALE) (role concrete) (slot age (source composite) (default 25) (range 18.0 100.0))) CLIPS> (defclass BOY (is-a MALE) (role concrete) (slot age (source composite) (default 4) (range 0.0 17.9))) CLIPS> (defclass MAN (is-a MALE) (role concrete) (slot age (source composite) (default 25) (range 18.0 100.0))) CLIPS> (definstances PEOPLE (Man-1 of MAN (age 18)) (Man-2 of MAN (age 60)) (Woman-1 of WOMAN (age 18)) (Woman-2 of WOMAN (age 60)) (Woman-3 of WOMAN) (Boy-1 of BOY (age 8)) (Boy-2 of BOY) (Boy-3 of BOY) (Boy-4 of BOY) (Girl-1 of GIRL (age 8)) (Girl-2 of GIRL)) CLIPS> (reset) CLIPS>