r4286: 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.28 2003/03/29 20:22:37 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 function :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-other-options
131                                             slot)
132        (list option `',value))
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 (defmethod compute-effective-slot-definition :around ((cl hyperobject-class)
195                                                       #+(or allegro lispworks) name
196                                                       dsds)
197   #+allergo (declare (ignore name))
198   (let* ((dsd (car dsds))
199          (value-type (canonicalize-value-type (slot-value dsd 'value-type))))
200     (multiple-value-bind (sql-type length) (value-type-to-sql-type value-type)
201       (setf (slot-value dsd 'sql-type) sql-type)
202       (setf (slot-value dsd 'type) (value-type-to-lisp-type value-type))
203       (let ((ia (compute-effective-slot-definition-initargs
204                  cl #+lispworks name dsds)))
205         (apply
206          #'make-instance 'hyperobject-esd 
207          :value-type value-type
208          :sql-type sql-type
209          :length length
210          :print-formatter (slot-value dsd 'print-formatter)
211          :subobject (slot-value dsd 'subobject)
212          :hyperlink (slot-value dsd 'hyperlink)
213          :hyperlink-parameters (slot-value dsd 'hyperlink-parameters)
214          :description (slot-value dsd 'description)
215          :user-name (slot-value dsd 'user-name)
216          :index (slot-value dsd 'index)
217          :value-constraint (slot-value dsd 'value-constraint)
218          :null-allowed (slot-value dsd 'null-allowed)
219          ia)))))
220   
221 (defun value-type-to-lisp-type (value-type)
222   (case (if (atom value-type)
223             value-type
224             (car value-type))
225     ((:string :cdata :varchar :char)
226      'string)
227     (:character
228      'character)
229     (:fixnum
230      'fixnum)
231     (:boolean
232      'boolean)
233     (:integer
234      'integer)
235     ((:float :single-float)
236      'single-float)
237     (:double-float
238      'double-float)
239     (otherwise
240      t)))
241
242 (defun value-type-to-sql-type (value-type)
243   "Return two values, the sql type and field length."
244   (let ((type (if (atom value-type)
245                   value-type
246                   (car value-type)))
247         (length (when (consp value-type)
248                   (cadr value-type))))
249     (values
250      (case type
251        ((:string :cdata)
252         :string)
253        ((:fixnum :integer)
254         :integer)
255        (:boolean
256         :boolean)
257        ((:float :single-float)
258         :single-float)
259        (:double-float
260         :double-float)
261        (otherwise
262         :text))
263      length)))
264
265 ;;;; Class initialization function
266
267 ;; defines a slot-unbound method for class and slot-name, fills
268 ;; the slot by calling reader function with the slot values of
269 ;; the instance's reader-keys
270 (defmacro def-lazy-reader (class slot-name reader &rest reader-keys)
271   (let* ((the-slot-name (gensym))
272          (the-class (gensym))
273          (the-instance (gensym))
274          (keys '()))
275     (dolist (key reader-keys)
276       (push (list 'slot-value the-instance (list 'quote key)) keys))
277     (setq keys (nreverse keys))
278     `(defmethod slot-unbound (,the-class (,the-instance ,class)
279                                          (,the-slot-name (eql ',slot-name)))
280        (declare (ignore ,the-class))
281        (setf (slot-value ,the-instance ,the-slot-name)
282            (,reader ,@keys)))))
283
284 #+(or sbcl scl cmu)
285 (defun ensure-lazy-reader (class slot-name reader &rest reader-keys)
286   (let ((keys nil)
287         (gf (ensure-generic-function 'slot-unbound)))
288     (dolist (key reader-keys)
289       (push (list 'slot-value 'the-instance (list 'quote key)) keys))
290     (setq keys (nreverse keys))
291     (multiple-value-bind (method-lambda init-args-values)
292       (make-method-lambda
293        gf
294        (class-prototype (generic-function-method-class gf))
295        `(lambda (the-class the-instance the-slot-name)
296          (declare (ignore the-class))
297          (setf (slot-value the-instance the-slot-name) (,reader ,@keys)))
298        nil)
299         (add-method gf
300                     (apply
301                      #'make-instance (generic-function-method-class gf)
302                      ':specializers (list t class (intern-eql-specializer slot-name))
303                      ':lambda-list '(the-class the-instance the-slot-name)
304                      ':function `(function ,method-lambda)
305                      init-args-values)))))
306
307 (defun finalize-subobjects (cl)
308   "Process class subobjects slot"
309   (setf (subobjects cl)
310     (let ((subobjects '()))
311       (dolist (slot (class-slots cl))
312         (let-when (subobj-def (esd-subobject slot))
313           (let ((subobject (make-instance 'subobject
314                                           :name-class (class-name cl)
315                                           :name-slot (slot-definition-name slot)
316                                           :lookup (if (atom subobj-def)
317                                                       subobj-def
318                                                       (car subobj-def))
319                                           :lookup-keys (if (atom subobj-def)
320                                                            nil
321                                                            (cdr subobj-def)))))
322             (unless (eq (lookup subobject) t)
323               #-(or sbcl cmu scl)
324               (eval
325                `(hyperobject::def-lazy-reader ,(name-class subobject)
326                  ,(name-slot subobject) ,(lookup subobject)
327                  ,@(lookup-keys subobject)))
328               #+(or sbcl cmu scl)
329               (apply #'ensure-lazy-reader 
330                      (name-class subobject) (name-slot subobject) (lookup subobject) (lookup-keys subobject))
331               )
332             (push subobject subobjects))))
333       subobjects)))
334
335 (defun finalize-documentation (cl)
336   "Calculate class documentation slot"
337   (awhen (slot-value cl 'user-name)
338          (setf (slot-value cl 'user-name)
339                (etypecase (slot-value cl 'user-name)
340                    (cons (car it))
341                    ((or string symbol) it))))
342   (awhen (slot-value cl 'description)
343          (setf (slot-value cl 'description)
344                (etypecase (slot-value cl 'description)
345                    (cons (car it))
346                    ((or string symbol) it))))
347
348   (let ((*print-circle* nil))
349     (setf (documentation (class-name cl) 'class)
350       (format nil "Hyperobject~A~A~A~A"
351               (aif (user-name cl)
352                    (format nil ": ~A" it ""))
353               (aif (description cl)
354                    (format nil "~%Class description: ~A" it) "")
355               (aif (subobjects cl)
356                    (format nil "~%Subobjects:~{ ~A~}" (mapcar #'name-slot it)) "")
357               (aif (default-print-slots cl)
358                    (format nil "~%Default print slots:~{ ~A~}" it) "")
359               ))))
360
361 (defun finalize-hyperlinks (cl)
362   (let ((hyperlinks '()))
363     (dolist (esd (class-slots cl))
364       (awhen (slot-value esd 'hyperlink)
365         (push
366          (make-instance 'hyperlink
367                         :name (slot-definition-name esd)
368                         :lookup it
369                         :link-parameters (slot-value esd 'hyperlink-parameters))
370          hyperlinks)))
371     (setf (slot-value cl 'hyperlinks) hyperlinks)))
372
373 (defun init-hyperobject-class (cl)
374   "Initialize a hyperobject class. Calculates all class slots"
375   (finalize-subobjects cl)
376   (finalize-views cl)
377   (finalize-hyperlinks cl)
378   (finalize-sql cl)
379   (finalize-rules cl)
380   (finalize-documentation cl))
381
382
383 ;;;; *************************************************************************
384 ;;;;  Metaclass Slot Accessors
385 ;;;; *************************************************************************
386
387 (defun find-slot-by-name (cl name)
388   (find name (class-slots cl) :key #'slot-definition-name))
389
390 (defun hyperobject-class-user-name (obj)
391   (awhen (user-name (class-of obj))
392          (if (consp it)
393              (car it)
394              it)))
395
396 (defun hyperobject-class-subobjects (obj)
397   (subobjects (class-of obj)))
398
399 (defun hyperobject-class-hyperlinks (obj)
400   (hyperlinks (class-of obj)))
401
402 (defun hyperobject-class-fields (obj)
403   (class-slots (class-of obj)))
404