rdf.scm
1 | ;;;; Copyright (C) 2020 Julien Lepiller <julien@lepiller.eu> |
2 | ;;;; |
3 | ;;;; This library is free software; you can redistribute it and/or |
4 | ;;;; modify it under the terms of the GNU Lesser General Public |
5 | ;;;; License as published by the Free Software Foundation; either |
6 | ;;;; version 3 of the License, or (at your option) any later version. |
7 | ;;;; |
8 | ;;;; This library is distributed in the hope that it will be useful, |
9 | ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
11 | ;;;; Lesser General Public License for more details. |
12 | ;;;; |
13 | ;;;; You should have received a copy of the GNU Lesser General Public |
14 | ;;;; License along with this library; if not, write to the Free Software |
15 | ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
16 | ;;;; |
17 | |
18 | (define-module (rdf rdf) |
19 | #:use-module (srfi srfi-1) |
20 | #:use-module (srfi srfi-9) |
21 | #:use-module (sxml simple) |
22 | #:use-module (ice-9 match) |
23 | #:export (rdf-datatype |
24 | make-rdf-datatype |
25 | rdf-datatype? |
26 | rdf-datatype-iris |
27 | rdf-datatype-description |
28 | rdf-datatype-lexical? |
29 | rdf-datatype-value? |
30 | rdf-datatype-lexical->value |
31 | rdf-datatype-value->lexical |
32 | |
33 | rdf:langString |
34 | rdf:XMLLiteral |
35 | |
36 | rdf-vocabulary |
37 | make-rdf-vocabulary |
38 | rdf-vocabulary? |
39 | rdf-vocabulary-datatypes |
40 | rdf-vocabulary-order |
41 | rdf-vocabulary-compatible? |
42 | |
43 | rdf-dataset |
44 | make-rdf-dataset |
45 | rdf-dataset? |
46 | rdf-dataset-default-graph |
47 | rdf-dataset-named-graphs |
48 | |
49 | rdf-triple |
50 | make-rdf-triple |
51 | rdf-triple? |
52 | rdf-triple-subject |
53 | rdf-triple-predicate |
54 | rdf-triple-object |
55 | |
56 | rdf-literal |
57 | make-rdf-literal |
58 | rdf-literal? |
59 | rdf-literal-lexical-form |
60 | rdf-literal-type |
61 | rdf-literal-langtag |
62 | |
63 | blank-node? |
64 | rdf-graph? |
65 | |
66 | merge-graphs |
67 | rdf-isomorphic? |
68 | rdf-dataset-isomorphic? |
69 | recognize)) |
70 | |
71 | ;; From the specification: |
72 | ;; Datatypes are used with RDF literals to represent values such as strings, |
73 | ;; numbers and dates. A datatype consists of a lexical space, a value space |
74 | ;; and a lexical-to-value mapping, and is denoted by one or more IRIs. |
75 | ;; |
76 | ;; The lexical space of a datatype is a set of Unicode [UNICODE] strings. |
77 | ;; |
78 | ;; The lexical-to-value mapping of a datatype is a set of pairs whose first |
79 | ;; element belongs to the lexical space, and the second element belongs to the |
80 | ;; value space of the datatype. Each member of the lexical space is paired |
81 | ;; with exactly one value, and is a lexical representation of that value. The |
82 | ;; mapping can be seen as a function from the lexical space to the value space. |
83 | ;; |
84 | ;; In addition to the specification, we introduce value->lexical, a canonical |
85 | ;; function to map values to the lexical space. An important property is that |
86 | ;; for any val, (value? val) implies: |
87 | ;; (equal? (lexical->value (value->lexical val)) val) |
88 | ;; |
89 | ;; We also introduce a list of IRIs that denote this type, as more than one |
90 | ;; IRI can denote a type. This is set to a list of IRIs, but may be changed |
91 | ;; to a function to denote a set in the future. |
92 | ;; |
93 | ;; We also introduce a description, a text that helps humans understand the |
94 | ;; purpose of the datatype. |
95 | |
96 | (define-record-type rdf-datatype |
97 | (make-rdf-datatype iris description lexical? value? lexical->value value->lexical) |
98 | rdf-datatype? |
99 | (iris rdf-datatype-iris) |
100 | (description rdf-datatype-description) |
101 | (lexical? rdf-datatype-lexical?) |
102 | (value? rdf-datatype-value?) |
103 | (lexical->value rdf-datatype-lexical->value) |
104 | (value->lexical rdf-datatype-value->lexical)) |
105 | |
106 | (define rdf:langString |
107 | (make-rdf-datatype |
108 | '("http://www.w3.org/1999/02/22-rdf-syntax-ns#langString") |
109 | "A literal is a language-tagged string if the third element is present. |
110 | Lexical representations of language tags MAY be converted to lower case. The |
111 | value space of language tags is always in lower case." |
112 | string? |
113 | string? |
114 | string-downcase |
115 | identity)) |
116 | |
117 | (define rdf:XMLLiteral |
118 | (make-rdf-datatype |
119 | '("http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral") |
120 | "RDF provides for XML content as a possible literal value. Such content |
121 | is indicated in an RDF graph using a literal whose datatype is set to |
122 | rdf:XMLLiteral. This datatype is defined as non-normative because it depends |
123 | on [DOM4], a specification that has not yet reached W3C Recommendation status." |
124 | (lambda (l) |
125 | (false-if-exception (xml->sxml l))) |
126 | (lambda (v) |
127 | (false-if-exception (sxml->xml v))) |
128 | xml->sxml |
129 | sxml->xml)) |
130 | |
131 | ;; In addition to the specification, we define a vocabulary, which will be |
132 | ;; passed to entailments that need one. |
133 | ;; |
134 | ;; datatypes: a list of <rdf-datatype> records. |
135 | ;; order: a procedure that takes two arguments and returns whether the value |
136 | ;; space of the firts is included in the value space of the second |
137 | ;; compatible?: a procedure that takes two arguments and returns whether the |
138 | ;; intersection of their value space is not empty |
139 | |
140 | (define-record-type rdf-vocabulary |
141 | (make-rdf-vocabulary datatypes order compatible?) |
142 | rdf-vocabulary? |
143 | (datatypes rdf-vocabulary-datatypes) |
144 | (order rdf-vocabulary-order) |
145 | (compatible? rdf-vocabulary-compatible?)) |
146 | |
147 | ;; From the specification: |
148 | ;; An RDF dataset is a collection of RDF graphs, and comprises: |
149 | ;; |
150 | ;; * Exactly one default graph, being an RDF graph. The default graph does |
151 | ;; not have a name and MAY be empty. |
152 | ;; * Zero or more named graphs. Each named graph is a pair consisting of an |
153 | ;; IRI or a blank node (the graph name), and an RDF graph. Graph names are |
154 | ;; unique within an RDF dataset. |
155 | ;; |
156 | ;; We represent named graphs with a association list whose keys are IRIs or |
157 | ;; blank nodes, and values are RDF graphs. |
158 | |
159 | (define-record-type rdf-dataset |
160 | (make-rdf-dataset default-graph named-graphs) |
161 | rdf-dataset? |
162 | (default-graph rdf-dataset-default-graph) |
163 | (named-graphs rdf-dataset-named-graphs)) |
164 | |
165 | ;; From the specification: |
166 | ;; An RDF triple consists of three components: |
167 | ;; |
168 | ;; * the subject, which is an IRI or a blank node |
169 | ;; * the predicate, which is an IRI |
170 | ;; * the object, which is an IRI, a literal or a blank node |
171 | |
172 | (define-record-type rdf-triple |
173 | (make-rdf-triple subject predicate object) |
174 | rdf-triple? |
175 | (subject rdf-triple-subject) |
176 | (predicate rdf-triple-predicate) |
177 | (object rdf-triple-object)) |
178 | |
179 | ;; From the specification: |
180 | ;; A literal in an RDF graph consists of two or three elements: |
181 | ;; |
182 | ;; * a lexical form, being a Unicode [UNICODE] string, which SHOULD be in |
183 | ;; Normal Form C [NFC], |
184 | ;; * a datatype IRI, being an IRI identifying a datatype that determines how |
185 | ;; the lexical form maps to a literal value, and |
186 | ;; * if and only if the datatype IRI is `http://www.w3.org/1999/02/22-rdf-syntax-ns#langString`, |
187 | ;; a non-empty language tag as defined by [BCP47]. The language tag MUST |
188 | ;; be well-formed according to section 2.2.9 of [BCP47]. |
189 | |
190 | (define-record-type rdf-literal |
191 | (make-rdf-literal lexical-form type langtag) |
192 | rdf-literal? |
193 | (lexical-form rdf-literal-lexical-form) |
194 | (type rdf-literal-type) |
195 | (langtag rdf-literal-langtag)) |
196 | |
197 | ;; From the specification: |
198 | ;; Blank nodes are disjoint from IRIs and literals. Otherwise, the set of |
199 | ;; possible blank nodes is arbitrary. RDF makes no reference to any internal |
200 | ;; structure of blank nodes. |
201 | ;; |
202 | ;; Here, we will use integers as blank nodes |
203 | |
204 | (define blank-node? integer?) |
205 | |
206 | ;; From the specification: |
207 | ;; An RDF graph is a set of RDF triples. |
208 | ;; |
209 | ;; We represent a graph as a list of RDF triples |
210 | |
211 | (define (rdf-graph? graph) |
212 | (and (list? graph) (null? (filter (lambda (t) (not (rdf-triple? t))) graph)))) |
213 | |
214 | ;; The following is for a merging procedure, where we rename blank nodes to ensure |
215 | ;; we are not merging blank nodes that have the same name |
216 | |
217 | (define (last-blank g) |
218 | "Retun the biggest blank node identifier in g" |
219 | (let loop ((g g) (m 0)) |
220 | (match g |
221 | ('() m) |
222 | ((triple g ...) |
223 | (loop g (max m |
224 | (if (blank-node? (rdf-triple-subject triple)) |
225 | (rdf-triple-subject triple) |
226 | 0) |
227 | (if (blank-node? (rdf-triple-object triple)) |
228 | (rdf-triple-object triple) |
229 | 0))))))) |
230 | |
231 | (define (rename-blanks g num) |
232 | "Return the same graph, but blank nodes are renamed from num" |
233 | (let loop ((g g) (renamings '()) (num num) (result '())) |
234 | (match g |
235 | ('() result) |
236 | ((triple g ...) |
237 | (let* ((subject (rdf-triple-subject triple)) |
238 | (num (if (and (blank-node? subject) |
239 | (assoc-ref renamings subject)) |
240 | num |
241 | (+ num 1))) |
242 | (renamings |
243 | (if (and (blank-node? subject) |
244 | (assoc-ref renamings subject)) |
245 | renamings |
246 | (cons (cons subject num) renamings))) |
247 | (subject |
248 | (if (blank-node? subject) |
249 | (assoc-ref renamings subject) |
250 | subject)) |
251 | (predicate (rdf-triple-predicate triple)) |
252 | (object (rdf-triple-object triple)) |
253 | (num (if (and (blank-node? object) |
254 | (assoc-ref renamings object)) |
255 | num |
256 | (+ num 1))) |
257 | (renamings |
258 | (if (and (blank-node? object) |
259 | (assoc-ref renamings object)) |
260 | renamings |
261 | (cons (cons object num) renamings))) |
262 | (object |
263 | (if (blank-node? object) |
264 | (assoc-ref renamings object) |
265 | object))) |
266 | (loop g renamings num (cons (make-rdf-triple subject predicate object) |
267 | result))))))) |
268 | |
269 | (define (merge-graphs g1 g2) |
270 | "Merge two graphs g1 and g2. This is the same as append, but we need to make |
271 | sure we rename blank nodes, or some nodes will be merged when they shouldn't." |
272 | (append g1 (rename-blanks g2 (last-blank g1)))) |
273 | |
274 | ;; Next, a predicate on isomorphisms between two graphs. Two graphs are isomorphic |
275 | ;; when each triple has a corresponding triple in the other graph. |
276 | ;; |
277 | ;; To take blank nodes into account, there needs to be a mapping from blank nodes |
278 | ;; of the first graph to blank nodes of the other graph in order to prove |
279 | ;; isomorphism. |
280 | ;; |
281 | ;; First, we compare the two graphs and find possible constraints on that mapping. |
282 | ;; for instance, if one graph has (_:1, p, o) and the other (_:2, p, o), then |
283 | ;; a possible constraint is that _:1 maps to _:2. If the other graph also has |
284 | ;; (_:3, p, o) then maybe _:1 actually maps to _:3. |
285 | ;; |
286 | ;; Constraints are either "none" (no constraint), "equiv" (a mapping between two |
287 | ;; blank node identifiers), "or" (a disjunction) or "and" (a conjunction). |
288 | ;; By comparing the triples of the first graph, we create an conjunction between |
289 | ;; the constraints collected from each triple. The constraints of a triple is |
290 | ;; a disjunction between every case where it matches a triple from the other graph. |
291 | ;; That creates zero, one or two constraints (depending on the number of blank |
292 | ;; nodes). |
293 | ;; |
294 | ;; These constraints are transformed in a normal form, as a list of lists of |
295 | ;; conjunctions. Each list is a candidate mapping. sat? is used to evaluate the |
296 | ;; candidate mapping and ensure it is an isomorphism between the two sets of |
297 | ;; blank nodes. For every sat? equivalences, we check that the mapping actually |
298 | ;; maps triples of g1 to triples of g2, and its reverse mapping maps triples of |
299 | ;; g2 to triples of g1. Whenever one mapping works, the two graphs are equivalent. |
300 | ;; If no mapping works, the two graphs are not equivalent. |
301 | |
302 | (define (sat? equivalences) |
303 | "Return whether the set of equivalences satisfies the condition that it represents |
304 | an isomorphism between two blank node sets: for every equality, check that the |
305 | first component is always associated to the same second component, and that the |
306 | second component is always associated with the first." |
307 | (match equivalences |
308 | ('() #t) |
309 | (((first . second) equivalences ...) |
310 | (if (and (null? (filter |
311 | (lambda (eq) |
312 | (and (equal? (car eq) first) |
313 | (not (equal? (cdr eq) second)))) |
314 | equivalences)) |
315 | (null? (filter |
316 | (lambda (eq) |
317 | (and (not (equal? (car eq) first)) |
318 | (equal? (cdr eq) second))) |
319 | equivalences))) |
320 | (sat? equivalences) |
321 | #f)))) |
322 | |
323 | (define (merge-joins l1 l2) |
324 | (cond |
325 | ((null? l1) l2) |
326 | ((null? l2) l1) |
327 | (else |
328 | (fold |
329 | (lambda (e1 res) |
330 | (append |
331 | (map (lambda (e2) |
332 | (append e1 e2)) |
333 | l2) |
334 | res)) |
335 | '() |
336 | l1)))) |
337 | |
338 | (define (to-disjunctions constraints) |
339 | (match constraints |
340 | (('equiv b1 b2) (list (list (cons b1 b2)))) |
341 | ('none (list (list))) |
342 | ('bot 'bot) |
343 | (('or e1 e2) |
344 | (let ((e1 (to-disjunctions e1)) |
345 | (e2 (to-disjunctions e2))) |
346 | (cond |
347 | ((equal? e2 'bot) |
348 | e1) |
349 | ((equal? e1 'bot) |
350 | e2) |
351 | (else |
352 | (append e1 e2))))) |
353 | (('and e1 e2) |
354 | (let ((e1 (to-disjunctions e1)) |
355 | (e2 (to-disjunctions e2))) |
356 | (cond |
357 | ((equal? e1 'bot) |
358 | 'bot) |
359 | ((equal? e2 'bot) |
360 | 'bot) |
361 | (else |
362 | (merge-joins e1 e2))))))) |
363 | |
364 | (define (generate-triple-constraints t1 t2) |
365 | (match t1 |
366 | (($ rdf-triple s1 p1 o1) |
367 | (match t2 |
368 | (($ rdf-triple s2 p2 o2) |
369 | (if (and (or (equal? s1 s2) (and (blank-node? s1) (blank-node? s2))) |
370 | (equal? p1 p2) |
371 | (or (equal? o1 o2) (and (blank-node? o1) (blank-node? o2)))) |
372 | (list 'and |
373 | (if (blank-node? s1) |
374 | (list 'equiv s1 s2) |
375 | 'none) |
376 | (if (blank-node? o1) |
377 | (list 'equiv o1 o2) |
378 | 'none)) |
379 | #f)))))) |
380 | |
381 | (define (generate-constraints t1 g2) |
382 | (match g2 |
383 | ('() 'bot) |
384 | ((t2 g2 ...) |
385 | (let ((c (generate-triple-constraints t1 t2))) |
386 | (if c |
387 | (list 'or c (generate-constraints t1 g2)) |
388 | (generate-constraints t1 g2)))))) |
389 | |
390 | (define (generate-graph-constraints g1 g2) |
391 | (fold (lambda (t constraints) |
392 | (list 'and (generate-constraints t g2) constraints)) |
393 | 'none g1)) |
394 | |
395 | (define (reverse-mapping mapping) |
396 | (let loop ((mapping mapping) (result '())) |
397 | (match mapping |
398 | ('() result) |
399 | (((first . second) mapping ...) |
400 | (loop mapping (cons (cons second first) result)))))) |
401 | |
402 | (define (validate-mapping mapping g1 g2) |
403 | (match g1 |
404 | ('() #t) |
405 | ((t1 g1 ...) |
406 | (and (not (null? (filter |
407 | (lambda (t2) |
408 | (let ((s1 (rdf-triple-subject t1)) |
409 | (s2 (rdf-triple-subject t2)) |
410 | (p1 (rdf-triple-predicate t1)) |
411 | (p2 (rdf-triple-predicate t2)) |
412 | (o1 (rdf-triple-object t1)) |
413 | (o2 (rdf-triple-object t2))) |
414 | (and |
415 | (if (blank-node? s1) |
416 | (equal? (assoc-ref mapping s1) s2) |
417 | (equal? s1 s2)) |
418 | (equal? p1 p2) |
419 | (if (blank-node? o1) |
420 | (equal? (assoc-ref mapping o1) o2) |
421 | (equal? o1 o2))))) |
422 | g2))) |
423 | (validate-mapping mapping g1 g2))))) |
424 | |
425 | (define (rdf-isomorphic? g1 g2) |
426 | "Compare two graphs and return whether they are isomorph." |
427 | (let* ((constraints (generate-graph-constraints g1 g2)) |
428 | (disjunctions (to-disjunctions constraints))) |
429 | (if (list? disjunctions) |
430 | (let loop ((disjunctions (filter sat? disjunctions))) |
431 | (match disjunctions |
432 | ('() (and (null? g1) (null? g2))) |
433 | ((mapping disjunctions ...) |
434 | (if (and (validate-mapping mapping g1 g2) |
435 | (validate-mapping (reverse-mapping mapping) g2 g1)) |
436 | #t |
437 | (loop disjunctions))))) |
438 | #f))) |
439 | |
440 | (define (generate-dataset-constraints d1 d2) |
441 | (let ((g1 (rdf-dataset-default-graph d1)) |
442 | (g2 (rdf-dataset-default-graph d2)) |
443 | (ng1 (rdf-dataset-named-graphs d1)) |
444 | (ng2 (rdf-dataset-named-graphs d2))) |
445 | (list 'and (generate-graph-constraints g1 g2) |
446 | (if (null? ng1) |
447 | 'none |
448 | (fold (lambda (ng1 constraints) |
449 | (match ng1 |
450 | ((n1 . g1) |
451 | (if (blank-node? n1) |
452 | (fold (lambda (ng2 constraints) |
453 | (list 'or (list 'and (list 'equiv n1 (car ng2)) |
454 | (generate-graph-constraints g1 g2)) |
455 | constraints)) |
456 | 'bot |
457 | (filter (lambda (g2) (blank-node? (car g2))) ng2)) |
458 | (let ((g2 (assoc-ref ng2 n1))) |
459 | (if g2 |
460 | (list 'and (generate-graph-constraints g1 g2) |
461 | constraints) |
462 | 'bot)))))) |
463 | 'bot ng1))))) |
464 | |
465 | (define (validate-dataset-mapping mapping d1 d2) |
466 | (define (validate-named-graph name graph) |
467 | (let ((graph2 (if (blank-node? name) |
468 | (assoc-ref (rdf-dataset-named-graphs d2) |
469 | (assoc-ref mapping name)) |
470 | (assoc-ref (rdf-dataset-named-graphs d2) name)))) |
471 | (validate-mapping mapping graph graph2))) |
472 | |
473 | (and (validate-mapping mapping (rdf-dataset-default-graph d1) |
474 | (rdf-dataset-default-graph d2)) |
475 | (null? (filter |
476 | (lambda (ng1) |
477 | (match ng1 |
478 | ((name . graph) |
479 | (not (validate-named-graph name graph))))) |
480 | (rdf-dataset-named-graphs d1))))) |
481 | |
482 | (define (rdf-dataset-isomorphic? d1 d2) |
483 | "Compare two datasets and return whether they are isomorphic." |
484 | (let* ((constraints (generate-dataset-constraints d1 d2)) |
485 | (disjunctions (to-disjunctions constraints))) |
486 | (if (list? disjunctions) |
487 | (let loop ((disjunctions (filter sat? disjunctions))) |
488 | (match disjunctions |
489 | ('() (and (null? (rdf-dataset-default-graph d1)) |
490 | (null? (rdf-dataset-default-graph d2)) |
491 | (null? (rdf-dataset-named-graphs d1)) |
492 | (null? (rdf-dataset-named-graphs d2)))) |
493 | ((mapping disjunctions ...) |
494 | (or (and (validate-dataset-mapping mapping d1 d2) |
495 | (validate-dataset-mapping (reverse-mapping mapping) d2 d1)) |
496 | (loop disjunctions))))) |
497 | #f))) |
498 | |
499 | ;; Recognizing datatypes is a transformation on the graph to add the proper |
500 | ;; datatype to literals, and replace IRIs that represent a datatype with the |
501 | ;; datatype it represents. This is useful for some entailment regimes, such |
502 | ;; as the RDF or RDFS entailment regimes. |
503 | |
504 | (define (recognize-data d datatypes) |
505 | (match d |
506 | ((? string? iri) |
507 | (let loop ((datatypes datatypes)) |
508 | (if (null? datatypes) |
509 | iri |
510 | (if (member iri (rdf-datatype-iris (car datatypes))) |
511 | (car datatypes) |
512 | (loop (cdr datatypes)))))) |
513 | (($ rdf-literal literal-form type langtag) |
514 | (let loop ((datatypes datatypes)) |
515 | (if (null? datatypes) |
516 | (make-rdf-literal literal-form type langtag) |
517 | (if (member type (rdf-datatype-iris (car datatypes))) |
518 | (make-rdf-literal literal-form (car datatypes) langtag) |
519 | (loop (cdr datatypes)))))) |
520 | (_ d))) |
521 | |
522 | (define (recognize-triple t datatypes) |
523 | (match t |
524 | (($ rdf-triple subject predicate object) |
525 | (make-rdf-triple |
526 | (recognize-data subject datatypes) |
527 | predicate |
528 | (recognize-data object datatypes))))) |
529 | |
530 | (define (recognize graph vocabulary) |
531 | (match graph |
532 | (() '()) |
533 | ((t graph ...) |
534 | (cons |
535 | (recognize-triple t (rdf-vocabulary-datatypes vocabulary)) |
536 | (recognize graph vocabulary))))) |
537 | |
538 |