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.44 2003/04/12 05:16:54 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)
87 (lookup :type function :initform nil :initarg :lookup :reader lookup)
88 (link-parameters :type list :initform nil :initarg :link-parameters
89 :reader link-parameters)))
91 (defmethod print-object ((obj hyperlink) (s stream))
92 (print-unreadable-object (obj s :type t :identity t)
93 (format s "~S" (name obj))))
96 (defmethod validate-superclass ((class hyperobject-class) (superclass standard-class))
99 (defmethod finalize-inheritance :after ((cl hyperobject-class))
100 (init-hyperobject-class cl)
104 (defmethod direct-slot-definition-class ((cl hyperobject-class)
107 (find-class 'hyperobject-dsd))
112 (eval-when (:compile-toplevel :load-toplevel :execute)
113 (defmacro process-class-option (slot-name &optional required)
115 `(defmethod clos:process-a-class-option ((class hyperobject-class)
116 (name (eql ,slot-name))
118 (when (and ,required (null value))
119 (error "hyperobject class slot ~A must have a value" name))
121 #+(or allegro sbcl cmu scl)
122 (declare (ignore slot-name required))
125 (defmacro process-slot-option (slot-name)
127 `(defmethod clos:process-a-slot-option ((class hyperobject-class)
128 (option (eql ,slot-name))
130 already-processed-options
132 (list* option value already-processed-options))
134 (declare (ignore slot-name))
137 (dolist (option *class-options*)
138 (eval `(process-class-option ,option)))
139 (dolist (option *slot-options*)
140 (eval `(process-slot-option ,option)))
143 `(defclass hyperobject-dsd (standard-direct-slot-definition)
144 (,@(mapcar #'(lambda (x)
145 `(,(intern (symbol-name x))
147 *slot-options-no-initarg*)
148 ,@(mapcar #'(lambda (x)
149 `(,(intern (symbol-name x))
151 ,(intern (symbol-name x) (symbol-name :keyword))
154 ,(intern (concatenate 'string
159 `(defclass hyperobject-esd (standard-effective-slot-definition)
160 (,@(mapcar #'(lambda (x)
161 `(,(intern (symbol-name x))
163 ,(intern (symbol-name x) (symbol-name :keyword))
166 ,(intern (concatenate 'string
169 (append *slot-options* *slot-options-no-initarg*)))))
172 (defun intern-in-keyword (obj)
179 (intern (symbol-name obj) (find-package 'keyword)))
181 (cons (intern-in-keyword (car obj) ) (intern-in-keyword (cdr obj))))
185 (defun canonicalize-value-type (vt)
190 (cons (ensure-keyword (car vt)) (cdr vt)))
195 (defmethod compute-effective-slot-definition :around ((cl hyperobject-class) dsds)
196 (let* ((dsd (car dsds))
197 (value-type (canonicalize-value-type (slot-value dsd 'value-type))))
198 (multiple-value-bind (sql-type length) (value-type-to-sql-type value-type)
199 (setf (slot-value dsd 'sql-type) sql-type)
200 (setf (slot-value dsd 'type) (value-type-to-lisp-type value-type))
201 (let ((ia (compute-effective-slot-definition-initargs
202 cl #+lispworks name dsds)))
204 #'make-instance 'hyperobject-esd
205 :value-type value-type
208 :print-formatter (slot-value dsd 'print-formatter)
209 :subobject (slot-value dsd 'subobject)
210 :hyperlink (slot-value dsd 'hyperlink)
211 :hyperlink-parameters (slot-value dsd 'hyperlink-parameters)
212 :description (slot-value dsd 'description)
213 :user-name (slot-value dsd 'user-name)
214 :index (slot-value dsd 'index)
215 :value-constraint (slot-value dsd 'value-constraint)
216 :null-allowed (slot-value dsd 'null-allowed)
219 (defmethod compute-effective-slot-definition :around ((cl hyperobject-class)
220 #+(or lispworks allegro)
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 (defun value-type-to-lisp-type (value-type)
246 (case (if (atom value-type)
249 ((:string :cdata :varchar :char)
259 ((:float :single-float)
266 (defun value-type-to-sql-type (value-type)
267 "Return two values, the sql type and field length."
268 (let ((type (if (atom value-type)
271 (length (when (consp value-type)
281 ((:float :single-float)
289 ;;;; Class initialization function
291 ;; defines a slot-unbound method for class and slot-name, fills
292 ;; the slot by calling reader function with the slot values of
293 ;; the instance's reader-keys
294 (defmacro def-lazy-reader (class slot-name reader &rest reader-keys)
295 (let* ((the-slot-name (gensym))
297 (the-instance (gensym))
299 (dolist (key reader-keys)
300 (push (list 'slot-value the-instance (list 'quote key)) keys))
301 (setq keys (nreverse keys))
302 `(defmethod slot-unbound (,the-class (,the-instance ,class)
303 (,the-slot-name (eql ',slot-name)))
304 (declare (ignore ,the-class))
305 (setf (slot-value ,the-instance ,the-slot-name)
309 (defun intern-eql-specializer (slot)
312 #+(or sbcl cmu lispworks)
313 (defun ensure-lazy-reader (class-name slot-name reader &rest reader-keys)
315 (gf (ensure-generic-function 'slot-unbound)))
316 (dolist (key reader-keys)
317 (push (list 'slot-value 'the-instance (list 'quote key)) keys))
318 (setq keys (nreverse keys))
319 (multiple-value-bind (method-lambda init-args-values)
322 (class-prototype (generic-function-method-class gf))
324 `(lambda (the-class the-instance the-slot-name)
325 (declare (ignore the-class))
326 (setf (slot-value the-instance the-slot-name) (,reader ,@keys)))
328 '(the-class the-instance the-slot-name)
332 `(setf (slot-value the-instance the-slot-name) (,reader ,@keys))
336 #'make-instance (generic-function-method-class gf)
337 ':specializers (list (class-of (find-class class-name))
338 (find-class class-name)
339 (intern-eql-specializer slot-name))
340 ':lambda-list '(the-class the-instance the-slot-name)
341 ':function (compile nil method-lambda)
342 init-args-values)))))
344 (defun finalize-subobjects (cl)
345 "Process class subobjects slot"
346 (setf (subobjects cl)
347 (let ((subobjects '()))
348 (dolist (slot (class-slots cl))
349 (let-when (subobj-def (esd-subobject slot))
350 (let ((subobject (make-instance 'subobject
351 :name-class (class-name cl)
352 :name-slot (slot-definition-name slot)
353 :lookup (if (atom subobj-def)
356 :lookup-keys (if (atom subobj-def)
359 (unless (eq (lookup subobject) t)
360 #-(or sbcl cmu lispworks)
362 `(hyperobject::def-lazy-reader ,(name-class subobject)
363 ,(name-slot subobject) ,(lookup subobject)
364 ,@(lookup-keys subobject)))
365 #+(or sbcl cmu lispworks)
366 (apply #'ensure-lazy-reader
367 (name-class subobject) (name-slot subobject) (lookup subobject) (lookup-keys subobject))
369 (push subobject subobjects))))
370 ;; sbcl/cmu reverse class-slots compared to the defclass form
371 ;; subobject is already reversed from the dolist/push loop, so re-reverse on cmu/sbcl
372 #+(or cmu sbcl) subobjects
373 #-(or cmu sbcl) (nreverse subobjects)
376 (defun finalize-documentation (cl)
377 "Calculate class documentation slot"
378 (awhen (slot-value cl 'user-name)
379 (setf (slot-value cl 'user-name)
380 (etypecase (slot-value cl 'user-name)
382 ((or string symbol) it))))
383 (awhen (slot-value cl 'description)
384 (setf (slot-value cl 'description)
385 (etypecase (slot-value cl 'description)
387 ((or string symbol) it))))
389 (let ((*print-circle* nil))
390 (setf (documentation (class-name cl) 'class)
391 (format nil "Hyperobject~A~A~A~A"
393 (format nil ": ~A" it ""))
394 (aif (description cl)
395 (format nil "~%Class description: ~A" it) "")
397 (format nil "~%Subobjects:~{ ~A~}" (mapcar #'name-slot it)) "")
398 (aif (default-print-slots cl)
399 (format nil "~%Default print slots:~{ ~A~}" it) "")
402 (defun finalize-hyperlinks (cl)
403 (let ((hyperlinks '()))
404 (dolist (esd (class-slots cl))
405 (awhen (slot-value esd 'hyperlink)
407 (make-instance 'hyperlink
408 :name (slot-definition-name esd)
410 :link-parameters (slot-value esd 'hyperlink-parameters))
412 ;; cmu/sbcl reverse class-slots compared to the defclass form
413 ;; hyperlinks is already reversed from the dolist/push loop, so re-reverse on sbcl/cmu
414 #-(or cmu sbcl) (setq hyperlinks (nreverse hyperlinks))
415 (setf (slot-value cl 'hyperlinks) hyperlinks)))
417 (defun init-hyperobject-class (cl)
418 "Initialize a hyperobject class. Calculates all class slots"
419 (finalize-subobjects cl)
421 (finalize-hyperlinks cl)
424 (finalize-documentation cl))
427 ;;;; *************************************************************************
428 ;;;; Metaclass Slot Accessors
429 ;;;; *************************************************************************
431 (defun find-slot-by-name (cl name)
432 (find name (class-slots cl) :key #'slot-definition-name))
434 (defun hyperobject-class-user-name (obj)
435 (awhen (user-name (class-of obj))
440 (defun hyperobject-class-subobjects (obj)
441 (subobjects (class-of obj)))
443 (defun hyperobject-class-hyperlinks (obj)
444 (hyperlinks (class-of obj)))
446 (defun hyperobject-class-slots (obj)
447 ;; cmucl/sbcl reverse class-slots
448 #+(or cmu sbcl) (reverse (class-slots (class-of obj)))
449 #-(or cmu sbcl) (class-slots (class-of obj)))