CLIPS(arguments) - 家庭关系:如何避免一个人是自己兄弟的问题?

CLIPS(arguments) - family relations: how to avoid an issue of one person being a brother of himself?

简介

我正在尝试用 CLIPS 语言实现一个规则——一个人是另一个人的兄弟的关系。 约束是这样的规则只能从以下前提推导出来:

(男?x) ("x is a female")

(女?y) ("y is a female")

(?x?y 的母亲) ("x is a mother of y")

(?x?y 的父亲) ("x is a father of 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 brother
    (and
        (male (person ?alpha))
        (mother-of (mother ?x) (child ?alpha))
        (father-of (father ?y) (child ?alpha))
        (mother-of (mother ?x) (child ?beta))
        (father-of (father ?y) (child ?beta))
    )
    =>
    (printout t ?alpha " is a brother of " ?beta crlf)
    (assert (brother ?alpha ?beta))
)

问题的要点

以上代码编译通过returns"true"(也就是说,构造出的规则在逻辑上是正确的)

但是,有一个微妙的问题:

如何避免添加事实的问题,例如"Mark is a brother of Mark"(我们假设每个名字都是唯一的,所以同一个名字对应同一个人)?显然,这样的事实是错误的,但我的规则输出如此愚蠢。

以上代码没有处理这个问题。

如何克服这个问题?

致谢

非常感谢在这个问题上的帮助!!!

这是我的独立解决方案:

(defrule brother
    (and
        (male (person ?alpha))

        (or
             (male (person ?beta))
             (female (person ?beta))
         )

        (mother-of (mother ?x) (child ?alpha))
        (father-of (father ?y) (child ?alpha))
        (mother-of (mother ?x) (child ?beta))
        (father-of (father ?y) (child ?beta))
        (test (neq ?alpha ?beta))
    )
    =>
    (printout t ?alpha " is a brother of " ?beta crlf)
    (assert (brother ?alpha ?beta))
)

将第二个母模式中的约束 ?beta 更改为 ?beta&~?alpha。

(defrule brother
    (and
        (male (person ?alpha))
        (mother-of (mother ?x) (child ?alpha))
        (father-of (father ?y) (child ?alpha))
        (mother-of (mother ?x) (child ?beta&~?alpha))
        (father-of (father ?y) (child ?beta))
    )
    =>
    (printout t ?alpha " is a brother of " ?beta crlf)
    (assert (brother ?alpha ?beta))
)