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.62 2003/04/25 04:53:52 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 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)
90 :initform nil :initarg :lookup :reader lookup)
91 (link-parameters :type list :initform nil :initarg :link-parameters
92 :reader link-parameters)))
94 (defmethod print-object ((obj hyperlink) (s stream))
95 (print-unreadable-object (obj s :type t :identity t)
96 (format s "~S" (name obj))))
99 (defmethod validate-superclass ((class hyperobject-class) (superclass standard-class))
102 (defmethod finalize-inheritance :after ((cl hyperobject-class))
103 (init-hyperobject-class cl)
107 (defmethod direct-slot-definition-class ((cl hyperobject-class)
110 (find-class 'hyperobject-dsd))
115 (eval-when (:compile-toplevel :load-toplevel :execute)
116 (defmacro process-class-option (slot-name &optional required)
118 `(defmethod clos:process-a-class-option ((class hyperobject-class)
119 (name (eql ,slot-name))
121 (when (and ,required (null value))
122 (error "hyperobject class slot ~A must have a value" name))
123 (list name `',value))
124 #+(or allegro sbcl cmu scl)
125 (declare (ignore slot-name required))
128 (defmacro process-slot-option (slot-name)
130 `(defmethod clos:process-a-slot-option ((class hyperobject-class)
131 (option (eql ,slot-name))
133 already-processed-options
135 (list* option `',value already-processed-options))
137 (declare (ignore slot-name))
140 (dolist (option *class-options*)
141 (eval `(process-class-option ,option)))
142 (dolist (option *slot-options*)
143 (eval `(process-slot-option ,option)))
146 `(defclass hyperobject-dsd (standard-direct-slot-definition)
147 (,@(mapcar #'(lambda (x)
148 `(,(intern (symbol-name x))
150 *slot-options-no-initarg*)
151 ,@(mapcar #'(lambda (x)
152 `(,(intern (symbol-name x))
154 ,(intern (symbol-name x) (symbol-name :keyword))
157 ,(intern (concatenate 'string
162 `(defclass hyperobject-esd (standard-effective-slot-definition)
163 (,@(mapcar #'(lambda (x)
164 `(,(intern (symbol-name x))
166 ,(intern (symbol-name x) (symbol-name :keyword))
169 ,(intern (concatenate 'string
172 (append *slot-options* *slot-options-no-initarg*)))))
175 (defun intern-in-keyword (obj)
182 (intern (symbol-name obj) (find-package 'keyword)))
184 (cons (intern-in-keyword (car obj) ) (intern-in-keyword (cdr obj))))
188 (defun canonicalize-value-type (vt)
193 (cons (ensure-keyword (car vt)) (cdr vt)))
197 (eval-when (:compile-toplevel :load-toplevel :execute)
198 (when (>= (length (generic-function-lambda-list
199 (ensure-generic-function
200 'compute-effective-slot-definition)))
202 (push :ho-named-cesd-fun cl:*features*)))
204 (defmethod compute-effective-slot-definition :around ((cl hyperobject-class)
205 #+ho-named-cesd-fun name
207 #+allegro (declare (ignore name))
208 (let* ((dsd (car dsds))
209 (value-type (canonicalize-value-type (slot-value dsd 'value-type))))
210 (multiple-value-bind (sql-type length) (value-type-to-sql-type value-type)
211 (setf (slot-value dsd 'sql-type) sql-type)
212 (setf (slot-value dsd 'type) (value-type-to-lisp-type value-type))
213 (let ((ia (compute-effective-slot-definition-initargs cl #+lispworks name dsds)))
215 #'make-instance 'hyperobject-esd
216 :value-type value-type
219 :print-formatter (slot-value dsd 'print-formatter)
220 :subobject (slot-value dsd 'subobject)
221 :hyperlink (slot-value dsd 'hyperlink)
222 :hyperlink-parameters (slot-value dsd 'hyperlink-parameters)
223 :description (slot-value dsd 'description)
224 :user-name (slot-value dsd 'user-name)
225 :index (slot-value dsd 'index)
226 :value-constraint (slot-value dsd 'value-constraint)
227 :null-allowed (slot-value dsd 'null-allowed)
232 (setq cl:*features* (delete :ho-named-cesd-fun cl:*features*))
234 (defun value-type-to-lisp-type (value-type)
235 (case (if (atom value-type)
238 ((:string :cdata :varchar :char)
241 '(or null character))
248 ((:float :single-float)
249 '(or null single-float))
251 '(or null double-float))
255 (defun value-type-to-sql-type (value-type)
256 "Return two values, the sql type and field length."
257 (let ((type (if (atom value-type)
260 (length (when (consp value-type)
270 ((:float :single-float)
278 ;;;; Class initialization function
280 ;; defines a slot-unbound method for class and slot-name, fills
281 ;; the slot by calling reader function with the slot values of
282 ;; the instance's reader-keys
283 (defmacro def-lazy-reader (class slot-name reader &rest reader-keys)
284 (let* ((the-slot-name (gensym))
286 (the-instance (gensym))
288 (dolist (key reader-keys)
289 (push (list 'slot-value the-instance (list 'quote key)) keys))
290 (setq keys (nreverse keys))
291 `(defmethod slot-unbound (,the-class (,the-instance ,class)
292 (,the-slot-name (eql ',slot-name)))
293 (declare (ignore ,the-class))
294 (setf (slot-value ,the-instance ,the-slot-name) (,reader ,@keys)))))
298 (defun intern-eql-specializer (slot)
301 #+(or sbcl cmu lispworks)
302 (defun ensure-lazy-reader (class-name slot-name reader &rest reader-keys)
304 (gf (ensure-generic-function 'slot-unbound)))
305 (dolist (key reader-keys)
306 (push (list 'slot-value 'the-instance (list 'quote key)) keys))
307 (setq keys (nreverse keys))
308 (multiple-value-bind (method-lambda init-args-values)
311 (class-prototype (generic-function-method-class gf))
313 `(lambda (the-class the-instance the-slot-name)
314 (declare (ignore the-class))
315 (setf (slot-value the-instance the-slot-name) (,reader ,@keys)))
317 '(the-class the-instance the-slot-name)
321 `(setf (slot-value the-instance the-slot-name) (,reader ,@keys))
325 #'make-instance (generic-function-method-class gf)
326 ':specializers (list (class-of (find-class class-name))
327 (find-class class-name)
328 (intern-eql-specializer slot-name))
329 ':lambda-list '(the-class the-instance the-slot-name)
330 ':function (compile nil method-lambda)
331 init-args-values)))))
333 (defun finalize-subobjects (cl)
334 "Process class subobjects slot"
335 (setf (subobjects cl)
336 (let ((subobjects '()))
337 (dolist (slot (class-slots cl))
338 (let-when (subobj-def (esd-subobject slot))
339 (let ((subobject (make-instance 'subobject
340 :name-class (class-name cl)
341 :name-slot (slot-definition-name slot)
342 :lookup (if (atom subobj-def)
345 :lookup-keys (if (atom subobj-def)
348 (unless (eq (lookup subobject) t)
349 #-(or sbcl cmu lispworks)
351 `(hyperobject::def-lazy-reader ,(name-class subobject)
352 ,(name-slot subobject) ,(lookup subobject)
353 ,@(lookup-keys subobject)))
354 #+(or sbcl cmu lispworks)
355 (apply #'ensure-lazy-reader
356 (name-class subobject) (name-slot subobject) (lookup subobject) (lookup-keys subobject))
358 (push subobject subobjects))))
359 ;; sbcl/cmu reverse class-slots compared to the defclass form
360 ;; subobject is already reversed from the dolist/push loop, so re-reverse on cmu/sbcl
361 #+(or cmu sbcl) subobjects
362 #-(or cmu sbcl) (nreverse subobjects)
365 (defun finalize-documentation (cl)
366 "Calculate class documentation slot"
367 (awhen (slot-value cl 'user-name)
368 (setf (slot-value cl 'user-name)
369 (etypecase (slot-value cl 'user-name)
371 ((or string symbol) it))))
372 (awhen (slot-value cl 'description)
373 (setf (slot-value cl 'description)
374 (etypecase (slot-value cl 'description)
376 ((or string symbol) it))))
378 (let ((*print-circle* nil))
379 (setf (documentation (class-name cl) 'class)
380 (format nil "Hyperobject~A~A~A~A"
382 (format nil ": ~A" it ""))
383 (aif (description cl)
384 (format nil "~%Class description: ~A" it) "")
386 (format nil "~%Subobjects:~{ ~A~}" (mapcar #'name-slot it)) "")
387 (aif (default-print-slots cl)
388 (format nil "~%Default print slots:~{ ~A~}" it) "")
391 (defun finalize-hyperlinks (cl)
392 (let ((hyperlinks '()))
393 (dolist (esd (class-slots cl))
394 (awhen (slot-value esd 'hyperlink)
396 (make-instance 'hyperlink
397 :name (slot-definition-name esd)
399 :link-parameters (slot-value esd 'hyperlink-parameters))
401 ;; cmu/sbcl reverse class-slots compared to the defclass form
402 ;; hyperlinks is already reversed from the dolist/push loop, so re-reverse on sbcl/cmu
403 #-(or cmu sbcl) (setq hyperlinks (nreverse hyperlinks))
404 (setf (slot-value cl 'hyperlinks) hyperlinks)))
406 (defun init-hyperobject-class (cl)
407 "Initialize a hyperobject class. Calculates all class slots"
408 (finalize-subobjects cl)
410 (finalize-hyperlinks cl)
413 (finalize-documentation cl))
416 ;;;; *************************************************************************
417 ;;;; Metaclass Slot Accessors
418 ;;;; *************************************************************************
420 (defun find-slot-by-name (cl name)
421 (find name (class-slots cl) :key #'slot-definition-name))
423 (defun hyperobject-class-user-name (obj)
424 (awhen (user-name (class-of obj))
429 (defun hyperobject-class-subobjects (obj)
430 (subobjects (class-of obj)))
432 (defun hyperobject-class-hyperlinks (obj)
433 (hyperlinks (class-of obj)))
435 (defun hyperobject-class-slots (obj)
436 ;; cmucl/sbcl reverse class-slots
437 #+(or cmu sbcl) (reverse (class-slots (class-of obj)))
438 #-(or cmu sbcl) (class-slots (class-of obj)))