删除嵌套实例



给定以下类层次结构:

(defclass ATOM (is-a USER))
(defclass ONE_CHILD (is-a USER)
    (slot next
        (type INSTANCE)))
(defclass MANY_CHILDREN (is-a USER)
    (multislot next
        (type INSTANCE)))

我想递归地删除以下实例数据:

(definstances EXAMPLE_DATA
    (instance-a of ATOM)
    (instance-b of ONE_CHILD
        (next (make-instance of ATOM)))
    (instance-c of MANY_CHILDREN
        (next (make-instance of ATOM)
              (make-instance of ATOM)
              (make-instance of ATOM)))
    (instance-d of MANY_CHILDREN)
)

如果我天真地给每个命名实例send一条delete消息,那么实例本身就会被删除,而嵌套实例则不会。虽然我确信它已经存在,但我在CLIPS 6.3用户手册或基本编程指南中找不到描述如何执行此操作的文档。

以下命令序列可预测地在完成时产生一组非空实例。为了执行递归删除,我可以有什么不同?

CLIPS> (reset)
CLIPS> (instances)
[initial-object] of INITIAL-OBJECT
[instance-a] of ATOM
[instance-b] of ONE_CHILD
[gen29] of ATOM
[instance-c] of MANY_CHILDREN
[gen30] of ATOM
[gen31] of ATOM
[gen32] of ATOM
[instance-d] of MANY_CHILDREN
For a total of 9 instances.
CLIPS> (send [instance-a] delete)
TRUE
CLIPS> (send [instance-b] delete)
TRUE
CLIPS> (send [instance-c] delete)
TRUE
CLIPS> (send [instance-d] delete)
TRUE
CLIPS> (instances)
[initial-object] of INITIAL-OBJECT
[gen29] of ATOM
[gen30] of ATOM
[gen31] of ATOM
[gen32] of ATOM
For a total of 5 instances.

为每个具有子实例的类创建删除消息的before处理程序:

(defmessage-handler ONE_CHILD delete before ()
   (send ?self:next delete))
(defmessage-handler MANY_CHILDREN delete before ()
   (progn$ (?c ?self:next)
      (send ?c delete)))

最新更新