【问题标题】:CLIPS sister, predecesor and age rulesCLIPS姐姐、前任和年龄规则
【发布时间】:2020-04-22 01:01:29
【问题描述】:

我遇到以下 CLIPS 编程问题: 新的家庭关系知识库。请应用以下规范:

  • 使用 deftemplate 来定义人

  • 一个人的属性是:姓名、年龄、性别、孩子、

  • 每个属性都应该定义为一个槽,除了子元素,它们是多槽

  • 在性别插槽中使用(允许的符号女性男性)

  • 为每个槽添加数据类型

  • 使用 deffacts 从图表中添加人员

  • 定义规则:

——妈妈,

——父亲,

——姐姐,

——前身

——老——给出前辈和后代之间的年龄差异

这是我制作的代码:

(deftemplate person
    (slot name (type SYMBOL))
    (slot age (type INTEGER))
    (slot sex (type SYMBOL) (allowed-symbols female male))
    (multislot children (type SYMBOL))
)

(deffacts init
    (person (name Fernando) (age 55) (sex male) (children Jesus Celia))
    (person (name Maria) (age 50) (sex female) (children Jesus Celia))
    (person (name Jesus) (age 25) (sex male))
    (person (name Celia) (age 20) (sex female))
    (person (name Antonio) (age 70) (sex male) (children Fernando Ana))
    (person (name Ana) (age 38) (sex female))
    (person (name Calra) (age 68) (sex female) (children Fernando Ana))
)


(defrule mother
    (person (name ?name) (sex female) (children $?before ?child $?after))


=>
    (assert (mother ?name ?child))
    (printout t ?name " is mother of " $?child crlf))

(defrule father
    (person (name ?name) (sex male) (children $?before ?child $?after))


=>
    (assert (father ?name ?child))
    (printout t ?name " is father of " $?child crlf))


(defrule sister
    (children ?z ?x)
    (children ?z ?y)
    (female ?x)
    (not (test (eq ?x ?y)))
=>
    (assert sister ?x ?y))
    (printout t ?x "is a sister to " ?y crlf))

(defrule predecessor
(or
    (children ?x ?y)
    (and (children ?x ?z)(predecessor ?z ?y))
)
=>
(assert (predecessor ?x ?y)
(printout t ?x "is a predecessor to " ?y crlf))

(defrule old
    ?fact_no <- (person (name ?n) (age ?a1 ?a2))
    (test (<= ?a1 ?a2))

=>
    (assert (result (- ?a1 ?a2)))
    (printout t "There are " ?result " years" crlf))

母亲和父亲的关系正在发挥作用,但其余的却没有。它给了我以下错误:

CLIPS> Loading Selection...

[CSTRCPSR4] Cannot redefine deftemplate person while it is in use.

ERROR:
(deftemplate MAIN::person

[CSTRCPSR1] WARNING: Redefining deffacts: init

[CSTRCPSR1] WARNING: Redefining defrule: mother +j+j
Defining defrule: father +j+j

[CSTRCPSR1] WARNING: Redefining defrule: sister 
[PRNTUTIL2] Syntax Error:  Check appropriate syntax for RHS patterns.

ERROR:
(defrule MAIN::sister
   (children ?z ?x)
   (children ?z ?y)
   (female ?x)
   (not (test (eq ?x ?y)))
   =>
   (assert sister
Defining defrule: predecessor 
[EXPRNPSR3] Missing function declaration for defrule.

ERROR:
(defrule MAIN::predecessor
   (or  (children ?x ?y)
        (and (children ?x ?z)
             (predecessor ?z ?y)))
   =>
   (assert (predecessor ?x ?y)
           (printout t ?x "is a predecessor to " ?y crlf))
   (defrule

非常感谢您

【问题讨论】:

    标签: clips


    【解决方案1】:

    如果您已经加载了代码,请在再次尝试加载相同的代码之前发出 clear 命令。这将删除 [CSTRCPSR4] 错误消息。

    您在姐妹和前任规则中缺少括号。更正这些将允许规则加载而不会出错。

    (defrule sister
        (children ?z ?x)
        (children ?z ?y)
        (female ?x)
        (not (test (eq ?x ?y)))
    =>
        (assert (sister ?x ?y))
        (printout t ?x "is a sister to " ?y crlf))
    
    (defrule predecessor
    (or
        (children ?x ?y)
        (and (children ?x ?z)(predecessor ?z ?y))
    )
    =>
    (assert (predecessor ?x ?y))
    (printout t ?x "is a predecessor to " ?y crlf))
    

    这些规则仍然不会在您现有的代码中执行,因为它们需要儿童和女性事实,并且您的规则都没有断言这些事实。

    尚不清楚您要如何处理旧规则,但由于您尝试匹配年龄槽中的两个值,因此该规则会产生错误,因为该槽只能包含一个值。

    【讨论】:

    • 您好,感谢您的回答。我尝试了一个新代码,但它仍然无法正常工作。
    • (defrule Sister "X is a Sister to Y" (person (name ?x) (sex female) (mother ?m ?x)) (person (name ?y) (mother ?m ? y)) (not (test (eq ?x ?y))) => (assert (siter ?x ?y)) (printout t ?x " is the Sister of " ?y crlf))
    • (defrule 前任 "X 是 Y 的祖先" (or (person (name ?x) (children $?before ?y $?after)) (and (person (name ?x) ( children $?before ?z $?after) (predecessor ?y ?z))))) => (assert (predecessor ?x ?z)) (printout t ?x" 是 "?z crlf) 的前身)跨度>
    • (defrule age_difference (person (name ?x) (age ?a1)) (person (name ?y) (age ?a2)) (test ( (assert (diff (- ?a1 ?a2))) (printout t "前任“?x”和后代“?y”的年龄差是“?diff crlf))
    • 我有一个问题:我可以使用之前定义的规则吗,例如可以在姐姐的规则中使用“父亲”吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-05
    • 1970-01-01
    • 1970-01-01
    • 2020-09-16
    • 2021-02-26
    • 1970-01-01
    • 2021-01-30
    相关资源
    最近更新 更多