next up previous

5.4.2 Test Conditional Element

Field constraints used within pattern CEs allow very descriptive constraints to be applied to patternmatching. Additional capability is provided with the test conditional element. The test CE is satisfied if the function call within the test CE evaluates to a nonFALSE value and unsatisfied if the function call evaluates to FALSE. As with predicate constraints, the user can compare the variable bindings that already have occurred in any manner. Mathematical comparisons on variables (e.g., is the difference between ?x and ?y greater than some value?) and complex logical or equality comparisons can be done. External functions also can be called which compare variables in any way that the user desires.

Any kind of external function may be embedded within a test conditional element (or within field constraints). Userdefined predicate functions must take arguments as defined in the Advanced Programming Guide. Several predicate functions are provided by CLIPS (see section 12.1).

Syntax

<test-CE> ::= (test <function-call>)

Since the symbol test is used to indicate this type of conditional element, rules may not use the symbol test as the first field in a pattern CE. A test CE is evaluated when all proceeding CEs are satisfied. This means that a test CE will be evaluated more than once if the proceeding CEs can be satisfied by more than one group of pattern entities. In order to cause the reevaluation of a test CE, a pattern entity matching a CE prior to the test CE must be changed. The use of test CEs can cause additional CEs to be added to the rule. In addition, test CEs may also be automatically reordered by CLIPS. See section 5.4.9 for more details.

Example 1

This example checks to see if the difference between two numbers is greater than or equal to three:

CLIPS> (clear)
CLIPS>
(defrule example-1
  (data ?x)
  (value ?y)
  (test (>= (abs (- ?y ?x)) 3))
  =>)
CLIPS> (assert (data 6) (value 9))
<Fact-1>
CLIPS> (agenda)
0      example-1: f-0,f-1
For a total of 1 activation.
CLIPS>

Example 2

This example checks to see if there is a positive slope between two points on a line.

CLIPS> (clear)
CLIPS>
(deffunction positive-slope
   (?x1 ?y1 ?x2 ?y2)
   (< 0 (/ (- ?y2 ?y1) (- ?x2 ?x1))))
CLIPS>
(defrule example-2
   (point ?a ?x1 ?y1)
   (point ?b ?x2 ?y2)
   (test (> ?b ?a))
   (test (positive-slope ?x1 ?y1 ?x2 ?y2))
   =>)
CLIPS>
(assert (point 1 4.0 7.0) (point 2 5.0 9.0))
<Fact-1>
CLIPS> (agenda)
0      example-2: f-0,f-1
For a total of 1 activation.
CLIPS>


next up previous