1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
\r
2 ;;;; *************************************************************************
\r
3 ;;;; FILE IDENTIFICATION
\r
6 ;;;; Purpose: Metaobject Protocol Interface
\r
7 ;;;; Programmer: Kevin M. Rosenberg
\r
8 ;;;; Date Started: Apr 2000
\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
14 ;;;; $Id: mop.lisp,v 1.74 2003/05/17 22:24:38 kevin Exp $
\r
16 ;;;; This file is Copyright (c) 2000-2003 by Kevin M. Rosenberg
\r
17 ;;;; *************************************************************************
\r
19 (in-package :hyperobject)
\r
21 (eval-when (:compile-toplevel :execute)
\r
22 (declaim (optimize (speed 2) (safety 2) (compilation-speed 0) (debug 2))))
\r
26 (defclass hyperobject-class (standard-class)
\r
27 ( ;; slots initialized in defclass
\r
28 (user-name :initarg :user-name :type string :initform nil
\r
30 :documentation "User name for class")
\r
31 (user-name-plural :initarg :user-name-plural :type string :initform nil
\r
32 :accessor user-name-plural
\r
33 :documentation "Plural user name for class")
\r
34 (default-print-slots :initarg :default-print-slots :type list :initform nil
\r
35 :accessor default-print-slots
\r
36 :documentation "Defaults slots for a view")
\r
37 (description :initarg :description :initform nil
\r
38 :accessor description
\r
39 :documentation "Class description")
\r
40 (version :initarg :version :initform nil
\r
42 :documentation "Version number for class")
\r
43 (sql-name :initarg :sql-name :initform nil)
\r
45 ;;; The remainder of these fields are calculated one time
\r
46 ;;; in finalize-inheritence.
\r
48 (subobjects :initform nil :accessor subobjects
\r
50 "List of fields that contain a list of subobjects objects.")
\r
51 (hyperlinks :type list :initform nil :accessor hyperlinks
\r
52 :documentation "List of fields that have hyperlinks")
\r
53 (direct-rules :type list :initform nil :initarg :direct-rules
\r
54 :accessor direct-rules
\r
55 :documentation "List of rules to fire on slot changes.")
\r
56 (class-id :type integer :initform nil
\r
58 :documentation "Unique ID for the class")
\r
59 (default-view :initform nil :initarg :default-view :accessor default-view
\r
60 :documentation "The default view for a class")
\r
63 (create-table-cmd :initform nil :reader create-table-cmd)
\r
64 (create-indices-cmds :initform nil :reader create-index-cmds)
\r
65 (drop-table-cmd :initform nil :reader drop-table-cmd)
\r
67 (views :type list :initform nil :initarg :views :accessor views
\r
68 :documentation "List of views")
\r
69 (rules :type list :initform nil :initarg :rules :accessor rules
\r
70 :documentation "List of rules")
\r
72 (:documentation "Metaclass for Markup Language classes."))
\r
74 (defclass subobject ()
\r
75 ((name-class :type symbol :initform nil :initarg :name-class :reader name-class)
\r
76 (name-slot :type symbol :initform nil :initarg :name-slot :reader name-slot)
\r
77 (lookup :type (or function symbol) :initform nil :initarg :lookup :reader lookup)
\r
78 (lookup-keys :type list :initform nil :initarg :lookup-keys
\r
79 :reader lookup-keys))
\r
80 (:documentation "Contains subobject information"))
\r
83 (defmethod print-object ((obj subobject) (s stream))
\r
84 (print-unreadable-object (obj s :type t)
\r
85 (format s "~S" (name-slot obj))))
\r
87 (defclass hyperlink ()
\r
88 ((name :type symbol :initform nil :initarg :name :reader name)
\r
90 ;; The type specifier seems to break sbcl
\r
91 :type (or function symbol)
\r
93 :initform nil :initarg :lookup :reader lookup)
\r
94 (link-parameters :type list :initform nil :initarg :link-parameters
\r
95 :reader link-parameters)))
\r
97 (defmethod print-object ((obj hyperlink) (s stream))
\r
98 (print-unreadable-object (obj s :type t :identity t)
\r
99 (format s "~S" (name obj))))
\r
101 #+(or cmu scl sbcl)
\r
102 (defmethod validate-superclass ((class hyperobject-class) (superclass standard-class))
\r
105 (defmethod finalize-inheritance :after ((cl hyperobject-class))
\r
106 (init-hyperobject-class cl)
\r
109 (eval-when (:compile-toplevel :load-toplevel :execute)
\r
110 (when (>= (length (generic-function-lambda-list
\r
111 (ensure-generic-function
\r
112 'compute-effective-slot-definition)))
\r
114 (pushnew :ho-normal-cesd cl:*features*))
\r
116 (when (>= (length (generic-function-lambda-list
\r
117 (ensure-generic-function
\r
118 'direct-slot-definition-class)))
\r
120 (pushnew :ho-normal-dsdc cl:*features*))
\r
122 (when (>= (length (generic-function-lambda-list
\r
123 (ensure-generic-function
\r
124 'effective-slot-definition-class)))
\r
126 (pushnew :ho-normal-esdc cl:*features*)))
\r
128 ;; Slot definitions
\r
129 (defmethod direct-slot-definition-class ((cl hyperobject-class)
\r
130 #+ho-normal-dsdc &rest iargs)
\r
131 (find-class 'hyperobject-dsd))
\r
133 (defmethod effective-slot-definition-class ((cl hyperobject-class)
\r
134 #+ho-normal-esdc &rest iargs)
\r
135 (find-class 'hyperobject-esd))
\r
138 ;;; Slot definitions
\r
140 (eval-when (:compile-toplevel :load-toplevel :execute)
\r
141 (defmacro process-class-option (slot-name &optional required)
\r
143 `(defmethod clos:process-a-class-option ((class hyperobject-class)
\r
144 (name (eql ,slot-name))
\r
146 (when (and ,required (null value))
\r
147 (error "hyperobject class slot ~A must have a value" name))
\r
148 (list name `',value))
\r
149 #+(or allegro sbcl cmu scl)
\r
150 (declare (ignore slot-name required))
\r
153 (defmacro process-slot-option (slot-name)
\r
155 `(defmethod clos:process-a-slot-option ((class hyperobject-class)
\r
156 (option (eql ,slot-name))
\r
158 already-processed-options
\r
160 (list* option `',value already-processed-options))
\r
162 (declare (ignore slot-name))
\r
165 (dolist (option *class-options*)
\r
166 (eval `(process-class-option ,option)))
\r
167 (dolist (option *slot-options*)
\r
168 (eval `(process-slot-option ,option)))
\r
171 `(defclass hyperobject-dsd (standard-direct-slot-definition)
\r
172 (,@(mapcar #'(lambda (x)
\r
173 `(,(intern (symbol-name x))
\r
175 *slot-options-no-initarg*)
\r
176 ,@(mapcar #'(lambda (x)
\r
177 `(,(intern (symbol-name x))
\r
179 ,(intern (symbol-name x) (symbol-name :keyword))
\r
182 ,(intern (concatenate 'string
\r
183 (symbol-name :dsd-)
\r
184 (symbol-name x)))))
\r
187 `(defclass hyperobject-esd (standard-effective-slot-definition)
\r
188 (,@(mapcar #'(lambda (x)
\r
189 `(,(intern (symbol-name x))
\r
191 ,(intern (symbol-name x) (symbol-name :keyword))
\r
194 ,(intern (concatenate 'string
\r
195 (symbol-name :esd-)
\r
196 (symbol-name x)))))
\r
197 (append *slot-options* *slot-options-no-initarg*)))))
\r
200 (defun intern-in-keyword (obj)
\r
207 (intern (symbol-name obj) (find-package 'keyword)))
\r
209 (cons (intern-in-keyword (car obj) ) (intern-in-keyword (cdr obj))))
\r
213 (defun canonicalize-value-type (vt)
\r
216 (ensure-keyword vt))
\r
218 (cons (ensure-keyword (car vt)) (cdr vt)))
\r
223 (defmethod compute-effective-slot-definition :around ((cl hyperobject-class) #+ho-normal-cesd name dsds)
\r
224 #+allegro (declare (ignore name))
\r
225 (let* ((dsd (car dsds))
\r
226 (value-type (canonicalize-value-type (slot-value dsd 'value-type))))
\r
227 (multiple-value-bind (sql-type length) (value-type-to-sql-type value-type)
\r
228 (setf (slot-value dsd 'sql-type) sql-type)
\r
229 (setf (slot-value dsd 'type) (value-type-to-lisp-type value-type))
\r
230 (let ((ia (compute-effective-slot-definition-initargs cl #+lispworks name dsds)))
\r
232 #'make-instance 'hyperobject-esd
\r
233 :value-type value-type
\r
236 :print-formatter (slot-value dsd 'print-formatter)
\r
237 :subobject (slot-value dsd 'subobject)
\r
238 :hyperlink (slot-value dsd 'hyperlink)
\r
239 :hyperlink-parameters (slot-value dsd 'hyperlink-parameters)
\r
240 :description (slot-value dsd 'description)
\r
241 :user-name (slot-value dsd 'user-name)
\r
242 :user-name-plural (slot-value dsd 'user-name-plural)
\r
243 :index (slot-value dsd 'index)
\r
244 :value-constraint (slot-value dsd 'value-constraint)
\r
245 :null-allowed (slot-value dsd 'null-allowed)
\r
248 (defmethod compute-effective-slot-definition :around ((cl hyperobject-class) #+ho-normal-cesd name dsds)
\r
249 #+ho-normal-cesd (declare (ignore name))
\r
250 (let* ((esd (call-next-method))
\r
252 (value-type (canonicalize-value-type (slot-value dsd 'value-type))))
\r
253 (multiple-value-bind (sql-type length) (value-type-to-sql-type value-type)
\r
254 (setf (slot-value esd 'sql-type) sql-type)
\r
255 (setf (slot-value esd 'length) length)
\r
256 (setf (slot-value esd 'type) (value-type-to-lisp-type value-type))
\r
257 (setf (slot-value esd 'value-type) value-type)
\r
258 (dolist (name '(print-formatter subobject hyperlink hyperlink-parameters
\r
259 description value-constraint index null-allowed user-name))
\r
260 (setf (slot-value esd name) (slot-value dsd name)))
\r
265 (setq cl:*features* (delete :ho-normal-cesd cl:*features*))
\r
267 (setq cl:*features* (delete :ho-normal-dsdc cl:*features*))
\r
269 (setq cl:*features* (delete :ho-normal-esdc cl:*features*))
\r
271 (defun lisp-type-is-a-string (type)
\r
272 (or (eq type 'string)
\r
273 (and (listp type) (some #'(lambda (x) (eq x 'string)) type))))
\r
275 (defun value-type-to-lisp-type (value-type)
\r
276 (case (if (atom value-type)
\r
279 ((:string :cdata :varchar :char)
\r
282 '(or null character))
\r
286 '(or null boolean))
\r
288 '(or null integer))
\r
289 ((:float :single-float)
\r
290 '(or null single-float))
\r
292 '(or null double-float))
\r
296 (defun value-type-to-sql-type (value-type)
\r
297 "Return two values, the sql type and field length."
\r
298 (let ((type (if (atom value-type)
\r
301 (length (when (consp value-type)
\r
302 (cadr value-type))))
\r
307 ((:fixnum :integer)
\r
311 ((:float :single-float)
\r
319 ;;;; Class initialization function
\r
321 ;; defines a slot-unbound method for class and slot-name, fills
\r
322 ;; the slot by calling reader function with the slot values of
\r
323 ;; the instance's reader-keys
\r
324 (defmacro def-lazy-reader (class slot-name reader &rest reader-keys)
\r
325 (let* ((the-slot-name (gensym))
\r
326 (the-class (gensym))
\r
327 (the-instance (gensym))
\r
329 (dolist (key reader-keys)
\r
330 (push (list 'slot-value the-instance (list 'quote key)) keys))
\r
331 (setq keys (nreverse keys))
\r
332 `(defmethod slot-unbound (,the-class (,the-instance ,class)
\r
333 (,the-slot-name (eql ',slot-name)))
\r
334 (declare (ignore ,the-class))
\r
335 (setf (slot-value ,the-instance ,the-slot-name) (,reader ,@keys)))))
\r
339 (defun intern-eql-specializer (slot)
\r
342 #+(or sbcl cmu lispworks)
\r
343 (defun ensure-lazy-reader (class-name slot-name reader &rest reader-keys)
\r
345 (gf (ensure-generic-function 'slot-unbound)))
\r
346 (dolist (key reader-keys)
\r
347 (push (list 'slot-value 'the-instance (list 'quote key)) keys))
\r
348 (setq keys (nreverse keys))
\r
349 (multiple-value-bind (method-lambda init-args-values)
\r
350 (make-method-lambda
\r
352 (class-prototype (generic-function-method-class gf))
\r
354 `(lambda (the-class the-instance the-slot-name)
\r
355 (declare (ignore the-class))
\r
356 (setf (slot-value the-instance the-slot-name) (,reader ,@keys)))
\r
358 '(the-class the-instance the-slot-name)
\r
362 `(setf (slot-value the-instance the-slot-name) (,reader ,@keys))
\r
366 #'make-instance (generic-function-method-class gf)
\r
367 ':specializers (list (class-of (find-class class-name))
\r
368 (find-class class-name)
\r
369 (intern-eql-specializer slot-name))
\r
370 ':lambda-list '(the-class the-instance the-slot-name)
\r
371 ':function (compile nil method-lambda)
\r
372 init-args-values)))))
\r
376 ;; One entry for each class with lazy readers defined. The value is a plist mapping
\r
377 ;; slot-name to a lazy reader, each of which is a list of a function and slot-names.
\r
378 (defvar *lazy-readers* (make-hash-table))
\r
380 (defmethod slot-unbound :around ((class hyperobject-class) instance slot-name)
\r
381 (let ((lazy-reader (loop for super in (class-precedence-list class)
\r
382 as lazy-reader = (getf (gethash super *lazy-readers*) slot-name)
\r
383 when lazy-reader return it)))
\r
385 (setf (slot-value instance slot-name)
\r
386 (apply (car lazy-reader)
\r
387 (loop for arg-slot-name in (cdr lazy-reader)
\r
388 collect (slot-value instance arg-slot-name))))
\r
389 ;; No lazy reader -- defer to regular slot-unbound handling.
\r
390 (call-next-method))))
\r
392 ;; The reader is a function and the reader-keys are slot names. The slot is lazily set to
\r
393 ;; the result of applying the function to the slot-values of those slots, and that value
\r
394 ;; is also returned.
\r
395 (defun ensure-lazy-reader (class-name slot-name reader &rest reader-keys)
\r
396 (setf (getf (gethash (find-class class-name) *lazy-readers*) slot-name)
\r
397 (list* reader (copy-list reader-keys))))
\r
399 (defun remove-lazy-reader (class-name slot-name)
\r
400 (setf (getf (gethash (find-class class-name) *lazy-readers*) slot-name)
\r
403 ) ;; #+(or allegro scl)
\r
406 (defun finalize-subobjects (cl)
\r
407 "Process class subobjects slot"
\r
408 (setf (subobjects cl)
\r
409 (let ((subobjects '()))
\r
410 (dolist (slot (class-slots cl))
\r
412 (subobj-def (esd-subobject slot))
\r
414 (make-instance 'subobject
\r
415 :name-class (class-name cl)
\r
416 :name-slot (slot-definition-name slot)
\r
417 :lookup (if (atom subobj-def)
\r
420 :lookup-keys (if (atom subobj-def)
\r
422 (cdr subobj-def)))))
\r
423 (unless (eq (lookup subobject) t)
\r
424 #+ignore ;; #-(or sbcl cmu lispworks)
\r
426 `(hyperobject::def-lazy-reader ,(name-class subobject)
\r
427 ,(name-slot subobject) ,(lookup subobject)
\r
428 ,@(lookup-keys subobject)))
\r
429 #+(or sbcl cmu lispworks allegro scl)
\r
430 (apply #'ensure-lazy-reader
\r
431 (name-class subobject) (name-slot subobject)
\r
432 (lookup subobject) (lookup-keys subobject)))
\r
433 (push subobject subobjects))))
\r
434 ;; sbcl/cmu reverse class-slots compared to the defclass form
\r
435 ;; so re-reverse on cmu/sbcl
\r
436 #+(or cmu sbcl) subobjects
\r
437 #-(or cmu sbcl) (nreverse subobjects)
\r
441 (defun finalize-class-slots (cl)
\r
442 "Make sure all class slots have an expected value"
\r
443 (unless (user-name cl)
\r
444 (setf (user-name cl) (format nil "~:(~A~)" (class-name cl))))
\r
446 (setf (user-name-plural cl)
\r
447 (if (and (consp (user-name cl)) (cadr (user-name cl)))
\r
448 (cadr (user-name cl))
\r
449 (format nil "~A~P" (if (consp (user-name cl))
\r
450 (car (user-name cl))
\r
454 (dolist (name '(user-name description))
\r
455 (awhen (slot-value cl name)
\r
456 (setf (slot-value cl name)
\r
457 (etypecase (slot-value cl name)
\r
459 ((or string symbol) it))))))
\r
461 (defun finalize-documentation (cl)
\r
462 "Calculate class documentation slot"
\r
463 (let ((*print-circle* nil))
\r
464 (setf (documentation (class-name cl) 'class)
\r
465 (format nil "Hyperobject~A~A~A~A"
\r
466 (aif (user-name cl)
\r
467 (format nil ": ~A" it ""))
\r
468 (aif (description cl)
\r
469 (format nil "~%Class description: ~A" it) "")
\r
470 (aif (subobjects cl)
\r
471 (format nil "~%Subobjects:~{ ~A~}" (mapcar #'name-slot it)) "")
\r
472 (aif (default-print-slots cl)
\r
473 (format nil "~%Default print slots:~{ ~A~}" it) "")
\r
476 (defun finalize-hyperlinks (cl)
\r
477 (let ((hyperlinks '()))
\r
478 (dolist (esd (class-slots cl))
\r
479 (awhen (slot-value esd 'hyperlink)
\r
481 (make-instance 'hyperlink
\r
482 :name (slot-definition-name esd)
\r
484 :link-parameters (slot-value esd 'hyperlink-parameters))
\r
486 ;; cmu/sbcl reverse class-slots compared to the defclass form
\r
487 ;; hyperlinks is already reversed from the dolist/push loop, so re-reverse on sbcl/cmu
\r
488 #-(or cmu sbcl) (setq hyperlinks (nreverse hyperlinks))
\r
489 (setf (slot-value cl 'hyperlinks) hyperlinks)))
\r
491 (defun init-hyperobject-class (cl)
\r
492 "Initialize a hyperobject class. Calculates all class slots"
\r
493 (finalize-subobjects cl)
\r
494 (finalize-views cl)
\r
495 (finalize-hyperlinks cl)
\r
497 (finalize-rules cl)
\r
498 (finalize-class-slots cl)
\r
499 (finalize-documentation cl))
\r
502 ;;;; *************************************************************************
\r
503 ;;;; Metaclass Slot Accessors
\r
504 ;;;; *************************************************************************
\r
506 (defun find-slot-by-name (cl name)
\r
507 (find name (class-slots cl) :key #'slot-definition-name))
\r
509 (defun hyperobject-class-user-name (obj)
\r
510 (user-name (class-of obj)))
\r
512 (defun hyperobject-class-user-name-plural (obj)
\r
513 (user-name-plural (class-of obj)))
\r
515 (defun hyperobject-class-subobjects (obj)
\r
516 (subobjects (class-of obj)))
\r
518 (defun hyperobject-class-hyperlinks (obj)
\r
519 (hyperlinks (class-of obj)))
\r
521 (defun hyperobject-class-slots (obj)
\r
522 ;; cmucl/sbcl reverse class-slots
\r
523 #+(or cmu sbcl) (reverse (class-slots (class-of obj)))
\r
524 #-(or cmu sbcl) (class-slots (class-of obj)))
\r
526 (defun all-subobjects (obj)
\r
527 "Returns a list of all subobjects in an object"
\r
528 (let ((so-list '()))
\r
529 (dolist (subobj-obj (subobjects (class-of obj)) (nreverse so-list))
\r
530 (dolist (so (funcall (name-slot subobj-obj) obj))
\r
531 (push so so-list)))))
\r