Menu

How to get instance from instance-name ?

Help
2013-06-03
2013-06-04
  • Christine DG

    Christine DG - 2013-06-03

    I would like to embedd an instance [a] of class A in an instance [b] of Class B and access [a] from [b]. But I haven't find a way !
    (make-instance) returns the instance-name ; how can I reach the instance itself ?

    CLIPS> (defclass A (is-a USER) (role concrete))
    CLIPS> (defclass B (is-a USER) (role concrete)  
        (slot A
            (type INSTANCE)
            (allowed-classes A)
            (default ?NONE)
        ) 
    )
    CLIPS> (make-instance of B (A (make-instance of A)))
    CLIPS> (instances)
    [initial-object] of INITIAL-OBJECT
    [gen1] of B
    [gen2] of A
    For a total of 3 instances.
    CLIPS> (send [gen1] print)
    [gen1] of B
    (A [gen2])
    CLIPS> (send [gen1]:A print)
    [MSGFUN1] No applicable primary message-handlers found for print.
    FALSE
    CLIPS> (send [gen2] print)
    [gen2] of A
    CLIPS> (class [gen1])
    B
    CLIPS> (class [gen1]:A)
    SYMBOL
    CLIPS> 
    
     
  • Gary Riley

    Gary Riley - 2013-06-04

    Short hand slot references work with variables in appropriate situations (such as ?self:A in a message-handler). The syntax you've used to retrieve a slot from an instance name, [gen1]:A, is not supported. Use (send [gen1] get-A) instead. If you do want to convert an instance name to an instance address, use the instance-address function.

    CLIPS> (defclass A (is-a USER) (role concrete))
    CLIPS> 
    (defclass B (is-a USER) (role concrete)  
        (slot A
            (type INSTANCE)
            (allowed-classes A)
            (default ?NONE)))
    CLIPS> (make-instance of B (A (make-instance of A)))
    [gen1]
    CLIPS> (instances)
    [initial-object] of INITIAL-OBJECT
    [gen1] of B
    [gen2] of A
    For a total of 3 instances.
    CLIPS> (send [gen1] print)
    [gen1] of B
    (A [gen2])
    CLIPS> (send (send [gen1] get-A) print)
    [gen2] of A
    CLIPS> (send [gen2] print)
    [gen2] of A
    CLIPS> (class [gen1])
    B
    CLIPS> (class (send [gen1] get-A))
    A
    CLIPS> (instance-address [gen2])
    <Instance-gen2>
    CLIPS> (instance-address [gen1])
    <Instance-gen1>
    CLIPS> 
    
     

    Last edit: Gary Riley 2013-06-04
  • Christine DG

    Christine DG - 2013-06-04

    Thank you Gary,

    I had seen this syntax in examples not in the reference manual.
    I understand that it is better to avoid it!

     

Log in to post a comment.