Logic CLIPS(递归)-家庭关系:如何正确实现作为祖先的关系?

Logic CLIPS(递归)-家庭关系:如何正确实现作为祖先的关系?,logic,relationship,clips,expert-system,ancestry,Logic,Relationship,Clips,Expert System,Ancestry,导言 我试图用CLIPS语言实现一个规则——一个人是另一个人的祖先的关系。 约束条件是,此类规则必须仅从以下前提派生: (男性?x)(“x是男性”) (女性?y)(“y是女性”) (x?y的母亲)(“x是y的母亲”) (x?y的父亲)(“x是y的父亲”) 我的尝试 我编写了以下代码: (deftemplate father-of (slot father) (slot child) ) (deftemplate mother-of

导言

我试图用CLIPS语言实现一个规则——一个人是另一个人的祖先的关系。 约束条件是,此类规则必须仅从以下前提派生:

(男性?x)(“x是男性”)

(女性?y)(“y是女性”)

(x?y的母亲)(“x是y的母亲”)

(x?y的父亲)(“x是y的父亲”)

我的尝试

我编写了以下代码:

    (deftemplate father-of 
        (slot father)
        (slot child)
    )

    (deftemplate mother-of 
        (slot mother)
        (slot child)
    )

    (deftemplate male 
        (slot person)
    )

    (deftemplate female
         (slot person)
    )

    (deffacts family
        (father-of (father John) (child Mark))
        (father-of (father John) (child Mary))
        (mother-of (mother Alice) (child Mark))
        (mother-of (mother Alice) (child Mary))
        (male (person John))
        (male (person Mark))
        (female (person Alice))
        (female (person Mary))
    )

    (defrule ancestor
    (or 

        (mother-of (mother ?x) (child ?w))
        (father-of (father ?x) (child ?w))


        (and
            (mother-of (mother ?x) (child ?y))
            (or
                (mother-of (mother ?y) (child ?w))
                (father-of (father ?y) (child ?w))  
            )
        )

        (and
            (father-of (father ?x) (child ?y))
            (or
                (mother-of (mother ?y) (child ?w))
                (father-of (father ?y) (child ?w))  
            )
        )
    )
    =>
    (printout t ?x " is an ancestor of " ?w crlf) 
    (assert (ancestor ?x ?w))   
)
问题的要点

上面的代码编译并返回“true”(换句话说,解释的规则在逻辑上是正确的),并在这种事实列表的情况下输出预期结果

然而,有一个微妙的问题:

此代码仅用于确定第一代和第二代祖先

换句话说,它只适用于某人的父亲/母亲或祖父/祖母的情况,但不适用于检查某人是否是曾祖父/曾祖母或曾曾祖父/曾祖母等

上述代码不处理此问题


如何克服这个问题?

多亏了您的帮助,现在我了解了CLIPS语言中递归的工作原理!
CLIPS> 
(deftemplate father-of 
   (slot father)
   (slot child))
CLIPS> 
(deftemplate mother-of 
   (slot mother)
   (slot child))
CLIPS> 
(deffacts family
   (father-of (father Bob) (child Frank))
   (mother-of (mother Linda) (child Frank))
   (father-of (father Frank) (child John))
   (mother-of (mother Susan) (child John))
   (father-of (father John) (child Mark))
   (mother-of (mother Alice) (child Mark)))
CLIPS> 
(defrule ancestor
   (or (mother-of (mother ?x) (child ?w))
       (father-of (father ?x) (child ?w))
       (and (ancestor ?x ?y)
            (ancestor ?y ?w)))
   (not (ancestor ?x ?w))
   =>
   (printout t ?x " is an ancestor of " ?w crlf) 
   (assert (ancestor ?x ?w)))
CLIPS> (reset)
CLIPS> (run)
Alice is an ancestor of Mark
John is an ancestor of Mark
Susan is an ancestor of John
Susan is an ancestor of Mark
Frank is an ancestor of John
Frank is an ancestor of Mark
Linda is an ancestor of Frank
Linda is an ancestor of Mark
Linda is an ancestor of John
Bob is an ancestor of Frank
Bob is an ancestor of Mark
Bob is an ancestor of John
CLIPS>