I have an ontology where I define two classes this way:
(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)))
So I create an instance of B [b] and another of A [a] referencing [b]. Then I have the following rule:
(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)
)
When I run I obtain the following output:
[b] <Instance-b>
B B
FALSE
<Instance-a>->[b]
Can someone explain me what is the difference between one and another? Why one has "<>" and the other "[]" but the type is the same? Do I have to compare the id's necessarily? There is no way I can obtain the <> from the []?
Thank you very much for your attention.
答案 0 :(得分:0)
您可以通过名称(字符串)或地址(指向实例的指针)来引用实例。将消息发送到实例地址比将消息发送到实例名称要快一些,因为没有涉及将名称转换为地址的查找。无论使用名称还是地址来确定有效实例的类型,结果都是相同的(实例的类)。在您的示例中,[b]是实例的名称,&lt; Instance-b&gt;是实例的地址。与实例名称不同,实例地址不能以文本方式指定(尽管可以打印地址),因为它们指的是内存位置。
只需使用实例的名称,而不是定义自己的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>