在 CLIPS 中获取事实并打印参数

Get facts and print parameters in CLIPS

我想用规则打印事实的所有数据。以下是事实:

(deffacts datas
(pile name 1 blocks A B C A B)
(pile name 2 blocks B A)
(pile name 3 blocks A B)
(pile name 4 blocks A))

这是我想看的印刷品:

pile 1 : A B C A B
pile 2 : B A
pile 3 : A B
pile 4 : A

有人知道怎么做吗?

感谢您的帮助!

如果打印堆的顺序无关紧要,你可以这样做:

CLIPS> (clear)
CLIPS> 
(deffacts datas
   (pile name 1 blocks A B C A B)
   (pile name 2 blocks B A)
   (pile name 3 blocks A B)
   (pile name 4 blocks A))
CLIPS> 
(defrule print
   (pile name ?name blocks $?blocks)
   =>
   (printout t pile " " ?name " : " (implode$ ?blocks) crlf))
CLIPS> (reset)
CLIPS> (run)
pile 4 : A
pile 3 : A B
pile 2 : B A
pile 1 : A B C A B
CLIPS> (clear)
CLIPS> 
(deftemplate pile
   (slot name)
   (multislot blocks))
CLIPS>    
(deffacts datas
   (pile (name 1) (blocks A B C A B))
   (pile (name 2) (blocks B A))
   (pile (name 3) (blocks A B))
   (pile (name 4) (blocks A)))
CLIPS> 
(defrule print
   (pile (name ?name) (blocks $?blocks))
   =>
   (printout t pile " " ?name " : " (implode$ ?blocks) crlf))
CLIPS> (reset)
CLIPS> (run)
pile 4 : A
pile 3 : A B
pile 2 : B A
pile 1 : A B C A B
CLIPS> 

如果顺序很重要,您可以按照以下方式使用事实来完成,但这在第一种情况下是破坏性的,或者在第二种情况下需要清理:

CLIPS> (clear)
CLIPS> 
(deftemplate pile
   (slot name)
   (multislot blocks))
CLIPS>    
(deffacts datas
   (pile (name 1) (blocks A B C A B))
   (pile (name 2) (blocks B A))
   (pile (name 3) (blocks A B))
   (pile (name 4) (blocks A)))
CLIPS> 
(defrule print
   ?pile <- (pile (name ?name) (blocks $?blocks))
   (not (pile (name ?name2&:(< ?name2 ?name))))
   =>
   (retract ?pile)
   (printout t pile " " ?name " : " (implode$ ?blocks) crlf))
CLIPS> (reset)
CLIPS> (run)
pile 1 : A B C A B
pile 2 : B A
pile 3 : A B
pile 4 : A
CLIPS> (facts)
f-0     (initial-fact)
For a total of 1 fact.
CLIPS> (clear)
CLIPS> 
(deftemplate pile
   (slot name)
   (multislot blocks))
CLIPS>    
(deffacts datas
   (pile (name 1) (blocks A B C A B))
   (pile (name 2) (blocks B A))
   (pile (name 3) (blocks A B))
   (pile (name 4) (blocks A)))
CLIPS> 
(defrule print
   (pile (name ?name) (blocks $?blocks))
   (not (and (pile (name ?name2&:(< ?name2 ?name)))
             (not (pile-printed ?name2))))
   =>
   (assert (pile-printed ?name))
   (printout t pile " " ?name " : " (implode$ ?blocks) crlf))
CLIPS> (reset)
CLIPS> (run)
pile 1 : A B C A B
pile 2 : B A
pile 3 : A B
pile 4 : A
CLIPS> (facts)
f-0     (initial-fact)
f-1     (pile (name 1) (blocks A B C A B))
f-2     (pile (name 2) (blocks B A))
f-3     (pile (name 3) (blocks A B))
f-4     (pile (name 4) (blocks A))
f-5     (pile-printed 1)
f-6     (pile-printed 2)
f-7     (pile-printed 3)
f-8     (pile-printed 4)
For a total of 9 facts.
CLIPS> 

最后,您可以使用事实查询函数检索所有桩事实,然后对它们进行排序以获得所需的顺序:

CLIPS> (clear)
CLIPS> 
(deffunction pile-sort (?f1 ?f2)
   (> (fact-slot-value ?f1 name)
      (fact-slot-value ?f2 name)))
CLIPS> 
(deftemplate pile
   (slot name)
   (multislot blocks))
CLIPS>       
(deffacts datas
   (pile (name 1) (blocks A B C A B))
   (pile (name 2) (blocks B A))
   (pile (name 3) (blocks A B))
   (pile (name 4) (blocks A)))
CLIPS>       
(defrule print
   =>
   (bind ?facts (find-all-facts ((?f pile)) TRUE))
   (bind ?facts (sort pile-sort ?facts))
   (progn$ (?f ?facts)
      (printout t pile " " (fact-slot-value ?f name) " : " 
                           (implode$ (fact-slot-value ?f blocks)) crlf)))
CLIPS> (reset)
CLIPS> (run) 
pile 1 : A B C A B
pile 2 : B A
pile 3 : A B
pile 4 : A
CLIPS>