两场比较器CLIPS

时间:2019-09-17 14:58:09

标签: clips

我需要编写一个比较器来按CLIPS排序事实。该比较器必须根据第一字段对事实进行排序(总和),如果第一字段不足以进行排序,我希望它根据第二字段对它们进行排序(总价)。

这是我写的,但是不起作用...

str1

确定性的降序和总价的升序。

1 个答案:

答案 0 :(得分:0)

您的功能很好。您只需要将其名称和事实列表传递给sort函数即可。

         CLIPS (6.31 6/12/19)
CLIPS> 
(deffunction MAIN::rating-sort (?f1 ?f2)
   (if (< (fact-slot-value ?f1 sum-certainties) (fact-slot-value ?f2 sum-certainties)) then return TRUE
   else (if (> (fact-slot-value ?f1 sum-certainties) (fact-slot-value ?f2 sum-certainties)) then return FALSE
        else (if (> (fact-slot-value ?f1 total-price) (fact-slot-value ?f2 total-price)) then return TRUE
             else (if (< (fact-slot-value ?f1 total-price) (fact-slot-value ?f2 total-price)) then return FALSE
                  else then return FALSE)))))
CLIPS>                   
(deftemplate thing
   (slot sum-certainties)
   (slot total-price))
CLIPS>    
(deffacts things
   (thing (sum-certainties 90) (total-price 200))
   (thing (sum-certainties 30) (total-price 100))
   (thing (sum-certainties 30) (total-price 300))
   (thing (sum-certainties 90) (total-price 150))
   (thing (sum-certainties 50) (total-price 150))
   (thing (sum-certainties 70) (total-price 200)))
CLIPS> (reset)
CLIPS>    
(foreach ?f (sort rating-sort (find-all-facts ((?f thing)) TRUE))
   (printout t (fact-slot-value ?f sum-certainties) " " (fact-slot-value ?f total-price) crlf))
90 150
90 200
70 200
50 150
30 100
30 300
CLIPS>