我有一棵家谱。我需要使用CLIPS找到一个人的姐夫。
家谱
寻找兄弟会的规则
Rule-1(寻找配偶的兄弟)
(defrule MAIN::fnd_BrthrsNLaw1 ;spouse's brother
(findRelative (person ?pn) (relationship b_i_lw)) ;Brothers-in-Law
(and
(and (and (or (marriage-between (personA ?pn) (personB ?sp))
(marriage-between (personA ?sp) (personB ?pn))
)
(child-of (relationship ?x) (person ?sp) (mother ?m) (father ?f))
)
(child-of (relationship son-of) (person ?pn2) (mother ?m) (father ?f))
)
(not (marriage-between (personA ?pn) (personB ?pn2)))
)
=>
(printout t ?pn "'s brothers-in-Law: (spouse's brother) " ?pn2 crlf)
)
规则-2(寻找配偶的姐姐的丈夫)
(defrule MAIN::fnd_BrthrsNLaw2 ;spouse's sister's husband
(findRelative (person ?pn) (relationship b_i_lw)) ;Brothers-in-Law
(and
(and
(and
(or
(marriage-between (personA ?pn) (personB ?sp))
(marriage-between (personA ?sp) (personB ?pn))
)
(and
(child-of (relationship ?x) (person ?sp) (mother ?a) (father ?b))
(child-of (relationship daughter-of) (person ?p2) (mother ?a) (father ?b))
)
)
(and
(not (eq ?sp ?p2))
(marriage-between (personA ?p2) (personB ?px2))
)
)
=>
(printout t ?pn "'s brothers-in-Law: (spouse's sister's husband) " ?px2 " <sister = " ?p2 " ; spouse = " ?sp " ; spouse's parents: " ?a " , " ?b ">" crlf)
)
Rule-3(寻找姐姐的丈夫)
(defrule MAIN::fnd_BrthrsNLaw3 ;sister's husband
(findRelative (person ?p) (relationship b_i_lw)) ;Brothers-in-Law
(and
(and
(child-of (relationship ?y) (person ?p) (mother ?c) (father ?d))
(child-of (relationship daughter-of) (person ?sist) (mother ?c) (father ?d))
)
(and
(not (eq ?p ?sist))
(marriage-between (personA ?sist) (personB ?p2))
)
)
=>
(printout t ?p "'s brothers-in-Law: (sister's husband) " ?p2 <sister = " ?sist " ; " ?p "'s parent = " ?sp " ; spouse's parents: " ?c " , " ?d ">"crlf)
(reset)
)
模板
(deftemplate MAIN::marriage-between
(slot personA)
(slot personB))
(deftemplate MAIN::child-of
(slot relationship (type SYMBOL) (allowed-symbols son-of daughter-of))
(slot person)
(slot mother)
(slot father))
(deftemplate MAIN::findRelative
(slot relationship (type SYMBOL)
(allowed-symbols g_g_p ;Great-GrandParents
g_ch ;GrandChildren
b_i_lw)) ;Brothers-in-Law
(slot person))
现在 - 1)戴安娜,姐夫(或b_i_lw)必须是安德鲁,马克和爱德华 2)马克,b_i_lw是安德鲁,爱德华和查尔斯。 3)Charles,b_i_lw'必须是Mark。
但是我的输出并不完全正确,因为一些错误的信息也被推断为配偶的兄弟姐妹(规则名为“fnd_BrthrsNLaw2”)和姐姐的丈夫(规则名为“fnd_BrthrsNLaw3”)。
Mark,Diana和Charles的输出如下:
搜索戴安娜的输出:
CLIPS> (assert (findRelative (person Diana) (relationship b_i_lw)))
<Fact-20>
CLIPS> (run)
Diana's brothers-in-Law: (spouse's brother) Edward
Diana's brothers-in-Law: (spouse's brother) Andrew
Diana's brothers-in-Law: (spouse's sister's husband) Mark <sister = Anne ; spouse = Charles ; Charles's parents: Elizabeth , Phillip>
Diana's brothers-in-Law: (sister's husband) Charles <sister = Diana ; Diana's parent: Kydd , Spencer>
搜索标记的输出:
CLIPS> (assert (findRelative (person Mark) (relationship b_i_lw)))
<Fact-20>
CLIPS> (run)
Mark's brothers-in-Law: (spouse's brother) Edward
Mark's brothers-in-Law: (spouse's brother) Andrew
Mark's brothers-in-Law: (spouse's brother) Charles
Mark's brothers-in-Law: (spouse's sister's husband) Mark <sister = Anne ; spouse = Anne ; Anne's parents: Elizabeth , Phillip>
搜索Charles的输出:
CLIPS> (assert (findRelative (person Charles) (relationship b_i_lw)))
<Fact-20>
CLIPS> (run)
Charles's brothers-in-Law: (spouse's sister's husband) Charles <sister = Diana ; spouse = Diana ; Diana's parents: Kydd , Spencer>
Charles's brothers-in-Law: (sister's husband) Mark <sister = Anne ; Anne's parent: Elizabeth , Phillip>
我的假设是在规则-2中使用(不是(eq))而规则-3不会影响它们所在的'和'块,尽管我已经测试过(不是(eq))在当它们是平等的时候是不平等和错误的,我打算得到它。
答案 0 :(得分:1)
您需要使用测试CE来评估规则条件下的表达式。例如,而不是
(not (eq ?sp ?p2))
使用
(test (not (eq ?sp ?p2)))
或
(test (neq ?sp ?p2))
您现有的规则试图将事实与关系名称eq匹配,而不是调用eq函数来比较值。
此外,在fnd_BrthrsNLaw3规则操作中调用reset命令是您可能不想做的事情。