Download PORK Object System Programmers` Guide

Transcript
2.3 Using Inverse Relations
Let us assume a denitions of classes called resource and resource-group. Now,
resource-group maintains a list of its members. To make individual resources
aware of which groups they belong to, we will dene a slot groups in the resource
class to be the inverse of the slot members in the class resource-group. When
either slot gets modied, the other is automatically changed, too.
(defframe resource (frame-object)
((total-capacity
:initarg :total-capacity
:reader total-capacity)
(groups
:initform nil
:reader groups
:adder add-group
:remover remove-group
:inverse (:adder add-member
:remover remove-member))))
(defframe resource-group (resource)
((members
:initarg :members
:initform nil
:reader members
:adder add-member
:remover remove-member
:inverse (:adder add-group
:remover remove-group))))
Now the functions add-member and add-group are each others' inverse adders,
and correspondingly remove-member and remove-group are each others' inverse
removers. Notice how the denition of resource-group was changed to reect
the introduction of the inverse relation.
If one wants to use single-valued slots (i.e. ordinary CLOS slots) with inverse
updating, it is possible by specifying an accessor and an inverse accessor (instead
of adders and removers). For example, if we have an object class to represent a
9