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