r4625: Auto commit for Debian build
[hyperobject.git] / rules.lisp
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          rules.lisp
6 ;;;; Purpose:       Slot and Class rules
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Apr 2000
9 ;;;;
10 ;;;; $Id: rules.lisp,v 1.31 2003/04/24 20:30:11 kevin Exp $
11 ;;;;
12 ;;;; This file is Copyright (c) 2000-2002 by Kevin M. Rosenberg
13 ;;;;
14 ;;;; *************************************************************************
15  
16 (in-package :hyperobject)
17
18 (eval-when (:compile-toplevel :execute)
19   (declaim (optimize (speed 2) (safety 2) (compilation-speed 0) (debug 2))))
20
21 ;;; Slot accessor and class rules
22
23 (defclass rule ()
24   ((name :initarg :name :initform nil :accessor name)
25    (dependants :initarg :dependants :initform nil :accessor dependants)
26    (volatile :initarg :volatile :initform nil :accessor volatile)
27    (access-slots :initarg :access-slots :initform nil :accessor access-slots)
28    (source-code :initarg :source-code :initform nil :accessor source-code)
29    (func :initform nil :initarg :func :accessor func)))
30
31 (defun compile-rule (source-code dependants volatile cl)
32   (let ((access (appendnew dependants volatile)))
33     (compile nil
34              (eval
35               `(lambda (obj)
36                  (when (every #'(lambda (x) (slot-boundp obj x))
37                               (quote ,dependants))
38                    (with-slots ,access obj
39                      ,@source-code)))))))
40   
41 (defun finalize-rules (cl)
42   (let* ((direct-rules (direct-rules cl))
43          (rules '()))
44     (dolist (rule direct-rules)
45       (destructuring-bind (name (&key dependants volatile) &rest source-code)
46           rule
47         (setf dependants (mklist dependants)
48               volatile (mklist volatile))
49         (push
50          (make-instance 'rule :name name :dependants dependants
51                         :volatile volatile :source-code source-code
52                         :access-slots (appendnew dependants volatile)
53                         :func (compile-rule
54                                source-code dependants volatile cl))
55          rules)))
56     (setf (rules cl) (nreverse rules))))
57
58
59 (defun fire-class-rules (cl obj slot)
60   "Fire all class rules. Called after a slot is modified."
61   (let ((name (slot-definition-name slot)))
62     (dolist (rule (rules cl))
63       (when (find name (dependants rule))
64         (cmsg-c :debug "firing rule: ~W" (source-code rule))
65         (funcall (func rule) obj)))))
66
67
68 #+ignore
69 (defmethod (setf slot-value-using-class) :around
70     (new-value (cl hyperobject-class) obj (slot hyperobject-esd))
71   ;; this does not work on gerd-pcl in cmu 18e+
72   (esd-value-constraint slot)
73   (call-next-method))
74
75 ;; required for cmucl18e+ work-around
76 (defun do-svuc (new-value cl obj slot)
77
78   #+ignore
79   (cmsg-c :verbose "Setf slot value: class: ~s, obj: ~s, slot: ~s, value: ~s" cl (class-of obj) slot new-value)
80   
81   (let ((func (esd-value-constraint slot)))
82     (cond
83       ((and func (not (funcall func new-value)))
84        (warn "Rejected change to value of slot ~a of object ~a"
85              (slot-definition-name slot) obj)
86        (values (slot-value obj (slot-definition-name slot)) nil)
87       (t
88        (values new-value t))))))
89
90
91 (defmethod (setf slot-value-using-class) :around 
92     (new-value (cl hyperobject-class) obj (slot hyperobject-esd))
93   (multiple-value-bind (value changed-p) (do-svuc new-value cl obj slot)
94     (when changed-p
95       (setq value (call-next-method))
96       (when (direct-rules cl)
97         (fire-class-rules cl obj slot)))
98     value))
99     
100
101
102 #+ignore
103 (defmethod (setf slot-value-using-class) :around
104     (new-value (cl hyperobject-class) obj (slot hyperobject-esd))
105   (let ((value (call-next-method)))
106     (cmsg-c :verbose "slot value: class: ~s, obj: ~s, slot: ~s" cl (class-of obj) slot)
107     value))