r4689: Auto commit for Debian build
[hyperobject.git] / mop.lisp
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          mop.lisp
6 ;;;; Purpose:       Metaobject Protocol Interface
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Apr 2000
9 ;;;;
10 ;;;; This metaclass as functions to classes to allow display
11 ;;;; in Text, HTML, and XML formats. This includes hyperlinking
12 ;;;; capability and sub-objects.
13 ;;;;
14 ;;;; $Id: mop.lisp,v 1.66 2003/04/29 09:25:55 kevin Exp $
15 ;;;;
16 ;;;; This file is Copyright (c) 2000-2002 by Kevin M. Rosenberg
17 ;;;;
18 ;;;; *************************************************************************
19  
20 (in-package :hyperobject)
21
22 (eval-when (:compile-toplevel :execute)
23   (declaim (optimize (speed 2) (safety 2) (compilation-speed 0) (debug 2))))
24
25 ;; Main class
26
27 (defclass hyperobject-class (standard-class)
28   ( ;; slots initialized in defclass
29    (user-name :initarg :user-name :type string :initform nil
30               :accessor user-name
31               :documentation "User name for class")
32    (default-print-slots :initarg :default-print-slots :type list :initform nil
33                         :accessor default-print-slots
34                         :documentation "Defaults slots for a view")
35    (description :initarg :description :initform nil
36                 :accessor description
37                 :documentation "Class description")
38    (version :initarg :version :initform nil
39             :accessor version
40             :documentation "Version number for class")
41    (sql-name :initarg :sql-name :initform nil)
42
43    ;;; The remainder of these fields are calculated one time
44    ;;; in finalize-inheritence.
45    
46    (subobjects :initform nil :accessor subobjects
47                :documentation
48                "List of fields that contain a list of subobjects objects.")
49    (hyperlinks :type list :initform nil :accessor hyperlinks
50                :documentation "List of fields that have hyperlinks")
51    (direct-rules :type list :initform nil :initarg :direct-rules
52                  :accessor direct-rules
53                  :documentation "List of rules to fire on slot changes.")
54    (class-id :type integer :initform nil
55              :accessor class-id
56              :documentation "Unique ID for the class")
57    (default-view :initform nil :initarg :default-view :accessor default-view
58                  :documentation "The default view for a class")
59
60    ;; SQL commands
61    (create-table-cmd :initform nil :reader create-table-cmd)
62    (create-indices-cmds :initform nil :reader create-index-cmds)
63    (drop-table-cmd :initform nil :reader drop-table-cmd)
64
65    (views :type list :initform nil :initarg :views :accessor views
66           :documentation "List of views")
67    (rules :type list :initform nil :initarg :rules :accessor rules
68           :documentation "List of rules")
69    )
70   (:documentation "Metaclass for Markup Language classes."))
71
72 (defclass subobject ()
73   ((name-class :type symbol :initform nil :initarg :name-class :reader name-class)
74    (name-slot :type symbol :initform nil :initarg :name-slot :reader name-slot)
75    (lookup :type (or function symbol) :initform nil :initarg :lookup :reader lookup)
76    (lookup-keys :type list :initform nil :initarg :lookup-keys
77                 :reader lookup-keys))
78   (:documentation "Contains subobject information"))
79
80
81 (defmethod print-object ((obj subobject) (s stream))
82   (print-unreadable-object (obj s :type t :identity t)
83     (format s "~S" (name obj))))
84
85 (defclass hyperlink ()
86   ((name :type symbol :initform nil :initarg :name :reader name)
87    (lookup
88     ;; The type specifier seems to break sbcl
89     :type (or function symbol)
90     ;;    :type t
91     :initform nil :initarg :lookup :reader lookup)
92    (link-parameters :type list :initform nil :initarg :link-parameters
93                     :reader link-parameters)))
94
95 (defmethod print-object ((obj hyperlink) (s stream))
96   (print-unreadable-object (obj s :type t :identity t)
97     (format s "~S" (name obj))))
98
99 #+(or cmu scl sbcl)
100 (defmethod validate-superclass ((class hyperobject-class) (superclass standard-class))
101   t)
102
103 (defmethod finalize-inheritance :after ((cl hyperobject-class))
104   (init-hyperobject-class cl)
105   )
106
107 (eval-when (:compile-toplevel :load-toplevel :execute)
108   (when (>= (length (generic-function-lambda-list
109                      (ensure-generic-function
110                       'compute-effective-slot-definition)))
111             3)
112     (pushnew :ho-normal-cesd cl:*features*))
113   
114     (when (>= (length (generic-function-lambda-list
115                        (ensure-generic-function
116                         'direct-slot-definition-class)))
117             3)
118       (pushnew :ho-normal-dsdc cl:*features*))
119     
120     (when (>= (length (generic-function-lambda-list
121                        (ensure-generic-function
122                         'effective-slot-definition-class)))
123               3)
124       (pushnew :ho-normal-esdc cl:*features*)))
125
126 ;; Slot definitions
127 (defmethod direct-slot-definition-class ((cl hyperobject-class)
128                                          #+ho-normal-dsdc &rest iargs)
129   (find-class 'hyperobject-dsd))
130
131 (defmethod effective-slot-definition-class ((cl hyperobject-class) 
132                                             #+ho-normal-esdc &rest iargs)
133   (find-class 'hyperobject-esd))
134
135
136 ;;; Slot definitions
137
138 (eval-when (:compile-toplevel :load-toplevel :execute)
139   (defmacro process-class-option (slot-name &optional required)
140     #+lispworks
141     `(defmethod clos:process-a-class-option ((class hyperobject-class)
142                                              (name (eql ,slot-name))
143                                              value)
144       (when (and ,required (null value))
145         (error "hyperobject class slot ~A must have a value" name))
146       (list name `',value))
147     #+(or allegro sbcl cmu scl)
148     (declare (ignore slot-name required))
149     )
150
151   (defmacro process-slot-option (slot-name)
152     #+lispworks
153     `(defmethod clos:process-a-slot-option ((class hyperobject-class)
154                                             (option (eql ,slot-name))
155                                             value
156                                             already-processed-options
157                                             slot)
158       (list* option `',value already-processed-options))
159     #-lispworks
160     (declare (ignore slot-name))
161     )
162   
163   (dolist (option *class-options*)
164     (eval `(process-class-option ,option)))
165   (dolist (option *slot-options*)
166     (eval `(process-slot-option ,option)))
167
168   (eval
169    `(defclass hyperobject-dsd (standard-direct-slot-definition)
170      (,@(mapcar #'(lambda (x)
171                     `(,(intern (symbol-name x))
172                       :initform nil))
173                 *slot-options-no-initarg*)
174       ,@(mapcar #'(lambda (x)
175                     `(,(intern (symbol-name x))
176                       :initarg
177                       ,(intern (symbol-name x) (symbol-name :keyword))
178                       :initform nil
179                       :accessor
180                       ,(intern (concatenate 'string
181                                             (symbol-name :dsd-)
182                                             (symbol-name x)))))
183                 *slot-options*))))
184   (eval
185    `(defclass hyperobject-esd (standard-effective-slot-definition)
186      (,@(mapcar #'(lambda (x)
187                     `(,(intern (symbol-name x))
188                       :initarg
189                       ,(intern (symbol-name x) (symbol-name :keyword))
190                       :initform nil
191                       :accessor
192                       ,(intern (concatenate 'string
193                                             (symbol-name :esd-)
194                                             (symbol-name x)))))
195                 (append *slot-options* *slot-options-no-initarg*)))))
196   ) ;; eval-when
197
198 (defun intern-in-keyword (obj)
199   (cond
200     ((null obj)
201      nil)
202     ((eq t obj)
203      t)
204     ((atom obj)
205      (intern (symbol-name obj) (find-package 'keyword)))
206     ((consp obj)
207      (cons (intern-in-keyword (car obj) ) (intern-in-keyword (cdr obj))))
208     (t
209      obj)))
210
211 (defun canonicalize-value-type (vt)
212   (typecase vt
213     (atom
214      (ensure-keyword vt))
215     (cons
216      (cons (ensure-keyword (car vt)) (cdr vt)))
217     (t
218      t)))
219
220 #+ignore
221 (defmethod compute-effective-slot-definition :around ((cl hyperobject-class) #+ho-normal-cesd name dsds)
222   #+allegro (declare (ignore name))
223   (let* ((dsd (car dsds))
224          (value-type (canonicalize-value-type (slot-value dsd 'value-type))))
225     (multiple-value-bind (sql-type length) (value-type-to-sql-type value-type)
226       (setf (slot-value dsd 'sql-type) sql-type)
227       (setf (slot-value dsd 'type) (value-type-to-lisp-type value-type))
228       (let ((ia (compute-effective-slot-definition-initargs cl #+lispworks name dsds)))
229         (apply
230          #'make-instance 'hyperobject-esd 
231          :value-type value-type
232          :sql-type sql-type
233          :length length
234          :print-formatter (slot-value dsd 'print-formatter)
235          :subobject (slot-value dsd 'subobject)
236          :hyperlink (slot-value dsd 'hyperlink)
237          :hyperlink-parameters (slot-value dsd 'hyperlink-parameters)
238          :description (slot-value dsd 'description)
239          :user-name (slot-value dsd 'user-name)
240          :index (slot-value dsd 'index)
241          :value-constraint (slot-value dsd 'value-constraint)
242          :null-allowed (slot-value dsd 'null-allowed)
243          ia)))))
244
245 (defmethod compute-effective-slot-definition :around ((cl hyperobject-class) #+ho-normal-cesd name dsds)
246   #+ho-normal-cesd (declare (ignore name))
247   (let ((esd (call-next-method))
248         (value-type (canonicalize-value-type (slot-value (car dsds) 'value-type))))
249     (multiple-value-bind (sql-type length) (value-type-to-sql-type value-type)
250       (setf (slot-value esd 'sql-type) sql-type)
251       (setf (slot-value esd 'length) length)
252       (setf (slot-value esd 'type) (value-type-to-lisp-type value-type))
253       (setf (slot-value esd 'value-type) value-type)
254       esd)))
255
256
257 #+ho-normal-cesd
258 (setq cl:*features* (delete :ho-normal-cesd cl:*features*))
259 #+ho-normal-dsdc
260 (setq cl:*features* (delete :ho-normal-dsdc cl:*features*))
261 #+ho-normal-esdc
262 (setq cl:*features* (delete :ho-normal-esdc cl:*features*))
263
264 (defun value-type-to-lisp-type (value-type)
265   (case (if (atom value-type)
266             value-type
267             (car value-type))
268     ((:string :cdata :varchar :char)
269      '(or null string))
270     (:character
271      '(or null character))
272     (:fixnum
273      '(or null fixnum))
274     (:boolean
275      '(or null boolean))
276     (:integer
277      '(or null integer))
278     ((:float :single-float)
279      '(or null single-float))
280     (:double-float
281      '(or null double-float))
282     (otherwise
283      t)))
284
285 (defun value-type-to-sql-type (value-type)
286   "Return two values, the sql type and field length."
287   (let ((type (if (atom value-type)
288                   value-type
289                   (car value-type)))
290         (length (when (consp value-type)
291                   (cadr value-type))))
292     (values
293      (case type
294        ((:string :cdata)
295         :string)
296        ((:fixnum :integer)
297         :integer)
298        (:boolean
299         :boolean)
300        ((:float :single-float)
301         :single-float)
302        (:double-float
303         :double-float)
304        (otherwise
305         :text))
306      length)))
307
308 ;;;; Class initialization function
309
310 ;; defines a slot-unbound method for class and slot-name, fills
311 ;; the slot by calling reader function with the slot values of
312 ;; the instance's reader-keys
313 (defmacro def-lazy-reader (class slot-name reader &rest reader-keys)
314   (let* ((the-slot-name (gensym))
315          (the-class (gensym))
316          (the-instance (gensym))
317          (keys '()))
318     (dolist (key reader-keys)
319       (push (list 'slot-value the-instance (list 'quote key)) keys))
320     (setq keys (nreverse keys))
321     `(defmethod slot-unbound (,the-class (,the-instance ,class)
322                               (,the-slot-name (eql ',slot-name)))
323       (declare (ignore ,the-class))
324       (setf (slot-value ,the-instance ,the-slot-name) (,reader ,@keys)))))
325
326
327 #+lispworks
328 (defun intern-eql-specializer (slot)
329   `(eql ,slot))
330
331 #+(or sbcl cmu lispworks)
332 (defun ensure-lazy-reader (class-name slot-name reader &rest reader-keys)
333   (let ((keys nil)
334         (gf (ensure-generic-function 'slot-unbound)))
335     (dolist (key reader-keys)
336       (push (list 'slot-value 'the-instance (list 'quote key)) keys))
337     (setq keys (nreverse keys))
338     (multiple-value-bind (method-lambda init-args-values)
339         (make-method-lambda
340          gf
341          (class-prototype (generic-function-method-class gf))
342          #-lispworks
343          `(lambda (the-class the-instance the-slot-name)
344            (declare (ignore the-class))
345            (setf (slot-value the-instance the-slot-name) (,reader ,@keys)))
346          #+lispworks
347          '(the-class the-instance the-slot-name)
348          #+lispworks
349          nil
350          #+lispworks
351          `(setf (slot-value the-instance the-slot-name) (,reader ,@keys))
352          nil)
353       (add-method gf
354                   (apply
355                    #'make-instance (generic-function-method-class gf)
356                    ':specializers (list (class-of (find-class class-name))
357                                         (find-class class-name)
358                                         (intern-eql-specializer slot-name))
359                    ':lambda-list '(the-class the-instance the-slot-name)
360                    ':function (compile nil method-lambda)
361                    init-args-values)))))
362
363 (defun finalize-subobjects (cl)
364   "Process class subobjects slot"
365   (setf (subobjects cl)
366         (let ((subobjects '()))
367           (dolist (slot (class-slots cl))
368             (let-when
369              (subobj-def (esd-subobject slot))
370              (let ((subobject
371                     (make-instance 'subobject
372                                    :name-class (class-name cl)
373                                    :name-slot (slot-definition-name slot)
374                                    :lookup (if (atom subobj-def)
375                                                subobj-def
376                                                (car subobj-def))
377                                    :lookup-keys (if (atom subobj-def)
378                                                     nil
379                                                     (cdr subobj-def)))))
380                (unless (eq (lookup subobject) t)
381                  #-(or sbcl cmu lispworks)
382                  (eval
383                   `(hyperobject::def-lazy-reader ,(name-class subobject)
384                     ,(name-slot subobject) ,(lookup subobject)
385                     ,@(lookup-keys subobject)))
386                  #+(or sbcl cmu lispworks)
387                  (apply #'ensure-lazy-reader 
388                         (name-class subobject) (name-slot subobject)
389                         (lookup subobject) (lookup-keys subobject)))
390                (push subobject subobjects))))
391           ;; sbcl/cmu reverse class-slots compared to the defclass form
392           ;; so re-reverse on cmu/sbcl
393           #+(or cmu sbcl) subobjects
394           #-(or cmu sbcl) (nreverse subobjects)
395           )))
396
397 (defun finalize-documentation (cl)
398   "Calculate class documentation slot"
399   (awhen (slot-value cl 'user-name)
400          (setf (slot-value cl 'user-name)
401                (etypecase (slot-value cl 'user-name)
402                  (cons (car it))
403                  ((or string symbol) it))))
404   (awhen (slot-value cl 'description)
405          (setf (slot-value cl 'description)
406                (etypecase (slot-value cl 'description)
407                  (cons (car it))
408                  ((or string symbol) it))))
409
410   (let ((*print-circle* nil))
411     (setf (documentation (class-name cl) 'class)
412           (format nil "Hyperobject~A~A~A~A"
413                   (aif (user-name cl)
414                        (format nil ": ~A" it ""))
415                   (aif (description cl)
416                        (format nil "~%Class description: ~A" it) "")
417                   (aif (subobjects cl)
418                        (format nil "~%Subobjects:~{ ~A~}" (mapcar #'name-slot it)) "")
419                   (aif (default-print-slots cl)
420                        (format nil "~%Default print slots:~{ ~A~}" it) "")
421                   ))))
422
423 (defun finalize-hyperlinks (cl)
424   (let ((hyperlinks '()))
425     (dolist (esd (class-slots cl))
426       (awhen (slot-value esd 'hyperlink)
427              (push
428               (make-instance 'hyperlink
429                              :name (slot-definition-name esd)
430                              :lookup it
431                              :link-parameters (slot-value esd 'hyperlink-parameters))
432               hyperlinks)))
433     ;; cmu/sbcl reverse class-slots compared to the defclass form
434     ;; hyperlinks is already reversed from the dolist/push loop, so re-reverse on sbcl/cmu
435     #-(or cmu sbcl) (setq hyperlinks (nreverse hyperlinks))
436     (setf (slot-value cl 'hyperlinks) hyperlinks)))
437
438 (defun init-hyperobject-class (cl)
439   "Initialize a hyperobject class. Calculates all class slots"
440   (finalize-subobjects cl)
441   (finalize-views cl)
442   (finalize-hyperlinks cl)
443   (finalize-sql cl)
444   (finalize-rules cl)
445   (finalize-documentation cl))
446
447
448 ;;;; *************************************************************************
449 ;;;;  Metaclass Slot Accessors
450 ;;;; *************************************************************************
451
452 (defun find-slot-by-name (cl name)
453   (find name (class-slots cl) :key #'slot-definition-name))
454
455 (defun hyperobject-class-user-name (obj)
456   (awhen (user-name (class-of obj))
457          (if (consp it)
458              (car it)
459              it)))
460
461 (defun hyperobject-class-subobjects (obj)
462   (subobjects (class-of obj)))
463
464 (defun hyperobject-class-hyperlinks (obj)
465   (hyperlinks (class-of obj)))
466
467 (defun hyperobject-class-slots (obj)
468   ;; cmucl/sbcl reverse class-slots
469   #+(or cmu sbcl) (reverse (class-slots (class-of obj)))
470   #-(or cmu sbcl) (class-slots (class-of obj)))
471