1 | ;;+----------------------------------------------------------------------------- |
---|
2 | ;;+ Isidorus |
---|
3 | ;;+ (c) 2008-2010 Marc Kuester, Christoph Ludwig, Lukas Georgieff |
---|
4 | ;;+ |
---|
5 | ;;+ Isidorus is freely distributable under the LLGPL license. |
---|
6 | ;;+ You can find a detailed description in trunk/docs/LLGPL-LICENSE.txt and |
---|
7 | ;;+ trunk/docs/LGPL-LICENSE.txt. |
---|
8 | ;;+----------------------------------------------------------------------------- |
---|
9 | |
---|
10 | |
---|
11 | (in-package :datamodel) |
---|
12 | |
---|
13 | |
---|
14 | (defgeneric roles-by-type (construct role-type &key revision) |
---|
15 | (:documentation "Returns all roles of the passed topic or |
---|
16 | association that is of the specified role-type. |
---|
17 | If role-type is set to nil all roles are returned.")) |
---|
18 | |
---|
19 | |
---|
20 | (defmethod roles-by-type ((construct TopicC) role-type &key (revision *TM-REVISION*)) |
---|
21 | (declare (integer revision) |
---|
22 | (type (or Null TopicC) role-type)) |
---|
23 | (if role-type |
---|
24 | (remove-null |
---|
25 | (map 'list #'(lambda(role) |
---|
26 | (when (eql (instance-of role :revision revision) |
---|
27 | role-type) |
---|
28 | role)) |
---|
29 | (player-in-roles construct :revision revision))) |
---|
30 | (player-in-roles construct :revision revision))) |
---|
31 | |
---|
32 | |
---|
33 | (defmethod roles-by-type ((construct AssociationC) role-type |
---|
34 | &key (revision *TM-REVISION*)) |
---|
35 | (declare (integer revision) |
---|
36 | (type (or Null TopicC) role-type)) |
---|
37 | (if role-type |
---|
38 | (remove-null |
---|
39 | (map 'list #'(lambda(role) |
---|
40 | (when (eql (instance-of role :revision revision) |
---|
41 | role-type) |
---|
42 | role)) |
---|
43 | (roles construct :revision revision))) |
---|
44 | (roles construct :revision revision))) |
---|
45 | |
---|
46 | |
---|
47 | (defgeneric roles-by-player (construct role-player |
---|
48 | &key role-player-is-type revision) |
---|
49 | (:documentation "Returns all roles that contains the corresponding player. |
---|
50 | If the player is set to nil all roles are returned.") |
---|
51 | (:method ((construct AssociationC) role-player |
---|
52 | &key role-player-is-type (revision *TM-REVISION*)) |
---|
53 | (declare (integer revision) |
---|
54 | (type (or Null TopicC) role-player) |
---|
55 | (boolean role-player-is-type)) |
---|
56 | (if role-player |
---|
57 | (remove-null |
---|
58 | (map 'list #'(lambda(role) |
---|
59 | (if role-player-is-type |
---|
60 | (when (isa (player role :revision revision) |
---|
61 | role-player) |
---|
62 | role) |
---|
63 | (when (eql (player role :revision revision) |
---|
64 | role-player) |
---|
65 | role))) |
---|
66 | (roles construct :revision revision))) |
---|
67 | (roles construct :revision revision)))) |
---|
68 | |
---|
69 | |
---|
70 | (defun filter-associations-by-type (associations association-type |
---|
71 | &key (revision *TM-REVISION*)) |
---|
72 | "Returns a list of associations that are an instance-of of the given |
---|
73 | association-type. If association-type is set to nil, all associations |
---|
74 | are returned." |
---|
75 | (declare (List associations) |
---|
76 | (type (or Null TopicC) association-type) |
---|
77 | (integer revision)) |
---|
78 | (if association-type |
---|
79 | (remove-if #'(lambda(assoc) |
---|
80 | (not (eql (instance-of assoc :revision revision) |
---|
81 | association-type))) |
---|
82 | associations) |
---|
83 | associations)) |
---|
84 | |
---|
85 | |
---|
86 | (defun filter-associations-by-role (associations role-type role-player |
---|
87 | &key role-player-is-type (revision *TM-REVISION*)) |
---|
88 | "Returns associations that have a role corresponding to the passed |
---|
89 | values. If any of the passed role-values is set to nil, it won't be used |
---|
90 | for the evaluation of the result." |
---|
91 | (declare (List associations) |
---|
92 | (type (or Null TopicC) role-type role-player) |
---|
93 | (boolean role-player-is-type)) |
---|
94 | (remove-null |
---|
95 | (intersection |
---|
96 | (map 'list #'(lambda(assoc) |
---|
97 | (when (roles-by-type assoc role-type |
---|
98 | :revision revision) |
---|
99 | assoc)) |
---|
100 | associations) |
---|
101 | (map 'list #'(lambda(assoc) |
---|
102 | (when (roles-by-player |
---|
103 | assoc role-player |
---|
104 | :role-player-is-type role-player-is-type |
---|
105 | :revision revision) |
---|
106 | assoc)) |
---|
107 | associations)))) |
---|
108 | |
---|
109 | |
---|
110 | (defgeneric associations-of (construct role-type association-type |
---|
111 | other-role-type other-player |
---|
112 | &key other-role-player-is-type |
---|
113 | revision) |
---|
114 | (:documentation "Returns all associations of the passed topic (construct) |
---|
115 | that corresponds to the given values. |
---|
116 | If any of the passed values is set to nil, it won't be |
---|
117 | used to evaluate the result.") |
---|
118 | (:method ((construct TopicC) role-type association-type other-role-type |
---|
119 | other-player &key other-role-player-is-type |
---|
120 | (revision *TM-REVISION*)) |
---|
121 | (declare (integer revision) |
---|
122 | (type (or Null TopicC) role-type association-type |
---|
123 | other-role-type other-player) |
---|
124 | (boolean other-role-player-is-type)) |
---|
125 | (let ((assocs-by-role (map 'list #'(lambda(role) |
---|
126 | (parent role :revision revision)) |
---|
127 | (roles-by-type construct role-type |
---|
128 | :revision revision)))) |
---|
129 | (let ((assocs-by-type |
---|
130 | (filter-associations-by-type assocs-by-role association-type |
---|
131 | :revision revision))) |
---|
132 | (filter-associations-by-role |
---|
133 | assocs-by-type other-role-type other-player |
---|
134 | :role-player-is-type other-role-player-is-type |
---|
135 | :revision revision))))) |
---|
136 | |
---|
137 | |
---|
138 | (defgeneric instance-of-associations (construct &key revision) |
---|
139 | (:documentation "Returns all type-instance associations of |
---|
140 | the passed instance topic.") |
---|
141 | (:method ((construct TopicC) &key (revision *TM-REVISION*)) |
---|
142 | (declare (integer revision)) |
---|
143 | (let ((type-top |
---|
144 | (get-item-by-psi *type-psi* :revision revision :error-if-nil t)) |
---|
145 | (instance-top |
---|
146 | (get-item-by-psi *instance-psi* :revision revision :error-if-nil t)) |
---|
147 | (type-instance-top |
---|
148 | (get-item-by-psi *type-instance-psi* :revision revision |
---|
149 | :error-if-nil t))) |
---|
150 | (let ((possible-assocs |
---|
151 | (map 'list #'(lambda(role) |
---|
152 | (parent role :revision revision)) |
---|
153 | (roles-by-type construct instance-top :revision revision)))) |
---|
154 | (let ((type-instance-assocs |
---|
155 | (filter-associations-by-type possible-assocs type-instance-top |
---|
156 | :revision revision))) |
---|
157 | (filter-associations-by-role type-instance-assocs type-top nil |
---|
158 | :revision revision)))))) |
---|
159 | |
---|
160 | |
---|
161 | (defgeneric supertype-associations (construct &key revision) |
---|
162 | (:documentation "Returns all supertype-subtype associations of |
---|
163 | the passed subtype topic.") |
---|
164 | (:method ((construct TopicC) &key (revision *TM-REVISION*)) |
---|
165 | (declare (integer revision)) |
---|
166 | (let ((supertype-top |
---|
167 | (get-item-by-psi *supertype-psi* :revision revision :error-if-nil t)) |
---|
168 | (subtype-top |
---|
169 | (get-item-by-psi *subtype-psi* :revision revision :error-if-nil t)) |
---|
170 | (supertype-subtype-top |
---|
171 | (get-item-by-psi *supertype-subtype-psi* :revision revision |
---|
172 | :error-if-nil t))) |
---|
173 | (let ((possible-assocs |
---|
174 | (map 'list #'(lambda(role) |
---|
175 | (parent role :revision revision)) |
---|
176 | (roles-by-type construct subtype-top :revision revision)))) |
---|
177 | (let ((type-instance-assocs |
---|
178 | (filter-associations-by-type possible-assocs supertype-subtype-top |
---|
179 | :revision revision))) |
---|
180 | (filter-associations-by-role type-instance-assocs supertype-top nil |
---|
181 | :revision revision)))))) |
---|
182 | |
---|
183 | |
---|
184 | (defgeneric direct-supertypes (construct &key revision) |
---|
185 | (:documentation "Returns all direct super type topics of the passed |
---|
186 | construct.") |
---|
187 | (:method ((construct TopicC) &key (revision *TM-REVISION*)) |
---|
188 | (declare (integer revision)) |
---|
189 | (let ((assocs (supertype-associations construct :revision revision))) |
---|
190 | (let ((other-roles |
---|
191 | (remove-null |
---|
192 | (map 'list |
---|
193 | #'(lambda(assoc) |
---|
194 | (find-if-not #'(lambda(role) |
---|
195 | (eql (player role :revision revision) |
---|
196 | construct)) |
---|
197 | (roles assoc :revision revision))) |
---|
198 | assocs)))) |
---|
199 | (remove-null (map 'list #'(lambda(role) |
---|
200 | (player role :revision revision)) |
---|
201 | other-roles)))))) |
---|
202 | |
---|
203 | |
---|
204 | (defgeneric supertypes (construct &key revision valid-supertypes) |
---|
205 | (:documentation "Returns all super type topics of the passed |
---|
206 | construct, also the transitive ones.") |
---|
207 | (:method ((construct TopicC) &key (revision *TM-REVISION*) valid-supertypes) |
---|
208 | (declare (integer revision)) |
---|
209 | (let ((direct-super-types (direct-supertypes construct :revision revision))) |
---|
210 | (let ((current-valid-super-types |
---|
211 | (append valid-supertypes direct-super-types))) |
---|
212 | (let ((recursive-super-types |
---|
213 | (loop for direct-super-type in direct-super-types |
---|
214 | append (supertypes |
---|
215 | direct-super-type :revision revision |
---|
216 | :valid-supertypes current-valid-super-types)))) |
---|
217 | (remove-duplicates |
---|
218 | (remove-null (union recursive-super-types |
---|
219 | current-valid-super-types)))))))) |
---|
220 | |
---|
221 | |
---|
222 | (defgeneric direct-instance-of (construct &key revision) |
---|
223 | (:documentation "Returns all direct type topics of the passed instance topic.") |
---|
224 | (:method ((construct TopicC) &key (revision *TM-REVISION*)) |
---|
225 | (declare (integer revision)) |
---|
226 | (let ((assocs (instance-of-associations construct :revision revision))) |
---|
227 | (let ((other-roles |
---|
228 | (remove-null |
---|
229 | (map 'list #'(lambda(assoc) |
---|
230 | (find-if-not #'(lambda(role) |
---|
231 | (eql (player role :revision revision) |
---|
232 | construct)) |
---|
233 | (roles assoc :revision revision))) |
---|
234 | assocs)))) |
---|
235 | (remove-null (map 'list #'(lambda(role) |
---|
236 | (player role :revision revision)) |
---|
237 | other-roles)))))) |
---|
238 | |
---|
239 | |
---|
240 | (defmethod instance-of (construct &key (revision *TM-REVISION*)) |
---|
241 | "Returns all type topics of the passed construct and their super-types." |
---|
242 | (declare (integer revision)) |
---|
243 | (let ((direct-types (direct-instance-of construct :revision revision))) |
---|
244 | (let ((supertypes-of-types |
---|
245 | (loop for type in direct-types |
---|
246 | append (supertypes type :revision revision)))) |
---|
247 | (union direct-types supertypes-of-types)))) |
---|
248 | |
---|
249 | |
---|
250 | (defgeneric invoke-on (construct operation) |
---|
251 | (:documentation "Invokes the passed main operation on the characteristic's |
---|
252 | value. |
---|
253 | If cast-operation is set to a function the characteristic's |
---|
254 | value is first casted by the cast-operation to another type |
---|
255 | and afterwords processed by main-opertion.") |
---|
256 | (:method ((construct CharacteristicC) (operation Function)) |
---|
257 | (funcall operation (charvalue construct)))) |
---|
258 | |
---|
259 | |
---|
260 | (defgeneric names-by-type (construct nametype &key revision) |
---|
261 | (:documentation "Returns all names that are of the corresponding type.") |
---|
262 | (:method ((construct TopicC) nametype &key (revision *TM-REVISION*)) |
---|
263 | (declare (integer revision) |
---|
264 | (type (or Null TopicC) nametype)) |
---|
265 | (remove-if-not #'(lambda(name) |
---|
266 | (eql nametype (instance-of name :revision revision))) |
---|
267 | (names construct :revision revision)))) |
---|
268 | |
---|
269 | |
---|
270 | (defgeneric occurrences-by-type (construct occurrencetype &key revision) |
---|
271 | (:documentation "Returns all names that are of the corresponding type.") |
---|
272 | (:method ((construct TopicC) (occurrencetype TopicC) |
---|
273 | &key (revision *TM-REVISION*)) |
---|
274 | (declare (integer revision)) |
---|
275 | (remove-if-not #'(lambda(occ) |
---|
276 | (eql occurrencetype (instance-of occ :revision revision))) |
---|
277 | (occurrences construct :revision revision)))) |
---|
278 | |
---|
279 | |
---|
280 | (defgeneric characteristics-by-type (construct chartype &key revision) |
---|
281 | (:documentation "Returns all characteristics that are of the |
---|
282 | corresponding type.") |
---|
283 | (:method ((construct TopicC) (chartype TopicC) &key (revision *TM-REVISION*)) |
---|
284 | (declare (integer revision)) |
---|
285 | (union (names-by-type construct chartype :revision revision) |
---|
286 | (occurrences-by-type construct chartype :revision revision)))) |
---|
287 | |
---|
288 | |
---|
289 | (defgeneric occurrences-by-value (construct filter &key revision) |
---|
290 | (:documentation "Returns a list of all occurrences of the passed |
---|
291 | topic, that return a true value when calling filter |
---|
292 | on their charvalue.") |
---|
293 | (:method ((construct TopicC) (filter Function) &key (revision *TM-REVISION*)) |
---|
294 | (let ((results |
---|
295 | (map 'list #'(lambda(occ) |
---|
296 | (when (invoke-on occ filter) |
---|
297 | occ)) |
---|
298 | (occurrences construct :revision revision)))) |
---|
299 | (remove-null results)))) |
---|
300 | |
---|
301 | |
---|
302 | (defgeneric names-by-value (construct filter &key revision) |
---|
303 | (:documentation "Returns a list of all names of the passed |
---|
304 | topic, that return a true value when calling filter |
---|
305 | on their charvalue.") |
---|
306 | (:method ((construct TopicC) (filter Function) &key (revision *TM-REVISION*)) |
---|
307 | (let ((results |
---|
308 | (map 'list #'(lambda(name) |
---|
309 | (when (invoke-on name filter) |
---|
310 | name)) |
---|
311 | (names construct :revision revision)))) |
---|
312 | (remove-null results)))) |
---|
313 | |
---|
314 | |
---|
315 | (defgeneric characteristics-by-value (construct filter &key revision) |
---|
316 | (:documentation "Returns a list of all characteristics of the passed |
---|
317 | topic, that return a true value when calling filter.") |
---|
318 | (:method ((construct TopicC) (filter Function) &key (revision *TM-REVISION*)) |
---|
319 | (declare (integer revision)) |
---|
320 | (union (names-by-value construct filter :revision revision) |
---|
321 | (occurrences-by-value construct filter :revision revision)))) |
---|
322 | |
---|
323 | |
---|
324 | (defgeneric occurrences-by-datatype (construct datatype &key revision) |
---|
325 | (:documentation "Returns all occurrences of the specified datatype.") |
---|
326 | (:method ((construct TopicC) datatype &key (revision *TM-REVISION*)) |
---|
327 | (declare (type (or Null String) datatype) |
---|
328 | (Integer revision)) |
---|
329 | (if datatype |
---|
330 | (remove-null |
---|
331 | (map 'list #'(lambda(occ) |
---|
332 | (when (string= (datatype occ) datatype) |
---|
333 | occ)) |
---|
334 | (occurrences construct :revision revision))) |
---|
335 | (occurrences construct :revision revision)))) |
---|
336 | |
---|
337 | |
---|
338 | (defgeneric isa (construct type &key revision) |
---|
339 | (:documentation "Returns all types if the passed construct |
---|
340 | is of the specified type.") |
---|
341 | (:method ((construct TopicC) (type TopicC) &key (revision *TM-REVISION*)) |
---|
342 | (declare (integer revision)) |
---|
343 | (let ((all-types (instance-of construct :revision revision))) |
---|
344 | (when (find type all-types) |
---|
345 | all-types)))) |
---|
346 | |
---|
347 | |
---|
348 | (defgeneric aka (construct supertype &key revision) |
---|
349 | (:documentation "Returns all types if the passed construct |
---|
350 | is of the specified type.") |
---|
351 | (:method ((construct TopicC) (supertype TopicC) &key (revision *TM-REVISION*)) |
---|
352 | (declare (integer revision)) |
---|
353 | (let ((all-supertypes (supertypes construct :revision revision))) |
---|
354 | (when (find supertype all-supertypes) |
---|
355 | all-supertypes)))) |
---|