删除嵌套实例

Delete Nested Instances

给定以下 class 层次结构:

(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.

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

(defmessage-handler ONE_CHILD delete before ()
   (send ?self:next delete))

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