CLIPS instance <> and []? Instance eq to itself FALSE?

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.

Upvotes: 0

Views: 247

Answers (1)

Gary Riley
Gary Riley

Reputation: 10757

You can refer to instances either by name (a string) or address (a pointer to the instance). Sending a message to an instance address is slightly faster than sending a message to an instance name since there is no lookup involved to convert the name to an address. Whether you use a name or address to determine the type of a valid instance, the result will be the same (the class of the instance). In your example, [b] is the name of the instance and <Instance-b> is the address of the instance. Unlike instance names, instance addresses can't be specified textually (although the address can be printed) since they refer to a memory location.

Rather than defining your own ID slot to have one instance point to another, just use the name of the instance:

         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>

Upvotes: 1

Related Questions