CLIPSdo-for-all-facts
使执行此类操作变得更容易,但不幸的是,默认情况下它在许多系统上不可用,并且需要重新编译 CLIPS 才能使其可用。
如果您为所有需要打印的项目断言事实,那么您可以使用forall
来确定评分最高的项目:
(defrule assert-unprinted "Asserts each item that needs to be printed."
(print-sorted)
(recommendation (name ?n))
=>
(assert (unprinted ?n)))
(defrule retract-print-sorted "Retract print-sorted after all items enumerated."
(declare (salience -10))
?f <- (print-sorted)
=>
(retract ?f))
(defrule print-greatest "Prints the unprinted item with the greatest rating."
(not (print-sorted))
?u <- (unprinted ?name)
(recommendation (name ?name) (rating ?rating))
(forall (and (unprinted ?n) (recommendation (name ?n) (rating ?r)))
(test (<= ?r ?rating)))
=>
(retract ?u)
(printout t ?name " has rating " ?rating "." crlf))
以下是一些示例事实:
(deffacts recommendations
(recommendation (name chocolate) (rating 10.0))
(recommendation (name vanilla) (rating 6.8))
(recommendation (name strawberry) (rating 8.5)))
它们按降序打印,如下所示:
CLIPS> (reset)
CLIPS> (assert (print-sorted))
<Fact-4>
CLIPS> (run)
chocolate has rating 10.0.
strawberry has rating 8.5.
vanilla has rating 6.8.
CLIPS>