next up previous

5.4.1.6 Return Value Constraints

It is possible to use the return value of an external function to constrain the value of a field. The return value constraint (=) allows the user to call external functions from inside a pattern. (This constraint is different from the comparison function which uses the same symbol. The difference can be determined from context.) The return value must be one of the primitive data types. This value is incorporated directly into the pattern at the position at which the function was called as if it were a literal constraint, and any matching patterns must match this value as though the rule were typed with that value. Note that the function is evaluated each time the constraint is checked (not just once).

Basic Syntax

=<function-call>

Syntax

Expanding on the syntax definition given in section 5.4.1.5 now gives:

<term> ::= <constant> |
           <single-field-variable> |
           <multifield-variable> |
           :<function-call> |
           =<function-call>

Example 1

CLIPS> (clear)
CLIPS> (deftemplate data (slot x) (slot y))
CLIPS>
(defrule twice
  (data (x ?x) (y =(* 2 ?x)))
  =>)
CLIPS> (assert (data (x 2) (y 4))  ; f-0
               (data (x 3) (y 9))) ; f-1
<Fact-1>
CLIPS> (agenda)
0      twice: f-0
For a total of 1 activation.
CLIPS>

Example 2

CLIPS> (clear)
CLIPS>
(defclass DATA (is-a USER)
   (role concrete) (pattern-match reactive)
   (slot x (create-accessor write)))
CLIPS>
(defrule return-value-example-2
   (object (is-a DATA)
           (x ?x1))
   (object (is-a DATA)
           (x ?x2&=(+ 5 ?x1)|=(- 12 ?x1)))
   =>)
CLIPS> (make-instance of DATA (x 4))
[gen1]
CLIPS> (make-instance of DATA (x 9))
[gen2]
CLIPS> (make-instance of DATA (x 3))
[gen3]
CLIPS> (agenda)
0      return-value-example-2: [gen3],[gen2]
0      return-value-example-2: [gen2],[gen3]
0      return-value-example-2: [gen1],[gen2]
For a total of 3 activations.
CLIPS>


next up previous