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-2006 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 (compute-cached-values :initform nil :accessor compute-cached-values
56 "List of fields that contain a list of compute-cached-value objects.")
57 (hyperlinks :type list :initform nil :accessor hyperlinks
58 :documentation "List of fields that have hyperlinks")
59 (direct-rules :type list :initform nil :initarg :direct-rules
60 :accessor direct-rules
61 :documentation "List of rules to fire on slot changes.")
62 (direct-views :type list :initform nil :initarg :direct-views
63 :accessor direct-views
64 :documentation "List of views")
65 (class-id :type integer :initform (+ (* 1000000 (get-universal-time)) (random 1000000))
67 :documentation "Unique ID for the class")
68 (default-view :initform nil :initarg :default-view :accessor default-view
69 :documentation "The default view for a class")
70 (documementation :initform nil :initarg :documentation
71 :documentation "Documentation string for hyperclass.")
74 (create-table-cmd :initform nil :reader create-table-cmd)
75 (create-indices-cmds :initform nil :reader create-index-cmds)
76 (drop-table-cmd :initform nil :reader drop-table-cmd)
78 (views :type list :initform nil :initarg :views :accessor views
79 :documentation "List of views")
80 (rules :type list :initform nil :initarg :rules :accessor rules
81 :documentation "List of rules")
83 (:documentation "Metaclass for Markup Language classes."))
85 (defclass subobject ()
86 ((name-class :type symbol :initarg :name-class :reader name-class)
87 (name-slot :type symbol :initarg :name-slot :reader name-slot)
88 (lazy-class :type symbol :initarg :lazy-class :reader lazy-class)
89 (lookup :type (or function symbol) :initarg :lookup :reader lookup)
90 (lookup-keys :type list :initarg :lookup-keys :reader lookup-keys))
91 (:documentation "subobject information")
92 (:default-initargs :name-class nil :name-slot nil :lazy-class nil
93 :lookup nil :lookup-keys nil))
95 (defclass compute-cached-value ()
96 ((name-class :type symbol :initarg :name-class :reader name-class)
97 (name-slot :type symbol :initarg :name-slot :reader name-slot)
98 (lazy-class :type symbol :initarg :lazy-class
100 (lookup :type (or function symbol) :initarg :lookup :reader lookup)
101 (lookup-keys :type list :initarg :lookup-keys :reader lookup-keys))
102 (:documentation "subobject information")
103 (:default-initargs :name-class nil :name-slot nil :lazy-class nil
104 :lookup nil :lookup-keys nil))
107 (defmethod print-object ((obj subobject) s)
108 (print-unreadable-object (obj s :type t)
109 (format s "~S" (name-slot obj))))
111 (defclass hyperlink ()
112 ((name :type symbol :initform nil :initarg :name :reader name)
114 ;; The type specifier seems to break sbcl
115 :type (or function symbol)
117 :initform nil :initarg :lookup :reader lookup)
118 (link-parameters :type list :initform nil :initarg :link-parameters
119 :reader link-parameters)))
121 (defmethod print-object ((obj hyperlink) s)
122 (print-unreadable-object (obj s :type t :identity t)
123 (format s "~S" (name obj))))
125 (defmethod validate-superclass ((class hyperobject-class) (superclass standard-class))
129 (declaim (inline delistify))
130 (defun delistify (list)
131 "Some MOPs, like openmcl 0.14.2, cons attribute values in a list."
136 (defun remove-keyword-arg (arglist akey)
137 (let ((mylist arglist)
139 (labels ((pop-arg (alist)
140 (let ((arg (pop alist))
142 (unless (equal arg akey)
143 (setf newlist (append (list arg val) newlist)))
144 (when alist (pop-arg alist)))))
148 (defun remove-keyword-args (arglist akeys)
149 (let ((mylist arglist)
151 (labels ((pop-arg (alist)
152 (let ((arg (pop alist))
154 (unless (find arg akeys)
155 (setf newlist (append (list arg val) newlist)))
156 (when alist (pop-arg alist)))))
161 (defmethod shared-initialize :around ((class hyperobject-class) slot-names
163 &key direct-superclasses
164 user-name sql-name name description
166 ;(format t "ii ~S ~S ~S ~S ~S~%" initargs base-table direct-superclasses user-name sql-name)
167 (let ((root-class (find-class 'hyperobject nil))
168 (vmc 'hyperobject-class)
169 user-name-plural user-name-str sql-name-str)
170 ;; when does CLSQL pass :qualifier to initialize instance?
173 (delistify user-name)
174 (and name (format nil "~:(~A~)" name))))
179 (and name (lisp-name-to-sql-name name))))
183 (and name (lisp-name-to-sql-name name)))
185 (setq description (delistify description))
187 (setq user-name-plural
188 (if (and (consp user-name) (second user-name))
190 (and user-name-str (format nil "~A~P" user-name-str 2))))
192 (flet ((do-call-next-method (direct-superclasses)
193 (let ((fn-args (list class slot-names :direct-superclasses direct-superclasses))
194 (rm-args '(:direct-superclasses)))
196 (setq fn-args (nconc fn-args (list :user-name user-name-str)))
197 (push :user-name rm-args))
198 (when user-name-plural
199 (setq fn-args (nconc fn-args (list :user-name-plural user-name-plural)))
200 (push :user-name-plural rm-args))
202 (setq fn-args (nconc fn-args (list :sql-name sql-name-str)))
203 (push :sql-name rm-args))
205 (setq fn-args (nconc fn-args (list :description description)))
206 (push :description rm-args))
207 (setq fn-args (nconc fn-args (remove-keyword-args initargs rm-args)))
208 (apply #'call-next-method fn-args))))
210 (if (some #'(lambda (super) (typep super vmc))
212 (do-call-next-method direct-superclasses)
213 (do-call-next-method direct-superclasses #+nil (append (list root-class)
214 direct-superclasses)))
215 (do-call-next-method direct-superclasses)))))
218 (defmethod finalize-inheritance :after ((cl hyperobject-class))
219 "Initialize a hyperobject class. Calculates all class slots"
220 (finalize-subobjects cl)
221 (finalize-compute-cached cl)
222 (init-hyperobject-class cl))
224 (eval-when (:compile-toplevel :load-toplevel :execute)
225 (when (>= (length (generic-function-lambda-list
226 (ensure-generic-function
227 'compute-effective-slot-definition)))
229 (pushnew :ho-normal-cesd cl:*features*))
231 (when (>= (length (generic-function-lambda-list
232 (ensure-generic-function
233 'direct-slot-definition-class)))
235 (pushnew :ho-normal-dsdc cl:*features*))
237 (when (>= (length (generic-function-lambda-list
238 (ensure-generic-function
239 'effective-slot-definition-class)))
241 (pushnew :ho-normal-esdc cl:*features*)))
243 (defmethod direct-slot-definition-class ((cl hyperobject-class)
244 #+ho-normal-dsdc &rest iargs)
245 (declare (ignore iargs))
246 (find-class 'hyperobject-dsd))
248 (defmethod effective-slot-definition-class ((cl hyperobject-class)
249 #+ho-normal-esdc &rest iargs)
250 (declare (ignore iargs))
251 (find-class 'hyperobject-esd))
255 (eval-when (:compile-toplevel :load-toplevel :execute)
256 (defmacro process-class-option (slot-name &optional required)
258 `(defmethod clos:process-a-class-option ((class hyperobject-class)
259 (name (eql ,slot-name))
261 (when (and ,required (null value))
262 (error "hyperobject class slot ~A must have a value" name))
263 (list name `',value))
264 #+(or allegro sbcl cmu scl openmcl)
265 (declare (ignore slot-name required))
268 (defmacro process-slot-option (slot-name)
270 `(defmethod clos:process-a-slot-option ((class hyperobject-class)
271 (option (eql ,slot-name))
273 already-processed-options
275 (list* option `',value already-processed-options))
277 (declare (ignore slot-name))
280 (dolist (option *class-options*)
281 (eval `(process-class-option ,option)))
282 (dolist (option *slot-options*)
283 (eval `(process-slot-option ,option)))
286 `(defclass hyperobject-dsd (standard-direct-slot-definition)
287 (,@(mapcar #'(lambda (x)
288 `(,(intern (symbol-name x))
290 *slot-options-no-initarg*)
291 ,@(mapcar #'(lambda (x)
292 `(,(intern (symbol-name x))
294 ,(intern (symbol-name x) (symbol-name :keyword))
297 ,(intern (concatenate 'string
302 `(defclass hyperobject-esd (standard-effective-slot-definition)
303 (,@(mapcar #'(lambda (x)
304 `(,(intern (symbol-name x))
306 ,(intern (symbol-name x) (symbol-name :keyword))
309 ,(intern (concatenate 'string
312 (append *slot-options* *slot-options-no-initarg*)))))
315 (defun intern-in-keyword (obj)
322 (intern (symbol-name obj) (find-package 'keyword)))
324 (cons (intern-in-keyword (car obj) ) (intern-in-keyword (cdr obj))))
328 (defun canonicalize-value-type (vt)
333 (list (ensure-keyword (car vt)) (cadr vt)))
338 (defmethod compute-effective-slot-definition :around ((cl hyperobject-class)
339 #+ho-normal-cesd name
341 (declare (ignore #+ho-normal-cesd name))
342 (let ((esd (call-next-method)))
343 (if (typep esd 'hyperobject-esd)
344 (compute-hyperobject-esd esd dsds)
347 (defun compute-hyperobject-esd (esd dsds)
348 (let* ((dsd (car dsds)))
349 (multiple-value-bind (sql-type sql-length)
350 (value-type-to-sql-type (dsd-value-type dsd))
351 (setf (esd-sql-type esd) sql-type)
352 (setf (esd-sql-length esd) sql-length))
353 (setf (esd-user-name esd)
354 (aif (dsd-user-name dsd)
356 (string-downcase (symbol-name (slot-definition-name dsd)))))
357 (setf (esd-sql-name esd)
358 (aif (dsd-sql-name dsd)
360 (lisp-name-to-sql-name (slot-definition-name dsd))))
361 (setf (esd-sql-name esd)
362 (aif (dsd-sql-name dsd)
364 (lisp-name-to-sql-name (slot-definition-name dsd))))
365 (dolist (name '(value-type print-formatter subobject hyperlink
366 hyperlink-parameters unbound-lookup
367 description value-constraint indexed null-allowed
368 unique short-description void-text read-only-groups
369 hidden-groups unit disable-predicate view-type
370 list-of-values compute-cached-value stored))
371 (setf (slot-value esd name) (slot-value dsd name)))
374 (defun lisp-name-to-sql-name (lisp)
375 "Convert a lisp name (atom or list, string or symbol) into a canonical
377 (unless (stringp lisp)
380 (symbol (symbol-name lisp))
381 (t (write-to-string lisp)))))
382 (do* ((len (length lisp))
383 (sql (make-string len))
385 ((= i len) (string-upcase sql))
389 (let ((c (char lisp i)))
391 ((#\- #\$ #\+ #\#) #\_)
395 (setq cl:*features* (delete :ho-normal-cesd cl:*features*))
397 (setq cl:*features* (delete :ho-normal-dsdc cl:*features*))
399 (setq cl:*features* (delete :ho-normal-esdc cl:*features*))
401 (defun lisp-type-is-a-string (type)
402 (or (eq type 'string)
403 (and (listp type) (some #'(lambda (x) (eq x 'string)) type))))
405 (defun value-type-is-a-string (type)
406 (or (eq type 'string)
408 (and (listp type) (some #'(lambda (x) (or (eq x 'string)
412 (defun base-value-type (value-type)
413 (if (atom value-type)
417 (defun value-type-to-lisp-type (value-type)
418 (case (base-value-type value-type)
419 ((:string :cdata :varchar :char)
424 '(or null character))
429 ((:integer :long-integer)
431 ((:float :single-float)
432 '(or null single-float))
434 '(or null double-float))
438 (defun value-type-to-sql-type (value-type)
439 "Return two values, the sql type and field length."
440 (let ((type (base-value-type value-type))
441 (length (when (consp value-type)
455 ((:float :single-float)
465 ;;;; Class initialization function
467 ;; One entry for each class with lazy readers defined. The value is a plist mapping
468 ;; slot-name to a lazy reader, each of which is a list of a function and slot-names.
469 (defvar *lazy-readers* (make-hash-table))
471 (defmethod slot-unbound ((class hyperobject-class) instance slot-name)
473 (loop for super in (class-precedence-list class)
474 as lazy-reader = (getf (gethash super *lazy-readers*) slot-name)
475 when lazy-reader return it)))
477 (setf (slot-value instance slot-name)
478 (if (atom lazy-reader)
479 (make-instance lazy-reader)
480 (apply (car lazy-reader)
481 (loop for arg-slot-name in (cdr lazy-reader)
482 collect (slot-value instance arg-slot-name)))))
483 ;; No lazy reader -- defer to regular slot-unbound handling.
484 (call-next-method))))
486 ;; The reader is a function and the reader-keys are slot names. The slot is lazily set to
487 ;; the result of applying the function to the slot-values of those slots, and that value
489 (defun ensure-lazy-reader (cl class-name slot-name lazy-class reader
491 (declare (ignore class-name))
492 (setf (getf (gethash cl *lazy-readers*) slot-name)
495 (list* reader (copy-list reader-keys)))))
497 (defun remove-lazy-reader (class-name slot-name)
498 (setf (getf (gethash (find-class class-name) *lazy-readers*) slot-name)
502 (defun store-lazily-computed-objects (cl slot esd-accessor obj-class)
503 (setf (slot-value cl slot)
505 (dolist (slot (class-slots cl))
507 (def (funcall esd-accessor slot))
508 (let ((obj (make-instance obj-class
509 :name-class (class-name cl)
510 :name-slot (slot-definition-name slot)
511 :lazy-class (when (atom def)
513 :lookup (when (listp def)
515 :lookup-keys (when (listp def)
517 (unless (eq (lookup obj) t)
518 (apply #'ensure-lazy-reader
520 (name-class obj) (name-slot obj)
522 (lookup obj) (lookup-keys obj))
524 ;; sbcl/cmu reverse class-slots compared to the defclass form
525 ;; so re-reverse on cmu/sbcl
527 #-(or cmu sbcl) (nreverse objs)
530 (defun finalize-subobjects (cl)
531 (store-lazily-computed-objects cl 'subobjects 'esd-subobject 'subobject))
533 (defun finalize-compute-cached (cl)
534 (store-lazily-computed-objects cl 'compute-cached-values
535 'esd-compute-cached-value 'compute-cached-value))
538 (defun finalize-documentation (cl)
539 "Calculate class documentation slot"
540 (let ((*print-circle* nil))
541 (setf (documentation cl 'type)
542 (format nil "Hyperobject~A~A~A~A"
544 (format nil ": ~A" it ""))
545 (aif (description cl)
546 (format nil "~%Class description: ~A" it) "")
548 (format nil "~%Subobjects:~{ ~A~}" (mapcar #'name-slot it)) "")
549 (aif (default-print-slots cl)
550 (format nil "~%Default print slots:~{ ~A~}" it) "")
553 (defun finalize-hyperlinks (cl)
554 (let ((hyperlinks '()))
555 (dolist (esd (class-slots cl))
556 (awhen (slot-value esd 'hyperlink)
558 (make-instance 'hyperlink
559 :name (slot-definition-name esd)
561 :link-parameters (slot-value esd 'hyperlink-parameters))
563 ;; cmu/sbcl reverse class-slots compared to the defclass form
564 ;; hyperlinks is already reversed from the dolist/push loop, so re-reverse on sbcl/cmu
565 #-(or cmu sbcl) (setq hyperlinks (nreverse hyperlinks))
566 (setf (slot-value cl 'hyperlinks) hyperlinks)))
568 (defun init-hyperobject-class (cl)
569 "Initialize a hyperobject class. Calculates all class slots"
571 (finalize-hyperlinks cl)
574 (finalize-documentation cl))
579 ;;;; *************************************************************************
580 ;;;; Metaclass Slot Accessors
581 ;;;; *************************************************************************
583 (defun find-slot-by-name (cl name)
584 (find name (class-slots cl) :key #'slot-definition-name))
586 (defun hyperobject-class-user-name (obj)
587 (user-name (class-of obj)))
589 (defun hyperobject-class-user-name-plural (obj)
590 (user-name-plural (class-of obj)))
592 (defun hyperobject-class-subobjects (obj)
593 (subobjects (class-of obj)))
595 (defun hyperobject-class-hyperlinks (obj)
596 (hyperlinks (class-of obj)))
598 (defun hyperobject-class-slots (obj)
599 ;; cmucl/sbcl reverse class-slots
600 #+(or cmu sbcl) (reverse (class-slots (class-of obj)))
601 #-(or cmu sbcl) (class-slots (class-of obj)))
603 (defun all-subobjects (obj)
604 "Returns a list of all subobjects in an object"
606 (dolist (subobj-obj (subobjects (class-of obj)) (nreverse so-list))
607 (dolist (so (funcall (name-slot subobj-obj) obj))
608 (push so so-list)))))