1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
6 ;;;; Purpose: Metaobject Protocol Interface
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Apr 2000
10 ;;;; This metaclass as functions to classes to allow display
11 ;;;; in Text, HTML, and XML formats. This includes hyperlinking
12 ;;;; capability and sub-objects.
16 ;;;; This file is Copyright (c) 2000-2003 by Kevin M. Rosenberg
17 ;;;; *************************************************************************
19 (in-package #:hyperobject)
23 (defclass hyperobject-class (standard-class)
24 ( ;; slots initialized in defclass
25 (user-name :initarg :user-name :type string :initform nil
27 :documentation "User name for class")
28 (user-name-plural :initarg :user-name-plural :type string :initform nil
29 :accessor user-name-plural
30 :documentation "Plural user name for class")
31 (default-print-slots :initarg :default-print-slots :type list :initform nil
32 :accessor default-print-slots
33 :documentation "Defaults slots for a view")
34 (description :initarg :description :initform nil
36 :documentation "Class description")
37 (version :initarg :version :initform nil
39 :documentation "Version number for class")
40 (closures :initarg :closures :initform nil
42 :documentation "Closures to call on slot chnages")
43 (sql-name :initarg :sql-name :accessor sql-name :initform nil
44 :documentation "SQL Name for this class")
45 (guid :initarg :guid :accessor guid :initform nil
46 :documentation "ID string for this class")
48 ;;; The remainder of these fields are calculated one time
49 ;;; in finalize-inheritence.
51 (subobjects :initform nil :accessor subobjects
53 "List of fields that contain a list of subobjects objects.")
54 (hyperlinks :type list :initform nil :accessor hyperlinks
55 :documentation "List of fields that have hyperlinks")
56 (direct-rules :type list :initform nil :initarg :direct-rules
57 :accessor direct-rules
58 :documentation "List of rules to fire on slot changes.")
59 (direct-views :type list :initform nil :initarg :direct-views
60 :accessor direct-views
61 :documentation "List of views")
62 (class-id :type integer :initform nil
64 :documentation "Unique ID for the class")
65 (default-view :initform nil :initarg :default-view :accessor default-view
66 :documentation "The default view for a class")
67 (documementation :initform nil :initarg :documentation
68 :documentation "Documentation string for hyperclass.")
71 (create-table-cmd :initform nil :reader create-table-cmd)
72 (create-indices-cmds :initform nil :reader create-index-cmds)
73 (drop-table-cmd :initform nil :reader drop-table-cmd)
75 (views :type list :initform nil :initarg :views :accessor views
76 :documentation "List of views")
77 (rules :type list :initform nil :initarg :rules :accessor rules
78 :documentation "List of rules")
80 (:documentation "Metaclass for Markup Language classes."))
82 (defclass subobject ()
83 ((name-class :type symbol :initarg :name-class :reader name-class)
84 (name-slot :type symbol :initarg :name-slot :reader name-slot)
85 (subobj-class :type symbol :initarg :subobj-class :reader subobj-class)
86 (lookup :type (or function symbol) :initarg :lookup :reader lookup)
87 (lookup-keys :type list :initarg :lookup-keys :reader lookup-keys))
88 (:documentation "subobject information")
89 (:default-initargs :name-class nil :name-slot nil :subobj-class nil
90 :lookup nil :lookup-keys nil))
93 (defmethod print-object ((obj subobject) (s stream))
94 (print-unreadable-object (obj s :type t)
95 (format s "~S" (name-slot obj))))
97 (defclass hyperlink ()
98 ((name :type symbol :initform nil :initarg :name :reader name)
100 ;; The type specifier seems to break sbcl
101 :type (or function symbol)
103 :initform nil :initarg :lookup :reader lookup)
104 (link-parameters :type list :initform nil :initarg :link-parameters
105 :reader link-parameters)))
107 (defmethod print-object ((obj hyperlink) (s stream))
108 (print-unreadable-object (obj s :type t :identity t)
109 (format s "~S" (name obj))))
111 (defmethod validate-superclass ((class hyperobject-class) (superclass standard-class))
114 (defmethod finalize-inheritance :after ((cl hyperobject-class))
115 ;; Work-around needed for OpenMCL
117 (unless (find-class (class-name cl))
118 (setf (find-class (class-name cl)) cl))
120 (init-hyperobject-class cl)
123 (eval-when (:compile-toplevel :load-toplevel :execute)
124 (when (>= (length (generic-function-lambda-list
125 (ensure-generic-function
126 'compute-effective-slot-definition)))
128 (pushnew :ho-normal-cesd cl:*features*))
130 (when (>= (length (generic-function-lambda-list
131 (ensure-generic-function
132 'direct-slot-definition-class)))
134 (pushnew :ho-normal-dsdc cl:*features*))
136 (when (>= (length (generic-function-lambda-list
137 (ensure-generic-function
138 'effective-slot-definition-class)))
140 (pushnew :ho-normal-esdc cl:*features*)))
143 (defmethod direct-slot-definition-class ((cl hyperobject-class)
144 #+ho-normal-dsdc &rest iargs)
145 (find-class 'hyperobject-dsd))
147 (defmethod effective-slot-definition-class ((cl hyperobject-class)
148 #+ho-normal-esdc &rest iargs)
149 (find-class 'hyperobject-esd))
154 (eval-when (:compile-toplevel :load-toplevel :execute)
155 (defmacro process-class-option (slot-name &optional required)
157 `(defmethod clos:process-a-class-option ((class hyperobject-class)
158 (name (eql ,slot-name))
160 (when (and ,required (null value))
161 (error "hyperobject class slot ~A must have a value" name))
162 (list name `',value))
163 #+(or allegro sbcl cmu scl openmcl)
164 (declare (ignore slot-name required))
167 (defmacro process-slot-option (slot-name)
169 `(defmethod clos:process-a-slot-option ((class hyperobject-class)
170 (option (eql ,slot-name))
172 already-processed-options
174 (list* option `',value already-processed-options))
176 (declare (ignore slot-name))
179 (dolist (option *class-options*)
180 (eval `(process-class-option ,option)))
181 (dolist (option *slot-options*)
182 (eval `(process-slot-option ,option)))
185 `(defclass hyperobject-dsd (standard-direct-slot-definition)
186 (,@(mapcar #'(lambda (x)
187 `(,(intern (symbol-name x))
189 *slot-options-no-initarg*)
190 ,@(mapcar #'(lambda (x)
191 `(,(intern (symbol-name x))
193 ,(intern (symbol-name x) (symbol-name :keyword))
196 ,(intern (concatenate 'string
201 `(defclass hyperobject-esd (standard-effective-slot-definition)
202 (,@(mapcar #'(lambda (x)
203 `(,(intern (symbol-name x))
205 ,(intern (symbol-name x) (symbol-name :keyword))
208 ,(intern (concatenate 'string
211 (append *slot-options* *slot-options-no-initarg*)))))
214 (defun intern-in-keyword (obj)
221 (intern (symbol-name obj) (find-package 'keyword)))
223 (cons (intern-in-keyword (car obj) ) (intern-in-keyword (cdr obj))))
227 (defun canonicalize-value-type (vt)
232 (list (ensure-keyword (car vt)) (cadr vt)))
236 (defmethod compute-effective-slot-definition :around ((cl hyperobject-class)
237 #+ho-normal-cesd name
239 (declare (ignore #+ho-normal-cesd name))
240 (let ((esd (call-next-method)))
241 (if (typep esd 'hyperobject-esd)
242 (compute-hyperobject-esd esd dsds)
245 (defun compute-hyperobject-esd (esd dsds)
246 (let* ((dsd (car dsds))
247 (value-type (canonicalize-value-type (slot-value dsd 'value-type))))
248 (multiple-value-bind (sql-type sql-length)
249 (value-type-to-sql-type value-type)
250 (setf (esd-sql-type esd) sql-type)
251 (setf (esd-sql-length esd) sql-length))
252 (setf (slot-value esd 'type) (value-type-to-lisp-type value-type))
253 (setf (esd-value-type esd) value-type)
254 (setf (esd-user-name esd)
255 (aif (dsd-user-name dsd)
257 (string-downcase (symbol-name (slot-definition-name dsd)))))
258 (setf (esd-sql-name esd)
259 (aif (dsd-sql-name dsd)
261 (lisp-name-to-sql-name (slot-definition-name dsd))))
262 (setf (esd-sql-name esd)
263 (aif (dsd-sql-name dsd)
265 (lisp-name-to-sql-name (slot-definition-name dsd))))
266 (dolist (name '(print-formatter subobject hyperlink hyperlink-parameters
267 description value-constraint indexed null-allowed
268 unique short-description void-text read-only-groups
269 hidden-groups unit disable-predicate view-type
270 list-of-values stored))
271 (setf (slot-value esd name) (slot-value dsd name)))
274 (defun lisp-name-to-sql-name (lisp)
275 "Convert a lisp name (atom or list, string or symbol) into a canonical
277 (unless (stringp lisp)
280 (symbol (symbol-name lisp))
281 (t (write-to-string lisp)))))
282 (do* ((len (length lisp))
283 (sql (make-string len))
285 ((= i len) (string-upcase sql))
289 (let ((c (char lisp i)))
291 ((#\- #\$ #\+ #\#) #\_)
295 (setq cl:*features* (delete :ho-normal-cesd cl:*features*))
297 (setq cl:*features* (delete :ho-normal-dsdc cl:*features*))
299 (setq cl:*features* (delete :ho-normal-esdc cl:*features*))
301 (defun lisp-type-is-a-string (type)
302 (or (eq type 'string)
303 (and (listp type) (some #'(lambda (x) (eq x 'string)) type))))
305 (defun base-value-type (value-type)
306 (if (atom value-type)
310 (defun value-type-to-lisp-type (value-type)
311 (case (base-value-type value-type)
312 ((:string :cdata :varchar :char)
317 '(or null character))
322 ((:integer :long-integer)
324 ((:float :single-float)
325 '(or null single-float))
327 '(or null double-float))
331 (defun value-type-to-sql-type (value-type)
332 "Return two values, the sql type and field length."
333 (let ((type (base-value-type value-type))
334 (length (when (consp value-type)
348 ((:float :single-float)
358 ;;;; Class initialization function
360 ;; One entry for each class with lazy readers defined. The value is a plist mapping
361 ;; slot-name to a lazy reader, each of which is a list of a function and slot-names.
362 (defvar *lazy-readers* (make-hash-table))
364 (defmethod slot-unbound ((class hyperobject-class) instance slot-name)
366 (loop for super in (class-precedence-list class)
367 as lazy-reader = (getf (gethash super *lazy-readers*) slot-name)
368 when lazy-reader return it)))
370 (setf (slot-value instance slot-name)
371 (if (atom lazy-reader)
372 (make-instance lazy-reader)
373 (apply (car lazy-reader)
374 (loop for arg-slot-name in (cdr lazy-reader)
375 collect (slot-value instance arg-slot-name)))))
376 ;; No lazy reader -- defer to regular slot-unbound handling.
377 (call-next-method))))
379 ;; The reader is a function and the reader-keys are slot names. The slot is lazily set to
380 ;; the result of applying the function to the slot-values of those slots, and that value
382 (defun ensure-lazy-reader (cl class-name slot-name subobj-class reader
384 (setf (getf (gethash cl *lazy-readers*) slot-name)
387 (list* reader (copy-list reader-keys)))))
389 (defun remove-lazy-reader (class-name slot-name)
390 (setf (getf (gethash (find-class class-name) *lazy-readers*) slot-name)
394 (defun finalize-subobjects (cl)
395 "Process class subobjects slot"
396 (setf (subobjects cl)
397 (let ((subobjects '()))
398 (dolist (slot (class-slots cl))
400 (subobj-def (esd-subobject slot))
402 (make-instance 'subobject
403 :name-class (class-name cl)
404 :name-slot (slot-definition-name slot)
405 :subobj-class (when (atom subobj-def)
407 :lookup (when (listp subobj-def)
409 :lookup-keys (when (listp subobj-def)
411 (unless (eq (lookup subobject) t)
412 (apply #'ensure-lazy-reader
414 (name-class subobject) (name-slot subobject)
415 (subobj-class subobject)
416 (lookup subobject) (lookup-keys subobject))
417 (push subobject subobjects)))))
418 ;; sbcl/cmu reverse class-slots compared to the defclass form
419 ;; so re-reverse on cmu/sbcl
420 #+(or cmu sbcl) subobjects
421 #-(or cmu sbcl) (nreverse subobjects)
424 (defun finalize-class-slots (cl)
425 "Make sure all class slots have an expected value"
426 (unless (user-name cl)
427 (setf (user-name cl) (format nil "~:(~A~)" (class-name cl))))
429 (setf (user-name-plural cl)
430 (if (and (consp (user-name cl)) (cadr (user-name cl)))
431 (cadr (user-name cl))
432 (format nil "~A~P" (if (consp (user-name cl))
437 (dolist (name '(user-name description version guid sql-name))
438 (awhen (slot-value cl name)
439 (setf (slot-value cl name)
444 (unless (sql-name cl)
445 (setf (sql-name cl) (lisp-name-to-sql-name (class-name cl))))
448 (defun finalize-documentation (cl)
449 "Calculate class documentation slot"
450 (let ((*print-circle* nil))
451 (setf (documentation cl 'type)
452 (format nil "Hyperobject~A~A~A~A"
454 (format nil ": ~A" it ""))
455 (aif (description cl)
456 (format nil "~%Class description: ~A" it) "")
458 (format nil "~%Subobjects:~{ ~A~}" (mapcar #'name-slot it)) "")
459 (aif (default-print-slots cl)
460 (format nil "~%Default print slots:~{ ~A~}" it) "")
463 (defun finalize-hyperlinks (cl)
464 (let ((hyperlinks '()))
465 (dolist (esd (class-slots cl))
466 (awhen (slot-value esd 'hyperlink)
468 (make-instance 'hyperlink
469 :name (slot-definition-name esd)
471 :link-parameters (slot-value esd 'hyperlink-parameters))
473 ;; cmu/sbcl reverse class-slots compared to the defclass form
474 ;; hyperlinks is already reversed from the dolist/push loop, so re-reverse on sbcl/cmu
475 #-(or cmu sbcl) (setq hyperlinks (nreverse hyperlinks))
476 (setf (slot-value cl 'hyperlinks) hyperlinks)))
478 (defun init-hyperobject-class (cl)
479 "Initialize a hyperobject class. Calculates all class slots"
480 (finalize-subobjects cl)
482 (finalize-hyperlinks cl)
485 (finalize-class-slots cl)
486 (finalize-documentation cl))
489 ;;;; *************************************************************************
490 ;;;; Metaclass Slot Accessors
491 ;;;; *************************************************************************
493 (defun find-slot-by-name (cl name)
494 (find name (class-slots cl) :key #'slot-definition-name))
496 (defun hyperobject-class-user-name (obj)
497 (user-name (class-of obj)))
499 (defun hyperobject-class-user-name-plural (obj)
500 (user-name-plural (class-of obj)))
502 (defun hyperobject-class-subobjects (obj)
503 (subobjects (class-of obj)))
505 (defun hyperobject-class-hyperlinks (obj)
506 (hyperlinks (class-of obj)))
508 (defun hyperobject-class-slots (obj)
509 ;; cmucl/sbcl reverse class-slots
510 #+(or cmu sbcl) (reverse (class-slots (class-of obj)))
511 #-(or cmu sbcl) (class-slots (class-of obj)))
513 (defun all-subobjects (obj)
514 "Returns a list of all subobjects in an object"
516 (dolist (subobj-obj (subobjects (class-of obj)) (nreverse so-list))
517 (dolist (so (funcall (name-slot subobj-obj) obj))
518 (push so so-list)))))