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.5 2002/12/02 15:57:17 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 (title :initarg :title :type string :initform nil
30 :documentation "Print Title for class")
31 (print-slots :initarg :print-slots :type list :initform nil
32 :documentation "List of slots to print")
33 (description :initarg :description :initform nil
34 :documentation "Class description")
35 (version :initarg :version :initform nil
36 :documentation "Version number for class")
37 (sql-name :initarg :table-name :initform nil :reader sql-name)
39 ;;; The remainder of these fields are calculated one time
40 ;;; in finalize-inheritence.
42 (subobjects :initform nil :documentation
43 "List of fields that contain a list of subobjects objects.")
44 (hyperlinks :type list :initform nil :documentation
45 "List of fields that have hyperlinks")
46 (class-id :type integer :initform nil :documentation
47 "Unique ID for the class")
49 (create-table-cmd :initform nil :reader create-table-cmd)
50 (create-index-cmds :initform nil :reader create-index-cmds)
51 (drop-table-cmd :initform nil :reader drop-table-cmd)
53 (value-func :initform nil :type function)
54 (xmlvalue-func :initform nil :type function)
55 (fmtstr-text :initform nil :type string)
56 (fmtstr-html :initform nil :type string)
57 (fmtstr-xml :initform nil :type string)
58 (fmtstr-text-labels :initform nil :type string)
59 (fmtstr-html-labels :initform nil :type string)
60 (fmtstr-xml-labels :initform nil :type string)
61 (fmtstr-html-ref :initform nil :type string)
62 (fmtstr-xml-ref :initform nil :type string)
63 (fmtstr-html-ref-labels :initform nil :type string)
64 (fmtstr-xml-ref-labels :initform nil :type string)
66 (:documentation "Metaclass for Markup Language classes."))
68 (defclass subobject ()
69 ((name :type symbol :initform nil :initarg :name :reader name)
70 (reader :type function :initform nil :initarg :reader :reader reader)))
72 (defmethod print-object ((obj subobject) (s stream))
73 (print-unreadable-object (obj s :type t :identity t)
74 (format s "~S" (name obj))))
76 (defclass hyperlink ()
77 ((name :type symbol :initform nil :initarg :name :reader name)
78 (lookup :type function :initform nil :initarg :lookup :reader lookup)
79 (link-parameters :type list :initform nil :initarg :link-parameters
80 :reader link-parameters)))
82 (defmethod print-object ((obj hyperlink) (s stream))
83 (print-unreadable-object (obj s :type t :identity t)
84 (format s "~S" (name obj))))
87 (defmethod validate-superclass ((class hyperobject-class) (superclass standard-class))
90 (defmethod finalize-inheritance :after ((cl hyperobject-class))
91 (init-hyperobject-class cl))
94 (defmethod direct-slot-definition-class ((cl hyperobject-class)
97 (find-class 'hyperobject-dsd))
102 (eval-when (:compile-toplevel :load-toplevel :execute)
103 (defmacro process-class-option (slot-name &optional required)
105 `(defmethod clos:process-a-class-option ((class hyperobject-class)
106 (name (eql ,slot-name))
108 (when (and ,required (null value))
109 (error "hyperobject class slot ~A must have a value" name))
110 (list name `',value))
111 #+(or allegro sbcl cmu scl)
112 (declare (ignore slot-name required))
115 (defmacro process-slot-option (slot-name)
117 `(defmethod clos:process-a-slot-option ((class hyperobject-class)
118 (option (eql ,slot-name))
120 already-processed-other-options
122 (list option `',value))
124 (declare (ignore slot-name))
127 (dolist (option *class-options*)
128 (eval `(process-class-option ,option)))
129 (dolist (option *slot-options*)
130 (eval `(process-slot-option ,option)))
133 `(defclass hyperobject-dsd (standard-direct-slot-definition)
134 (,@(mapcar #'(lambda (x)
135 `(,(intern (symbol-name x))
137 *slot-options-no-initarg*)
138 ,@(mapcar #'(lambda (x)
139 `(,(intern (symbol-name x))
141 ,(intern (symbol-name x) (symbol-name :keyword))
145 `(defclass hyperobject-esd (standard-effective-slot-definition)
146 (,@(mapcar #'(lambda (x)
147 `(,(intern (symbol-name x))
149 ,(intern (symbol-name x) (symbol-name :keyword))
151 (append *slot-options* *slot-options-no-initarg*)))))
154 (defmethod compute-effective-slot-definition :around
155 ((cl hyperobject-class) #+(or allegro lispworks) name dsds)
156 #+allergo (declare (ignore name))
157 (let* ((dsd (car dsds))
158 (ho-type (slot-value dsd 'type))
159 (sql-type (ho-type-to-sql-type ho-type)))
160 (setf (slot-value dsd 'ho-type) ho-type)
161 (setf (slot-value dsd 'sql-type) sql-type)
162 (setf (slot-value dsd 'type) (ho-type-to-lisp-type ho-type))
163 (let ((ia (compute-effective-slot-definition-initargs
164 cl #+lispworks name dsds)))
166 #'make-instance 'hyperobject-esd
169 :print-formatter (slot-value dsd 'print-formatter)
170 :subobject (slot-value dsd 'subobject)
171 :hyperlink (slot-value dsd 'hyperlink)
172 :hyperlink-parameters (slot-value dsd 'hyperlink-parameters)
173 :description (slot-value dsd 'description)
176 (defun ho-type-to-lisp-type (ho-type)
177 (check-type ho-type symbol)
178 (case (intern (symbol-name ho-type) (symbol-name :keyword))
196 (defun ho-type-to-sql-type (ho-type)
197 (check-type ho-type symbol)
198 (case (intern (symbol-name ho-type) (symbol-name :keyword))
217 ;;;; Class initialization function
219 (defun finalize-subobjects (cl)
220 "Process class subobjects slot"
221 (setf (slot-value cl 'subobjects)
222 (let ((subobjects '()))
223 (dolist (slot (class-slots cl))
224 (when (slot-value slot 'subobject)
225 (push (make-instance 'subobject :name (slot-definition-name slot)
226 :reader (if (eq t (slot-value slot 'subobject))
227 (slot-definition-name slot)
228 (slot-value slot 'subobject)))
232 (defun finalize-documentation (cl)
233 "Calculate class documentation slot"
234 (awhen (slot-value cl 'title)
235 (setf (slot-value cl 'title) (car it)))
236 (awhen (slot-value cl 'description)
237 (setf (slot-value cl 'description) (car it)))
239 (let ((*print-circle* nil))
240 (setf (documentation (class-name cl) 'class)
241 (format nil "Hyperobject~A~A~A~A"
242 (aif (slot-value cl 'title)
243 (format nil ": ~A" it ""))
244 (aif (slot-value cl 'description)
245 (format nil "~%Class description: ~A" it) "")
246 (aif (slot-value cl 'subobjects)
247 (format nil "~%Subobjects:~{ ~A~}" (mapcar #'name it)) "")
248 (aif (slot-value cl 'print-slots)
249 (format nil "~%Print-slots:~{ ~A~}" it) "")
252 (defun init-hyperobject-class (cl)
253 "Initialize a hyperobject class. Calculates all class slots"
254 (finalize-subobjects cl)
256 (finalize-hyperlinks cl)
258 (finalize-documentation cl))
261 ;;;; *************************************************************************
262 ;;;; Metaclass Slot Accessors
263 ;;;; *************************************************************************
265 (defun find-slot-by-name (cl name)
266 (find name (class-slots cl) :key #'slot-definition-name))
268 (defun hyperobject-class-fmtstr-text (obj)
269 (slot-value (class-of obj) 'fmtstr-text))
271 (defun hyperobject-class-fmtstr-html (obj)
272 (slot-value (class-of obj) 'fmtstr-html))
274 (defun hyperobject-class-fmtstr-xml (obj)
275 (slot-value (class-of obj) 'fmtstr-xml))
277 (defun hyperobject-class-fmtstr-text-labels (obj)
278 (slot-value (class-of obj) 'fmtstr-text-labels))
280 (defun hyperobject-class-fmtstr-html-labels (obj)
281 (slot-value (class-of obj) 'fmtstr-html-labels))
283 (defun hyperobject-class-fmtstr-xml-labels (obj)
284 (slot-value (class-of obj) 'fmtstr-xml-labels))
286 (defun hyperobject-class-value-func (obj)
287 (slot-value (class-of obj) 'value-func))
289 (defun hyperobject-class-xmlvalue-func (obj)
290 (slot-value (class-of obj) 'xmlvalue-func))
292 (eval-when (:compile-toplevel :load-toplevel :execute)
294 (defun hyperobject-class-title (obj)
295 (awhen (slot-value (class-of obj) 'title)
300 (defun hyperobject-class-subobjects (obj)
301 (slot-value (class-of obj) 'subobjects))
303 (defun hyperobject-class-hyperlinks (obj)
304 (slot-value (class-of obj) 'hyperlinks))
306 (defun hyperobject-class-fields (obj)
307 (class-slots (class-of obj)))
309 (defun hyperobject-class-fmtstr-html-ref (obj)
310 (slot-value (class-of obj) 'fmtstr-html-ref))
312 (defun hyperobject-class-fmtstr-xml-ref (obj)
313 (slot-value (class-of obj) 'fmtstr-xml-ref))
315 (defun hyperobject-class-fmtstr-html-ref-labels (obj)
316 (slot-value (class-of obj) 'fmtstr-html-ref-labels))
318 (defun hyperobject-class-fmtstr-xml-ref-labels (obj)
319 (slot-value (class-of obj) 'fmtstr-xml-ref-labels))