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