0
votes

How to make fact in multiple type? like in this code get the same rank fact together.

(P X Y) means X is Y's elder member i had tried this:

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

i want to make a complete seniority in the family, and get (R a bd cef), but i just get (R a b c) (R a d e) (R a d f) can u help me?

1

1 Answers

0
votes

It's a bit more complicated than what you've attempted, particularly if you want it to work properly for more than 3 generations and/or multiple family groups.

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>