1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: hyperobject.lisp
6 ;;;; Purpose: Hyper Object Metaclass
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: hyperobject.lisp,v 1.15 2002/11/25 04:49:22 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))))
25 (eval-when (:compile-toplevel :load-toplevel :execute)
28 `(mop::class-slots mop::slot-definition-name mop:finalize-inheritance
29 mop::standard-direct-slot-definition mop::standard-effective-slot-definition
30 mop:direct-slot-definition-class mop:compute-effective-slot-definition
31 excl::compute-effective-slot-definition-initargs)
33 `(clos:class-slots clos::slot-definition-name clos:finalize-inheritance
34 clos::standard-direct-slot-definition clos::standard-effective-slot-definition
35 clos:direct-slot-definition-class clos:compute-effective-slot-definition
36 clos::compute-effective-slot-definition-initargs)
38 `(sb-pcl:class-of sb-pcl:class-name sb-pcl:class-slots sb-pcl::standard-class
39 sb-pcl::slot-definition-name sb-pcl:finalize-inheritance
40 sb-pcl::standard-direct-slot-definition
41 sb-pcl::standard-effective-slot-definition sb-pcl::validate-superclass
42 sb-pcl:direct-slot-definition-class sb-pcl:compute-effective-slot-definition
43 sb-pcl::compute-effective-slot-definition-initargs)
45 `(pcl:class-of pcl:class-name pcl:class-slots pcl::standard-class
46 pcl::slot-definition-name pcl:finalize-inheritance
47 pcl::standard-direct-slot-definition pcl::standard-effective-slot-definition
48 pcl::validate-superclass pcl:direct-slot-definition-class
49 pcl:compute-effective-slot-definition
50 pcl::compute-effective-slot-definition-initargs)
57 (defclass hyperobject-class (standard-class)
58 ( ;; slots initialized in defclass
59 (title :initarg :title :type string :initform nil
60 :documentation "Print Title for class")
61 (print-slots :initarg :print-slots :type list :initform nil
62 :documentation "List of slots to print")
63 (description :initarg :description :initform nil
64 :documentation "Class description")
66 ;;; The remainder of these fields are calculated one time
67 ;;; in finalize-inheritence.
69 (subobjects :initform nil :documentation
70 "List of fields that contain a list of subobjects objects.")
71 (references :type list :initform nil :documentation
72 "List of fields that have references")
74 (value-func :initform nil :type function)
75 (xmlvalue-func :initform nil :type function)
76 (fmtstr-text :initform nil :type string)
77 (fmtstr-html :initform nil :type string)
78 (fmtstr-xml :initform nil :type string)
79 (fmtstr-text-labels :initform nil :type string)
80 (fmtstr-html-labels :initform nil :type string)
81 (fmtstr-xml-labels :initform nil :type string)
82 (fmtstr-html-ref :initform nil :type string)
83 (fmtstr-xml-ref :initform nil :type string)
84 (fmtstr-html-ref-labels :initform nil :type string)
85 (fmtstr-xml-ref-labels :initform nil :type string)
87 (:documentation "Metaclass for Markup Language classes."))
89 (defclass subobject ()
90 ((name :type symbol :initform nil :initarg :name :reader name)
91 (reader :type function :initform nil :initarg :reader :reader reader)))
93 (defmethod print-object ((obj subobject) (s stream))
94 (print-unreadable-object (obj s :type t :identity t)
95 (format s "~S" (name obj))))
97 (defclass reference ()
98 ((name :type symbol :initform nil :initarg :name :reader name)
99 (lookup :type function :initform nil :initarg :lookup :reader lookup)
100 (link-parameters :type list :initform nil :initarg :link-parameters
101 :reader link-parameters)))
103 (defmethod print-object ((obj reference) (s stream))
104 (print-unreadable-object (obj s :type t :identity t)
105 (format s "~S" (name obj))))
108 (defmethod validate-superclass ((class hyperobject-class) (superclass standard-class))
111 (defmethod finalize-inheritance :after ((cl hyperobject-class))
112 (init-hyperobject-class cl))
115 (defmethod direct-slot-definition-class ((cl hyperobject-class)
118 (find-class 'hyperobject-dsd))
120 (defmacro define-class-option (slot-name &optional required)
122 `(defmethod clos:process-a-class-option ((class hyperobject-class)
123 (name (eql ,slot-name))
125 (when (and ,required (null value))
126 (error "hyperobject class slot ~A must have a value" name))
127 (if (null (cdr value))
129 `(name (quote value))))
130 #+(or allegro sbcl cmu scl)
131 (declare (ignore slot-name required))
134 (defmacro define-slot-option (slot-name)
136 `(defmethod clos:process-a-slot-option ((class hyperobject-class)
137 (option (eql ,slot-name))
139 already-processed-other-options
143 (declare (ignore slot-name))
146 (define-class-option :title)
147 (define-class-option :print-slots)
148 (define-class-option :description)
150 (define-slot-option :print-formatter)
151 (define-slot-option :subobject)
152 (define-slot-option :reference)
153 (define-slot-option :description)
157 (defclass hyperobject-dsd (standard-direct-slot-definition)
158 ((ho-type :initarg :ho-type)
159 (print-formatter :initarg :print-formatter)
160 (subobject :initarg :subobject :initarg nil)
161 (reference :initarg :reference :initarg nil)
162 (description :initarg :description :initarg nil)
165 (defclass hyperobject-esd (standard-effective-slot-definition)
166 ((ho-type :initarg :ho-type :accessor esd-ho-type)
167 (print-formatter :initarg :print-formatter :accessor esd-print-formatter)
168 (subobject :initarg :subobject :accessor esd-subobject)
169 (reference :initarg :reference :accessor esd-reference)
170 (description :initarg :description :accessor esd-description)
175 (defmethod compute-effective-slot-definition :around
176 ((cl hyperobject-class) #+(or allegro lispworks) name dsds)
177 #+allergo (declare (ignore name))
178 (let* ((dsd (car dsds))
179 (ho-type (slot-value dsd 'type)))
180 (setf (slot-value dsd 'ho-type) ho-type)
181 (setf (slot-value dsd 'type) (convert-ho-type ho-type))
182 (let ((ia (compute-effective-slot-definition-initargs
183 cl #+lispworks name dsds)))
185 #'make-instance 'hyperobject-esd
187 :print-formatter (slot-value dsd 'print-formatter)
188 :subobject (slot-value dsd 'subobject)
189 :reference (slot-value dsd 'reference)
190 :description (slot-value dsd 'description)
194 (defun convert-ho-type (ho-type)
195 (check-type ho-type symbol)
196 (case (intern (symbol-name ho-type) (symbol-name :keyword))
212 ;;;; Class initialization function
214 (defun find-slot-by-name (cl name)
215 (find name (class-slots cl) :key #'slot-definition-name))
218 (defun process-subobjects (cl)
219 "Process class subobjects slot"
220 (setf (slot-value cl 'subobjects)
221 (let ((subobjects '()))
222 (dolist (slot (class-slots cl))
223 (when (slot-value slot 'subobject)
224 (push (make-instance 'subobject :name (slot-definition-name slot)
225 :reader (if (eq t (esd-subobject slot))
226 (slot-definition-name slot)
227 (esd-subobject slot)))
231 (defun process-documentation (cl)
232 "Calculate class documentation slot"
233 (awhen (slot-value cl 'title)
234 (setf (slot-value cl 'title) (car it)))
235 (awhen (slot-value cl 'description)
236 (setf (slot-value cl 'description) (car it)))
238 (let ((*print-circle* nil))
239 (setf (documentation (class-name cl) 'class)
240 (format nil "Hyperobject~A~A~A~A"
241 (aif (slot-value cl 'title)
242 (format nil ": ~A" it ""))
243 (aif (slot-value cl 'description)
244 (format nil "~%Class description: ~A" it) "")
245 (aif (slot-value cl 'subobjects)
246 (format nil "~%Subobjects:~{ ~A~}" (mapcar #'name it)) "")
247 (aif (slot-value cl 'print-slots)
248 (format nil "~%Print-slots:~{ ~A~}" it) "")
251 (defun process-views (cl)
252 "Calculate all view slots for a hyperobject class"
253 (let ((fmtstr-text "")
256 (fmtstr-text-labels "")
257 (fmtstr-html-labels "")
258 (fmtstr-xml-labels "")
261 (fmtstr-html-ref-labels "")
262 (fmtstr-xml-ref-labels "")
266 (classname (class-name cl))
267 (package (symbol-package (class-name cl)))
269 (declare (ignore classname))
270 (dolist (slot-name (slot-value cl 'print-slots))
271 (let ((slot (find-slot-by-name cl slot-name)))
273 (error "Slot ~A is not found in class ~S" slot-name cl))
274 (let ((name (slot-definition-name slot))
275 (namestr (symbol-name (slot-definition-name slot)))
276 (namestr-lower (string-downcase (symbol-name (slot-definition-name slot))))
277 (type (slot-value slot 'ho-type))
278 (print-formatter (slot-value slot 'print-formatter))
280 (plain-value-func nil)
281 html-str xml-str html-label-str xml-label-str)
283 (when (or (eql type :integer) (eql type :fixnum))
284 (setq value-fmt "~d"))
286 (when (eql type :boolean)
287 (setq value-fmt "~a"))
290 (setq first-field nil)
292 (string-append fmtstr-text " ")
293 (string-append fmtstr-html " ")
294 (string-append fmtstr-xml " ")
295 (string-append fmtstr-text-labels " ")
296 (string-append fmtstr-html-labels " ")
297 (string-append fmtstr-xml-labels " ")
298 (string-append fmtstr-html-ref " ")
299 (string-append fmtstr-xml-ref " ")
300 (string-append fmtstr-html-ref-labels " ")
301 (string-append fmtstr-xml-ref-labels " ")))
303 (setq html-str (concatenate 'string "<span class=\"" namestr-lower "\">" value-fmt "</span>"))
304 (setq xml-str (concatenate 'string "<" namestr-lower ">" value-fmt "</" namestr-lower ">"))
305 (setq html-label-str (concatenate 'string "<span class=\"label\">" namestr-lower "</span> <span class=\"" namestr-lower "\">" value-fmt "</span>"))
306 (setq xml-label-str (concatenate 'string "<label>" namestr-lower "</label> <" namestr-lower ">" value-fmt "</" namestr-lower ">"))
308 (string-append fmtstr-text value-fmt)
309 (string-append fmtstr-html html-str)
310 (string-append fmtstr-xml xml-str)
311 (string-append fmtstr-text-labels namestr-lower " " value-fmt)
312 (string-append fmtstr-html-labels html-label-str)
313 (string-append fmtstr-xml-labels xml-label-str)
315 (if (esd-reference slot)
317 (string-append fmtstr-html-ref "<~~a>" value-fmt "</~~a>")
318 (string-append fmtstr-xml-ref "<~~a>" value-fmt "</~~a>")
319 (string-append fmtstr-html-ref-labels "<span class=\"label\">" namestr-lower "</span> <~~a>" value-fmt "</~~a>")
320 (string-append fmtstr-xml-ref-labels "<label>" namestr-lower "</label> <~~a>" value-fmt "</~~a>")
321 (push (make-instance 'reference :name name :lookup (esd-reference slot))
324 (string-append fmtstr-html-ref html-str)
325 (string-append fmtstr-xml-ref xml-str)
326 (string-append fmtstr-html-ref-labels html-label-str)
327 (string-append fmtstr-xml-ref-labels xml-label-str)))
330 (setq plain-value-func
331 (list `(,print-formatter (,(intern namestr package) x))))
332 (setq plain-value-func
333 (list `(,(intern namestr package) x))))
334 (setq value-func (append value-func plain-value-func))
336 (if (eql type :cdata)
337 (setq xmlvalue-func (append xmlvalue-func (list `(xml-cdata ,@plain-value-func))))
338 (setq xmlvalue-func (append xmlvalue-func plain-value-func)))
341 (setf (slot-value cl 'references) references)
344 (setq value-func `(lambda (x) (values ,@value-func)))
345 (setq value-func `(lambda () (values))))
346 (setq value-func (compile nil (eval value-func)))
349 (setq xmlvalue-func `(lambda (x) (values ,@xmlvalue-func)))
350 (setq xmlvalue-func `(lambda () (values))))
351 (setq xmlvalue-func (compile nil (eval xmlvalue-func)))
353 (setf (slot-value cl 'fmtstr-text) fmtstr-text)
354 (setf (slot-value cl 'fmtstr-html) fmtstr-html)
355 (setf (slot-value cl 'fmtstr-xml) fmtstr-xml)
356 (setf (slot-value cl 'fmtstr-text-labels) fmtstr-text-labels)
357 (setf (slot-value cl 'fmtstr-html-labels) fmtstr-html-labels)
358 (setf (slot-value cl 'fmtstr-xml-labels) fmtstr-xml-labels)
359 (setf (slot-value cl 'fmtstr-html-ref) fmtstr-html-ref)
360 (setf (slot-value cl 'fmtstr-xml-ref) fmtstr-xml-ref)
361 (setf (slot-value cl 'fmtstr-html-ref-labels) fmtstr-html-ref-labels)
362 (setf (slot-value cl 'fmtstr-xml-ref-labels) fmtstr-xml-ref-labels)
363 (setf (slot-value cl 'value-func) value-func)
364 (setf (slot-value cl 'xmlvalue-func) xmlvalue-func))
367 (defun init-hyperobject-class (cl)
368 "Initialize a hyperobject class. Calculates all class slots"
369 (process-subobjects cl)
371 (process-documentation cl))
373 (defun hyperobject-class-fmtstr-text (obj)
374 (slot-value (class-of obj) 'fmtstr-text))
376 (defun hyperobject-class-fmtstr-html (obj)
377 (slot-value (class-of obj) 'fmtstr-html))
379 (defun hyperobject-class-fmtstr-xml (obj)
380 (slot-value (class-of obj) 'fmtstr-xml))
382 (defun hyperobject-class-fmtstr-text-labels (obj)
383 (slot-value (class-of obj) 'fmtstr-text-labels))
385 (defun hyperobject-class-fmtstr-html-labels (obj)
386 (slot-value (class-of obj) 'fmtstr-html-labels))
388 (defun hyperobject-class-fmtstr-xml-labels (obj)
389 (slot-value (class-of obj) 'fmtstr-xml-labels))
391 (defun hyperobject-class-value-func (obj)
392 (slot-value (class-of obj) 'value-func))
394 (defun hyperobject-class-xmlvalue-func (obj)
395 (slot-value (class-of obj) 'xmlvalue-func))
397 (eval-when (:compile-toplevel :load-toplevel :execute)
398 (defun hyperobject-class-title (obj)
399 (awhen (slot-value (class-of obj) 'title)
404 (defun hyperobject-class-subobjects (obj)
405 (slot-value (class-of obj) 'subobjects))
407 (defun hyperobject-class-references (obj)
408 (slot-value (class-of obj) 'references))
410 (defun hyperobject-class-fields (obj)
411 (class-slots (class-of obj)))
413 (defun hyperobject-class-fmtstr-html-ref (obj)
414 (slot-value (class-of obj) 'fmtstr-html-ref))
416 (defun hyperobject-class-fmtstr-xml-ref (obj)
417 (slot-value (class-of obj) 'fmtstr-xml-ref))
419 (defun hyperobject-class-fmtstr-html-ref-labels (obj)
420 (slot-value (class-of obj) 'fmtstr-html-ref-labels))
422 (defun hyperobject-class-fmtstr-xml-ref-labels (obj)
423 (slot-value (class-of obj) 'fmtstr-xml-ref-labels))