如何在剪辑中共同制作事实

时间:2015-05-09 12:57:29

标签: clips

如何在多种类型中制作事实?就像在这段代码中一样获得相同的排名事实。

(P X Y)表示X是Y的老成员 我试过这个:

(deffacts  people
    (P a b)
    (P b c)
    (P a d)
    (P d e)
    (P d f)
)


(defrule ranking
    (P ?x ?y)
    (P ?y ?z)
    =>
    (assert (R ?x $?y $?z))
)

我想在家里做一个完整的资历, 并得到(R a bd cef),但我得到(R a b c)(R a d e)(R a d f) 你可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

它比您尝试过的更复杂,特别是如果您希望它能够在3代以上和/或多个家庭群体中正常工作。

CLIPS> 
(defmethod concat$ ((?m1 MULTIFIELD) (?m2 MULTIFIELD (>= (length$ ?m1) (length$ ?m2))))
   (bind ?rv (create$))
   (loop-for-count (?i 1 (length$ ?m2))
       (bind ?rv (create$ ?rv (sym-cat (nth$ ?i ?m1) (nth$ ?i ?m2)))))
   (create$ ?rv (mv-subseq (+ 1 (length$ ?m2)) (length$ ?m1) ?m1)))
CLIPS> 
(defmethod concat$ ((?m1 MULTIFIELD) (?m2 MULTIFIELD (< (length$ ?m1) (length$ ?m2))))
   (bind ?rv (create$))
   (loop-for-count (?i 1 (length$ ?m1))
       (bind ?rv (create$ ?rv (sym-cat (nth$ ?i ?m1) (nth$ ?i ?m2)))))
   (create$ ?rv (mv-subseq (+ 1 (length$ ?m1)) (length$ ?m2) ?m2)))
CLIPS> 
(deffacts  people
   (P a b) ; Family 1
   (P b c)
   (P a d)
   (P d e)
   (P d f)
   (P g h) ; Family 2
   (P h j)
   (P h k)
   (P k l)
   (P k m)
   (P k n)
   (P j o)
   (P j p)
   (P j q)
   (P q r)
   (P q s))
CLIPS> 
(defrule copy
   (P ?x ?y)
   =>
   (assert (R ?x ?y)))
CLIPS>   
(defrule extend
   ?f1 <- (R $?b ?x ?ym $?e1)
   ?f2 <- (R ?y $?z)
   (test (str-index ?y ?ym))
   =>
   (retract ?f1 ?f2)
   (assert (R ?b ?x ?ym (concat$ ?e1 ?z))))
CLIPS> 
(defrule combine
   ?f1 <- (R ?x $?b ?y1 $?e1)
   ?f2 <- (R ?x $?b ?y2&~?y1 $?e2)
   =>
   (retract ?f1 ?f2)
   (assert (R ?x ?b (sym-cat ?y1 ?y2) (concat$ ?e1 ?e2))))
CLIPS> (reset)
CLIPS> (run)
CLIPS> (facts)
f-0     (initial-fact)
f-1     (P a b)
f-2     (P b c)
f-3     (P a d)
f-4     (P d e)
f-5     (P d f)
f-6     (P g h)
f-7     (P h j)
f-8     (P h k)
f-9     (P k l)
f-10    (P k m)
f-11    (P k n)
f-12    (P j o)
f-13    (P j p)
f-14    (P j q)
f-15    (P q r)
f-16    (P q s)
f-37    (R g h jk opqlmn rs)
f-46    (R a bd cef)
For a total of 19 facts.
CLIPS>