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