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 | (in-package :rdf-importer) |
---|
11 | |
---|
12 | (defun map-to-tm (tm-id start-revision |
---|
13 | &key (document-id *document-id*)) |
---|
14 | (let ((topics-to-map (get-isi-topics tm-id start-revision |
---|
15 | :document-id document-id)) |
---|
16 | (associations-to-map (get-isi-topics |
---|
17 | tm-id start-revision |
---|
18 | :document-id document-id |
---|
19 | :type-psi *tm2rdf-association-type-uri*))) |
---|
20 | (let ((mapped-topics |
---|
21 | (map 'list #'(lambda(top) |
---|
22 | (map-isi-topic top start-revision)) |
---|
23 | topics-to-map)) |
---|
24 | (mapped-associations |
---|
25 | (map 'list #'(lambda(top) |
---|
26 | (map-isi-association top start-revision tm-id |
---|
27 | :document-id document-id)) |
---|
28 | associations-to-map))) |
---|
29 | (let ((constructs |
---|
30 | (append mapped-topics mapped-associations))) |
---|
31 | (clear-store start-revision) |
---|
32 | (map 'list #'d::check-for-duplicate-identifiers constructs) |
---|
33 | constructs)))) |
---|
34 | |
---|
35 | |
---|
36 | (defun clear-store(start-revision) |
---|
37 | "Deletes all topics that are neede for RDF2TM mapping and are not |
---|
38 | referenced in an associaiton, as type or scope." |
---|
39 | (let ((psi-uris |
---|
40 | (list *tm2rdf-topic-type-uri* *tm2rdf-name-type-uri* |
---|
41 | *tm2rdf-variant-type-uri* *tm2rdf-occurrence-type-uri* |
---|
42 | *tm2rdf-association-type-uri* *tm2rdf-role-type-uri* |
---|
43 | *tm2rdf-itemIdentity-property* *tm2rdf-subjectLocator-property* |
---|
44 | *tm2rdf-subjectIdentifier-property* *tm2rdf-role-property* |
---|
45 | *tm2rdf-subjectIdentifier-property* *tm2rdf-player-property* |
---|
46 | *tm2rdf-nametype-property* *tm2rdf-value-property* |
---|
47 | *tm2rdf-occurrence-property* *tm2rdf-roletype-property* |
---|
48 | *tm2rdf-variant-property* *tm2rdf-occurrencetype-property* |
---|
49 | *tm2rdf-name-property* *tm2rdf-associationtype-property* |
---|
50 | *tm2rdf-scope-property* *tm2rdf-reifier-property*))) |
---|
51 | (dolist (uri psi-uris) |
---|
52 | (delete-topic-if-not-referenced uri start-revision)))) |
---|
53 | |
---|
54 | |
---|
55 | (defun delete-topic-if-not-referenced(type-psi start-revision) |
---|
56 | "Deletes a topic when it is not referenced." |
---|
57 | (declare (string type-psi)) |
---|
58 | (declare (integer start-revision)) |
---|
59 | (let ((type-topic (get-item-by-psi type-psi |
---|
60 | :revision start-revision))) |
---|
61 | (when type-topic |
---|
62 | (when (and (not (player-in-roles type-topic :revision start-revision)) |
---|
63 | (not (used-as-type type-topic :revision start-revision)) |
---|
64 | (not (used-as-theme type-topic :revision start-revision))) |
---|
65 | (d::delete-construct type-topic))))) |
---|
66 | |
---|
67 | |
---|
68 | (defun delete-instance-of-association(instance-topic type-topic start-revision) |
---|
69 | "Deletes a type-instance associaiton that corresponds with the passed |
---|
70 | parameters." |
---|
71 | (when (and instance-topic type-topic) |
---|
72 | (let ((instance (get-item-by-psi *instance-psi* :revision start-revision)) |
---|
73 | (type-instance (get-item-by-psi *type-instance-psi* |
---|
74 | :revision start-revision)) |
---|
75 | (type (get-item-by-psi *type-psi* :revision start-revision))) |
---|
76 | (declare (TopicC instance-topic type-topic) |
---|
77 | (integer start-revision)) |
---|
78 | (let ((assocs (remove-if |
---|
79 | #'null |
---|
80 | (map 'list |
---|
81 | #'(lambda(role) |
---|
82 | (when (and |
---|
83 | (eql (instance-of role :revision start-revision) |
---|
84 | instance) |
---|
85 | (eql (instance-of |
---|
86 | (parent role :revision start-revision) |
---|
87 | :revision start-revision) |
---|
88 | type-instance)) |
---|
89 | (parent role :revision start-revision))) |
---|
90 | (player-in-roles instance-topic :revision start-revision))))) |
---|
91 | (map 'list #'(lambda(assoc) |
---|
92 | (when (find-if |
---|
93 | #'(lambda(role) |
---|
94 | (and (eql (instance-of role :revision start-revision) |
---|
95 | type) |
---|
96 | (eql (player role :revision start-revision) |
---|
97 | type-topic))) |
---|
98 | (roles assoc :revision start-revision)) |
---|
99 | (d::delete-construct assoc))) |
---|
100 | assocs) |
---|
101 | nil)))) |
---|
102 | |
---|
103 | |
---|
104 | (defun delete-related-associations (top start-revision) |
---|
105 | "Deletes all associaitons related to the passed topic." |
---|
106 | (dolist (assoc-role (player-in-roles top :revision start-revision)) |
---|
107 | (d::delete-construct (parent assoc-role))) |
---|
108 | top) |
---|
109 | |
---|
110 | |
---|
111 | (defun get-isi-roles(assoc-top start-revision) |
---|
112 | "Returns all topics representing association-roles." |
---|
113 | (declare (TopicC assoc-top)) |
---|
114 | (declare (integer start-revision)) |
---|
115 | (let ((role-assocs |
---|
116 | (get-associations-by-type assoc-top start-revision |
---|
117 | *tm2rdf-role-property* |
---|
118 | *rdf2tm-subject*))) |
---|
119 | (let ((players |
---|
120 | (get-players-by-role-type |
---|
121 | role-assocs start-revision *rdf2tm-object*))) |
---|
122 | (map 'list #'d::delete-construct role-assocs) |
---|
123 | players))) |
---|
124 | |
---|
125 | |
---|
126 | (defun map-isi-role(role-top start-revision) |
---|
127 | "Maps a passed topic with all its isidorus:types to a |
---|
128 | property list representing an association-role." |
---|
129 | (declare (TopicC role-top)) |
---|
130 | (declare (integer start-revision)) |
---|
131 | (let ((err-pref "From map-isi-role(): ") |
---|
132 | (ids (map-isi-identifiers role-top start-revision)) |
---|
133 | (type-assocs |
---|
134 | (get-associations-by-type |
---|
135 | role-top start-revision *tm2rdf-roletype-property* |
---|
136 | *rdf2tm-subject*)) |
---|
137 | (player-assocs |
---|
138 | (get-associations-by-type |
---|
139 | role-top start-revision *tm2rdf-player-property* |
---|
140 | *rdf2tm-subject*))) |
---|
141 | (let ((types (get-players-by-role-type |
---|
142 | type-assocs start-revision *rdf2tm-object*)) |
---|
143 | (role-players (get-players-by-role-type |
---|
144 | player-assocs start-revision *rdf2tm-object*)) |
---|
145 | (reifiers (get-isi-reifiers role-top start-revision))) |
---|
146 | (elephant:ensure-transaction (:txn-nosync t) |
---|
147 | (map 'list #'d::delete-construct type-assocs) |
---|
148 | (map 'list #'d::delete-construct player-assocs) |
---|
149 | (when (/= 1 (length types)) |
---|
150 | (error "~aexpect one type topic but found: ~a" |
---|
151 | err-pref (length types))) |
---|
152 | (when (= 0 (length role-players)) |
---|
153 | (error "~aexpect one player but found: ~a" |
---|
154 | err-pref (length role-players))) |
---|
155 | (delete-related-associations role-top start-revision) |
---|
156 | (d::delete-construct role-top) |
---|
157 | (list :instance-of (first types) |
---|
158 | :player (first role-players) |
---|
159 | :item-identifiers ids |
---|
160 | :start-revision start-revision |
---|
161 | :reifiers reifiers))))) |
---|
162 | |
---|
163 | |
---|
164 | (defun map-isi-association(assoc-top start-revision tm-id |
---|
165 | &key (document-id *document-id*)) |
---|
166 | "Maps a passed topic with all its isidorus:types to a TM association." |
---|
167 | (declare (TopicC assoc-top)) |
---|
168 | (declare (integer start-revision)) |
---|
169 | (format t "A") |
---|
170 | (let ((err-pref "From map-isi-association(): ") |
---|
171 | (ids (map-isi-identifiers assoc-top start-revision)) |
---|
172 | (type-assocs |
---|
173 | (get-associations-by-type |
---|
174 | assoc-top start-revision *tm2rdf-associationtype-property* |
---|
175 | *rdf2tm-subject*)) |
---|
176 | (scope-assocs |
---|
177 | (get-associations-by-type |
---|
178 | assoc-top start-revision *tm2rdf-scope-property* |
---|
179 | *rdf2tm-subject*)) |
---|
180 | (role-topics (get-isi-roles assoc-top start-revision))) |
---|
181 | (let ((types (get-players-by-role-type |
---|
182 | type-assocs start-revision *rdf2tm-object*)) |
---|
183 | (scopes (get-players-by-role-type |
---|
184 | scope-assocs start-revision *rdf2tm-object*)) |
---|
185 | (reifier-topics (get-isi-reifiers assoc-top start-revision)) |
---|
186 | (assoc-roles |
---|
187 | (remove-if #'null (map 'list |
---|
188 | #'(lambda(role-topic) |
---|
189 | (map-isi-role role-topic start-revision)) |
---|
190 | role-topics)))) |
---|
191 | (elephant:ensure-transaction (:txn-nosync t) |
---|
192 | (map 'list #'d::delete-construct type-assocs) |
---|
193 | (map 'list #'d::delete-construct scope-assocs) |
---|
194 | (when (/= 1 (length types)) |
---|
195 | (error "~aexpect one type topic but found: ~a" |
---|
196 | err-pref (length types))) |
---|
197 | (when (= 0 (length assoc-roles)) |
---|
198 | (error "~aexpect at least one role but found: ~a" |
---|
199 | err-pref (length assoc-roles))) |
---|
200 | (delete-related-associations assoc-top start-revision) |
---|
201 | (d::delete-construct assoc-top) |
---|
202 | (with-tm (start-revision document-id tm-id) |
---|
203 | (add-to-tm |
---|
204 | xtm-importer::tm |
---|
205 | (let ((association |
---|
206 | (make-construct 'AssociationC |
---|
207 | :start-revision start-revision |
---|
208 | :item-identifiers ids |
---|
209 | :instance-of (first types) |
---|
210 | :themes scopes |
---|
211 | :roles assoc-roles))) |
---|
212 | (map 'list #'(lambda(association-role) |
---|
213 | (let ((found-item |
---|
214 | (find-if #'(lambda(list-item) |
---|
215 | (and (eql (instance-of association-role) |
---|
216 | (getf list-item :instance-of)) |
---|
217 | (eql (player association-role) |
---|
218 | (getf list-item :player)) |
---|
219 | (getf list-item :reifiers))) |
---|
220 | assoc-roles))) |
---|
221 | (when found-item |
---|
222 | (dolist (reifier-topic (getf found-item :reifiers)) |
---|
223 | (add-reifier association-role reifier-topic |
---|
224 | :revision start-revision))))) |
---|
225 | (roles association :revision start-revision)) |
---|
226 | (dolist (reifier-topic reifier-topics) |
---|
227 | (add-reifier association reifier-topic :revision start-revision)) |
---|
228 | association))))))) |
---|
229 | |
---|
230 | |
---|
231 | (defun map-isi-topic(top start-revision) |
---|
232 | "Maps a passed topic with all its isidorus:types to a TM topic." |
---|
233 | (declare (integer start-revision)) |
---|
234 | (declare(TopicC top)) |
---|
235 | (format t "T") |
---|
236 | (let ((new-psis (map-isi-identifiers |
---|
237 | top start-revision |
---|
238 | :id-type-uri *tm2rdf-subjectidentifier-property*)) |
---|
239 | (new-locators (map-isi-identifiers |
---|
240 | top start-revision |
---|
241 | :id-type-uri *tm2rdf-subjectlocator-property*)) |
---|
242 | (new-item-ids (map-isi-identifiers top start-revision)) |
---|
243 | (occurrence-topics (get-isi-occurrences top start-revision)) |
---|
244 | (name-topics (get-isi-names top start-revision))) |
---|
245 | (bound-subject-identifiers top new-psis start-revision) |
---|
246 | (bound-subject-locators top new-locators start-revision) |
---|
247 | (bound-item-identifiers top new-item-ids start-revision) |
---|
248 | (map 'list #'(lambda(occurrence-topic) |
---|
249 | (map-isi-occurrence top occurrence-topic start-revision)) |
---|
250 | occurrence-topics) |
---|
251 | (map 'list #'(lambda(name-topic) |
---|
252 | (map-isi-name top name-topic start-revision)) |
---|
253 | name-topics)) |
---|
254 | top) |
---|
255 | |
---|
256 | |
---|
257 | (defun get-isi-variants(name-top start-revision) |
---|
258 | "Returns all topics representing a name's variant." |
---|
259 | (declare (TopicC name-top)) |
---|
260 | (declare (integer start-revision)) |
---|
261 | (let ((variant-assocs |
---|
262 | (get-associations-by-type name-top start-revision |
---|
263 | *tm2rdf-variant-property* |
---|
264 | *rdf2tm-subject*))) |
---|
265 | (let ((players |
---|
266 | (get-players-by-role-type |
---|
267 | variant-assocs start-revision *rdf2tm-object*))) |
---|
268 | (map 'list #'d::delete-construct variant-assocs) |
---|
269 | players))) |
---|
270 | |
---|
271 | |
---|
272 | (defun map-isi-variant (name variant-top start-revision) |
---|
273 | "Maps the passed variant-topic to a TM variant." |
---|
274 | (declare (TopicC variant-top)) |
---|
275 | (declare (NameC name)) |
---|
276 | (declare (integer start-revision)) |
---|
277 | (let ((ids (map-isi-identifiers variant-top start-revision)) |
---|
278 | (scope-assocs |
---|
279 | (get-associations-by-type |
---|
280 | variant-top start-revision *tm2rdf-scope-property* |
---|
281 | *rdf2tm-subject*)) |
---|
282 | (value-type-topic |
---|
283 | (get-item-by-psi *tm2rdf-value-property* :revision start-revision))) |
---|
284 | (let ((scopes |
---|
285 | (remove-duplicates |
---|
286 | (append (get-players-by-role-type |
---|
287 | scope-assocs start-revision *rdf2tm-object*) |
---|
288 | (when name |
---|
289 | (themes name))))) |
---|
290 | (value-and-datatype |
---|
291 | (let ((value-occ |
---|
292 | (find-if #'(lambda(occ) |
---|
293 | (eql (instance-of occ) value-type-topic)) |
---|
294 | (occurrences variant-top)))) |
---|
295 | (if value-occ |
---|
296 | (list :value (charvalue value-occ) |
---|
297 | :datatype (datatype value-occ)) |
---|
298 | (list :value "" |
---|
299 | :datatype *xml-string*)))) |
---|
300 | (reifiers (get-isi-reifiers variant-top start-revision))) |
---|
301 | (elephant:ensure-transaction (:txn-nosync t) |
---|
302 | (map 'list #'d::delete-construct scope-assocs) |
---|
303 | (delete-related-associations variant-top start-revision) |
---|
304 | (d::delete-construct variant-top) |
---|
305 | (let ((variant |
---|
306 | (make-construct 'VariantC |
---|
307 | :start-revision start-revision |
---|
308 | :item-identifiers ids |
---|
309 | :themes scopes |
---|
310 | :charvalue (getf value-and-datatype :value) |
---|
311 | :datatype (getf value-and-datatype :datatype) |
---|
312 | :parent name))) |
---|
313 | (dolist (reifier-topic reifiers) |
---|
314 | (add-reifier variant reifier-topic :revision start-revision)) |
---|
315 | variant))))) |
---|
316 | |
---|
317 | |
---|
318 | (defun map-isi-name (top name-top start-revision) |
---|
319 | "Maps the passed occurrence-topic to a TM occurrence." |
---|
320 | (declare (TopicC top name-top)) |
---|
321 | (declare (integer start-revision)) |
---|
322 | (let ((ids (map-isi-identifiers name-top start-revision)) |
---|
323 | (type-assocs |
---|
324 | (get-associations-by-type |
---|
325 | name-top start-revision *tm2rdf-nametype-property* |
---|
326 | *rdf2tm-subject*)) |
---|
327 | (scope-assocs |
---|
328 | (get-associations-by-type |
---|
329 | name-top start-revision *tm2rdf-scope-property* |
---|
330 | *rdf2tm-subject*)) |
---|
331 | (value-type-topic |
---|
332 | (get-item-by-psi *tm2rdf-value-property* :revision start-revision)) |
---|
333 | (variant-topics (get-isi-variants name-top start-revision))) |
---|
334 | (let ((type (let ((fn-types |
---|
335 | (get-players-by-role-type |
---|
336 | type-assocs start-revision *rdf2tm-object*))) |
---|
337 | (when fn-types |
---|
338 | (first fn-types)))) |
---|
339 | (scopes (get-players-by-role-type |
---|
340 | scope-assocs start-revision *rdf2tm-object*)) |
---|
341 | (value |
---|
342 | (let ((value-occ |
---|
343 | (find-if #'(lambda(occ) |
---|
344 | (eql (instance-of occ) value-type-topic)) |
---|
345 | (occurrences name-top)))) |
---|
346 | (if value-occ |
---|
347 | (charvalue value-occ) |
---|
348 | ""))) |
---|
349 | (reifiers (get-isi-reifiers name-top start-revision))) |
---|
350 | (elephant:ensure-transaction (:txn-nosync t) |
---|
351 | (map 'list #'d::delete-construct type-assocs) |
---|
352 | (map 'list #'d::delete-construct scope-assocs) |
---|
353 | (let ((name |
---|
354 | (make-construct 'NameC |
---|
355 | :start-revision start-revision |
---|
356 | :parent top |
---|
357 | :charvalue value |
---|
358 | :instance-of (if type |
---|
359 | type |
---|
360 | (get-item-by-psi |
---|
361 | *topic-name-psi* |
---|
362 | :revision start-revision |
---|
363 | :error-if-nil t)) |
---|
364 | :item-identifiers ids |
---|
365 | :themes scopes))) |
---|
366 | (map 'list #'(lambda(variant-topic) |
---|
367 | (map-isi-variant name variant-topic |
---|
368 | start-revision)) |
---|
369 | variant-topics) |
---|
370 | (delete-related-associations name-top start-revision) |
---|
371 | (d::delete-construct name-top) |
---|
372 | (dolist (reifier-topic reifiers) |
---|
373 | (add-reifier name reifier-topic :revision start-revision)) |
---|
374 | name))))) |
---|
375 | |
---|
376 | |
---|
377 | (defun get-isi-names(top start-revision) |
---|
378 | "Returns all topics that represents names for the passed top." |
---|
379 | (declare (TopicC top)) |
---|
380 | (declare (integer start-revision)) |
---|
381 | (let ((assocs (get-associations-by-type |
---|
382 | top start-revision *tm2rdf-name-property* |
---|
383 | *rdf2tm-subject*))) |
---|
384 | (let ((name-topics |
---|
385 | (get-players-by-role-type |
---|
386 | assocs start-revision *rdf2tm-object*))) |
---|
387 | (map 'list #'d::delete-construct assocs) |
---|
388 | name-topics))) |
---|
389 | |
---|
390 | |
---|
391 | (defun map-isi-occurrence(top occ-top start-revision) |
---|
392 | "Maps all topics that represents occurrences of the passed topic top |
---|
393 | to occurrence objects." |
---|
394 | (declare (TopicC top occ-top)) |
---|
395 | (declare (integer start-revision)) |
---|
396 | (let ((err-pref "From map-isi-occurrence(): ") |
---|
397 | (ids (map-isi-identifiers occ-top start-revision)) |
---|
398 | (type-assocs |
---|
399 | (get-associations-by-type |
---|
400 | occ-top start-revision *tm2rdf-occurrencetype-property* |
---|
401 | *rdf2tm-subject*)) |
---|
402 | (scope-assocs |
---|
403 | (get-associations-by-type |
---|
404 | occ-top start-revision *tm2rdf-scope-property* |
---|
405 | *rdf2tm-subject*)) |
---|
406 | (value-type-topic |
---|
407 | (get-item-by-psi *tm2rdf-value-property*))) |
---|
408 | (let ((types (get-players-by-role-type |
---|
409 | type-assocs start-revision *rdf2tm-object*)) |
---|
410 | (scopes (get-players-by-role-type |
---|
411 | scope-assocs start-revision *rdf2tm-object*)) |
---|
412 | (value-and-datatype |
---|
413 | (let ((value-occ |
---|
414 | (find-if #'(lambda(occ) |
---|
415 | (eql (instance-of occ) value-type-topic)) |
---|
416 | (occurrences occ-top)))) |
---|
417 | (if value-occ |
---|
418 | (list :value (charvalue value-occ) |
---|
419 | :datatype (datatype value-occ)) |
---|
420 | (list :value "" |
---|
421 | :datatype *xml-string*)))) |
---|
422 | (reifiers (get-isi-reifiers occ-top start-revision))) |
---|
423 | (elephant:ensure-transaction (:txn-nosync t) |
---|
424 | (map 'list #'d::delete-construct type-assocs) |
---|
425 | (map 'list #'d::delete-construct scope-assocs) |
---|
426 | (when (/= 1 (length types)) |
---|
427 | (error "~aexpect one type topic but found: ~a" |
---|
428 | err-pref (length types))) |
---|
429 | (delete-related-associations occ-top start-revision) |
---|
430 | (d::delete-construct occ-top) |
---|
431 | (let ((occurrence |
---|
432 | (make-construct 'OccurrenceC |
---|
433 | :start-revision start-revision |
---|
434 | :parent top |
---|
435 | :themes scopes |
---|
436 | :item-identifiers ids |
---|
437 | :instance-of (first types) |
---|
438 | :charvalue (getf value-and-datatype :value) |
---|
439 | :datatype (getf value-and-datatype :datatype)))) |
---|
440 | (dolist (reifier-topic reifiers) |
---|
441 | (add-reifier occurrence reifier-topic :revision start-revision)) |
---|
442 | occurrence))))) |
---|
443 | |
---|
444 | |
---|
445 | (defun get-isi-occurrences(top start-revision) |
---|
446 | "Returns all topics that represents occurrences for the passed top." |
---|
447 | (declare (TopicC top)) |
---|
448 | (declare (integer start-revision)) |
---|
449 | (let ((assocs (get-associations-by-type |
---|
450 | top start-revision *tm2rdf-occurrence-property* |
---|
451 | *rdf2tm-subject*))) |
---|
452 | (let ((occurrence-topics |
---|
453 | (get-players-by-role-type |
---|
454 | assocs start-revision *rdf2tm-object*))) |
---|
455 | (map 'list #'d::delete-construct assocs) |
---|
456 | occurrence-topics))) |
---|
457 | |
---|
458 | |
---|
459 | (defun get-isi-topics (tm-id start-revision |
---|
460 | &key (document-id *document-id*) |
---|
461 | (type-psi *tm2rdf-topic-type-uri*)) |
---|
462 | "Returns all topics of the given tm and revision." |
---|
463 | (let ((type-topic (get-item-by-psi type-psi |
---|
464 | :revision start-revision))) |
---|
465 | (when type-topic |
---|
466 | (let ((assocs (get-associations-by-type |
---|
467 | type-topic start-revision *type-instance-psi* |
---|
468 | *type-psi*))) |
---|
469 | (let ((isi-topics (get-players-by-role-type |
---|
470 | assocs start-revision *instance-psi*))) |
---|
471 | (let ((topics-in-tm |
---|
472 | (with-tm (start-revision document-id tm-id) |
---|
473 | (intersection isi-topics (topics xtm-importer::tm))))) |
---|
474 | (map 'list |
---|
475 | #'(lambda(top) |
---|
476 | (map 'list |
---|
477 | #'(lambda(role) |
---|
478 | (when (find (parent role :revision start-revision) |
---|
479 | assocs) |
---|
480 | (d::delete-construct |
---|
481 | (parent role :revision start-revision)))) |
---|
482 | (player-in-roles top :revision start-revision))) |
---|
483 | topics-in-tm) |
---|
484 | topics-in-tm)))))) |
---|
485 | |
---|
486 | |
---|
487 | (defun get-associations-by-type (top start-revision association-type-psi |
---|
488 | role-type-psi) |
---|
489 | "Returns all associations of the passed associaiton type where the |
---|
490 | topic top is a player in a role of the given roletype." |
---|
491 | (declare (TopicC top)) |
---|
492 | (declare (string association-type-psi role-type-psi)) |
---|
493 | (declare (integer start-revision)) |
---|
494 | (let ((assoc-type (get-item-by-psi association-type-psi |
---|
495 | :revision start-revision)) |
---|
496 | (role-type (get-item-by-psi role-type-psi |
---|
497 | :revision start-revision))) |
---|
498 | (when (and assoc-type role-type) |
---|
499 | (let ((assocs |
---|
500 | (remove-if |
---|
501 | #'null |
---|
502 | (map 'list |
---|
503 | #'(lambda(role) |
---|
504 | (when (and (eql (instance-of (parent role)) assoc-type) |
---|
505 | (eql (instance-of role) role-type)) |
---|
506 | (parent role))) |
---|
507 | (player-in-roles top))))) |
---|
508 | assocs)))) |
---|
509 | |
---|
510 | |
---|
511 | (defun get-players-by-role-type (associations start-revision |
---|
512 | role-type-psi) |
---|
513 | "Returns all players of the passed associaiton that are contained |
---|
514 | in roles of the given type." |
---|
515 | (declare (list associations)) |
---|
516 | (declare (integer start-revision)) |
---|
517 | (declare (string role-type-psi)) |
---|
518 | (let ((role-type (get-item-by-psi role-type-psi |
---|
519 | :revision start-revision))) |
---|
520 | (let ((players |
---|
521 | (remove-if |
---|
522 | #'null |
---|
523 | (map 'list |
---|
524 | #'(lambda(assoc) |
---|
525 | (let ((role |
---|
526 | (find-if |
---|
527 | #'(lambda(role) |
---|
528 | (eql role-type (instance-of role |
---|
529 | :revision start-revision))) |
---|
530 | (roles assoc :revision start-revision)))) |
---|
531 | (when role |
---|
532 | (player role :revision start-revision)))) |
---|
533 | associations)))) |
---|
534 | players))) |
---|
535 | |
---|
536 | |
---|
537 | (defun get-occurrences-by-type (top start-revision |
---|
538 | &key (occurrence-type-uri |
---|
539 | *tm2rdf-itemIdentity-property*)) |
---|
540 | "Returns all occurrences of the given topic, that is of the type |
---|
541 | bound to occurrence-type-uri." |
---|
542 | (declare (TopicC top)) |
---|
543 | (with-revision start-revision |
---|
544 | (let ((identifier-occs |
---|
545 | (remove-if #'null |
---|
546 | (map 'list |
---|
547 | #'(lambda(occurrence) |
---|
548 | (let ((type |
---|
549 | (instance-of occurrence |
---|
550 | :revision start-revision))) |
---|
551 | (let ((type-psi |
---|
552 | (find-if #'(lambda(psi) |
---|
553 | (string= |
---|
554 | occurrence-type-uri |
---|
555 | (uri psi))) |
---|
556 | (psis type :revision start-revision)))) |
---|
557 | (when type-psi |
---|
558 | occurrence)))) |
---|
559 | (occurrences top :revision start-revision))))) |
---|
560 | identifier-occs))) |
---|
561 | |
---|
562 | |
---|
563 | (defun map-isi-identifiers (top start-revision |
---|
564 | &key (id-type-uri |
---|
565 | *tm2rdf-itemIdentity-property*)) |
---|
566 | "Maps identifiers of the type depending on id-type-uri from topic occurrences |
---|
567 | imported from RDF to the corresponding TM constructs." |
---|
568 | (declare (TopicC top)) |
---|
569 | (let ((id-occs (get-occurrences-by-type top start-revision |
---|
570 | :occurrence-type-uri id-type-uri)) |
---|
571 | (class-symbol (cond |
---|
572 | ((string= id-type-uri |
---|
573 | *tm2rdf-itemIdentity-property*) |
---|
574 | 'ItemIdentifierC) |
---|
575 | ((string= id-type-uri |
---|
576 | *tm2rdf-subjectLocator-property*) |
---|
577 | 'SubjectLocatorC) |
---|
578 | ((string= id-type-uri |
---|
579 | *tm2rdf-subjectIdentifier-property*) |
---|
580 | 'PersistentIdC)))) |
---|
581 | (let ((id-uris (map 'list #'charvalue id-occs))) |
---|
582 | (elephant:ensure-transaction (:txn-nosync t) |
---|
583 | (map 'list #'d::delete-construct id-occs) |
---|
584 | (let ((ids (map 'list |
---|
585 | #'(lambda(id-uri) |
---|
586 | (make-instance class-symbol |
---|
587 | :uri id-uri |
---|
588 | :start-revision start-revision)) |
---|
589 | id-uris))) |
---|
590 | ids))))) |
---|
591 | |
---|
592 | |
---|
593 | (defun bound-item-identifiers (construct identifiers start-revision) |
---|
594 | "Bounds the passed item-identifier to the passed construct." |
---|
595 | (declare (ReifiableConstructC construct)) |
---|
596 | (dolist (id identifiers) |
---|
597 | (declare (ItemIdentifierC id)) |
---|
598 | (if (find-if #'(lambda(ii) |
---|
599 | (and (string= (uri ii) (uri id)) |
---|
600 | (not (eql ii id)))) |
---|
601 | (item-identifiers construct :revision start-revision)) |
---|
602 | (d::delete-construct id) |
---|
603 | (add-item-identifier construct id :revision start-revision))) |
---|
604 | construct) |
---|
605 | |
---|
606 | |
---|
607 | (defun bound-subject-identifiers (top identifiers start-revision) |
---|
608 | "Bounds the passed psis to the passed topic." |
---|
609 | (declare (TopicC top)) |
---|
610 | (dolist (id identifiers) |
---|
611 | (declare (PersistentIdC id)) |
---|
612 | (if (find-if #'(lambda(psi) |
---|
613 | (and (string= (uri psi) (uri id)) |
---|
614 | (not (eql psi id)))) |
---|
615 | (psis top :revision start-revision)) |
---|
616 | (d::delete-construct id) |
---|
617 | (add-psi top id :revision start-revision))) |
---|
618 | top) |
---|
619 | |
---|
620 | |
---|
621 | (defun bound-subject-locators (top locators start-revision) |
---|
622 | "Bounds the passed locators to the passed topic." |
---|
623 | (declare (TopicC top)) |
---|
624 | (dolist (id locators) |
---|
625 | (declare (SubjectLocatorC id)) |
---|
626 | (if (find-if #'(lambda(locator) |
---|
627 | (and (string= (uri locator) (uri id)) |
---|
628 | (not (eql locator id)))) |
---|
629 | (locators top :revision start-revision)) |
---|
630 | (d::delete-construct id) |
---|
631 | (add-locator top id :revision start-revision))) |
---|
632 | top) |
---|
633 | |
---|
634 | |
---|
635 | (defun get-isi-reifiers (construct start-revision) |
---|
636 | "Returns all reifiers from the passed construct." |
---|
637 | (declare (TopicC construct)) |
---|
638 | (let ((reifier-assocs |
---|
639 | (get-associations-by-type |
---|
640 | construct start-revision *tm2rdf-reifier-property* |
---|
641 | *rdf2tm-subject*))) |
---|
642 | (let ((reifiers |
---|
643 | (get-players-by-role-type |
---|
644 | reifier-assocs start-revision *rdf2tm-object*))) |
---|
645 | reifiers))) |
---|