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