Search code examples
artificial-intelligenceclipsexpert-system

Output specific facts by increasing order of a certain value in CLIPS


I have the following list of facts :

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!)

Is there a way to somehow choose and print from all these facts the ones that contain only the number and the type of error? For example I want to print in increasing order:

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

Note that I actually have more facts and the order that they are in the fact list is not in increasing order as I have shown. So I have to somehow make it increasing.


Solution

  •          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>