2014-12-06 126 views
0

我有一棵家譜。我需要使用CLIPS找到一個人的姐夫。無法讓'不'和'eq'一起工作

家譜

http://oi61.tinypic.com/viik4y.jpg

規則找到妹夫

規則-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) 
) 

規則-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)) 

Now for - 1)戴安娜的姐夫(或b_i_lw)必須是Andrew,Mark和Edward 2)Mark,b_i_lw是Andrew,Edward和Charles。 3)Charles,b_i_lw'必須是Mark。

但我的輸出是不完全正確的,因爲一些錯誤的信息也與正確的參數推導出配偶的兄弟姐妹(規則命名爲「fnd_BrthrsNLaw2」)和妹妹的丈夫(名爲「fnd_BrthrsNLaw3」規則)的規則。馬克,戴安娜與查爾斯

輸出給出如下:

輸出用於搜索戴安娜:

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和規則3使用(未(當量))的是不影響「和」塊,他們都在裏面,雖然我已經測試(不(當量))是在我平等的時候給予真實的,當我不打算平等,假如他們是平等的,我打算得到。

回答

1

您需要使用測試CE來評估規則條件中的表達式。例如,而不是

(not (eq ?sp ?p2)) 

使用

(test (not (eq ?sp ?p2))) 

(test (neq ?sp ?p2)) 

您現有的規則試圖匹配關係名EQ事實,而不是調用EQ功能來比較值。

此外,調用fnd_BrthrsNLaw3規則操作中的reset命令是您可能不想執行的操作。

+0

非常感謝。爲了解決這個謎團,我已經失去了3天的無關緊要的事情。 – fs71gh 2014-12-06 18:35:34

+0

爲了便於閱讀,您還可以從規則中刪除「和」條件元素。在列出的規則中,它們是不必要的。當你必須放置幾個帶有「not」條件元素的模式時,你通常會使用它們。 – 2014-12-06 20:34:36