Search code examples
clips

CLIPS Printout Number Of Times Rule Fired


I have the following CLIPS construct below and I'm interested in tallying up how many times a particular rule fired so the output will be a series of the rules fired followed by their count (see picture for example). CLIPS Output So it would be all those rules followed by:

temperature: (times fired) seconds
pressure: (times fired) seconds

(deftemplate oil-measure
  (slot utc-time (type STRING))
  (slot temperature (type INTEGER))
  (slot pressure (type INTEGER)))

(defrule oil-is-hot
  (oil-measure (temperature ?temp) (utc-time ?time))
  (test (> ?temp 32))
  =>
  (printout t ?time tab "temperature:" tab ?temp crlf))

(defrule pressure-is-high
  (oil-measure (pressure ?press&:(> ?press 0)) (utc-time ?time))
  =>
  (printout t ?time tab "pressure:" tab ?press crlf))

Solution

  • You can store counters in global variables and increase them accordingly.

    (defglobal ?*hot-oil-times* = 0)
    
    (defrule oil-is-hot
      (oil-measure (temperature ?temp) (utc-time ?time))
      (test (> ?temp 32))
      =>
      (bind ?*hot-oil-times* (+ ?*hot-oil-times* 1))
      (printout t ?time tab "temperature:" tab ?*hot-oil-times* tab ?temp crlf))