我需要一些关于 CLIPS 问题的帮助。这就是问题所在:
“假设 CLIPS 数据库包含以下模板实例化的事实:
(deftemplate recommendation
(slot name)
(slot message)
(slot rating)
)
编写 CLIPS 规则以根据评级按降序打印消息。每条消息都将与其相关的名称和评级一起打印。”
当有一个列表时,我知道排序规则,例如:
(deffacts testlist
(list 1 4 2 3 5 8 7 6 9 0)
)
(defrule sort
?f <- (list $?head ?a ?b&:(< ?b ?a) $?tail)
=>
(retract ?f)
(assert (list $?head ?b ?a $?tail))
)
但是我不确定什么时候是deftemplate格式。有人可以帮忙吗?
最佳答案
CLIPS do-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>
关于CLIPS 规则以降序打印消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16128257/