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.86 2003/07/29 21:21:52 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 (closures :initarg :closures :initform nil
\r
42 :documentation "Closures to call on slot chnages")
\r
43 (sql-name :initarg :sql-name :accessor sql-name :initform nil
\r
44 :documentation "SQL Name for this class")
\r
45 (guid :initarg :guid :accessor guid :initform nil
\r
46 :documentation "ID string for this class")
\r
48 ;;; The remainder of these fields are calculated one time
\r
49 ;;; in finalize-inheritence.
\r
51 (subobjects :initform nil :accessor subobjects
\r
53 "List of fields that contain a list of subobjects objects.")
\r
54 (hyperlinks :type list :initform nil :accessor hyperlinks
\r
55 :documentation "List of fields that have hyperlinks")
\r
56 (direct-rules :type list :initform nil :initarg :direct-rules
\r
57 :accessor direct-rules
\r
58 :documentation "List of rules to fire on slot changes.")
\r
59 (direct-views :type list :initform nil :initarg :direct-views
\r
60 :accessor direct-views
\r
61 :documentation "List of views")
\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 :initarg :name-class :reader name-class)
\r
82 (name-slot :type symbol :initarg :name-slot :reader name-slot)
\r
83 (subobj-class :type symbol :initarg :subobj-class :reader subobj-class)
\r
84 (lookup :type (or function symbol) :initarg :lookup :reader lookup)
\r
85 (lookup-keys :type list :initarg :lookup-keys :reader lookup-keys))
\r
86 (:documentation "subobject information")
\r
87 (:default-initargs :name-class nil :name-slot nil :subobj-class nil
\r
88 :lookup nil :lookup-keys nil))
\r
91 (defmethod print-object ((obj subobject) (s stream))
\r
92 (print-unreadable-object (obj s :type t)
\r
93 (format s "~S" (name-slot obj))))
\r
95 (defclass hyperlink ()
\r
96 ((name :type symbol :initform nil :initarg :name :reader name)
\r
98 ;; The type specifier seems to break sbcl
\r
99 :type (or function symbol)
\r
101 :initform nil :initarg :lookup :reader lookup)
\r
102 (link-parameters :type list :initform nil :initarg :link-parameters
\r
103 :reader link-parameters)))
\r
105 (defmethod print-object ((obj hyperlink) (s stream))
\r
106 (print-unreadable-object (obj s :type t :identity t)
\r
107 (format s "~S" (name obj))))
\r
109 (defmethod validate-superclass ((class hyperobject-class) (superclass standard-class))
\r
112 (defmethod finalize-inheritance :after ((cl hyperobject-class))
\r
113 ;; Work-around needed for OpenMCL
\r
115 (unless (find-class (class-name cl))
\r
116 (setf (find-class (class-name cl)) cl))
\r
118 (init-hyperobject-class cl)
\r
121 (eval-when (:compile-toplevel :load-toplevel :execute)
\r
122 (when (>= (length (generic-function-lambda-list
\r
123 (ensure-generic-function
\r
124 'compute-effective-slot-definition)))
\r
126 (pushnew :ho-normal-cesd cl:*features*))
\r
128 (when (>= (length (generic-function-lambda-list
\r
129 (ensure-generic-function
\r
130 'direct-slot-definition-class)))
\r
132 (pushnew :ho-normal-dsdc cl:*features*))
\r
134 (when (>= (length (generic-function-lambda-list
\r
135 (ensure-generic-function
\r
136 'effective-slot-definition-class)))
\r
138 (pushnew :ho-normal-esdc cl:*features*)))
\r
140 ;; Slot definitions
\r
141 (defmethod direct-slot-definition-class ((cl hyperobject-class)
\r
142 #+ho-normal-dsdc &rest iargs)
\r
143 (find-class 'hyperobject-dsd))
\r
145 (defmethod effective-slot-definition-class ((cl hyperobject-class)
\r
146 #+ho-normal-esdc &rest iargs)
\r
147 (find-class 'hyperobject-esd))
\r
150 ;;; Slot definitions
\r
152 (eval-when (:compile-toplevel :load-toplevel :execute)
\r
153 (defmacro process-class-option (slot-name &optional required)
\r
155 `(defmethod clos:process-a-class-option ((class hyperobject-class)
\r
156 (name (eql ,slot-name))
\r
158 (when (and ,required (null value))
\r
159 (error "hyperobject class slot ~A must have a value" name))
\r
160 (list name `',value))
\r
161 #+(or allegro sbcl cmu scl openmcl)
\r
162 (declare (ignore slot-name required))
\r
165 (defmacro process-slot-option (slot-name)
\r
167 `(defmethod clos:process-a-slot-option ((class hyperobject-class)
\r
168 (option (eql ,slot-name))
\r
170 already-processed-options
\r
172 (list* option `',value already-processed-options))
\r
174 (declare (ignore slot-name))
\r
177 (dolist (option *class-options*)
\r
178 (eval `(process-class-option ,option)))
\r
179 (dolist (option *slot-options*)
\r
180 (eval `(process-slot-option ,option)))
\r
183 `(defclass hyperobject-dsd (standard-direct-slot-definition)
\r
184 (,@(mapcar #'(lambda (x)
\r
185 `(,(intern (symbol-name x))
\r
187 *slot-options-no-initarg*)
\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 :dsd-)
\r
196 (symbol-name x)))))
\r
199 `(defclass hyperobject-esd (standard-effective-slot-definition)
\r
200 (,@(mapcar #'(lambda (x)
\r
201 `(,(intern (symbol-name x))
\r
203 ,(intern (symbol-name x) (symbol-name :keyword))
\r
206 ,(intern (concatenate 'string
\r
207 (symbol-name :esd-)
\r
208 (symbol-name x)))))
\r
209 (append *slot-options* *slot-options-no-initarg*)))))
\r
212 (defun intern-in-keyword (obj)
\r
219 (intern (symbol-name obj) (find-package 'keyword)))
\r
221 (cons (intern-in-keyword (car obj) ) (intern-in-keyword (cdr obj))))
\r
225 (defun canonicalize-value-type (vt)
\r
228 (ensure-keyword vt))
\r
230 (list (ensure-keyword (car vt)) (cadr vt)))
\r
234 (defmethod compute-effective-slot-definition :around ((cl hyperobject-class)
\r
235 #+ho-normal-cesd name
\r
237 (declare (ignore #+ho-normal-cesd name))
\r
238 (let ((esd (call-next-method)))
\r
239 (if (typep esd 'hyperobject-esd)
\r
240 (compute-hyperobject-esd esd dsds)
\r
243 (defun compute-hyperobject-esd (esd dsds)
\r
244 (let* ((dsd (car dsds))
\r
245 (value-type (canonicalize-value-type (slot-value dsd 'value-type))))
\r
246 (multiple-value-bind (sql-type sql-length)
\r
247 (value-type-to-sql-type value-type)
\r
248 (setf (esd-sql-type esd) sql-type)
\r
249 (setf (esd-sql-length esd) sql-length))
\r
250 (setf (slot-value esd 'type) (value-type-to-lisp-type value-type))
\r
251 (setf (esd-value-type esd) value-type)
\r
252 (setf (esd-user-name esd)
\r
253 (aif (dsd-user-name dsd)
\r
255 (string-downcase (symbol-name (slot-definition-name dsd)))))
\r
256 (setf (esd-sql-name esd)
\r
257 (aif (dsd-sql-name dsd)
\r
259 (lisp-name-to-sql-name (slot-definition-name dsd))))
\r
260 (setf (esd-sql-name esd)
\r
261 (aif (dsd-sql-name dsd)
\r
263 (lisp-name-to-sql-name (slot-definition-name dsd))))
\r
264 (dolist (name '(print-formatter subobject hyperlink hyperlink-parameters
\r
265 description value-constraint indexed null-allowed
\r
266 unique short-description void-text read-only-groups
\r
267 hidden-groups unit disable-predicate view-type
\r
268 list-of-values stored))
\r
269 (setf (slot-value esd name) (slot-value dsd name)))
\r
272 (defun lisp-name-to-sql-name (lisp)
\r
273 "Convert a lisp name (atom or list, string or symbol) into a canonical
\r
275 (unless (stringp lisp)
\r
278 (symbol (symbol-name lisp))
\r
279 (t (write-to-string lisp)))))
\r
280 (do* ((len (length lisp))
\r
281 (sql (make-string len))
\r
283 ((= i len) (string-upcase sql))
\r
284 (declare (fixnum i)
\r
285 (simple-string sql))
\r
286 (setf (schar sql i)
\r
287 (let ((c (char lisp i)))
\r
289 ((#\- #\$ #\+ #\#) #\_)
\r
293 (setq cl:*features* (delete :ho-normal-cesd cl:*features*))
\r
295 (setq cl:*features* (delete :ho-normal-dsdc cl:*features*))
\r
297 (setq cl:*features* (delete :ho-normal-esdc cl:*features*))
\r
299 (defun lisp-type-is-a-string (type)
\r
300 (or (eq type 'string)
\r
301 (and (listp type) (some #'(lambda (x) (eq x 'string)) type))))
\r
303 (defun base-value-type (value-type)
\r
304 (if (atom value-type)
\r
308 (defun value-type-to-lisp-type (value-type)
\r
309 (case (base-value-type value-type)
\r
310 ((:string :cdata :varchar :char)
\r
313 '(or null character))
\r
317 '(or null boolean))
\r
318 ((:integer :long-integer)
\r
319 '(or null integer))
\r
320 ((:float :single-float)
\r
321 '(or null single-float))
\r
323 '(or null double-float))
\r
327 (defun value-type-to-sql-type (value-type)
\r
328 "Return two values, the sql type and field length."
\r
329 (let ((type (base-value-type value-type))
\r
330 (length (when (consp value-type)
\r
331 (cadr value-type))))
\r
334 ((:char :character)
\r
338 ((:fixnum :integer)
\r
344 ((:float :single-float)
\r
352 ;;;; Class initialization function
\r
354 ;; One entry for each class with lazy readers defined. The value is a plist mapping
\r
355 ;; slot-name to a lazy reader, each of which is a list of a function and slot-names.
\r
356 (defvar *lazy-readers* (make-hash-table))
\r
358 (defmethod slot-unbound ((class hyperobject-class) instance slot-name)
\r
360 (loop for super in (class-precedence-list class)
\r
361 as lazy-reader = (getf (gethash super *lazy-readers*) slot-name)
\r
362 when lazy-reader return it)))
\r
364 (setf (slot-value instance slot-name)
\r
365 (if (atom lazy-reader)
\r
366 (make-instance lazy-reader)
\r
367 (apply (car lazy-reader)
\r
368 (loop for arg-slot-name in (cdr lazy-reader)
\r
369 collect (slot-value instance arg-slot-name)))))
\r
370 ;; No lazy reader -- defer to regular slot-unbound handling.
\r
371 (call-next-method))))
\r
373 ;; The reader is a function and the reader-keys are slot names. The slot is lazily set to
\r
374 ;; the result of applying the function to the slot-values of those slots, and that value
\r
375 ;; is also returned.
\r
376 (defun ensure-lazy-reader (cl class-name slot-name subobj-class reader
\r
378 (setf (getf (gethash cl *lazy-readers*) slot-name)
\r
381 (list* reader (copy-list reader-keys)))))
\r
383 (defun remove-lazy-reader (class-name slot-name)
\r
384 (setf (getf (gethash (find-class class-name) *lazy-readers*) slot-name)
\r
388 (defun finalize-subobjects (cl)
\r
389 "Process class subobjects slot"
\r
390 (setf (subobjects cl)
\r
391 (let ((subobjects '()))
\r
392 (dolist (slot (class-slots cl))
\r
394 (subobj-def (esd-subobject slot))
\r
396 (make-instance 'subobject
\r
397 :name-class (class-name cl)
\r
398 :name-slot (slot-definition-name slot)
\r
399 :subobj-class (when (atom subobj-def)
\r
401 :lookup (when (listp subobj-def)
\r
403 :lookup-keys (when (listp subobj-def)
\r
404 (cdr subobj-def)))))
\r
405 (unless (eq (lookup subobject) t)
\r
406 (apply #'ensure-lazy-reader
\r
408 (name-class subobject) (name-slot subobject)
\r
409 (subobj-class subobject)
\r
410 (lookup subobject) (lookup-keys subobject))
\r
411 (push subobject subobjects)))))
\r
412 ;; sbcl/cmu reverse class-slots compared to the defclass form
\r
413 ;; so re-reverse on cmu/sbcl
\r
414 #+(or cmu sbcl) subobjects
\r
415 #-(or cmu sbcl) (nreverse subobjects)
\r
418 (defun finalize-class-slots (cl)
\r
419 "Make sure all class slots have an expected value"
\r
420 (unless (user-name cl)
\r
421 (setf (user-name cl) (format nil "~:(~A~)" (class-name cl))))
\r
423 (setf (user-name-plural cl)
\r
424 (if (and (consp (user-name cl)) (cadr (user-name cl)))
\r
425 (cadr (user-name cl))
\r
426 (format nil "~A~P" (if (consp (user-name cl))
\r
427 (car (user-name cl))
\r
431 (dolist (name '(user-name description version guid sql-name))
\r
432 (awhen (slot-value cl name)
\r
433 (setf (slot-value cl name)
\r
438 (unless (sql-name cl)
\r
439 (setf (sql-name cl) (lisp-name-to-sql-name (class-name cl))))
\r
442 (defun finalize-documentation (cl)
\r
443 "Calculate class documentation slot"
\r
444 (let ((*print-circle* nil))
\r
445 (setf (documentation (class-name cl) 'class)
\r
446 (format nil "Hyperobject~A~A~A~A"
\r
447 (aif (user-name cl)
\r
448 (format nil ": ~A" it ""))
\r
449 (aif (description cl)
\r
450 (format nil "~%Class description: ~A" it) "")
\r
451 (aif (subobjects cl)
\r
452 (format nil "~%Subobjects:~{ ~A~}" (mapcar #'name-slot it)) "")
\r
453 (aif (default-print-slots cl)
\r
454 (format nil "~%Default print slots:~{ ~A~}" it) "")
\r
457 (defun finalize-hyperlinks (cl)
\r
458 (let ((hyperlinks '()))
\r
459 (dolist (esd (class-slots cl))
\r
460 (awhen (slot-value esd 'hyperlink)
\r
462 (make-instance 'hyperlink
\r
463 :name (slot-definition-name esd)
\r
465 :link-parameters (slot-value esd 'hyperlink-parameters))
\r
467 ;; cmu/sbcl reverse class-slots compared to the defclass form
\r
468 ;; hyperlinks is already reversed from the dolist/push loop, so re-reverse on sbcl/cmu
\r
469 #-(or cmu sbcl) (setq hyperlinks (nreverse hyperlinks))
\r
470 (setf (slot-value cl 'hyperlinks) hyperlinks)))
\r
472 (defun init-hyperobject-class (cl)
\r
473 "Initialize a hyperobject class. Calculates all class slots"
\r
474 (finalize-subobjects cl)
\r
475 (finalize-views cl)
\r
476 (finalize-hyperlinks cl)
\r
478 (finalize-rules cl)
\r
479 (finalize-class-slots cl)
\r
480 (finalize-documentation cl))
\r
483 ;;;; *************************************************************************
\r
484 ;;;; Metaclass Slot Accessors
\r
485 ;;;; *************************************************************************
\r
487 (defun find-slot-by-name (cl name)
\r
488 (find name (class-slots cl) :key #'slot-definition-name))
\r
490 (defun hyperobject-class-user-name (obj)
\r
491 (user-name (class-of obj)))
\r
493 (defun hyperobject-class-user-name-plural (obj)
\r
494 (user-name-plural (class-of obj)))
\r
496 (defun hyperobject-class-subobjects (obj)
\r
497 (subobjects (class-of obj)))
\r
499 (defun hyperobject-class-hyperlinks (obj)
\r
500 (hyperlinks (class-of obj)))
\r
502 (defun hyperobject-class-slots (obj)
\r
503 ;; cmucl/sbcl reverse class-slots
\r
504 #+(or cmu sbcl) (reverse (class-slots (class-of obj)))
\r
505 #-(or cmu sbcl) (class-slots (class-of obj)))
\r
507 (defun all-subobjects (obj)
\r
508 "Returns a list of all subobjects in an object"
\r
509 (let ((so-list '()))
\r
510 (dolist (subobj-obj (subobjects (class-of obj)) (nreverse so-list))
\r
511 (dolist (so (funcall (name-slot subobj-obj) obj))
\r
512 (push so so-list)))))
\r