r4517: Auto commit for Debian build
[hyperobject.git] / mop.lisp
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          mop.lisp
6 ;;;; Purpose:       Metaobject Protocol Interface
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Apr 2000
9 ;;;;
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.
13 ;;;;
14 ;;;; $Id: mop.lisp,v 1.56 2003/04/16 20:11:46 kevin Exp $
15 ;;;;
16 ;;;; This file is Copyright (c) 2000-2002 by Kevin M. Rosenberg
17 ;;;;
18 ;;;; *************************************************************************
19  
20 (in-package :hyperobject)
21
22 (eval-when (:compile-toplevel :execute)
23   (declaim (optimize (speed 2) (safety 2) (compilation-speed 0) (debug 2))))
24
25 ;; Main class
26
27 (defclass hyperobject-class (standard-class)
28   ( ;; slots initialized in defclass
29    (user-name :initarg :user-name :type string :initform nil
30               :accessor user-name
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
36                 :accessor description
37                 :documentation "Class description")
38    (version :initarg :version :initform nil
39             :accessor version
40             :documentation "Version number for class")
41    (sql-name :initarg :sql-name :initform nil)
42
43    ;;; The remainder of these fields are calculated one time
44    ;;; in finalize-inheritence.
45    
46    (subobjects :initform nil :accessor subobjects
47                :documentation
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
55              :accessor class-id
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")
59
60    ;; SQL commands
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)
64
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")
69    )
70   (:documentation "Metaclass for Markup Language classes."))
71
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
77                 :reader lookup-keys))
78   (:documentation "Contains subobject information"))
79
80
81 (defmethod print-object ((obj subobject) (s stream))
82   (print-unreadable-object (obj s :type t :identity t)
83     (format s "~S" (name obj))))
84
85 (defclass hyperlink ()
86   ((name :type symbol :initform nil :initarg :name :reader name)
87    (lookup :type (or function symbol) :initform nil :initarg :lookup :reader lookup)
88    (link-parameters :type list :initform nil :initarg :link-parameters
89                     :reader link-parameters)))
90
91 (defmethod print-object ((obj hyperlink) (s stream))
92   (print-unreadable-object (obj s :type t :identity t)
93     (format s "~S" (name obj))))
94
95 #+(or cmu scl sbcl)
96 (defmethod validate-superclass ((class hyperobject-class) (superclass standard-class))
97   t)
98
99 (defmethod finalize-inheritance :after ((cl hyperobject-class))
100   (init-hyperobject-class cl)
101   )
102
103 ;; Slot definitions
104 (defmethod direct-slot-definition-class ((cl hyperobject-class) 
105                                          #+allegro &rest
106                                          iargs)
107   (find-class 'hyperobject-dsd))
108
109
110                                         ; Slot definitions
111
112 (eval-when (:compile-toplevel :load-toplevel :execute)
113   (defmacro process-class-option (slot-name &optional required)
114     #+lispworks
115     `(defmethod clos:process-a-class-option ((class hyperobject-class)
116                                              (name (eql ,slot-name))
117                                              value)
118       (when (and ,required (null value))
119         (error "hyperobject class slot ~A must have a value" name))
120       (list name `',value))
121     #+(or allegro sbcl cmu scl)
122     (declare (ignore slot-name required))
123     )
124
125   (defmacro process-slot-option (slot-name)
126     #+lispworks
127     `(defmethod clos:process-a-slot-option ((class hyperobject-class)
128                                             (option (eql ,slot-name))
129                                             value
130                                             already-processed-options
131                                             slot)
132       (list* option `',value already-processed-options))
133     #-lispworks
134     (declare (ignore slot-name))
135     )
136   
137   (dolist (option *class-options*)
138     (eval `(process-class-option ,option)))
139   (dolist (option *slot-options*)
140     (eval `(process-slot-option ,option)))
141
142   (eval
143    `(defclass hyperobject-dsd (standard-direct-slot-definition)
144      (,@(mapcar #'(lambda (x)
145                     `(,(intern (symbol-name x))
146                       :initform nil))
147                 *slot-options-no-initarg*)
148       ,@(mapcar #'(lambda (x)
149                     `(,(intern (symbol-name x))
150                       :initarg
151                       ,(intern (symbol-name x) (symbol-name :keyword))
152                       :initform nil
153                       :accessor
154                       ,(intern (concatenate 'string
155                                             (symbol-name :dsd-)
156                                             (symbol-name x)))))
157                 *slot-options*))))
158   (eval
159    `(defclass hyperobject-esd (standard-effective-slot-definition)
160      (,@(mapcar #'(lambda (x)
161                     `(,(intern (symbol-name x))
162                       :initarg
163                       ,(intern (symbol-name x) (symbol-name :keyword))
164                       :initform nil
165                       :accessor
166                       ,(intern (concatenate 'string
167                                             (symbol-name :esd-)
168                                             (symbol-name x)))))
169                 (append *slot-options* *slot-options-no-initarg*)))))
170   ) ;; eval-when
171
172 (defun intern-in-keyword (obj)
173   (cond
174     ((null obj)
175      nil)
176     ((eq t obj)
177      t)
178     ((atom obj)
179      (intern (symbol-name obj) (find-package 'keyword)))
180     ((consp obj)
181      (cons (intern-in-keyword (car obj) ) (intern-in-keyword (cdr obj))))
182     (t
183      obj)))
184
185 (defun canonicalize-value-type (vt)
186   (typecase vt
187     (atom
188      (ensure-keyword vt))
189     (cons
190      (cons (ensure-keyword (car vt)) (cdr vt)))
191     (t
192      t)))
193
194 (eval-when (:compile-toplevel :load-toplevel :execute)
195   (when (>= (length (generic-function-lambda-list
196                      (ensure-generic-function
197                       'compute-effective-slot-definition)))
198             3)
199     (push :ho-named-cesd-fun cl:*features*)))
200
201 (defmethod compute-effective-slot-definition :around ((cl hyperobject-class)
202                                                       #+ho-named-cesd-fun name
203                                                       dsds)
204   #+allegro (declare (ignore name))
205   (let* ((dsd (car dsds))
206          (value-type (canonicalize-value-type (slot-value dsd 'value-type))))
207     (multiple-value-bind (sql-type length) (value-type-to-sql-type value-type)
208       (setf (slot-value dsd 'sql-type) sql-type)
209       (setf (slot-value dsd 'type) (value-type-to-lisp-type value-type))
210       (let ((ia (compute-effective-slot-definition-initargs cl #+lispworks name dsds)))
211         (apply
212          #'make-instance 'hyperobject-esd 
213          :value-type value-type
214          :sql-type sql-type
215          :length length
216          :print-formatter (slot-value dsd 'print-formatter)
217          :subobject (slot-value dsd 'subobject)
218          :hyperlink (slot-value dsd 'hyperlink)
219          :hyperlink-parameters (slot-value dsd 'hyperlink-parameters)
220          :description (slot-value dsd 'description)
221          :user-name (slot-value dsd 'user-name)
222          :index (slot-value dsd 'index)
223          :value-constraint (slot-value dsd 'value-constraint)
224          :null-allowed (slot-value dsd 'null-allowed)
225          ia)))))
226
227
228 #+ho-named-cesd-fun
229 (setq cl:*features* (delete :ho-named-cesd-fun cl:*features*))
230
231 (defun value-type-to-lisp-type (value-type)
232   (case (if (atom value-type)
233             value-type
234             (car value-type))
235     ((:string :cdata :varchar :char)
236      'string)
237     (:character
238      'character)
239     (:fixnum
240      'fixnum)
241     (:boolean
242      'boolean)
243     (:integer
244      'integer)
245     ((:float :single-float)
246      'single-float)
247     (:double-float
248      'double-float)
249     (otherwise
250      t)))
251
252 (defun value-type-to-sql-type (value-type)
253   "Return two values, the sql type and field length."
254   (let ((type (if (atom value-type)
255                   value-type
256                   (car value-type)))
257         (length (when (consp value-type)
258                   (cadr value-type))))
259     (values
260      (case type
261        ((:string :cdata)
262         :string)
263        ((:fixnum :integer)
264         :integer)
265        (:boolean
266         :boolean)
267        ((:float :single-float)
268         :single-float)
269        (:double-float
270         :double-float)
271        (otherwise
272         :text))
273      length)))
274
275 ;;;; Class initialization function
276
277 ;; defines a slot-unbound method for class and slot-name, fills
278 ;; the slot by calling reader function with the slot values of
279 ;; the instance's reader-keys
280 (defmacro def-lazy-reader (class slot-name reader &rest reader-keys)
281   (let* ((the-slot-name (gensym))
282          (the-class (gensym))
283          (the-instance (gensym))
284          (keys '()))
285     (dolist (key reader-keys)
286       (push (list 'slot-value the-instance (list 'quote key)) keys))
287     (setq keys (nreverse keys))
288     `(defmethod slot-unbound (,the-class (,the-instance ,class)
289                               (,the-slot-name (eql ',slot-name)))
290       (declare (ignore ,the-class))
291       (setf (slot-value ,the-instance ,the-slot-name)
292        (funcall ',reader ,@keys)))))
293
294 #+lispworks
295 (defun intern-eql-specializer (slot)
296   `(eql ,slot))
297
298 #+(or sbcl cmu lispworks)
299 (defun ensure-lazy-reader (class-name slot-name reader &rest reader-keys)
300   (let ((keys nil)
301         (gf (ensure-generic-function 'slot-unbound)))
302     (dolist (key reader-keys)
303       (push (list 'slot-value 'the-instance (list 'quote key)) keys))
304     (setq keys (nreverse keys))
305     (multiple-value-bind (method-lambda init-args-values)
306         (make-method-lambda
307          gf
308          (class-prototype (generic-function-method-class gf))
309          #-lispworks
310          `(lambda (the-class the-instance the-slot-name)
311            (declare (ignore the-class))
312            (setf (slot-value the-instance the-slot-name) (,reader ,@keys)))
313          #+lispworks
314          '(the-class the-instance the-slot-name)
315          #+lispworks
316          nil
317          #+lispworks
318          `(setf (slot-value the-instance the-slot-name) (,reader ,@keys))
319          nil)
320       (add-method gf
321                   (apply
322                    #'make-instance (generic-function-method-class gf)
323                    ':specializers (list (class-of (find-class class-name))
324                                         (find-class class-name)
325                                         (intern-eql-specializer slot-name))
326                    ':lambda-list '(the-class the-instance the-slot-name)
327                    ':function (compile nil method-lambda)
328                    init-args-values)))))
329
330 (defun finalize-subobjects (cl)
331   "Process class subobjects slot"
332   (setf (subobjects cl)
333         (let ((subobjects '()))
334           (dolist (slot (class-slots cl))
335             (let-when (subobj-def (esd-subobject slot))
336                       (let ((subobject (make-instance 'subobject
337                                                       :name-class (class-name cl)
338                                                       :name-slot (slot-definition-name slot)
339                                                       :lookup (if (atom subobj-def)
340                                                                   subobj-def
341                                                                   (car subobj-def))
342                                                       :lookup-keys (if (atom subobj-def)
343                                                                        nil
344                                                                        (cdr subobj-def)))))
345                         (unless (eq (lookup subobject) t)
346                           #-(or sbcl lispworks)
347                           (eval
348                            `(hyperobject::def-lazy-reader ,(name-class subobject)
349                              ,(name-slot subobject) ,(lookup subobject)
350                              ,@(lookup-keys subobject)))
351                           #+(or sbcl lispworks)
352                           (apply #'ensure-lazy-reader 
353                                  (name-class subobject) (name-slot subobject) (lookup subobject) (lookup-keys subobject))
354                           )
355                         (push subobject subobjects))))
356           ;; sbcl/cmu reverse class-slots compared to the defclass form
357           ;; subobject is already reversed from the dolist/push loop, so re-reverse on cmu/sbcl
358           #+(or cmu sbcl) subobjects
359           #-(or cmu sbcl) (nreverse subobjects)
360           )))
361
362 (defun finalize-documentation (cl)
363   "Calculate class documentation slot"
364   (awhen (slot-value cl 'user-name)
365          (setf (slot-value cl 'user-name)
366                (etypecase (slot-value cl 'user-name)
367                  (cons (car it))
368                  ((or string symbol) it))))
369   (awhen (slot-value cl 'description)
370          (setf (slot-value cl 'description)
371                (etypecase (slot-value cl 'description)
372                  (cons (car it))
373                  ((or string symbol) it))))
374
375   (let ((*print-circle* nil))
376     (setf (documentation (class-name cl) 'class)
377           (format nil "Hyperobject~A~A~A~A"
378                   (aif (user-name cl)
379                        (format nil ": ~A" it ""))
380                   (aif (description cl)
381                        (format nil "~%Class description: ~A" it) "")
382                   (aif (subobjects cl)
383                        (format nil "~%Subobjects:~{ ~A~}" (mapcar #'name-slot it)) "")
384                   (aif (default-print-slots cl)
385                        (format nil "~%Default print slots:~{ ~A~}" it) "")
386                   ))))
387
388 (defun finalize-hyperlinks (cl)
389   (let ((hyperlinks '()))
390     (dolist (esd (class-slots cl))
391       (awhen (slot-value esd 'hyperlink)
392              (push
393               (make-instance 'hyperlink
394                              :name (slot-definition-name esd)
395                              :lookup it
396                              :link-parameters (slot-value esd 'hyperlink-parameters))
397               hyperlinks)))
398     ;; cmu/sbcl reverse class-slots compared to the defclass form
399     ;; hyperlinks is already reversed from the dolist/push loop, so re-reverse on sbcl/cmu
400     #-(or cmu sbcl) (setq hyperlinks (nreverse hyperlinks))
401     (setf (slot-value cl 'hyperlinks) hyperlinks)))
402
403 (defun init-hyperobject-class (cl)
404   "Initialize a hyperobject class. Calculates all class slots"
405   (finalize-subobjects cl)
406   (finalize-views cl)
407   (finalize-hyperlinks cl)
408   (finalize-sql cl)
409   (finalize-rules cl)
410   (finalize-documentation cl))
411
412
413 ;;;; *************************************************************************
414 ;;;;  Metaclass Slot Accessors
415 ;;;; *************************************************************************
416
417 (defun find-slot-by-name (cl name)
418   (find name (class-slots cl) :key #'slot-definition-name))
419
420 (defun hyperobject-class-user-name (obj)
421   (awhen (user-name (class-of obj))
422          (if (consp it)
423              (car it)
424              it)))
425
426 (defun hyperobject-class-subobjects (obj)
427   (subobjects (class-of obj)))
428
429 (defun hyperobject-class-hyperlinks (obj)
430   (hyperlinks (class-of obj)))
431
432 (defun hyperobject-class-slots (obj)
433   ;; cmucl/sbcl reverse class-slots
434   #+(or cmu sbcl) (reverse (class-slots (class-of obj)))
435   #-(or cmu sbcl) (class-slots (class-of obj)))
436