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.77 2003/06/20 08:35:21 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 (direct-rules :initarg :direct-rules :initform nil
\r
41 :accessor dirst-rules
\r
42 :documentation "Rules to fire on slot changes")
\r
43 (closures :initarg :closures :initform nil
\r
45 :documentation "Closures to call on slot chnages")
\r
46 (sql-name :initarg :sql-name :accessor sql-name :initform nil
\r
47 :documentation "SQL Name for this class")
\r
48 (guid :initarg :guid :accessor guid :initform nil
\r
49 :documentation "ID string for this class")
\r
51 ;;; The remainder of these fields are calculated one time
\r
52 ;;; in finalize-inheritence.
\r
54 (subobjects :initform nil :accessor subobjects
\r
56 "List of fields that contain a list of subobjects objects.")
\r
57 (hyperlinks :type list :initform nil :accessor hyperlinks
\r
58 :documentation "List of fields that have hyperlinks")
\r
59 (direct-rules :type list :initform nil :initarg :direct-rules
\r
60 :accessor direct-rules
\r
61 :documentation "List of rules to fire on slot changes.")
\r
62 (class-id :type integer :initform nil
\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
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
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
78 (:documentation "Metaclass for Markup Language classes."))
\r
80 (defclass subobject ()
\r
81 ((name-class :type symbol :initform nil :initarg :name-class :reader name-class)
\r
82 (name-slot :type symbol :initform nil :initarg :name-slot :reader name-slot)
\r
83 (lookup :type (or function symbol) :initform nil :initarg :lookup :reader lookup)
\r
84 (lookup-keys :type list :initform nil :initarg :lookup-keys
\r
85 :reader lookup-keys))
\r
86 (:documentation "Contains subobject information"))
\r
89 (defmethod print-object ((obj subobject) (s stream))
\r
90 (print-unreadable-object (obj s :type t)
\r
91 (format s "~S" (name-slot obj))))
\r
93 (defclass hyperlink ()
\r
94 ((name :type symbol :initform nil :initarg :name :reader name)
\r
96 ;; The type specifier seems to break sbcl
\r
97 :type (or function symbol)
\r
99 :initform nil :initarg :lookup :reader lookup)
\r
100 (link-parameters :type list :initform nil :initarg :link-parameters
\r
101 :reader link-parameters)))
\r
103 (defmethod print-object ((obj hyperlink) (s stream))
\r
104 (print-unreadable-object (obj s :type t :identity t)
\r
105 (format s "~S" (name obj))))
\r
107 #+(or cmu scl sbcl)
\r
108 (defmethod validate-superclass ((class hyperobject-class) (superclass standard-class))
\r
111 (defmethod finalize-inheritance :after ((cl hyperobject-class))
\r
112 (init-hyperobject-class cl)
\r
115 (eval-when (:compile-toplevel :load-toplevel :execute)
\r
116 (when (>= (length (generic-function-lambda-list
\r
117 (ensure-generic-function
\r
118 'compute-effective-slot-definition)))
\r
120 (pushnew :ho-normal-cesd cl:*features*))
\r
122 (when (>= (length (generic-function-lambda-list
\r
123 (ensure-generic-function
\r
124 'direct-slot-definition-class)))
\r
126 (pushnew :ho-normal-dsdc cl:*features*))
\r
128 (when (>= (length (generic-function-lambda-list
\r
129 (ensure-generic-function
\r
130 'effective-slot-definition-class)))
\r
132 (pushnew :ho-normal-esdc cl:*features*)))
\r
134 ;; Slot definitions
\r
135 (defmethod direct-slot-definition-class ((cl hyperobject-class)
\r
136 #+ho-normal-dsdc &rest iargs)
\r
137 (find-class 'hyperobject-dsd))
\r
139 (defmethod effective-slot-definition-class ((cl hyperobject-class)
\r
140 #+ho-normal-esdc &rest iargs)
\r
141 (find-class 'hyperobject-esd))
\r
144 ;;; Slot definitions
\r
146 (eval-when (:compile-toplevel :load-toplevel :execute)
\r
147 (defmacro process-class-option (slot-name &optional required)
\r
149 `(defmethod clos:process-a-class-option ((class hyperobject-class)
\r
150 (name (eql ,slot-name))
\r
152 (when (and ,required (null value))
\r
153 (error "hyperobject class slot ~A must have a value" name))
\r
154 (list name `',value))
\r
155 #+(or allegro sbcl cmu scl)
\r
156 (declare (ignore slot-name required))
\r
159 (defmacro process-slot-option (slot-name)
\r
161 `(defmethod clos:process-a-slot-option ((class hyperobject-class)
\r
162 (option (eql ,slot-name))
\r
164 already-processed-options
\r
166 (list* option `',value already-processed-options))
\r
168 (declare (ignore slot-name))
\r
171 (dolist (option *class-options*)
\r
172 (eval `(process-class-option ,option)))
\r
173 (dolist (option *slot-options*)
\r
174 (eval `(process-slot-option ,option)))
\r
177 `(defclass hyperobject-dsd (standard-direct-slot-definition)
\r
178 (,@(mapcar #'(lambda (x)
\r
179 `(,(intern (symbol-name x))
\r
181 *slot-options-no-initarg*)
\r
182 ,@(mapcar #'(lambda (x)
\r
183 `(,(intern (symbol-name x))
\r
185 ,(intern (symbol-name x) (symbol-name :keyword))
\r
188 ,(intern (concatenate 'string
\r
189 (symbol-name :dsd-)
\r
190 (symbol-name x)))))
\r
193 `(defclass hyperobject-esd (standard-effective-slot-definition)
\r
194 (,@(mapcar #'(lambda (x)
\r
195 `(,(intern (symbol-name x))
\r
197 ,(intern (symbol-name x) (symbol-name :keyword))
\r
200 ,(intern (concatenate 'string
\r
201 (symbol-name :esd-)
\r
202 (symbol-name x)))))
\r
203 (append *slot-options* *slot-options-no-initarg*)))))
\r
206 (defun intern-in-keyword (obj)
\r
213 (intern (symbol-name obj) (find-package 'keyword)))
\r
215 (cons (intern-in-keyword (car obj) ) (intern-in-keyword (cdr obj))))
\r
219 (defun canonicalize-value-type (vt)
\r
222 (ensure-keyword vt))
\r
224 (list (ensure-keyword (car vt)) (cadr vt)))
\r
228 (defmethod compute-effective-slot-definition :around ((cl hyperobject-class)
\r
229 #+ho-normal-cesd name
\r
231 #+ho-normal-cesd (declare (ignore name))
\r
232 (let* ((esd (call-next-method))
\r
234 (value-type (canonicalize-value-type (slot-value dsd 'value-type))))
\r
235 (multiple-value-bind (sql-type sql-length)
\r
236 (value-type-to-sql-type value-type)
\r
237 (setf (slot-value esd 'sql-type) sql-type)
\r
238 (setf (slot-value esd 'sql-length) sql-length))
\r
239 (setf (slot-value esd 'type) (value-type-to-lisp-type value-type))
\r
240 (setf (slot-value esd 'value-type) value-type)
\r
241 (setf (slot-value esd 'user-name)
\r
242 (aif (slot-value dsd 'user-name)
\r
244 (string-downcase (symbol-name (slot-definition-name dsd)))))
\r
245 (setf (slot-value esd 'sql-name)
\r
246 (aif (slot-value dsd 'sql-name)
\r
248 (lisp-name-to-sql-name (slot-definition-name dsd))))
\r
249 (setf (slot-value esd 'sql-name)
\r
250 (aif (slot-value dsd 'sql-name)
\r
252 (lisp-name-to-sql-name (slot-definition-name dsd))))
\r
253 (dolist (name '(print-formatter subobject hyperlink hyperlink-parameters
\r
254 description value-constraint indexed null-allowed
\r
255 unique short-description void-text read-only-groups
\r
256 hidden-groups unit disable-predicate view-type
\r
257 list-of-values stored))
\r
258 (setf (slot-value esd name) (slot-value dsd name)))
\r
261 (defun lisp-name-to-sql-name (lisp)
\r
262 "Convert a lisp name (atom or list, string or symbol) into a canonical
\r
264 (unless (stringp lisp)
\r
267 (symbol (symbol-name lisp))
\r
268 (t (write-to-string lisp)))))
\r
269 (do* ((len (length lisp))
\r
270 (sql (make-string len))
\r
272 ((= i len) (string-upcase sql))
\r
273 (declare (fixnum i)
\r
274 (simple-string sql))
\r
275 (setf (schar sql i)
\r
276 (let ((c (char lisp i)))
\r
278 ((#\- #\$ #\+ #\#) #\_)
\r
282 (setq cl:*features* (delete :ho-normal-cesd cl:*features*))
\r
284 (setq cl:*features* (delete :ho-normal-dsdc cl:*features*))
\r
286 (setq cl:*features* (delete :ho-normal-esdc cl:*features*))
\r
288 (defun lisp-type-is-a-string (type)
\r
289 (or (eq type 'string)
\r
290 (and (listp type) (some #'(lambda (x) (eq x 'string)) type))))
\r
292 (defun base-value-type (value-type)
\r
293 (if (atom value-type)
\r
297 (defun value-type-to-lisp-type (value-type)
\r
298 (case (base-value-type value-type)
\r
299 ((:string :cdata :varchar :char)
\r
302 '(or null character))
\r
306 '(or null boolean))
\r
307 ((:integer :long-integer)
\r
308 '(or null integer))
\r
309 ((:float :single-float)
\r
310 '(or null single-float))
\r
312 '(or null double-float))
\r
316 (defun value-type-to-sql-type (value-type)
\r
317 "Return two values, the sql type and field length."
\r
318 (let ((type (base-value-type value-type))
\r
319 (length (when (consp value-type)
\r
320 (cadr value-type))))
\r
323 ((:char :character)
\r
327 ((:fixnum :integer)
\r
333 ((:float :single-float)
\r
341 ;;;; Class initialization function
\r
343 ;; defines a slot-unbound method for class and slot-name, fills
\r
344 ;; the slot by calling reader function with the slot values of
\r
345 ;; the instance's reader-keys
\r
346 (defmacro def-lazy-reader (class slot-name reader &rest reader-keys)
\r
347 (let* ((the-slot-name (gensym))
\r
348 (the-class (gensym))
\r
349 (the-instance (gensym))
\r
351 (dolist (key reader-keys)
\r
352 (push (list 'slot-value the-instance (list 'quote key)) keys))
\r
353 (setq keys (nreverse keys))
\r
354 `(defmethod slot-unbound (,the-class (,the-instance ,class)
\r
355 (,the-slot-name (eql ',slot-name)))
\r
356 (declare (ignore ,the-class))
\r
357 (setf (slot-value ,the-instance ,the-slot-name) (,reader ,@keys)))))
\r
361 (defun intern-eql-specializer (slot)
\r
364 #+(or sbcl cmu lispworks)
\r
365 (defun ensure-lazy-reader (class-name slot-name reader &rest reader-keys)
\r
367 (gf (ensure-generic-function 'slot-unbound)))
\r
368 (dolist (key reader-keys)
\r
369 (push (list 'slot-value 'the-instance (list 'quote key)) keys))
\r
370 (setq keys (nreverse keys))
\r
371 (multiple-value-bind (method-lambda init-args-values)
\r
372 (make-method-lambda
\r
374 (class-prototype (generic-function-method-class gf))
\r
376 `(lambda (the-class the-instance the-slot-name)
\r
377 (declare (ignore the-class))
\r
378 (setf (slot-value the-instance the-slot-name) (,reader ,@keys)))
\r
380 '(the-class the-instance the-slot-name)
\r
384 `(setf (slot-value the-instance the-slot-name) (,reader ,@keys))
\r
388 #'make-instance (generic-function-method-class gf)
\r
389 ':specializers (list (class-of (find-class class-name))
\r
390 (find-class class-name)
\r
391 (intern-eql-specializer slot-name))
\r
392 ':lambda-list '(the-class the-instance the-slot-name)
\r
393 ':function (compile nil method-lambda)
\r
394 init-args-values)))))
\r
398 ;; One entry for each class with lazy readers defined. The value is a plist mapping
\r
399 ;; slot-name to a lazy reader, each of which is a list of a function and slot-names.
\r
400 (defvar *lazy-readers* (make-hash-table))
\r
402 (defmethod slot-unbound :around ((class hyperobject-class) instance slot-name)
\r
403 (let ((lazy-reader (loop for super in (class-precedence-list class)
\r
404 as lazy-reader = (getf (gethash super *lazy-readers*) slot-name)
\r
405 when lazy-reader return it)))
\r
407 (setf (slot-value instance slot-name)
\r
408 (apply (car lazy-reader)
\r
409 (loop for arg-slot-name in (cdr lazy-reader)
\r
410 collect (slot-value instance arg-slot-name))))
\r
411 ;; No lazy reader -- defer to regular slot-unbound handling.
\r
412 (call-next-method))))
\r
414 ;; The reader is a function and the reader-keys are slot names. The slot is lazily set to
\r
415 ;; the result of applying the function to the slot-values of those slots, and that value
\r
416 ;; is also returned.
\r
417 (defun ensure-lazy-reader (class-name slot-name reader &rest reader-keys)
\r
418 (setf (getf (gethash (find-class class-name) *lazy-readers*) slot-name)
\r
419 (list* reader (copy-list reader-keys))))
\r
421 (defun remove-lazy-reader (class-name slot-name)
\r
422 (setf (getf (gethash (find-class class-name) *lazy-readers*) slot-name)
\r
425 ) ;; #+(or allegro scl)
\r
428 (defun finalize-subobjects (cl)
\r
429 "Process class subobjects slot"
\r
430 (setf (subobjects cl)
\r
431 (let ((subobjects '()))
\r
432 (dolist (slot (class-slots cl))
\r
434 (subobj-def (esd-subobject slot))
\r
436 (make-instance 'subobject
\r
437 :name-class (class-name cl)
\r
438 :name-slot (slot-definition-name slot)
\r
439 :lookup (if (atom subobj-def)
\r
442 :lookup-keys (if (atom subobj-def)
\r
444 (cdr subobj-def)))))
\r
445 (unless (eq (lookup subobject) t)
\r
446 #+ignore ;; #-(or sbcl cmu lispworks)
\r
448 `(hyperobject::def-lazy-reader ,(name-class subobject)
\r
449 ,(name-slot subobject) ,(lookup subobject)
\r
450 ,@(lookup-keys subobject)))
\r
451 #+(or sbcl cmu lispworks allegro scl)
\r
452 (apply #'ensure-lazy-reader
\r
453 (name-class subobject) (name-slot subobject)
\r
454 (lookup subobject) (lookup-keys subobject)))
\r
455 (push subobject subobjects))))
\r
456 ;; sbcl/cmu reverse class-slots compared to the defclass form
\r
457 ;; so re-reverse on cmu/sbcl
\r
458 #+(or cmu sbcl) subobjects
\r
459 #-(or cmu sbcl) (nreverse subobjects)
\r
463 (defun finalize-class-slots (cl)
\r
464 "Make sure all class slots have an expected value"
\r
465 (unless (user-name cl)
\r
466 (setf (user-name cl) (format nil "~:(~A~)" (class-name cl))))
\r
468 (setf (user-name-plural cl)
\r
469 (if (and (consp (user-name cl)) (cadr (user-name cl)))
\r
470 (cadr (user-name cl))
\r
471 (format nil "~A~P" (if (consp (user-name cl))
\r
472 (car (user-name cl))
\r
476 (dolist (name '(user-name description version guid sql-name))
\r
477 (awhen (slot-value cl name)
\r
478 (setf (slot-value cl name)
\r
479 (etypecase (slot-value cl name)
\r
481 ((or string symbol) it)))))
\r
483 (unless (sql-name cl)
\r
484 (setf (sql-name cl) (lisp-name-to-sql-name (class-name cl))))
\r
487 (defun finalize-documentation (cl)
\r
488 "Calculate class documentation slot"
\r
489 (let ((*print-circle* nil))
\r
490 (setf (documentation (class-name cl) 'class)
\r
491 (format nil "Hyperobject~A~A~A~A"
\r
492 (aif (user-name cl)
\r
493 (format nil ": ~A" it ""))
\r
494 (aif (description cl)
\r
495 (format nil "~%Class description: ~A" it) "")
\r
496 (aif (subobjects cl)
\r
497 (format nil "~%Subobjects:~{ ~A~}" (mapcar #'name-slot it)) "")
\r
498 (aif (default-print-slots cl)
\r
499 (format nil "~%Default print slots:~{ ~A~}" it) "")
\r
502 (defun finalize-hyperlinks (cl)
\r
503 (let ((hyperlinks '()))
\r
504 (dolist (esd (class-slots cl))
\r
505 (awhen (slot-value esd 'hyperlink)
\r
507 (make-instance 'hyperlink
\r
508 :name (slot-definition-name esd)
\r
510 :link-parameters (slot-value esd 'hyperlink-parameters))
\r
512 ;; cmu/sbcl reverse class-slots compared to the defclass form
\r
513 ;; hyperlinks is already reversed from the dolist/push loop, so re-reverse on sbcl/cmu
\r
514 #-(or cmu sbcl) (setq hyperlinks (nreverse hyperlinks))
\r
515 (setf (slot-value cl 'hyperlinks) hyperlinks)))
\r
517 (defun init-hyperobject-class (cl)
\r
518 "Initialize a hyperobject class. Calculates all class slots"
\r
519 (finalize-subobjects cl)
\r
520 (finalize-views cl)
\r
521 (finalize-hyperlinks cl)
\r
523 (finalize-rules cl)
\r
524 (finalize-class-slots cl)
\r
525 (finalize-documentation cl))
\r
528 ;;;; *************************************************************************
\r
529 ;;;; Metaclass Slot Accessors
\r
530 ;;;; *************************************************************************
\r
532 (defun find-slot-by-name (cl name)
\r
533 (find name (class-slots cl) :key #'slot-definition-name))
\r
535 (defun hyperobject-class-user-name (obj)
\r
536 (user-name (class-of obj)))
\r
538 (defun hyperobject-class-user-name-plural (obj)
\r
539 (user-name-plural (class-of obj)))
\r
541 (defun hyperobject-class-subobjects (obj)
\r
542 (subobjects (class-of obj)))
\r
544 (defun hyperobject-class-hyperlinks (obj)
\r
545 (hyperlinks (class-of obj)))
\r
547 (defun hyperobject-class-slots (obj)
\r
548 ;; cmucl/sbcl reverse class-slots
\r
549 #+(or cmu sbcl) (reverse (class-slots (class-of obj)))
\r
550 #-(or cmu sbcl) (class-slots (class-of obj)))
\r
552 (defun all-subobjects (obj)
\r
553 "Returns a list of all subobjects in an object"
\r
554 (let ((so-list '()))
\r
555 (dolist (subobj-obj (subobjects (class-of obj)) (nreverse so-list))
\r
556 (dolist (so (funcall (name-slot subobj-obj) obj))
\r
557 (push so so-list)))))
\r