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.4 2002/12/01 21:07:28 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 3) (safety 1) (compilation-speed 0) (debug 3))))
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 (setf (slot-value dsd 'ho-type) ho-type)
160 (setf (slot-value dsd 'type) (ho-type-to-lisp-type ho-type))
161 (setf (slot-value dsd 'sql-type) (ho-type-to-sql-type ho-type))
162 (let ((ia (compute-effective-slot-definition-initargs
163 cl #+lispworks name dsds)))
165 #'make-instance 'hyperobject-esd
168 :print-formatter (slot-value dsd 'print-formatter)
169 :subobject (slot-value dsd 'subobject)
170 :hyperlink (slot-value dsd 'hyperlink)
171 :description (slot-value dsd 'description)
175 (defun ho-type-to-lisp-type (ho-type)
176 (check-type ho-type symbol)
177 (case (intern (symbol-name ho-type) (symbol-name :keyword))
195 (defun ho-type-to-sql-type (ho-type)
196 (check-type ho-type symbol)
197 (case (intern (symbol-name ho-type) (symbol-name :keyword))
217 (defun ho-type-to-sql-type (sqltype)
232 ;;;; Class initialization function
234 (defun process-subobjects (cl)
235 "Process class subobjects slot"
236 (setf (slot-value cl 'subobjects)
237 (let ((subobjects '()))
238 (dolist (slot (class-slots cl))
239 (when (slot-value slot 'subobject)
240 (push (make-instance 'subobject :name (slot-definition-name slot)
241 :reader (if (eq t (slot-value slot 'subobject))
242 (slot-definition-name slot)
243 (slot-value slot 'subobject)))
247 (defun process-documentation (cl)
248 "Calculate class documentation slot"
249 (awhen (slot-value cl 'title)
250 (setf (slot-value cl 'title) (car it)))
251 (awhen (slot-value cl 'description)
252 (setf (slot-value cl 'description) (car it)))
254 (let ((*print-circle* nil))
255 (setf (documentation (class-name cl) 'class)
256 (format nil "Hyperobject~A~A~A~A"
257 (aif (slot-value cl 'title)
258 (format nil ": ~A" it ""))
259 (aif (slot-value cl 'description)
260 (format nil "~%Class description: ~A" it) "")
261 (aif (slot-value cl 'subobjects)
262 (format nil "~%Subobjects:~{ ~A~}" (mapcar #'name it)) "")
263 (aif (slot-value cl 'print-slots)
264 (format nil "~%Print-slots:~{ ~A~}" it) "")
267 (defun init-hyperobject-class (cl)
268 "Initialize a hyperobject class. Calculates all class slots"
269 (process-subobjects cl)
272 (process-documentation cl))
275 ;;;; *************************************************************************
276 ;;;; Metaclass Slot Accessors
277 ;;;; *************************************************************************
279 (defun find-slot-by-name (cl name)
280 (find name (class-slots cl) :key #'slot-definition-name))
282 (defun hyperobject-class-fmtstr-text (obj)
283 (slot-value (class-of obj) 'fmtstr-text))
285 (defun hyperobject-class-fmtstr-html (obj)
286 (slot-value (class-of obj) 'fmtstr-html))
288 (defun hyperobject-class-fmtstr-xml (obj)
289 (slot-value (class-of obj) 'fmtstr-xml))
291 (defun hyperobject-class-fmtstr-text-labels (obj)
292 (slot-value (class-of obj) 'fmtstr-text-labels))
294 (defun hyperobject-class-fmtstr-html-labels (obj)
295 (slot-value (class-of obj) 'fmtstr-html-labels))
297 (defun hyperobject-class-fmtstr-xml-labels (obj)
298 (slot-value (class-of obj) 'fmtstr-xml-labels))
300 (defun hyperobject-class-value-func (obj)
301 (slot-value (class-of obj) 'value-func))
303 (defun hyperobject-class-xmlvalue-func (obj)
304 (slot-value (class-of obj) 'xmlvalue-func))
306 (eval-when (:compile-toplevel :load-toplevel :execute)
308 (defun hyperobject-class-title (obj)
309 (awhen (slot-value (class-of obj) 'title)
314 (defun hyperobject-class-subobjects (obj)
315 (slot-value (class-of obj) 'subobjects))
317 (defun hyperobject-class-hyperlinks (obj)
318 (slot-value (class-of obj) 'hyperlinks))
320 (defun hyperobject-class-fields (obj)
321 (class-slots (class-of obj)))
323 (defun hyperobject-class-fmtstr-html-ref (obj)
324 (slot-value (class-of obj) 'fmtstr-html-ref))
326 (defun hyperobject-class-fmtstr-xml-ref (obj)
327 (slot-value (class-of obj) 'fmtstr-xml-ref))
329 (defun hyperobject-class-fmtstr-html-ref-labels (obj)
330 (slot-value (class-of obj) 'fmtstr-html-ref-labels))
332 (defun hyperobject-class-fmtstr-xml-ref-labels (obj)
333 (slot-value (class-of obj) 'fmtstr-xml-ref-labels))