Demons
The next step is to decide what form attachments should take. In the parlance of object-oriented programming, a demon is something that keeps watch over an object and does something whenever it changes. The things we are going to store in the attachments of database entries will be demons, defined thus:
(defstruct demon (pre-action #'nilfn) (post-action #'nilfn) (self-destruct #'nilfn)) window (defun nilfn (&rest args) (ignore args))
If a demon is the sort that runs a graphic object, its window field will contain the window in which the graphic object resides. Demons do not necessarily have to run graphic objects -- if one doesn't, its window field will simply be nil.
The three fields of a demon structure will contain functions (more precisely, closures) that, when called, perform certain actions. Note that the first three functions in Listing Two all have the same form. The first one, pre-update, is the function called by db-update just before it overwrites a database entry. The effect of calling pre-uPdate for an entry is to call the pre-action function of each of its demons. The functions post-update and delete-attachments behave analogously.
(defun pre-update (entry) (dolist (d (db-entry-attachments entry)) (funcall (demon-pre-action d)))) (defun post-update (entry) (dolist (d (db-entry-attachments entry)) (funcall (demon-post-action d)))) (defun delete-update (entry) (dolist (d (db-entry-attachments entry)) (funcall (demon-self-destruct d)))) (defun add-demon (db key access-fn pre-fn post-fn) (let ((entry (gethash key db)) (d (make-demon))) (setf (demon-pre-action d) #'(lambda () (funcall pre-fn (funcall access-fn (db-entry-value entry))))) (setf (demon-post-action d) #'(lambda() (funcall post-fn (funcall access-fn (db-entry-value entry))))) (setf (demon-self-destruct d) #'(lambda() (delete d(db-entry-attachments entry)))) (push d (db-entry-attachments entry)) d))
Listing Three shows how demons are used. We'll attach a demon to the temperature entry of our-db, (in accordance with the greenhouse effect) sets the temp-in-50-years entry to be 10 degrees' higher. There is no need for this end after only one cycle. If we wanted, we could attach further demons to temp-in-50-years, which would update sea-level-in-50-years, and so on.
> (setq d (let ((entry (gethas 'temperature our-db))) (make-demon :post-action #'(lambda () (db-update our-db 'temp-in-50-years (+ (db-entry-value entry) 10)))))) #S(DEMON...) > (push d(db-entry-attachments (gethash 'temperature our-db))) (#S(DEMON...)) > (db-update our-db 'tempature 98) 98 > (db-query out-db 'temp-in-50-years) 108
The remaining function in Listing Two, add-demon, does more or less what we did in Listing Three. This function looks a bit imposing, but it's long only because it's rather repetitive.
One thing that might seem confusing is the purpose of the parameter access-fn. We need it because the value field of the entry which we attach a demon might contain a structure instead of a simple atom, as temperature did. In that case we may want the demon to be attached to a subpart of the structure. The access-fn specifies where the demon should attached to within the entry's value field. If we had used add-demon to attach a demon to the temperature entry as the access-fn since we are looking at the value field itself. If we attaching a demon to an entry whose value field contained a list and wanted the demon to react specifically to the car of the list, we'd see #'car as the access-fn.
Note that add-demon fills new demon's fields with closures, not merely functions. The pre-action and post-action functions are closed over the entry to they are attached, and the self-destruct function is closed over the demon itself.