简体   繁体   English

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

[英]CLIPS(recursion) - family relations: how to implement correctly the relation of being an ancestor?

Introduction 介绍

I am trying to implement a rule in CLIPS language - the relation that a person is an ancestor of other person. 我正在尝试以CLIPS语言实施规则-一个人是另一个人的祖先的关系。 The constraint is that such rule must be derived only from the following premises: 约束条件是该规则必须仅从以下前提得出:

(male ?x) ("x is a male") (男性x)(“ x是男性”)

(female ?y) ("y is a female") (女y)(“ y是女”)

(mother-of ?x ?y) ("x is a mother of y") (?x?y的母亲)(“ x是y的母亲”)

(father-of ?x ?y) ("x is a father of y") (?x?y的父亲)(“ x是y的父亲”)

My attempt 我的尝试

I wrote the following code: 我写了以下代码:

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

The gist of the problem 问题要点

The above code compiles and returns "true"(in other words, the construced rule is logically correct) and outputs expected results in the case of such list of facts. 上面的代码编译并返回“ true”(换句话说,构造规则在逻辑上是正确的),并在此类事实列表的情况下输出预期结果。

However , there is a subtle problem: 但是 ,存在一个细微的问题:

This codes works for determining first and second generation of ancestors only . 此代码适用于确定祖先第一代和第二代。

In other words, it works only in the case if someone is a father/mother of someone or a grandfather/grandmother of someone, but not for checking if someone is a great grandfather/great grandmother or great great grandfather/great great grandmother of someone, etc. 换句话说,它仅在某人是某人的父亲/母亲或某人的祖父/祖母的情况下有效, 但不适用于检查某人是某人的曾祖父/曾祖母还是曾曾祖父/曾曾祖母等

The above code does not handle this issue. 上面的代码无法解决此问题。

How to overcome this problem? 如何克服这个问题?

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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM