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.
14 ;;;; $Id: mop.lisp,v 1.65 2003/04/29 09:24:27 kevin Exp $
16 ;;;; This file is Copyright (c) 2000-2002 by Kevin M. Rosenberg
18 ;;;; *************************************************************************
20 (in-package :hyperobject)
22 (eval-when (:compile-toplevel :execute)
23 (declaim (optimize (speed 2) (safety 2) (compilation-speed 0) (debug 2))))
27 (defclass hyperobject-class (standard-class)
28 ( ;; slots initialized in defclass
29 (user-name :initarg :user-name :type string :initform nil
31 :documentation "User name for class")
32 (default-print-slots :initarg :default-print-slots :type list :initform nil
33 :accessor default-print-slots
34 :documentation "Defaults slots for a view")
35 (description :initarg :description :initform nil
37 :documentation "Class description")
38 (version :initarg :version :initform nil
40 :documentation "Version number for class")
41 (sql-name :initarg :sql-name :initform nil)
43 ;;; The remainder of these fields are calculated one time
44 ;;; in finalize-inheritence.
46 (subobjects :initform nil :accessor subobjects
48 "List of fields that contain a list of subobjects objects.")
49 (hyperlinks :type list :initform nil :accessor hyperlinks
50 :documentation "List of fields that have hyperlinks")
51 (direct-rules :type list :initform nil :initarg :direct-rules
52 :accessor direct-rules
53 :documentation "List of rules to fire on slot changes.")
54 (class-id :type integer :initform nil
56 :documentation "Unique ID for the class")
57 (default-view :initform nil :initarg :default-view :accessor default-view
58 :documentation "The default view for a class")
61 (create-table-cmd :initform nil :reader create-table-cmd)
62 (create-indices-cmds :initform nil :reader create-index-cmds)
63 (drop-table-cmd :initform nil :reader drop-table-cmd)
65 (views :type list :initform nil :initarg :views :accessor views
66 :documentation "List of views")
67 (rules :type list :initform nil :initarg :rules :accessor rules
68 :documentation "List of rules")
70 (:documentation "Metaclass for Markup Language classes."))
72 (defclass subobject ()
73 ((name-class :type symbol :initform nil :initarg :name-class :reader name-class)
74 (name-slot :type symbol :initform nil :initarg :name-slot :reader name-slot)
75 (lookup :type (or function symbol) :initform nil :initarg :lookup :reader lookup)
76 (lookup-keys :type list :initform nil :initarg :lookup-keys
78 (:documentation "Contains subobject information"))
81 (defmethod print-object ((obj subobject) (s stream))
82 (print-unreadable-object (obj s :type t :identity t)
83 (format s "~S" (name obj))))
85 (defclass hyperlink ()
86 ((name :type symbol :initform nil :initarg :name :reader name)
88 ;; The type specifier seems to break sbcl
89 :type (or function symbol)
91 :initform nil :initarg :lookup :reader lookup)
92 (link-parameters :type list :initform nil :initarg :link-parameters
93 :reader link-parameters)))
95 (defmethod print-object ((obj hyperlink) (s stream))
96 (print-unreadable-object (obj s :type t :identity t)
97 (format s "~S" (name obj))))
100 (defmethod validate-superclass ((class hyperobject-class) (superclass standard-class))
103 (defmethod finalize-inheritance :after ((cl hyperobject-class))
104 (init-hyperobject-class cl)
107 (eval-when (:compile-toplevel :load-toplevel :execute)
108 (when (>= (length (generic-function-lambda-list
109 (ensure-generic-function
110 'compute-effective-slot-definition)))
112 (pushnew :ho-normal-cesd cl:*features*))
114 (when (>= (length (generic-function-lambda-list
115 (ensure-generic-function
116 'direct-slot-definition-class)))
118 (pushnew :ho-normal-dsdc cl:*features*))
120 (when (>= (length (generic-function-lambda-list
121 (ensure-generic-function
122 'effective-slot-definition-class)))
124 (pushnew :ho-normal-esdc cl:*features*)))
127 (defmethod direct-slot-definition-class ((cl hyperobject-class)
128 #+ho-normal-dsdc &rest iargs)
129 (find-class 'hyperobject-dsd))
131 (defmethod effective-slot-definition-class ((cl hyperobject-class)
132 #+ho-normal-esdc &rest iargs)
133 (find-class 'hyperobject-esd))
138 (eval-when (:compile-toplevel :load-toplevel :execute)
139 (defmacro process-class-option (slot-name &optional required)
141 `(defmethod clos:process-a-class-option ((class hyperobject-class)
142 (name (eql ,slot-name))
144 (when (and ,required (null value))
145 (error "hyperobject class slot ~A must have a value" name))
146 (list name `',value))
147 #+(or allegro sbcl cmu scl)
148 (declare (ignore slot-name required))
151 (defmacro process-slot-option (slot-name)
153 `(defmethod clos:process-a-slot-option ((class hyperobject-class)
154 (option (eql ,slot-name))
156 already-processed-options
158 (list* option `',value already-processed-options))
160 (declare (ignore slot-name))
163 (dolist (option *class-options*)
164 (eval `(process-class-option ,option)))
165 (dolist (option *slot-options*)
166 (eval `(process-slot-option ,option)))
169 `(defclass hyperobject-dsd (standard-direct-slot-definition)
170 (,@(mapcar #'(lambda (x)
171 `(,(intern (symbol-name x))
173 *slot-options-no-initarg*)
174 ,@(mapcar #'(lambda (x)
175 `(,(intern (symbol-name x))
177 ,(intern (symbol-name x) (symbol-name :keyword))
180 ,(intern (concatenate 'string
185 `(defclass hyperobject-esd (standard-effective-slot-definition)
186 (,@(mapcar #'(lambda (x)
187 `(,(intern (symbol-name x))
189 ,(intern (symbol-name x) (symbol-name :keyword))
192 ,(intern (concatenate 'string
195 (append *slot-options* *slot-options-no-initarg*)))))
198 (defun intern-in-keyword (obj)
205 (intern (symbol-name obj) (find-package 'keyword)))
207 (cons (intern-in-keyword (car obj) ) (intern-in-keyword (cdr obj))))
211 (defun canonicalize-value-type (vt)
216 (cons (ensure-keyword (car vt)) (cdr vt)))
221 (defmethod compute-effective-slot-definition :around ((cl hyperobject-class) #+ho-normal-cesd name dsds)
222 #+allegro (declare (ignore name))
223 (let* ((dsd (car dsds))
224 (value-type (canonicalize-value-type (slot-value dsd 'value-type))))
225 (multiple-value-bind (sql-type length) (value-type-to-sql-type value-type)
226 (setf (slot-value dsd 'sql-type) sql-type)
227 (setf (slot-value dsd 'type) (value-type-to-lisp-type value-type))
228 (let ((ia (compute-effective-slot-definition-initargs cl #+lispworks name dsds)))
230 #'make-instance 'hyperobject-esd
231 :value-type value-type
234 :print-formatter (slot-value dsd 'print-formatter)
235 :subobject (slot-value dsd 'subobject)
236 :hyperlink (slot-value dsd 'hyperlink)
237 :hyperlink-parameters (slot-value dsd 'hyperlink-parameters)
238 :description (slot-value dsd 'description)
239 :user-name (slot-value dsd 'user-name)
240 :index (slot-value dsd 'index)
241 :value-constraint (slot-value dsd 'value-constraint)
242 :null-allowed (slot-value dsd 'null-allowed)
245 (defmethod compute-effective-slot-definition :around ((cl hyperobject-class) #+ho-normal-cesd name dsds)
246 #+ho-normal-cesd (declare (ignore name))
247 (let ((esd (call-next-method)))
248 (multiple-value-bind (sql-type length) (value-type-to-sql-type value-type)
249 (setf (slot-value esd 'sql-type) sql-type)
250 (setf (slot-value esd 'length) length)
251 (setf (slot-value esd 'type) (value-type-to-lisp-type value-type))
252 (setf (slot-value esd 'value-type) (canonicalize-value-type (slot-value (car dsds) 'value-type)))
257 (setq cl:*features* (delete :ho-normal-cesd cl:*features*))
259 (setq cl:*features* (delete :ho-normal-dsdc cl:*features*))
261 (setq cl:*features* (delete :ho-normal-esdc cl:*features*))
263 (defun value-type-to-lisp-type (value-type)
264 (case (if (atom value-type)
267 ((:string :cdata :varchar :char)
270 '(or null character))
277 ((:float :single-float)
278 '(or null single-float))
280 '(or null double-float))
284 (defun value-type-to-sql-type (value-type)
285 "Return two values, the sql type and field length."
286 (let ((type (if (atom value-type)
289 (length (when (consp value-type)
299 ((:float :single-float)
307 ;;;; Class initialization function
309 ;; defines a slot-unbound method for class and slot-name, fills
310 ;; the slot by calling reader function with the slot values of
311 ;; the instance's reader-keys
312 (defmacro def-lazy-reader (class slot-name reader &rest reader-keys)
313 (let* ((the-slot-name (gensym))
315 (the-instance (gensym))
317 (dolist (key reader-keys)
318 (push (list 'slot-value the-instance (list 'quote key)) keys))
319 (setq keys (nreverse keys))
320 `(defmethod slot-unbound (,the-class (,the-instance ,class)
321 (,the-slot-name (eql ',slot-name)))
322 (declare (ignore ,the-class))
323 (setf (slot-value ,the-instance ,the-slot-name) (,reader ,@keys)))))
327 (defun intern-eql-specializer (slot)
330 #+(or sbcl cmu lispworks)
331 (defun ensure-lazy-reader (class-name slot-name reader &rest reader-keys)
333 (gf (ensure-generic-function 'slot-unbound)))
334 (dolist (key reader-keys)
335 (push (list 'slot-value 'the-instance (list 'quote key)) keys))
336 (setq keys (nreverse keys))
337 (multiple-value-bind (method-lambda init-args-values)
340 (class-prototype (generic-function-method-class gf))
342 `(lambda (the-class the-instance the-slot-name)
343 (declare (ignore the-class))
344 (setf (slot-value the-instance the-slot-name) (,reader ,@keys)))
346 '(the-class the-instance the-slot-name)
350 `(setf (slot-value the-instance the-slot-name) (,reader ,@keys))
354 #'make-instance (generic-function-method-class gf)
355 ':specializers (list (class-of (find-class class-name))
356 (find-class class-name)
357 (intern-eql-specializer slot-name))
358 ':lambda-list '(the-class the-instance the-slot-name)
359 ':function (compile nil method-lambda)
360 init-args-values)))))
362 (defun finalize-subobjects (cl)
363 "Process class subobjects slot"
364 (setf (subobjects cl)
365 (let ((subobjects '()))
366 (dolist (slot (class-slots cl))
368 (subobj-def (esd-subobject slot))
370 (make-instance 'subobject
371 :name-class (class-name cl)
372 :name-slot (slot-definition-name slot)
373 :lookup (if (atom subobj-def)
376 :lookup-keys (if (atom subobj-def)
379 (unless (eq (lookup subobject) t)
380 #-(or sbcl cmu lispworks)
382 `(hyperobject::def-lazy-reader ,(name-class subobject)
383 ,(name-slot subobject) ,(lookup subobject)
384 ,@(lookup-keys subobject)))
385 #+(or sbcl cmu lispworks)
386 (apply #'ensure-lazy-reader
387 (name-class subobject) (name-slot subobject)
388 (lookup subobject) (lookup-keys subobject)))
389 (push subobject subobjects))))
390 ;; sbcl/cmu reverse class-slots compared to the defclass form
391 ;; so re-reverse on cmu/sbcl
392 #+(or cmu sbcl) subobjects
393 #-(or cmu sbcl) (nreverse subobjects)
396 (defun finalize-documentation (cl)
397 "Calculate class documentation slot"
398 (awhen (slot-value cl 'user-name)
399 (setf (slot-value cl 'user-name)
400 (etypecase (slot-value cl 'user-name)
402 ((or string symbol) it))))
403 (awhen (slot-value cl 'description)
404 (setf (slot-value cl 'description)
405 (etypecase (slot-value cl 'description)
407 ((or string symbol) it))))
409 (let ((*print-circle* nil))
410 (setf (documentation (class-name cl) 'class)
411 (format nil "Hyperobject~A~A~A~A"
413 (format nil ": ~A" it ""))
414 (aif (description cl)
415 (format nil "~%Class description: ~A" it) "")
417 (format nil "~%Subobjects:~{ ~A~}" (mapcar #'name-slot it)) "")
418 (aif (default-print-slots cl)
419 (format nil "~%Default print slots:~{ ~A~}" it) "")
422 (defun finalize-hyperlinks (cl)
423 (let ((hyperlinks '()))
424 (dolist (esd (class-slots cl))
425 (awhen (slot-value esd 'hyperlink)
427 (make-instance 'hyperlink
428 :name (slot-definition-name esd)
430 :link-parameters (slot-value esd 'hyperlink-parameters))
432 ;; cmu/sbcl reverse class-slots compared to the defclass form
433 ;; hyperlinks is already reversed from the dolist/push loop, so re-reverse on sbcl/cmu
434 #-(or cmu sbcl) (setq hyperlinks (nreverse hyperlinks))
435 (setf (slot-value cl 'hyperlinks) hyperlinks)))
437 (defun init-hyperobject-class (cl)
438 "Initialize a hyperobject class. Calculates all class slots"
439 (finalize-subobjects cl)
441 (finalize-hyperlinks cl)
444 (finalize-documentation cl))
447 ;;;; *************************************************************************
448 ;;;; Metaclass Slot Accessors
449 ;;;; *************************************************************************
451 (defun find-slot-by-name (cl name)
452 (find name (class-slots cl) :key #'slot-definition-name))
454 (defun hyperobject-class-user-name (obj)
455 (awhen (user-name (class-of obj))
460 (defun hyperobject-class-subobjects (obj)
461 (subobjects (class-of obj)))
463 (defun hyperobject-class-hyperlinks (obj)
464 (hyperlinks (class-of obj)))
466 (defun hyperobject-class-slots (obj)
467 ;; cmucl/sbcl reverse class-slots
468 #+(or cmu sbcl) (reverse (class-slots (class-of obj)))
469 #-(or cmu sbcl) (class-slots (class-of obj)))