2017-03-17 151 views
0

this讀的屬性,以便回答,這是CLIPS訪問屬性

better to explicitly retrieve the slot value by matching it rather than using the slot accessor as this will cause the condition to be reevaluated whenever the slot value changes

如果我想訪問一個屬性的屬性?例如,

分別給出了分別爲ABab的兩個實例。

a有一個名爲ref_to_b的屬性,它是對b的引用。 b有一個屬性some_prop_of_b

如何匹配以下:

aref_to_b等於bsome_prop_of_b等於 「some_string」。

我試過,但得到了一個錯誤:

(defrule my_rule "comment me" 
    (object (is-a A) 
     (ref_to_b ?ref_to_b)) 
    (?ref_to_b 
     (some_prop_of_b "some_string")) 
=> 
) 

回答

1

放入ref_to_b插槽引用的實例的實例名稱,然後使用名稱插槽相匹配的參考:

CLIPS> 
(defclass A (is-a USER) (slot ref_to_b)) 
CLIPS> 
(defclass B (is-a USER) (slot some_prop_of_b)) 
CLIPS> 
(make-instance [b1] of B (some_prop_of_b "some_string")) 
[b1] 
CLIPS> 
(make-instance [b2] of B (some_prop_of_b "not_some_string")) 
[b2] 
CLIPS> 
(make-instance [a] of A (ref_to_b [b2])) 
[a] 
CLIPS> 
(defrule my_rule 
    (object (is-a A) (ref_to_b ?name_b)) 
    (object (name ?name_b) (some_prop_of_b "some_string")) 
    =>) 
CLIPS> (agenda) 
CLIPS> (send [a] put-ref_to_b [b1]) 
[b1] 
CLIPS> (agenda) 
0  my_rule: [a],[b1] 
For a total of 1 activation. 
CLIPS> 
+0

阿名稱插槽。這是關鍵。 – stackoverflowwww