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 :TM-SPARQL) |
---|
12 | |
---|
13 | |
---|
14 | (defmacro with-triple-nodes (triple-construct &body body) |
---|
15 | "Generates the variables subj, pred, obj that references the triple's |
---|
16 | nodes. Additionaly the variables subj-uri, pred-uri and obj-uri are |
---|
17 | generated when the corresponding node is a resource-nodes." |
---|
18 | `(let* ((subj (subject ,triple-construct)) |
---|
19 | (pred (predicate ,triple-construct)) |
---|
20 | (obj (object ,triple-construct)) |
---|
21 | (subj-uri (unless (variable-p subj) |
---|
22 | (sparql-node (value subj) :revision revision))) |
---|
23 | (pred-uri (unless (variable-p pred) |
---|
24 | (sparql-node (value pred) :revision revision))) |
---|
25 | (obj-uri (when (and (not (variable-p obj)) |
---|
26 | (not (literal-p obj))) |
---|
27 | (sparql-node (value obj) :revision revision))) |
---|
28 | (literal-datatype (when (literal-p obj) |
---|
29 | (literal-datatype obj)))) |
---|
30 | (declare (Ignorable subj-uri pred-uri obj-uri literal-datatype)) |
---|
31 | ,@body)) |
---|
32 | |
---|
33 | |
---|
34 | (defgeneric filter-by-special-uris (construct &key revision) |
---|
35 | (:documentation "Returns lists representing triples that handles special |
---|
36 | predicate uris defined in tmsparql.") |
---|
37 | (:method ((construct SPARQL-Triple) &key (revision d:*TM-REVISION*)) |
---|
38 | (let ((pred (predicate construct)) |
---|
39 | (pred-val (value (predicate construct)))) |
---|
40 | (if (variable-p pred) |
---|
41 | (filter-for-special-uris construct :revision revision) |
---|
42 | (cond ((has-identifier pred-val *tms-reifier*) |
---|
43 | (filter-for-reifier construct :revision revision)) |
---|
44 | ((has-identifier pred-val *tms-scope*) |
---|
45 | (filter-for-scopes construct :revision revision)) |
---|
46 | ((has-identifier pred-val *tms-value*) |
---|
47 | (filter-for-values construct :revision revision)) |
---|
48 | ((has-identifier pred-val *tms-topicProperty*) |
---|
49 | (filter-for-topicProperties construct :revision revision)) |
---|
50 | ((has-identifier pred-val *tms-role*) |
---|
51 | (filter-for-roles construct :revision revision)) |
---|
52 | ((has-identifier pred-val *tms-player*) |
---|
53 | (filter-for-player construct :revision revision))))))) |
---|
54 | |
---|
55 | |
---|
56 | (defgeneric filter-for-special-uris (construct &key revision) |
---|
57 | (:documentation "Returns a list of triples representing the subject |
---|
58 | and its objects corresponding to the defined |
---|
59 | special-uris, e.g. <subj> var <obj>.") |
---|
60 | (:method ((construct SPARQL-Triple) &key (revision *TM-REVISION*)) |
---|
61 | (let* ((pred (predicate construct)) |
---|
62 | (old-pred-value (value pred)) |
---|
63 | (res-1 |
---|
64 | (progn |
---|
65 | (setf (value pred) (get-item-by-psi *tms-reifier* :revision revision)) |
---|
66 | (let ((val (filter-for-reifier construct :revision revision))) |
---|
67 | (setf (value pred) old-pred-value) |
---|
68 | val))) |
---|
69 | (res-2 |
---|
70 | (progn |
---|
71 | (setf (value pred) (get-item-by-psi *tms-scope* :revision revision)) |
---|
72 | (let ((val (filter-for-scopes construct :revision revision))) |
---|
73 | (setf (value pred) old-pred-value) |
---|
74 | val))) |
---|
75 | (res-3 |
---|
76 | (progn |
---|
77 | (setf (value pred) (get-item-by-psi *tms-value* :revision revision)) |
---|
78 | (let ((val (filter-for-values construct :revision revision))) |
---|
79 | (setf (value pred) old-pred-value) |
---|
80 | val))) |
---|
81 | (res-4 |
---|
82 | (progn |
---|
83 | (setf (value pred) (get-item-by-psi *tms-role* :revision revision)) |
---|
84 | (let ((val (filter-for-roles construct :revision revision))) |
---|
85 | (setf (value pred) old-pred-value) |
---|
86 | val))) |
---|
87 | (res-5 |
---|
88 | (progn |
---|
89 | (setf (value pred) (get-item-by-psi *tms-player* :revision revision)) |
---|
90 | (let ((val (filter-for-player construct :revision revision))) |
---|
91 | (setf (value pred) old-pred-value) |
---|
92 | val)))) |
---|
93 | (append res-1 res-2 res-3 res-4 res-5)))) |
---|
94 | |
---|
95 | |
---|
96 | (defgeneric filter-for-player (construct &key revision) |
---|
97 | (:documentation "Returns a list with triples where the subject |
---|
98 | represents a role and the object represents a player.") |
---|
99 | (:method ((construct SPARQL-Triple) &key (revision *TM-REVISION*)) |
---|
100 | (unless (literal-p (object construct)) |
---|
101 | (with-triple-nodes construct |
---|
102 | (when (and (or (typep (value subj) 'RoleC) |
---|
103 | (variable-p subj)) |
---|
104 | (or (typep (value obj) 'TopicC) |
---|
105 | (variable-p obj))) |
---|
106 | (cond ((and (not (variable-p subj)) |
---|
107 | (not (variable-p obj))) |
---|
108 | (when (eql (player (value subj) :revision revision) |
---|
109 | (value obj)) |
---|
110 | (list (list :subject subj-uri |
---|
111 | :predicate pred-uri |
---|
112 | :object obj-uri)))) |
---|
113 | ((not (variable-p subj)) |
---|
114 | (let ((player-top |
---|
115 | (player (value subj) :revision revision))) |
---|
116 | (when player-top |
---|
117 | (list :subject subj-uri |
---|
118 | :predicate pred-uri |
---|
119 | :object (sparql-node player-top :revision revision))))) |
---|
120 | ((not (variable-p obj)) |
---|
121 | (let ((parent-roles |
---|
122 | (player-in-roles (value obj) :revision revision))) |
---|
123 | (loop for role in parent-roles |
---|
124 | collect (list :subject (sparql-node role :revision revision) |
---|
125 | :predicate pred-uri |
---|
126 | :object (sparql-node (player role :revision revision) |
---|
127 | :revision revision))))) |
---|
128 | (t ; only pred is given |
---|
129 | (let ((all-roles |
---|
130 | (remove-null |
---|
131 | (map 'list #'(lambda(role) |
---|
132 | (when (player role :revision revision) |
---|
133 | role)) |
---|
134 | (get-all-roles revision))))) |
---|
135 | (loop for role in all-roles |
---|
136 | collect (list :subject (sparql-node role :revision revision) |
---|
137 | :predicate pred-uri |
---|
138 | :object (sparql-node (player role :revision revision) |
---|
139 | :revision revision))))))))))) |
---|
140 | |
---|
141 | |
---|
142 | (defgeneric filter-for-roles (construct &key revision) |
---|
143 | (:documentation "Returns a list of triples where the subject represents |
---|
144 | an Association and the object represents a role.") |
---|
145 | (:method((construct SPARQL-Triple) &key (revision *TM-REVISION*)) |
---|
146 | (unless (literal-p (object construct)) |
---|
147 | (with-triple-nodes construct |
---|
148 | (when (and (or (variable-p subj) |
---|
149 | (typep (value subj) 'd:AssociationC)) |
---|
150 | (or (variable-p obj) |
---|
151 | (typep (value subj) 'd:RoleC))) |
---|
152 | (cond ((and (not (variable-p subj)) |
---|
153 | (not (variable-p obj))) |
---|
154 | (when (find obj (roles (value subj) :revision revision)) |
---|
155 | (list (list :subject subj-uri |
---|
156 | :predicate pred-uri |
---|
157 | :object obj-uri)))) |
---|
158 | ((not (variable-p subj)) |
---|
159 | (loop for role in (roles (value subj) :revision revision) |
---|
160 | collect (list :subject subj-uri |
---|
161 | :predicate pred-uri |
---|
162 | :object (sparql-node role :revision revision)))) |
---|
163 | ((not (variable-p obj)) |
---|
164 | (let ((parent-assoc (parent (value obj) :revision revision))) |
---|
165 | (when revision |
---|
166 | (list :subject (sparql-node parent-assoc :revision revision) |
---|
167 | :predicate pred-uri |
---|
168 | :object obj-uri)))) |
---|
169 | (t ; only pred is given |
---|
170 | (let ((assocs |
---|
171 | (remove-null |
---|
172 | (map 'list #'(lambda(assoc) |
---|
173 | (when (roles assoc :revision revision) |
---|
174 | assoc)) |
---|
175 | (get-all-associations revision))))) |
---|
176 | (loop for assoc in assocs |
---|
177 | append (loop for role in (roles assoc :revision revision) |
---|
178 | collect (list :subject (sparql-node |
---|
179 | assoc :revision revision) |
---|
180 | :predicate pred-uri |
---|
181 | :object (sparql-node |
---|
182 | role :revision revision)))))))))))) |
---|
183 | |
---|
184 | |
---|
185 | (defgeneric filter-for-topicProperties (construct &key revision) |
---|
186 | (:documentation "Returns a list of triples where the subject represents |
---|
187 | a topic and the object represents a name or occurrence.") |
---|
188 | (:method ((construct SPARQL-Triple) &key (revision *TM-REVISION*)) |
---|
189 | (unless (literal-p (object construct)) |
---|
190 | (with-triple-nodes construct |
---|
191 | (when (and (or (variable-p subj) |
---|
192 | (typep (value subj) 'd:TopicC)) |
---|
193 | (or (variable-p obj) |
---|
194 | (typep (value obj) 'd:OccurrenceC) |
---|
195 | (typep (value obj) 'd:NameC))) |
---|
196 | (cond ((and (not (variable-p subj)) |
---|
197 | (not (variable-p obj))) |
---|
198 | (when (find obj (append (names (value subj) :revision revision) |
---|
199 | (occurrences (value subj) :revision revision))) |
---|
200 | (list (list :subject subj-uri |
---|
201 | :predicate pred-uri |
---|
202 | :object obj-uri)))) |
---|
203 | ((not (variable-p subj)) |
---|
204 | (loop for property in (append |
---|
205 | (names (value subj) :revision revision) |
---|
206 | (occurrences (value subj) :revision revision)) |
---|
207 | collect (list :subject subj-uri |
---|
208 | :predicate pred-uri |
---|
209 | :object |
---|
210 | (sparql-node property :revision revision)))) |
---|
211 | ((not (variable-p obj)) |
---|
212 | (let ((parent-top (parent (value obj) :revision revision))) |
---|
213 | (when revision |
---|
214 | (list :subject (sparql-node parent-top :revision revision) |
---|
215 | :predicate pred-uri |
---|
216 | :object obj-uri)))) |
---|
217 | (t ; only pred is given |
---|
218 | (let ((topics |
---|
219 | (remove-null |
---|
220 | (map 'list #'(lambda(top) |
---|
221 | (when (append |
---|
222 | (names top :revision revision) |
---|
223 | (occurrences top :revision revision)) |
---|
224 | top)) |
---|
225 | (get-all-topics revision))))) |
---|
226 | (loop for top in topics |
---|
227 | append (loop for prop in (append |
---|
228 | (names top :revision revision) |
---|
229 | (occurrences top :revision revision)) |
---|
230 | collect (list :subject (sparql-node |
---|
231 | top :revision revision) |
---|
232 | :predicate pred-uri |
---|
233 | :object (sparql-node |
---|
234 | prop :revision revision)))))))))))) |
---|
235 | |
---|
236 | |
---|
237 | (defgeneric filter-for-values (construct &key revision) |
---|
238 | (:documentation "Returns a list of triples that represent a |
---|
239 | subject and its literal value as object.") |
---|
240 | (:method ((construct SPARQL-Triple) &key revision) |
---|
241 | (declare (ignorable revision)) |
---|
242 | (with-triple-nodes construct |
---|
243 | (when (and (or (variable-p subj) |
---|
244 | (typep (value subj) 'd:OccurrenceC) |
---|
245 | (typep (value subj) 'd:NameC) |
---|
246 | (typep (value subj) 'd:VariantC)) |
---|
247 | (or (variable-p obj) |
---|
248 | (literal-p obj))) |
---|
249 | (cond ((and (not (variable-p subj)) |
---|
250 | (not (variable-p obj))) |
---|
251 | (when (or (and (typep subj 'NameC) |
---|
252 | (string= literal-datatype *xml-string*) |
---|
253 | (string= (charvalue subj) (value obj))) |
---|
254 | (filter-datatypable-by-value subj obj literal-datatype)) |
---|
255 | (list (list :subject subj-uri |
---|
256 | :predicate pred-uri |
---|
257 | :object (value obj) |
---|
258 | :literal-datatype literal-datatype)))) |
---|
259 | ((not (variable-p subj)) |
---|
260 | (list (list :subject subj-uri |
---|
261 | :predicate pred-uri |
---|
262 | :object (charvalue subj) |
---|
263 | :literal-datatype (if (typep subj 'd:NameC) |
---|
264 | *xml-string* |
---|
265 | (datatype subj))))) |
---|
266 | ((not (variable-p obj)) |
---|
267 | (loop for char in (return-characteristics (value obj) literal-datatype) |
---|
268 | collect (list :subject (sparql-node char :revision revision) |
---|
269 | :predicate pred-uri |
---|
270 | :object (charvalue char) |
---|
271 | :literal-datatype (if (typep char 'd:NameC) |
---|
272 | *xml-string* |
---|
273 | (datatype char))))) |
---|
274 | (t ;only pred is given |
---|
275 | (let ((chars (append (get-all-names revision) |
---|
276 | (get-all-occurrences revision) |
---|
277 | (get-all-variants revision)))) |
---|
278 | (loop for char in chars |
---|
279 | collect (list :subject (sparql-node char :revision revision) |
---|
280 | :predicate pred-uri |
---|
281 | :object (charvalue char) |
---|
282 | :literal-datatype (if (typep char 'd:NameC) |
---|
283 | *xml-string* |
---|
284 | (datatype char))))))))))) |
---|
285 | |
---|
286 | |
---|
287 | (defgeneric filter-for-scopes (construct &key revision) |
---|
288 | (:documentation "Returns a list of triples that represent a subject as the |
---|
289 | scoped item and the object as the scope-topic.") |
---|
290 | (:method ((construct SPARQL-Triple) &key (revision *TM-REVISION*)) |
---|
291 | (unless (literal-p (object construct)) |
---|
292 | (with-triple-nodes construct |
---|
293 | (when (and (or (variable-p subj) |
---|
294 | (typep (value subj) 'd:ScopableC)) |
---|
295 | (or (variable-p obj) |
---|
296 | (typep (value obj) 'd:TopicC))) |
---|
297 | (cond ((and (not (variable-p subj)) |
---|
298 | (not (variable-p obj))) |
---|
299 | (when (find obj (themes (value subj) :revision revision)) |
---|
300 | (list (list :subject subj-uri |
---|
301 | :predicate pred-uri |
---|
302 | :object obj-uri)))) |
---|
303 | ((not (variable-p subj)) |
---|
304 | (loop for scope in (themes (value subj) :revision revision) |
---|
305 | collect (list :subject subj-uri |
---|
306 | :predicate pred-uri |
---|
307 | :object (sparql-node scope :revision revision)))) |
---|
308 | ((not (variable-p obj)) |
---|
309 | (let ((scoped-constructs |
---|
310 | (used-as-theme (value obj) :revision revision))) |
---|
311 | (loop for construct in scoped-constructs |
---|
312 | collect (list :subject (sparql-node construct :revision revision) |
---|
313 | :predicate pred-uri |
---|
314 | :object obj-uri)))) |
---|
315 | (t ;only pred is given |
---|
316 | (let ((scoped-constructs |
---|
317 | (remove-null |
---|
318 | (map 'list #'(lambda(construct) |
---|
319 | (when (themes construct :revision revision) |
---|
320 | construct)) |
---|
321 | (append (get-all-associations revision) |
---|
322 | (get-all-occurrences revision) |
---|
323 | (get-all-names revision) |
---|
324 | (get-all-variants)))))) |
---|
325 | (loop for construct in scoped-constructs |
---|
326 | append (loop for scope in (themes construct :revision revision) |
---|
327 | collect |
---|
328 | (list :subject (sparql-node |
---|
329 | construct :revision revision) |
---|
330 | :predicate pred-uri |
---|
331 | :object (sparql-node |
---|
332 | construct :revision revision)))))))))))) |
---|
333 | |
---|
334 | |
---|
335 | (defgeneric filter-for-reifier (construct &key revision) |
---|
336 | (:documentation "Returns a list with triples representing a reifier |
---|
337 | and the corresponding reified construct.") |
---|
338 | (:method ((construct SPARQL-Triple) &key (revision *TM-REVISION*)) |
---|
339 | (unless (literal-p (object construct)) |
---|
340 | (with-triple-nodes construct |
---|
341 | (when (and (or (variable-p subj) |
---|
342 | (typep (value subj) 'd:ReifiableConstructC)) |
---|
343 | (or (variable-p obj) |
---|
344 | (typep (value obj) 'd:TopicC))) |
---|
345 | (cond ((and (not (variable-p subj)) |
---|
346 | (not (variable-p obj))) |
---|
347 | (when (eql (reifier (value subj) :revision revision) |
---|
348 | (value obj)) |
---|
349 | (list (list :subject subj-uri |
---|
350 | :predicate pred-uri |
---|
351 | :object obj-uri)))) |
---|
352 | ((not (variable-p subj)) |
---|
353 | (let ((reifier-top |
---|
354 | (reifier (value subj) :revision revision))) |
---|
355 | (when reifier-top |
---|
356 | (list :subject subj-uri |
---|
357 | :predicate pred-uri |
---|
358 | :object (sparql-node reifier-top :revision revision))))) |
---|
359 | ((not (variable-p obj)) |
---|
360 | (let ((reified-cons |
---|
361 | (reified-construct (value obj) :revision revision))) |
---|
362 | (when reified-cons |
---|
363 | (list (list :subject |
---|
364 | (sparql-node reified-cons :revision revision) |
---|
365 | :predicate pred-uri |
---|
366 | :object obj-uri))))) |
---|
367 | (t ; only pred is given |
---|
368 | (let ((topics |
---|
369 | (remove-null |
---|
370 | (map 'list #'(lambda(top) |
---|
371 | (when (reified-construct top :revision revision) |
---|
372 | top)) |
---|
373 | (get-all-topics revision))))) |
---|
374 | (loop for top in topics |
---|
375 | collect (list :subject |
---|
376 | (sparql-node (reified-construct top :revision revision) |
---|
377 | :revision revision) |
---|
378 | :predicate pred-uri |
---|
379 | :object (sparql-node top :revision revision))))))))))) |
---|