问题
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). 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))
回答1:
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))
回答2:
Code incorporating noxdafox's solution:
(defglobal ?*hot-oil-times* = 0)
(defglobal ?*high-pressure-times* = 0)
(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))
=>
(bind ?*hot-oil-times* (+ ?*hot-oil-times* 1))
(printout t ?time tab "temperature:" tab ?*hot-oil-times* tab ?temp crlf))
(defrule pressure-is-high
(oil-measure (pressure ?press&:(> ?press 0)) (utc-time ?time))
=>
(bind ?*high-pressure-times* (+ ?*high-pressure-times* 1))
(printout t ?time tab "pressure:" tab ?*high-pressure-times* tab ?press crlf))
来源:https://stackoverflow.com/questions/54173106/clips-printout-number-of-times-rule-fired