Двухполевой компаратор CLIPS

Мне нужно написать компаратор, чтобы упорядочить факты в 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)))))

Порядок убывания для определенности суммы и порядок возрастания для общей цены.


person user3254491    schedule 17.09.2019    source источник
comment
Возможный дубликат Сортировка набора фактов CLIPS   -  person Gary Riley    schedule 20.09.2019


Ответы (1)


Ваша функция в порядке. Вам просто нужно передать его имя и список фактов в функцию сортировки.

         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> 
person Gary Riley    schedule 17.09.2019