source: trunk/src/TM-SPARQL/sparql_special_uris.lisp

Last change on this file was 411, checked in by lgiessmann, 13 years ago

TM-SPARQL: fixed a bug in the processing of the property tms:topicProperty; finished the unit-tests for triples of the form ?var1 ?var2 <obj>

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