OneToMany 关系和在 CLIPS 中添加元素的方法

OneToMany relationship and method to add an element in CLIPS

我需要在 CLIPS 中建立一个 OneToMany 关系模型。 我有可能与某个区域相关的 ROOMS。 所以我写道:

(defclass ROOM (is-a USER)
(slot id
    (type SYMBOL))    
(slot zone
    (type SYMBOL)))


(defclass ZONE (is-a USER)
(slot id-zone
    (type SYMBOL))   
(multislot rooms
    (type INSTANCE) (create-accessor read-write))
(slot room-count
    (type INTEGER)))

只要我实例化了一个新房间,它就应该被添加到一个区域(如果那个房间与之相关联)。 我写了这个处理程序:

(defmessage-handler ZONE add-room-to-zone (?room)
(bind ?self:rooms ?room)   ;#1
(bind ?self:room-count (length$ ?self:rooms))
(printout t "Room [" ?room "] added to zone [" ?self "]" crlf)))

执行此操作的规则是:

(defrule build-air-temperature-zone
?room <- (object (is-a ROOM) (id ?id) (id-zone ?id-zone-room))
(test (symbolp ?id))
?zone <- (object (is-a ZONE) (id-zone ?id-zone))
(test (eq ?id-zone ?id-zone-room))
=>
(send ?zone add-room-to-zone ?room)

如果我实例化多个房间,我只会看到一个实例添加到我的区域。 如果认为是因为#1(在处理程序内部),它只是设置了房间,并没有添加它。

有没有办法增加多槽字段中的元素?

谢谢 尼克

像这样定义您的消息处理程序:

(defmessage-handler ZONE add-room-to-zone (?room)
  (bind ?self:rooms ?self:rooms ?room)   
  (bind ?self:room-count (length$ ?self:rooms))
  (printout t "Room [" ?room "] added to zone [" ?self "]" crlf))

或者这样:

(defmessage-handler ZONE add-room-to-zone (?room)
  (slot-direct-insert$ rooms 1 ?room)
  (bind ?self:room-count (length$ ?self:rooms))
  (printout t "Room [" ?room "] added to zone [" ?self "]" crlf))