CLIPS 正在访问 属性 的 属性
CLIPS accessing a property of a property
我读到 所以回答是
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
如果我想访问 属性 的 属性 怎么办?例如,
给定 类 A
和 B
的两个实例 a
和 b
。
a
有一个名为 ref_to_b
的 属性,它是对 b
的引用。
b
有一个名为 some_prop_of_b
的 属性。
如何匹配以下内容:
a
其中 ref_to_b
等于 b
并且 some_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"))
=>
)
将引用实例的实例名称放在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>
我读到
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
如果我想访问 属性 的 属性 怎么办?例如,
给定 类 A
和 B
的两个实例 a
和 b
。
a
有一个名为 ref_to_b
的 属性,它是对 b
的引用。
b
有一个名为 some_prop_of_b
的 属性。
如何匹配以下内容:
a
其中 ref_to_b
等于 b
并且 some_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"))
=>
)
将引用实例的实例名称放在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>