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