CLIPS:两个事实列表之间的差异

时间:2022-09-10 21:33:02

If I have a bunch of facts like (example (fact 1)), (example (fact 2)), (example (fact 3)), and have another list of facts like (myfact (number 2)), how can I perform a printout on each item in the first list that is not in the second (based on the number in the fact/number slots)? I suspect I need do-for-all-facts, but I'm not sure exactly how. Here's my incomplete code:

如果我有一堆事实,例如(例子(事实1)),(例子(事实2)),(例子(事实3)),并且有另一个事实清单,如(myfact(数字2)),我怎么能对第一个列表中不在第二个列表中的每个项目执行打印输出(基于事实/数字插槽中的数字)?我怀疑我需要做所有事实,但我不确定如何。这是我不完整的代码:

(deffunction difference ()
    (do-for-all-facts ((?f1 example)) TRUE
        (find-all-facts ((?f2 myfact)) (eq 1 1))
        (if (somehow check if ?f1:fact does not equal ANY of number slots in ?f2) then
            (printout t "..." crlf))))

1 个解决方案

#1


CLIPS> (clear)
CLIPS> (deftemplate example (slot fact))
CLIPS> (deftemplate myfact (slot number))
CLIPS> 
(deffacts start
   (example (fact 1))
   (example (fact 2))
   (example (fact 3))
   (myfact (number 2))
   (myfact (number 4)))
CLIPS> 
(deffunction difference ()
   (do-for-all-facts ((?f1 example))
      (not (any-factp ((?f2 myfact)) (eq ?f1:fact ?f2:number)))
      (printout t "difference " ?f1:fact crlf)))
CLIPS> (reset)
CLIPS> (difference)
difference 1
difference 3
CLIPS> 
(defrule difference
   (example (fact ?n))
   (not (myfact (number ?n)))
   =>
   (printout t "difference " ?n crlf))
CLIPS> (run)
difference 3
difference 1
CLIPS> 

#1


CLIPS> (clear)
CLIPS> (deftemplate example (slot fact))
CLIPS> (deftemplate myfact (slot number))
CLIPS> 
(deffacts start
   (example (fact 1))
   (example (fact 2))
   (example (fact 3))
   (myfact (number 2))
   (myfact (number 4)))
CLIPS> 
(deffunction difference ()
   (do-for-all-facts ((?f1 example))
      (not (any-factp ((?f2 myfact)) (eq ?f1:fact ?f2:number)))
      (printout t "difference " ?f1:fact crlf)))
CLIPS> (reset)
CLIPS> (difference)
difference 1
difference 3
CLIPS> 
(defrule difference
   (example (fact ?n))
   (not (myfact (number ?n)))
   =>
   (printout t "difference " ?n crlf))
CLIPS> (run)
difference 3
difference 1
CLIPS>