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.76 2003/06/06 21:59:29 kevin Exp $
\r
16 ;;;; This file is Copyright (c) 2000-2003 by Kevin M. Rosenberg
\r
17 ;;;; *************************************************************************
\r
19 (in-package #:hyperobject)
\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
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
39 :documentation "Version number for class")
\r
40 (sql-name :initarg :sql-name :initform nil)
\r
42 ;;; The remainder of these fields are calculated one time
\r
43 ;;; in finalize-inheritence.
\r
45 (subobjects :initform nil :accessor subobjects
\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
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
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
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
69 (:documentation "Metaclass for Markup Language classes."))
\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
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
84 (defclass hyperlink ()
\r
85 ((name :type symbol :initform nil :initarg :name :reader name)
\r
87 ;; The type specifier seems to break sbcl
\r
88 :type (or function symbol)
\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
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
99 (defmethod validate-superclass ((class hyperobject-class) (superclass standard-class))
\r
102 (defmethod finalize-inheritance :after ((cl hyperobject-class))
\r
103 (init-hyperobject-class cl)
\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
111 (pushnew :ho-normal-cesd cl:*features*))
\r
113 (when (>= (length (generic-function-lambda-list
\r
114 (ensure-generic-function
\r
115 'direct-slot-definition-class)))
\r
117 (pushnew :ho-normal-dsdc cl:*features*))
\r
119 (when (>= (length (generic-function-lambda-list
\r
120 (ensure-generic-function
\r
121 'effective-slot-definition-class)))
\r
123 (pushnew :ho-normal-esdc cl:*features*)))
\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
130 (defmethod effective-slot-definition-class ((cl hyperobject-class)
\r
131 #+ho-normal-esdc &rest iargs)
\r
132 (find-class 'hyperobject-esd))
\r
135 ;;; Slot definitions
\r
137 (eval-when (:compile-toplevel :load-toplevel :execute)
\r
138 (defmacro process-class-option (slot-name &optional required)
\r
140 `(defmethod clos:process-a-class-option ((class hyperobject-class)
\r
141 (name (eql ,slot-name))
\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
150 (defmacro process-slot-option (slot-name)
\r
152 `(defmethod clos:process-a-slot-option ((class hyperobject-class)
\r
153 (option (eql ,slot-name))
\r
155 already-processed-options
\r
157 (list* option `',value already-processed-options))
\r
159 (declare (ignore slot-name))
\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
168 `(defclass hyperobject-dsd (standard-direct-slot-definition)
\r
169 (,@(mapcar #'(lambda (x)
\r
170 `(,(intern (symbol-name x))
\r
172 *slot-options-no-initarg*)
\r
173 ,@(mapcar #'(lambda (x)
\r
174 `(,(intern (symbol-name x))
\r
176 ,(intern (symbol-name x) (symbol-name :keyword))
\r
179 ,(intern (concatenate 'string
\r
180 (symbol-name :dsd-)
\r
181 (symbol-name x)))))
\r
184 `(defclass hyperobject-esd (standard-effective-slot-definition)
\r
185 (,@(mapcar #'(lambda (x)
\r
186 `(,(intern (symbol-name x))
\r
188 ,(intern (symbol-name x) (symbol-name :keyword))
\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
197 (defun intern-in-keyword (obj)
\r
204 (intern (symbol-name obj) (find-package 'keyword)))
\r
206 (cons (intern-in-keyword (car obj) ) (intern-in-keyword (cdr obj))))
\r
210 (defun canonicalize-value-type (vt)
\r
213 (ensure-keyword vt))
\r
215 (cons (ensure-keyword (car vt)) (cdr vt)))
\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
229 #'make-instance 'hyperobject-esd
\r
230 :value-type value-type
\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
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
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
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
266 (setq cl:*features* (delete :ho-normal-cesd cl:*features*))
\r
268 (setq cl:*features* (delete :ho-normal-dsdc cl:*features*))
\r
270 (setq cl:*features* (delete :ho-normal-esdc cl:*features*))
\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
276 (defun value-type-to-lisp-type (value-type)
\r
277 (case (if (atom value-type)
\r
280 ((:string :cdata :varchar :char)
\r
283 '(or null character))
\r
287 '(or null boolean))
\r
289 '(or null integer))
\r
290 ((:float :single-float)
\r
291 '(or null single-float))
\r
293 '(or null double-float))
\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
302 (length (when (consp value-type)
\r
303 (cadr value-type))))
\r
308 ((:fixnum :integer)
\r
312 ((:float :single-float)
\r
320 ;;;; Class initialization function
\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
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
340 (defun intern-eql-specializer (slot)
\r
343 #+(or sbcl cmu lispworks)
\r
344 (defun ensure-lazy-reader (class-name slot-name reader &rest reader-keys)
\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
353 (class-prototype (generic-function-method-class gf))
\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
359 '(the-class the-instance the-slot-name)
\r
363 `(setf (slot-value the-instance the-slot-name) (,reader ,@keys))
\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
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
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
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
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
400 (defun remove-lazy-reader (class-name slot-name)
\r
401 (setf (getf (gethash (find-class class-name) *lazy-readers*) slot-name)
\r
404 ) ;; #+(or allegro scl)
\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
413 (subobj-def (esd-subobject slot))
\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
421 :lookup-keys (if (atom subobj-def)
\r
423 (cdr subobj-def)))))
\r
424 (unless (eq (lookup subobject) t)
\r
425 #+ignore ;; #-(or sbcl cmu lispworks)
\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
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
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
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
460 ((or string symbol) it))))))
\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
477 (defun finalize-hyperlinks (cl)
\r
478 (let ((hyperlinks '()))
\r
479 (dolist (esd (class-slots cl))
\r
480 (awhen (slot-value esd 'hyperlink)
\r
482 (make-instance 'hyperlink
\r
483 :name (slot-definition-name esd)
\r
485 :link-parameters (slot-value esd 'hyperlink-parameters))
\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
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
498 (finalize-rules cl)
\r
499 (finalize-class-slots cl)
\r
500 (finalize-documentation cl))
\r
503 ;;;; *************************************************************************
\r
504 ;;;; Metaclass Slot Accessors
\r
505 ;;;; *************************************************************************
\r
507 (defun find-slot-by-name (cl name)
\r
508 (find name (class-slots cl) :key #'slot-definition-name))
\r
510 (defun hyperobject-class-user-name (obj)
\r
511 (user-name (class-of obj)))
\r
513 (defun hyperobject-class-user-name-plural (obj)
\r
514 (user-name-plural (class-of obj)))
\r
516 (defun hyperobject-class-subobjects (obj)
\r
517 (subobjects (class-of obj)))
\r
519 (defun hyperobject-class-hyperlinks (obj)
\r
520 (hyperlinks (class-of obj)))
\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
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