Commit | Line | Data |
---|---|---|
5dea194e | 1 | (in-package :lisp-on-lines) |
13ebe12f | 2 | |
2b0fd9c8 DC |
3 | (define-layered-class description () |
4 | ((description-type | |
5 | :initarg :type | |
6 | :accessor description.type | |
7 | :initform 'viewer | |
8 | :special t) | |
9 | (description-layers | |
10 | :initarg :layers | |
11 | :accessor description.layers | |
12 | :initform nil | |
13 | :special t) | |
14 | (description-properties | |
15 | :accessor description.properties | |
16 | :initform nil | |
fb04c0a8 | 17 | :special t |
18 | :documentation "TODO: not used much anymore, and shouldn't be relied on") | |
0386c736 | 19 | (described-object |
20 | :layered-accessor object | |
21 | :initform nil | |
22 | :special t) | |
ebabbd23 | 23 | (description-default-attributes |
24 | :accessor default-attributes | |
25 | :initarg :default-attributes | |
26 | :initform nil | |
27 | :special t) | |
2b0fd9c8 DC |
28 | (description-attributes |
29 | :accessor attributes | |
30 | :initarg :attributes | |
31 | :initform nil | |
fb04c0a8 | 32 | :special t) |
33 | (description-default-properties | |
34 | :accessor default-properties | |
35 | :initarg :default-properties | |
36 | :initform '() | |
2b0fd9c8 | 37 | :special t))) |
579597e3 | 38 | |
fb04c0a8 | 39 | (defmethod attributes :around ((description description)) |
40 | "Add any default properties to the attributes" | |
41 | ||
42 | (let ((default-properties (default-properties description))) | |
43 | (if (and (listp default-properties) | |
44 | (not (null default-properties))) | |
45 | (let ((a (mapcar #'(lambda (att) | |
46 | (append (ensure-list att) default-properties)) | |
47 | (call-next-method)))) | |
48 | ||
49 | ||
50 | a) | |
51 | (call-next-method)))) | |
52 | ||
2b0fd9c8 DC |
53 | (defmethod print-object ((self description) stream) |
54 | (print-unreadable-object (self stream :type t) | |
55 | (with-slots (description-type) self | |
7553e5e8 | 56 | (format stream "~A" description-type)))) |
579597e3 | 57 | |
15bc66bd DC |
58 | ;;;; * Occurences |
59 | ||
60 | (defvar *occurence-map* (make-hash-table) | |
2b0fd9c8 | 61 | "a display is generated by associating an 'occurence' |
15bc66bd | 62 | with an instance of a class. This is usually keyed off class-name, |
7553e5e8 | 63 | although an arbitrary occurence could be used with an arbitrary class.") |
15bc66bd DC |
64 | |
65 | (define-layered-class | |
2b0fd9c8 | 66 | standard-occurence (description) |
ebabbd23 | 67 | ((occurence-name :accessor name :initarg :name) |
68 | (attribute-map :accessor attribute-map :initform (make-hash-table))) | |
15bc66bd DC |
69 | (:documentation |
70 | "an occurence holds the attributes like a class holds slot-definitions. | |
71 | Attributes are the metadata used to display, validate, and otherwise manipulate actual values stored in lisp objects.")) | |
72 | ||
73 | (defun find-or-create-occurence (name) | |
74 | "Returns the occurence associated with this name." | |
75 | (let ((occurence (gethash name *occurence-map*))) | |
76 | (if occurence | |
77 | occurence | |
ebabbd23 | 78 | (let ((new-occurence (make-instance 'standard-occurence :name name))) |
15bc66bd DC |
79 | (setf (gethash name *occurence-map*) new-occurence) |
80 | new-occurence)))) | |
81 | ||
82 | (defun clear-occurence (occurence) | |
83 | "removes all attributes from the occurence" | |
84 | (setf (attribute-map occurence) (make-hash-table))) | |
85 | ||
86 | (defgeneric find-occurence (name) | |
bf12489a DC |
87 | (:method (thing) |
88 | nil) | |
15bc66bd DC |
89 | (:method ((name symbol)) |
90 | (find-or-create-occurence name)) | |
bf12489a | 91 | (:method ((instance standard-object)) |
15bc66bd DC |
92 | (find-or-create-occurence (class-name (class-of instance))))) |
93 | ||
94 | ||
2b0fd9c8 DC |
95 | (define-layered-class |
96 | attribute (description) | |
0386c736 | 97 | ((attribute-name :layered-accessor attribute.name |
2b0fd9c8 DC |
98 | :initarg :name |
99 | :initform (gensym "ATTRIBUTE-") | |
100 | :special t) | |
101 | (occurence :accessor occurence :initarg :occurence :initform nil) | |
0386c736 | 102 | (label :initarg :label :layered-accessor label :initform nil :special t))) |
2b0fd9c8 | 103 | |
fb04c0a8 | 104 | |
15bc66bd | 105 | ;;;; * Attributes |
2b0fd9c8 DC |
106 | (defmethod print-object ((self attribute) stream) |
107 | (print-unreadable-object (self stream :type t) | |
ebabbd23 | 108 | (with-slots (attribute-name description-type) self |
109 | (format stream "~A ~A" description-type attribute-name)))) | |
15bc66bd DC |
110 | |
111 | (define-layered-class | |
2b0fd9c8 DC |
112 | standard-attribute (attribute) |
113 | ((setter :accessor setter :initarg :setter :special t :initform nil) | |
114 | (getter :accessor getter :initarg :getter :special t :initform nil) | |
ebabbd23 | 115 | (value :accessor value :initarg :value :special t) |
116 | (slot-name :accessor slot-name :initarg :slot-name :special t :initform nil)) | |
15bc66bd DC |
117 | (:documentation "Attributes are used to display a part of a thing, such as a slot of an object, a text label, the car of a list, etc.")) |
118 | ||
fb04c0a8 | 119 | (define-layered-method label :around ((attribute standard-attribute)) |
120 | (or (call-next-method) (attribute.name attribute))) | |
121 | ||
6f63d3a4 | 122 | (defmacro defattribute (name supers slots &rest args) |
0386c736 | 123 | (let* ( |
124 | (type-provided-p (second (assoc :type-name args))) | |
125 | (type (or type-provided-p name)) | |
2b0fd9c8 DC |
126 | (layer (or (second (assoc :in-layer args)) nil)) |
127 | (properties (cdr (assoc :default-properties args))) | |
ebabbd23 | 128 | (cargs (remove-if #'(lambda (key) |
2b0fd9c8 DC |
129 | (or (eql key :type-name) |
130 | (eql key :default-properties) | |
131 | (eql key :default-initargs) | |
132 | (eql key :in-layer))) | |
133 | args | |
134 | :key #'car))) | |
135 | ||
6f63d3a4 | 136 | `(progn |
6f63d3a4 | 137 | (define-layered-class |
91f2ab7b | 138 | ;;;; TODO: fix the naive way of making sure s-a is a superclass |
2b0fd9c8 DC |
139 | ;;;; Need some MOPey goodness. |
140 | ,name ,@ (when layer `(:in-layer ,layer)),(or supers '(standard-attribute)) | |
141 | ,(append slots (properties-as-slots properties)) | |
142 | #+ (or) ,@ (cdr cargs) | |
143 | ,@cargs | |
144 | (:default-initargs :properties (list ,@properties) | |
145 | ,@ (cdr (assoc :default-initargs args)))) | |
146 | ||
ebabbd23 | 147 | ,(when (or |
148 | type-provided-p | |
149 | (not (find-attribute-class-for-type name))) | |
150 | `(defmethod find-attribute-class-for-type ((type (eql ',type))) | |
0386c736 | 151 | ',name))))) |
15bc66bd DC |
152 | |
153 | (defun clear-attributes (name) | |
154 | "removes all attributes from an occurance" | |
155 | (clear-occurence (find-occurence name))) | |
156 | ||
6f63d3a4 DC |
157 | (defmethod find-attribute-class-for-type (type) |
158 | nil) | |
159 | ||
ebabbd23 | 160 | (defun make-attribute (&rest args &key type &allow-other-keys) |
2b0fd9c8 DC |
161 | (apply #'make-instance |
162 | (or (find-attribute-class-for-type type) | |
ebabbd23 | 163 | 'standard-attribute) |
164 | :properties args | |
2b0fd9c8 | 165 | args)) |
6f63d3a4 | 166 | |
2b0fd9c8 | 167 | (defmethod ensure-attribute ((occurence standard-occurence) &rest args &key name &allow-other-keys) |
15bc66bd | 168 | "Creates an attribute in the given occurence" |
2b0fd9c8 | 169 | (let ((attribute (apply #'make-attribute :occurence occurence args))) |
ebabbd23 | 170 | (setf (find-attribute occurence name) attribute))) |
171 | ||
172 | (defmethod find-attribute ((occurence null) name) | |
173 | nil) | |
15bc66bd DC |
174 | |
175 | (defmethod find-attribute ((occurence standard-occurence) name) | |
ebabbd23 | 176 | (or (gethash name (attribute-map occurence)) |
177 | (let* ((class (ignore-errors (find-class (name occurence)))) | |
178 | (class-direct-superclasses | |
179 | (when class | |
180 | (closer-mop:class-direct-superclasses | |
181 | class)))) | |
182 | (when class-direct-superclasses | |
183 | (let ((attribute | |
184 | (find-attribute | |
185 | (find-occurence (class-name | |
186 | (car | |
187 | class-direct-superclasses))) | |
188 | name))) | |
189 | attribute))))) | |
15bc66bd DC |
190 | |
191 | (defmethod find-all-attributes ((occurence standard-occurence)) | |
192 | (loop for att being the hash-values of (attribute-map occurence) | |
fb04c0a8 | 193 | collect att)) |
15bc66bd | 194 | |
2b0fd9c8 DC |
195 | (defmethod ensure-attribute (occurence-name &rest args &key name type &allow-other-keys) |
196 | (declare (ignore name type)) | |
197 | (apply #'ensure-attribute | |
15bc66bd | 198 | (find-occurence occurence-name) |
2b0fd9c8 | 199 | args)) |
15bc66bd DC |
200 | |
201 | ;;;; The following functions make up the public interface to the | |
202 | ;;;; MEWA Attribute Occurence system. | |
203 | ||
204 | (defmethod find-all-attributes (occurence-name) | |
205 | (find-all-attributes (find-occurence occurence-name))) | |
206 | ||
207 | (defmethod find-attribute (occurence-name attribute-name) | |
2b0fd9c8 | 208 | "Return the ATTRIBUTE named by ATTRIBUTE-NAME in OCCURANCE-name" |
15bc66bd DC |
209 | (find-attribute (find-occurence occurence-name) attribute-name)) |
210 | ||
2b0fd9c8 DC |
211 | (defmethod (setf find-attribute) ((attribute-spec list) occurence-name attribute-name) |
212 | "Create a new attribute in the occurence. | |
213 | ATTRIBUTE-SPEC: a list of (type name &rest initargs)" | |
214 | (apply #'ensure-attribute occurence-name :name attribute-name :type (first attribute-spec) (rest attribute-spec))) | |
15bc66bd | 215 | |
ebabbd23 | 216 | (defmethod (setf find-attribute) ((attribute standard-attribute) occurence attribute-name) |
217 | "Create a new attribute in the occurence. | |
218 | ATTRIBUTE-SPEC: a list of (type name &rest initargs)" | |
219 | (setf (gethash attribute-name (attribute-map occurence)) | |
220 | attribute)) | |
221 | ||
222 | (defmethod (setf find-attribute) ((attribute null) occurence attribute-name) | |
223 | "Create a new attribute in the occurence. | |
224 | ATTRIBUTE-SPEC: a list of (type name &rest initargs)" | |
225 | (setf (gethash attribute-name (attribute-map occurence)) | |
226 | attribute)) | |
227 | ||
2b0fd9c8 DC |
228 | (defmethod find-attribute ((attribute-with-occurence attribute) attribute-name) |
229 | (find-attribute (occurence attribute-with-occurence) attribute-name)) | |
579597e3 | 230 | |
15bc66bd | 231 | (defmethod set-attribute-properties ((occurence-name t) attribute properties) |
2b0fd9c8 DC |
232 | (setf (description.properties attribute) (plist-nunion |
233 | properties | |
234 | (description.properties attribute))) | |
235 | (loop for (initarg value) on (description.properties attribute) | |
236 | by #'cddr | |
237 | with map = (initargs.slot-names attribute) | |
238 | do (let ((s-n (assoc-if #'(lambda (x) (member initarg x)) map))) | |
239 | ||
240 | (if s-n | |
241 | (progn | |
242 | (setf (slot-value attribute | |
243 | (cdr s-n)) | |
244 | value)) | |
245 | (warn "Cannot find initarg ~A in attribute ~S" initarg attribute))) | |
246 | finally (return attribute))) | |
247 | ||
248 | (defmethod set-attribute (occurence-name attribute-name attribute-spec &key (inherit t)) | |
249 | "If inherit is T, sets the properties of the attribute only, unless the type has changed. | |
250 | otherwise, (setf find-attribute)" | |
251 | (let ((att (find-attribute occurence-name attribute-name))) | |
252 | (if (and att inherit (or (eql (car attribute-spec) | |
253 | (description.type att)) | |
254 | (eq (car attribute-spec) t))) | |
255 | (set-attribute-properties occurence-name att (cdr attribute-spec)) | |
256 | (setf (find-attribute occurence-name attribute-name) | |
257 | (cons (car attribute-spec) | |
258 | (plist-nunion | |
259 | (cdr attribute-spec) | |
260 | (when att (description.properties att)))))))) | |
fc3e754f | 261 | |
15bc66bd | 262 | (defmethod perform-define-attributes ((occurence-name t) attributes) |
fc3e754f DC |
263 | (loop for attribute in attributes |
264 | do (destructuring-bind (name type &rest args) | |
265 | attribute | |
2b0fd9c8 DC |
266 | (cond ((not (null type)) |
267 | ;;set the type as well | |
268 | (set-attribute occurence-name name (cons type args))))))) | |
fc3e754f | 269 | |
15bc66bd | 270 | (defmacro define-attributes (occurence-names &body attribute-definitions) |
fc3e754f | 271 | `(progn |
15bc66bd DC |
272 | ,@(loop for occurence-name in occurence-names |
273 | collect `(perform-define-attributes (quote ,occurence-name) (quote ,attribute-definitions))))) | |
274 | ||
2b0fd9c8 DC |
275 | |
276 | (defmethod find-description (object type) | |
277 | (let ((occurence (find-occurence object))) | |
fb04c0a8 | 278 | occurence)) |
15bc66bd | 279 | |
ebabbd23 | 280 | ;;"Unused???" |
15bc66bd | 281 | (defmethod setter (attribute) |
6f63d3a4 | 282 | (warn "Setting ~A in ~A" attribute *context*) |
2b0fd9c8 DC |
283 | (let ((setter (getf (description.properties attribute) :setter)) |
284 | (slot-name (getf (description.properties attribute) :slot-name))) | |
15bc66bd DC |
285 | (cond (setter |
286 | setter) | |
287 | (slot-name | |
288 | #'(lambda (value object) | |
289 | (setf (slot-value object slot-name) value))) | |
290 | (t | |
291 | #'(lambda (value object) | |
2b0fd9c8 | 292 | (warn "Can't find anywere to set ~A in ~A using ~A" value object attribute)))))) |
15bc66bd | 293 | |
d5e996b3 | 294 | |
6f63d3a4 DC |
295 | (define-layered-function attribute-value (instance attribute) |
296 | (:documentation " Like SLOT-VALUE for instances, the base method calls GETTER.")) | |
297 | ||
ebabbd23 | 298 | (defmethod attribute-slot-value (instance attribute) |
7553e5e8 | 299 | "Return (VALUES slot-value-or-nil existsp boundp |
300 | ||
301 | If this attribute, in its current context, refers to a slot, | |
302 | we return slot-value-or nil either boundp or not." | |
ebabbd23 | 303 | (let (existsp boundp slot-value-or-nil) |
304 | (cond | |
305 | ((and (slot-boundp attribute 'slot-name) (slot-name attribute)) | |
306 | (when (slot-exists-p instance (slot-name attribute)) | |
307 | (setf existsp t) | |
308 | (when (slot-boundp instance (slot-name attribute)) | |
309 | (setf boundp t | |
310 | slot-value-or-nil (slot-value | |
311 | instance | |
312 | (slot-name attribute)))))) | |
313 | ((and (slot-exists-p instance (attribute.name attribute))) | |
314 | (setf existsp t) | |
315 | (when (slot-boundp instance (attribute.name attribute)) | |
316 | (setf boundp t | |
317 | slot-value-or-nil (slot-value | |
318 | instance | |
319 | (attribute.name attribute)))))) | |
320 | (VALUES slot-value-or-nil existsp boundp))) | |
321 | ||
6f63d3a4 | 322 | (define-layered-method attribute-value (instance (attribute standard-attribute)) |
ebabbd23 | 323 | "return the attribute value or NIL if it cannot be found" |
324 | (with-slots (getter value) attribute | |
325 | (when (slot-boundp attribute 'value) | |
326 | (setf getter (constantly value))) | |
327 | (if (and (slot-boundp attribute 'getter) getter) | |
328 | ;;;; call the getter | |
329 | (funcall getter instance) | |
330 | ;;;; or default to the attribute-slot-value | |
331 | (attribute-slot-value instance attribute)))) | |
6f63d3a4 | 332 | |
a4e6154d | 333 | (define-layered-function (setf attribute-value) (value instance attribute)) |
6f63d3a4 | 334 | |
2b0fd9c8 DC |
335 | (define-layered-method |
336 | (setf attribute-value) (value instance (attribute standard-attribute)) | |
2b0fd9c8 DC |
337 | (with-slots (setter slot-name) attribute |
338 | (cond ((and (slot-boundp attribute 'setter) setter) | |
2b0fd9c8 DC |
339 | (funcall setter value instance)) |
340 | ((and (slot-boundp attribute 'slot-name) slot-name) | |
341 | (setf (slot-value instance slot-name) value)) | |
ebabbd23 | 342 | ((and (slot-exists-p instance (attribute.name attribute))) |
2b0fd9c8 DC |
343 | (setf (slot-value instance (attribute.name attribute)) value)) |
344 | (t | |
345 | (error "Cannot set ~A in ~A" attribute instance))))) | |
d5e996b3 DC |
346 | |
347 | ||
a4e6154d | 348 | |
d5e996b3 | 349 | ;;;; ** Default Attributes |
fb04c0a8 | 350 | ;;;; TODO: This is mosty an ugly hack and should be reworked. |
351 | ;;;; | |
d5e996b3 DC |
352 | ;;;; The default mewa class contains the types use as defaults. |
353 | ;;;; maps meta-model slot-types to slot-presentation | |
354 | ||
355 | (defvar *default-attributes-class-name* 'default) | |
356 | ||
15bc66bd DC |
357 | (defmacro with-default-attributes ((occurence-name) &body body) |
358 | `(let ((*default-attributes-class-name* ',occurence-name)) | |
359 | ,@body)) | |
360 | ||
d5e996b3 | 361 | (define-attributes (default) |
ebabbd23 | 362 | (boolean boolean) |
363 | (string string) | |
364 | (number currency) | |
365 | (integer integer) | |
366 | (currency currency) | |
367 | (clsql:generalized-boolean boolean) | |
fb04c0a8 | 368 | (foreign-key has-a)) |
d5e996b3 | 369 | |
15bc66bd DC |
370 | (defun attribute-to-definition (attribute) |
371 | (nconc (list (attribute.name attribute) | |
2b0fd9c8 DC |
372 | (description.type attribute)) |
373 | (description.properties attribute))) | |
d5e996b3 | 374 | |
15bc66bd | 375 | (defun find-default-presentation-attribute-definitions () |
fb04c0a8 | 376 | nil) |
377 | ||
d5e996b3 | 378 | (defun gen-ptype (type) |
15bc66bd DC |
379 | (let* ((type (if (consp type) (car type) type)) |
380 | (possible-default (find-attribute *default-attributes-class-name* type)) | |
381 | (real-default (find-attribute 'default type))) | |
382 | (cond | |
383 | (possible-default | |
2b0fd9c8 | 384 | (description.type possible-default)) |
15bc66bd | 385 | (real-default |
2b0fd9c8 | 386 | (description.type real-default)) |
15bc66bd | 387 | (t type)))) |
d5e996b3 DC |
388 | |
389 | (defun gen-presentation-slots (instance) | |
390 | (mapcar #'(lambda (x) (gen-pslot (cadr x) | |
391 | (string (car x)) | |
392 | (car x))) | |
393 | (meta-model:list-slot-types instance))) | |
394 | ||
34e8e2d6 | 395 | |
d5e996b3 DC |
396 | (defun gen-pslot (type label slot-name) |
397 | (copy-list `(,(gen-ptype type) | |
398 | :label ,label | |
399 | :slot-name ,slot-name))) | |
400 | ||
233380f7 | 401 | ;; This software is Copyright (c) Drew Crampsie, 2004-2005. |