获取对象的槽值

时间:2016-10-05 16:35:30

标签: clips

我需要从可能包含其他实例的实例中检索插槽值(传递插槽名称)。 例如:

(defclass MAINCONTROLLER (is-a USER)
    (slot uuid
        (type STRING))
    (slot param
        (type INSTANCE))
    (multislot zones
        (type INSTANCE))        
    (slot state
        (allowed-values a b c))
    (slot pump
        (allowed-values on off)))

(make-instance mainController of MAINCONTROLLER
    (uuid "myController123")
    (param [param-mainController])
    (zones [zone1] [zone2])                  
    (state a)                                       
    (pump on))

Slot named" param"包含一个名为[param-mainController]的实例。

CLIPS文档建议使用带有put参数的send命令检索槽值。 我尝试使用泛型函数只通过传递slotname来检索参数。

(defmessage-handler USER get-param (?param-name)
    (printout t "Slot value: " ?self:?param-name crlf))

但执行它我得到:

  

(发送[mainController] get-param state)

     

[MSGPASS2]函数send中没有这样的实例mainController。

     

FALSE

有些问题:

1)我是否需要始终为需要使用send命令读取的每个插槽定义(创建访问器读取)?

2)您能否提供一些示例,其中包含从实例中检索插槽值的最佳实践?

谢谢你, NIC

1 个答案:

答案 0 :(得分:1)

默认情况下,为插槽创建get-和put-处理程序。要检索特定的广告位,请在类的消息处理程序和?self:< symbol>之外使用(发送< instance> get-< symbol>)内。要检索插槽名称存储在变量中的插槽,请使用(发送< instance>(sym-cat get-< variable>))。使用?self:< variable>是插槽速记引用的无效语法。

CLIPS> (clear)
CLIPS> 
(defclass MAINCONTROLLER (is-a USER)
    (slot uuid
        (type STRING))
    (slot param
        (type INSTANCE))
    (multislot zones
        (type INSTANCE))        
    (slot state
        (allowed-values a b c))
    (slot pump
        (allowed-values on off)))
CLIPS> 
(defmessage-handler MAINCONTROLLER myprint ()
   (printout t ?self:state crlf))
CLIPS>    
(deffunction retrieve-slot (?ins ?slot-name)
   (printout t (send ?ins (sym-cat get- ?slot-name)) crlf))
CLIPS>        
(make-instance mainController of MAINCONTROLLER
    (uuid "myController123")
    (param [param-mainController])
    (zones [zone1] [zone2])                  
    (state a)                                       
    (pump on))
[mainController]
CLIPS> (send [mainController] get-state)
a
CLIPS> (send [mainController] myprint)
a
CLIPS> (retrieve-slot [mainController] state)
a
CLIPS>