Search code examples
clips

Check multiple facts in CLIPS


Let's say I have some facts (I do not know how many there are) like this: lamp x is off. With a defrule I proggressively turn all lamps on so every fact will be: lamp x is on. How do I check every lamp that is on. I know that if there were three lamps I could write:

(defrule checkAllLamps
    (lamp 1 is on)
    (lamp 2 is on)
    (lamp 3 is on)
    =>
    (printout t "All lamps are on now")
)

But for x lamps? Thank you!


Solution

  • You can use fact-set query functions for that (chapter 12.9.12 of the Basic Programming Guide).

    (deftemplate lamp 
      (slot id (type INTEGER)) 
      (slot state (type SYMBOL)))
    
    (defrule all-lamps-are-on 
      (lamp (state on)) 
      (test (>= (length$ (find-all-facts ((?l lamp)) (eq ?l:state on))) 3)) 
      => 
      (printout t "All lamps are on" crlf))