通过增加CLIPS中特定值的顺序来输出特定事实

时间:2019-01-21 18:28:40

标签: artificial-intelligence clips expert-system

我有以下事实清单:

f-0    (initial-fact)
f-1    (fact 1 [input_1] 21)
f-2    (fact 1 [input_2] 28)
f-3    (fact 1 [input_3] 10)
f-4    (fact 1 [input_4] 25)
f-5    (fact 1 Normal Operation!)
f-6    (fact 2 [input_1] 7)
f-7    (fact 2 [input_2] 25)
f-8    (fact 2 [input_3] 13)
f-9    (fact 2 [input_4] 15)
f-10   (fact 2 adder a1 error!)
f-11   (fact 3 [input_1] 11)
f-12   (fact 3 [input_2] 17)
f-13   (fact 3 [input_3] 24)
f-14   (fact 3 [input_4] 31)
f-15   (fact 3 multiplier p1 error!)

是否有办法从所有这些事实中选择和打印仅包含错误的数量和类型的事实?例如,我想按升序打印:

1 Normal Operation!
2 adder a1 error!
3 multiplier p1 error!

请注意,我实际上有更多的事实,并且它们在事实列表中的顺序没有如我所显示的那样递增。所以我必须以某种方式使其增加。

1 个答案:

答案 0 :(得分:1)

         CLIPS (6.31 2/3/18)
CLIPS> 
(deffacts initial
   (fact 1 [input_1] 21)
   (fact 1 [input_2] 28)
   (fact 1 [input_3] 10)
   (fact 1 [input_4] 25)
   (fact 1 Normal Operation!)
   (fact 2 [input_1] 7)
   (fact 2 [input_2] 25)
   (fact 2 [input_3] 13)
   (fact 2 [input_4] 15)
   (fact 2 adder a1 error!)
   (fact 3 [input_1] 11)
   (fact 3 [input_2] 17)
   (fact 3 [input_3] 24)
   (fact 3 [input_4] 31)
   (fact 3 multiplier p1 error!))
CLIPS> 
(deffunction compare-1st (?f1 ?f2)
   (> (nth$ 1 (fact-slot-value ?f1 implied))
      (nth$ 1 (fact-slot-value ?f2 implied))))
CLIPS> 
(defrule print
   =>
   (bind ?facts 
      (find-all-facts ((?f fact)) 
                      (not (instance-namep (nth$ 2 ?f:implied)))))
   (bind ?facts (sort compare-1st ?facts))
   (foreach ?f ?facts
      (bind ?data (fact-slot-value ?f implied))
      (printout t (implode$ (first$ ?data)) " " 
                  (implode$ (rest$ ?data)) crlf))) 
CLIPS> (reset)
CLIPS> (run)
1 Normal Operation!
2 adder a1 error!
3 multiplier p1 error!
CLIPS>