CLIPS 实例 <> 和 []?实例 eq 本身为 FALSE?
CLIPS instance <> and []? Instance eq to itself FALSE?
我有一个 ontology,我用这种方式定义了两个 类:
(defclass A
(is-a USER)
(role concrete)
(single-slot TheB
(type INSTANCE)
(allowed-classes B)
(create-accessor read-write)))
(defclass B
(is-a USER)
(role concrete)
(single-slot Id
(type STRING)
(create-accessor read-write)))
所以我创建了一个 B [b] 的实例和另一个引用 [b] 的 A [a] 的实例。然后我有以下规则:
(defrule myrule
?b <- (object (is-a B))
?a <- (object (is-a A)
(TheB ?y&:(= (str-compare (send ?b get-Id) (send ?y get-Id)) 0)))
=>
(printout t ?y " " ?b crlf)
(printout t (type ?y) " " (type ?b) crlf)
(printout t (eq ?y ?b) crlf)
(printout t ?a "->" (send ?a get-TheB) crlf)
)
当我 运行 我得到以下输出:
[b] <Instance-b>
B B
FALSE
<Instance-a>->[b]
谁能解释一下一个和另一个有什么区别?为什么一个有“<>”,另一个有“[]”,但类型是一样的?我必须比较 id 吗?我无法从 []?
获得 <>
非常感谢您的关注。
您可以通过名称(字符串)或地址(指向实例的指针)来引用实例。向实例地址发送消息比向实例名称发送消息稍快,因为不涉及将名称转换为地址的查找。无论您使用名称还是地址来确定有效实例的类型,结果都是相同的(实例的class)。在您的示例中,[b] 是实例的名称, 是实例的地址。与实例名称不同,实例地址不能以文本方式指定(尽管可以打印地址),因为它们指的是内存位置。
与其定义您自己的 ID 槽以使一个实例指向另一个实例,不如使用实例的名称:
CLIPS (6.31 2/3/18)
CLIPS>
(defclass A
(is-a USER)
(role concrete)
(single-slot TheB
(type INSTANCE)
(allowed-classes B)
(create-accessor read-write)))
CLIPS>
(defclass B
(is-a USER)
(role concrete))
CLIPS>
(defrule myrule
?b <- (object (is-a B) (name ?name))
?a <- (object (is-a A) (TheB ?name))
=>
(printout t ?a " " ?b crlf)
(printout t ?name crlf)
(printout t (eq ?b ?name) crlf)
(printout t (eq ?b (instance-address ?name)) crlf))
CLIPS> (make-instance [b] of B)
[b]
CLIPS> (make-instance [a] of A (TheB [b]))
[a]
CLIPS> (agenda)
0 myrule: [b],[a]
For a total of 1 activation.
CLIPS> (run)
<Instance-a> <Instance-b>
[b]
FALSE
TRUE
CLIPS>
我有一个 ontology,我用这种方式定义了两个 类:
(defclass A
(is-a USER)
(role concrete)
(single-slot TheB
(type INSTANCE)
(allowed-classes B)
(create-accessor read-write)))
(defclass B
(is-a USER)
(role concrete)
(single-slot Id
(type STRING)
(create-accessor read-write)))
所以我创建了一个 B [b] 的实例和另一个引用 [b] 的 A [a] 的实例。然后我有以下规则:
(defrule myrule
?b <- (object (is-a B))
?a <- (object (is-a A)
(TheB ?y&:(= (str-compare (send ?b get-Id) (send ?y get-Id)) 0)))
=>
(printout t ?y " " ?b crlf)
(printout t (type ?y) " " (type ?b) crlf)
(printout t (eq ?y ?b) crlf)
(printout t ?a "->" (send ?a get-TheB) crlf)
)
当我 运行 我得到以下输出:
[b] <Instance-b>
B B
FALSE
<Instance-a>->[b]
谁能解释一下一个和另一个有什么区别?为什么一个有“<>”,另一个有“[]”,但类型是一样的?我必须比较 id 吗?我无法从 []?
获得 <>非常感谢您的关注。
您可以通过名称(字符串)或地址(指向实例的指针)来引用实例。向实例地址发送消息比向实例名称发送消息稍快,因为不涉及将名称转换为地址的查找。无论您使用名称还是地址来确定有效实例的类型,结果都是相同的(实例的class)。在您的示例中,[b] 是实例的名称,
与其定义您自己的 ID 槽以使一个实例指向另一个实例,不如使用实例的名称:
CLIPS (6.31 2/3/18)
CLIPS>
(defclass A
(is-a USER)
(role concrete)
(single-slot TheB
(type INSTANCE)
(allowed-classes B)
(create-accessor read-write)))
CLIPS>
(defclass B
(is-a USER)
(role concrete))
CLIPS>
(defrule myrule
?b <- (object (is-a B) (name ?name))
?a <- (object (is-a A) (TheB ?name))
=>
(printout t ?a " " ?b crlf)
(printout t ?name crlf)
(printout t (eq ?b ?name) crlf)
(printout t (eq ?b (instance-address ?name)) crlf))
CLIPS> (make-instance [b] of B)
[b]
CLIPS> (make-instance [a] of A (TheB [b]))
[a]
CLIPS> (agenda)
0 myrule: [b],[a]
For a total of 1 activation.
CLIPS> (run)
<Instance-a> <Instance-b>
[b]
FALSE
TRUE
CLIPS>