【问题标题】:Clips Family Expert SystemClips 家族专家系统
【发布时间】:2018-03-05 08:12:47
【问题描述】:

我正在尝试用 Clips 编程语言实现一个基本的专家系统。我和他们各自的父母有一个关于孩子的知识库。我想设置一个规则,如果两个孩子有相同的父母,那么它就断言他们是兄弟姐妹。


(deftemplate person "family tree"
          (slot name)
          (slot father)
          (slot mother))

(assert
        (person
                (name "William")
                (father "John")
                (mother "Megan")))
(assert
        (person (name "David")
                (father "John")
                (mother "Megan")))

(defrule sibling
        (person
                (name ?name1)
                (father ?x)
                (mother ?x))
        (person
                (name ?name2)
                (father ?y)
                (mother ?y)))

当我定义规则时出现语法错误:

Syntax Error:  Check appropriate syntax for defrule.

【问题讨论】:

    标签: clips expert-system family-tree


    【解决方案1】:

    您的规则的正确语法是:

    (defrule sibling 
       (person (name ?name1) (father ?x) (mother ?x)) 
       (person (name ?name2) (father ?y) (mother ?y)) 
       => 
       ...)
    

    rule 中,template 被称为:

    (template_name (slot_name value) (slot_name value)) 
    

    规则分为两部分:定义满足此类规则的条件的 LHS(左侧)和定义后续操作的 RHS(右侧)。

    在 CLIPS 中,=> 运算符将两侧分开。

    例子:

    (defrule animal-is-a-duck
        (animal ?name)
        (quacks)
        (two legs)
        (lay eggs)
        =>
        (assert (animal ?name is a duck)))
    

    您可以在basic programming guide 中阅读有关 CLIPS 语法的更多信息。

    【讨论】:

    • 谢谢,但我不得不把 MAIN:: 放在我的同级标题前面。但是现在当我在assert 中调用规则时,我不断收到错误消息。我试过语法 (assert MAIN::sibling (person (name "First_Person")) (person (name "Second_Person")))(assert (MAIN::sibling (person ("David")) (person ("William")))) 我也试过没有 MAIN:: 有什么建议吗?
    • 请编辑您的问题,说明您遇到的错误。还要编写定义模板、事实和规则,用换行符分隔每个语句,这有助于提高可读性。
    • 抱歉,我不得不接受这个答案。这条规则实际上并没有通过。我现在遇到语法错误。我编辑了问题以显示我的代码和错误。
    • 编辑了aswer。
    • 所以我的 RHS 看起来像 (assert (person (?name1)) is siblings with (person (?name2))) 对吗?
    【解决方案2】:

    你的规则应该是这样的:

    
        (defrule sibling 
            (person
                    (name ?name1)
                    (father ?x)
                    (mother ?y))
            (person
                    (name ?name2)
                    (father ?x)
                    (mother ?y))
            (test (neq ?name1 ?name2))
        =>
        (assert (siblings ?name1 ?name2)))
    

    只有在匹配的每个事实中父亲和母亲是同一个人时,才能满足原始规则。

    此规则允许重复:

    
        f-3     (siblings "David" "William")
        f-4     (siblings "William" "David")
    

    所以您可以在另一个规则中捕获它,或者您可以编写一个更复杂的规则(或另一个规则),也与当前生成的匹配兄弟事实相匹配。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-15
      • 2021-02-05
      • 2010-09-15
      • 1970-01-01
      • 2019-11-28
      相关资源
      最近更新 更多