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