家谱获取家庭成员

Family tree get family member

我有一些家谱,想通过 defrule 从那棵树上得到祖父。要是能搞到小舅子就完美了

(deftemplate person
   (slot name) 
   (slot gender) 
   (slot father)
   (slot wife))

(deffacts   people
   (person (name Vasya) (gender male) (wife Liza))                 
   (person (name Liza) (gender female))                                 
   (person (name Vladimir) (gender male) (father Vasya))
   (person (name Natasha) (gender female) (father Vasya))
   (person (name Viktor) (gender male) (father Vasya))
   (person (name Misha) (gender male) (wife Natasha))
   (person (name Kostya) (gender male) (father Misha) (wife Liza))
   (person (name Masha) (gender female) (father Misha)))

(deftemplate mother
   (slot namel)
   (slot name2))

(deftemplate brother
   (slot namel)
   (slot name2))

(defrule Brother
   (person (name ?x) (gender male) (father ?y&~nil))
   (person (name ?z&~?x) (gender male) (father ?y&~nil))
   (not (brother (namel ?x) (name2 ?z)))
   (not (brother (namel ?z) (name2 ?x))) 
   =>
   (printout t  ?x " brother of " ?z crlf) 
   (assert (brother (namel ?x) (name2 ?z))))

假设您将同父异母兄弟视为兄弟,那么您现有的兄弟规则存在两个问题。首先,它要求一个兄弟有一个男性兄弟姐妹,所以玛莎和娜塔莎没有被认定为有兄弟。其次,同父异母的兄弟也可以共享母亲,而不仅仅是父亲。

修改你的人 detemplate 以包括母亲并将妻子位置更改为配偶:

(deftemplate person
   (slot name)
   (slot gender)
   (slot mother)
   (slot father)
   (slot spouse))

创建一个新的 detemplate 来表示人物事实之间的关系:

(deftemplate relation
   (slot p1)
   (slot is)
   (slot p2))

修改您的 defacts 以反映更新的 deftemplate:

(deffacts people
   (person (name Vasya) (gender male) (spouse Liza))                 
   (person (name Liza) (gender female))                                 
   (person (name Vladimir) (gender male) (father Vasya))
   (person (name Natasha) (gender female) (father Vasya))
   (person (name Viktor) (gender male) (father Vasya))
   (person (name Misha) (gender male) (spouse Natasha))
   (person (name Kostya) (gender male) (father Misha) (spouse Liza))
   (person (name Masha) (gender female) (father Misha)))

您可以更新兄弟规则:

(defrule brother
   (person (name ?name)
           (mother ?mother)
           (father ?father))
   (or (person (name ?brother&~?name)
               (gender male)
               (father ?father&~nil))
       (person (name ?brother&~?name)
               (gender male)
               (mother ?mother&~nil)))
   =>
   (assert (relation (p1 ?brother)
                     (is brother-of)
                     (p2 ?name))))

并为祖父和姐夫添加新规则:

(defrule grandfather
   (person (name ?name)
           (mother ?mother)
           (father ?father))
   (person (name ?mother | ?father)
           (father ?grandfather&~nil))
   =>
   (assert (relation (p1 ?grandfather)
                     (is grandfather-of)
                     (p2 ?name))))

(defrule brother-in-law
   (person (name ?name)
           (spouse ?spouse))
   (relation (p1 ?brother-in-law)
             (is brother-of)
             (p2 ?spouse))
   =>
   (assert (relation (p1 ?brother-in-law)
                     (is brother-in-law-of)
                     (p2 ?name))))