CLIPS Printout Number Of Times Rule Fired

半世苍凉 提交于 2019-12-11 15:33:17

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!