next up previous

12.6.2 If...then...else Function

The if function provides an if...then...else structure to allow for conditional execution of a set of actions.

Syntax

(if <expression>
   then
      <action>*
   [else
      <action>*])

Any number of allowable actions may be used inside of the then or else portion, including another if...then...else structure. The else portion is optional. If <expression> evaluates to anything other than the symbol FALSE, then the actions associated with the then portion are executed. Otherwise, the actions associated with the else portion are executed. The return value of the if function is the value of the last <expression> or <action> evaluated.

Example

(defrule closed-valves
   (temp high)
   (valve ?v closed)
   =>
   (if (= ?v 6)
      then
      (printout t "The special valve " ?v " is closed!" crlf)
      (assert (perform special operation))
      else
      (printout t "Valve " ?v " is normally closed" crlf)))

Note that this rule could have been accomplished just as easily with two rules, and that it is usually better to accomplish this with two rules.

(defrule closed-valves-number-6
   (temp high)
   (valve 6 closed)
   =>
   (printout t "The special valve 6 is closed!" crlf)
   (assert (perform special operation)))
(defrule closed-valves-other-than-6
   (temp high)
   (valve ?v&~6 closed)
   =>
   (printout t "Valve " ?v " is normally closed" crlf))


next up previous