next up previous

5.4.1.2 Wildcards Single and Multifield

CLIPS has two wildcard symbols that may be used to match fields in a pattern. CLIPS interprets these wildcard symbols as standing in place of some part of a pattern entity. The singlefield wildcard, denoted by a question mark character (?), matches any value stored in exactly one field in the pattern entity. The multifield wildcard, denoted by a dollar sign followed by a question mark ($?), matches any value in zero or more fields in a pattern entity. Singlefield and multifield wildcards may be combined in a single pattern in any combination. It is illegal to use a multifield wildcard in a single field slot of a deftemplate or object pattern. By default, an unspecified singlefield slot in a deftemplate/object pattern is matched against an implied singlefield wildcard. Similarly, an unspecified multifield slot in a deftemplate/object pattern is matched against an implied multifieldwildcard.

Syntax

An ordered pattern conditional element containing only literals and wildcards has the following basic syntax:

(<constraint-1> ... <constraint-n>)

where

<constraint> ::= <constant> | ? | $?

A deftemplate pattern conditional element containing only literals and wildcards has the following basic syntax:

(<deftemplate-name> (<slot-name-1> <constraint-1>)
                                        ï
                                        ï
                                        ï
                    (<slot-name-n> <constraint-n>))

Example 1

This example utilizes the datafacts deffacts shown in section 5.4.1.

CLIPS> (clear)
CLIPS>
(defrule find-data
  (data ? blue red $?)
  =>)
CLIPS> (reset)
CLIPS> (agenda)
0      find-data: f-5
0      find-data: f-3
For a total of 2 activations.
CLIPS> (facts)
f-0     (initial-fact)
f-1     (data 1.0 blue "red")
f-2     (data 1 blue)
f-3     (data 1 blue red)
f-4     (data 1 blue RED)
f-5     (data 1 blue red 6.9)
For a total of 6 facts.
CLIPS>

Example 2

This example utilizes the person deftemplate and people deffacts shown in section 5.4.1.

CLIPS> (clear)
CLIPS>
(defrule match-all-persons
  (person)
  =>)
CLIPS> (reset)
CLIPS> (agenda)
0      match-all-persons: f-5
0      match-all-persons: f-4
0      match-all-persons: f-3
0      match-all-persons: f-2
0      match-all-persons: f-1
For a total of 5 activations.
CLIPS> (facts)
f-0     (initial-fact)
f-1     (person (name Joe) (age 20) (friends))
f-2     (person (name Bob) (age 20) (friends))
f-3     (person (name Joe) (age 34) (friends))
f-4     (person (name Sue) (age 34) (friends))
f-5     (person (name Sue) (age 20) (friends))
For a total of 6 facts.
CLIPS>

Multifield wildcard and literal constraints can be combined to yield some powerful patternmatching capabilities. A pattern to match all of the facts that have the symbol YELLOW in any field (other than the first) could be written as

(data $? YELLOW $?)

Some examples of what this pattern would match are

(data YELLOW blue red green)
(data YELLOW red)
(data red YELLOW)
(data YELLOW)
(data YELLOW data YELLOW)

The last fact will match twice since YELLOW appears twice in the fact. The use of multifield wildcards should be confined to cases of patterns in which the singlefield wildcard cannot create a pattern that satisfies the match required, since the multifield wildcard produces every possible match combination that can be derived from a pattern entity. This derivation of matches requires a significant amount of time to perform when compared to the time needed to perform a singlefield match.


next up previous