Sort a set of facts CLIPS

半城伤御伤魂 提交于 2019-12-02 09:36:58

You need to be using (return TRUE) and (return FALSE) in your deffunction rather than return TRUE and return FALSE, but the code works the same even with this change. The comparator never prints 2.

         CLIPS (6.31 6/12/19)
CLIPS> 
(deffunction rating-sort (?f1 ?f2)
   (printout t ?f1 crlf)
   (printout t ?f2 crlf)
   (printout t "f1-SC " (fact-slot-value ?f1 sum-certainties) crlf)
   (printout t "f2-SC " (fact-slot-value ?f2 sum-certainties) crlf)
   (printout t "f1-TP " (fact-slot-value ?f1 total-price) crlf)
   (printout t "f2-TP " (fact-slot-value ?f2 total-price) crlf)
   (if (< (fact-slot-value ?f1 sum-certainties) (fact-slot-value ?f2 sum-certainties)) 
      then 
      (printout t "1" crlf) return TRUE
      else 
      (if (> (fact-slot-value ?f1 sum-certainties) (fact-slot-value ?f2 sum-certainties)) 
          then (printout t "2" crlf) return FALSE
          else 
          (if (> (fact-slot-value ?f1 total-price) (fact-slot-value ?f2 total-price)) 
              then (printout t "3" crlf) return TRUE
              else 
              (if (< (fact-slot-value ?f1 total-price) (fact-slot-value ?f2 total-price)) 
                  then (printout t "4" crlf) return FALSE
                  else (printout t "5" crlf) return FALSE)))))
CLIPS> 

(deftemplate alternative
   (multislot hotels)
   (multislot times)
   (slot total-price)
   (multislot certainty)
   (slot sum-certainties)
   (slot flag))            
CLIPS> 
(deffacts alternatives
   (alternative (hotels hotel4 hotel3 hotel2 hotel1) (times 1 0 0 0) 
                (total-price 75.0) (certainty 14.0 -0.001 -0.001 -0.001) 
                (sum-certainties 13.997) (flag TRUE))
   (alternative (hotels hotel4 hotel3 hotel2 hotel1) (times 0 1 0 0) 
                (total-price 100.0) (certainty -0.001 14.0 -0.001 -0.001) 
                (sum-certainties 13.997) (flag TRUE))
   (alternative (hotels hotel4 hotel3 hotel2 hotel1) (times 0 0 1 0) 
                (total-price 75.0) (certainty -0.001 -0.001 14.0 -0.001) 
                (sum-certainties 13.997) (flag TRUE))
   (alternative (hotels hotel4 hotel3 hotel2 hotel1) (times 0 0 0 1) 
                (total-price 100.0) (certainty -0.001 -0.001 -0.001 14.0) 
                (sum-certainties 13.997) (flag TRUE)))
CLIPS> (reset)
CLIPS> (sort rating-sort (find-all-facts ((?f alternative)) TRUE))
<Fact-1>
<Fact-2>
f1-SC 13.997
f2-SC 13.997
f1-TP 75.0
f2-TP 100.0
4
<Fact-3>
<Fact-4>
f1-SC 13.997
f2-SC 13.997
f1-TP 75.0
f2-TP 100.0
4
<Fact-1>
<Fact-3>
f1-SC 13.997
f2-SC 13.997
f1-TP 75.0
f2-TP 75.0
5
<Fact-2>
<Fact-3>
f1-SC 13.997
f2-SC 13.997
f1-TP 100.0
f2-TP 75.0
3
<Fact-2>
<Fact-4>
f1-SC 13.997
f2-SC 13.997
f1-TP 100.0
f2-TP 100.0
5
(<Fact-1> <Fact-3> <Fact-2> <Fact-4>)
CLIPS>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!