Search code examples
clipsexpert-systemjess

Exit rule execution in Jess with condition


I am reading several user inputs in Jess. The rule is:

(defrule specify-input
    ?act <- (Actuator (name 0) (inputVoltage ?v1&0) )
    =>
    (printout t "Please specify input voltage of the actuator. [V] " crlf)
    (modify ?act (inputVoltage (read)))
    (printout t "Please specify desired force of the actuator. [N] " crlf)
    (modify ?act (Force (read)))
    (printout t "Please specify desired stroke lenght of the actuator. [mm] " crlf)
    (modify ?act (StrokeLength (read))))

I would like to be able to check value of input voltage, and if it is out of the defined range, to set it to 0 and exit further rule execution. Is is there a way to do that?


Solution

  • You might use an if function (cf. Jess manual Section 3.8.2).

    (printout t "Please specify input voltage of the actuator. [V] " crlf)
    (bind ?v (read))
    (if (and (> ?v 0) (<= ?v 1000)) then
      (printout t "Please specify desired force of the actuator. [N] " crlf)
      (bind ?f (read))
      (printout t "Please specify desired stroke lenght of the actuator. [mm] " crlf)
      (bind ?sl (read))
      (modify ?act (inputVoltage ?iv)(Force ?f)(StrokeLength ?sl))
    ) else (
      (printout t "invalid voltage" crlf)
    )
    

    Similar checks might be made for the other values as well.

    But shouldn't the user given another chance? Cf. Section 3.8.1.

    (while true do
      (printout t "Please specify input voltage of the actuator. [V] " crlf)
      (bind ?v (read))
      (if (and (> ?v 0) (<= ?v 1000)) then (break))
      (printout t "invalid voltage, not in (0,1000]" crlf)
    )