我需要在CLIPS中建模一个OneToMany关系。 我有可能与区域相关的房间。 所以我写道:
(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(在处理程序内),它只是设置房间,而不是添加它。
有没有办法在多时隙字段中增加元素?
由于 NIC
答案 0 :(得分: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))