2015-04-23 39 views
0

我想知道如何我的兩個規則相結合,例如:如何將兩個規則合併爲一個?

(defrule Rules::pants 
    (declare (auto-focus TRUE)) 
(answer (ident color) (text red)) 
    (answer (ident pants) (text yes)) 
    => 
(printout t "you are wearing red pants")) 

(defrule Rules::shirt 
    (declare (auto-focus TRUE)) 
(answer (ident shirt) (text blue)) 
    (answer (ident red) (text yes)) 
    => 
(printout t "you are wearing blue shirt")) 

如果我會寫這兩個規則,如:

(defrule Rules::pants 
    (declare (auto-focus TRUE)) 
(answer (ident red) (text yes)) 
    (answer (ident pants) (text yes)) 
(answer (ident shirt) (text yes)) 
    (answer (ident blue) (text yes)) 
    => 
(printout t "you are wearing blue shirt and red pants")) 

我希望它像一個OR聲明,如果滿足任何條件就會被觸發。

回答

0

天真的回答將是

(defrule Rules::pants  
    (or (and (answer (ident red) (text yes)) 
      (answer (ident pants) (text yes))) 
     (and (answer (ident shirt) (text yes)) 
      (answer (ident blue) (text yes))) 
) 
=> 
    (printout t "you are wearing blue shirt or red pants") 
) 

第一個障礙是,這個規則被觸發兩次,如果這個人有紅色的褲子藍色襯衫。很可能,這並不重要,因爲這個人沒有被識別,所以我們可能會認爲只有一個人在走秀。

編輯如果屬性不相互關聯,即顏色和項目可以自由組合,則會發生第二個障礙。考慮一件藍色牛仔褲和一件紅色格仔襯衫的鄉下人。規則會因爲有「褲子」和「襯衫」以及「紅色」和「藍色」而觸發,足以根據模式進行匹配。但是OP在評論中聲稱(見下文)有一些方法可以避免這種情況。

+0

謝謝我認爲這是有效的,我只是沒有得到什麼問題,如果可能的話可以請多解釋一下嗎? –

+0

只要插入(回答襯衫是),(回答紅色是),(回答褲子是),(回答藍色是)穿着紅色襯衫和藍色褲子的紳士。 – laune

+0

哦不,我會添加一些其他信息,使其意味着什麼,這只是我有問題的部分,謝謝 –