2013-04-21 45 views
2

我需要CLIPS問題的一些幫助。這就是問題所在: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)) 
) 

但是,當它是在自定義模板格式我不知道。有人可以幫忙嗎?

回答

1

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> 
+0

是的,我想過爲所有項目聲明事實,但不確定如何使用它進行排序。這很好用!謝謝! – user2303699 2013-04-26 03:13:42

+0

默認情況下,CLIPS 6.23版(2005年發佈)和更高版本中包含事實集查詢功能,其中包括「全部事實」。在過去的9年中尚未更新的第三方擴展將不包含這些功能,否則,如果您使用的是當前版本的CLIPS,則這些功能將可用。 – 2014-05-20 00:45:58

0
CLIPS> 
(deftemplate recommendation 
    (slot name) 
    (slot message) 
    (slot rating)) 
CLIPS>  
(deffacts recommendations 
    (recommendation (name chocolate) (rating 10.0)) 
    (recommendation (name vanilla) (rating 6.8)) 
    (recommendation (name strawberry) (rating 8.5))) 
CLIPS> 
(deffunction rating-sort (?f1 ?f2) 
    (< (fact-slot-value ?f1 rating) (fact-slot-value ?f2 rating))) 
CLIPS>  
(defrule print 
    => 
    (bind ?facts (find-all-facts ((?f recommendation)) TRUE)) 
    (bind ?facts (sort rating-sort ?facts)) 
    (progn$ (?f ?facts) 
     (printout t (fact-slot-value ?f name) " has rating " (fact-slot-value ?f rating) "." crlf))) 
CLIPS> (reset) 
CLIPS> (run) 
chocolate has rating 10.0. 
strawberry has rating 8.5. 
vanilla has rating 6.8. 
CLIPS>