Add rdf vocabulary type and use it in entailments
rdf/entailment/rdf.scm
26 | 26 | (define (rdf-iri name) | |
27 | 27 | (string-append "http://www.w3.org/1999/02/22-rdf-syntax-ns#" name)) | |
28 | 28 | ||
29 | - | (define (consistent-graph? graph) | |
30 | - | (define (non-overlapping-types? graph) | |
29 | + | (define (consistent-graph? graph vocabulary) | |
30 | + | (define (alist-set lst key val) | |
31 | + | (match lst | |
32 | + | (() (list (cons key val))) | |
33 | + | (((k . v) lst ...) | |
34 | + | (if (equal? k key) | |
35 | + | (cons (cons key val) lst) | |
36 | + | (cons (cons k v) (alist-set lst key val)))))) | |
37 | + | ||
38 | + | (define (compatible? types) | |
39 | + | (match types | |
40 | + | (() #t) | |
41 | + | ((_) #t) | |
42 | + | ((a b ...) | |
43 | + | (and (null? (filter | |
44 | + | (lambda (t) | |
45 | + | ((rdf-vocabulary-compatible? vocabulary) a t)) | |
46 | + | b)) | |
47 | + | (compatible? b))))) | |
48 | + | ||
49 | + | (define (compatible-types? graph) | |
31 | 50 | (let loop ((graph graph) (type-mappings '())) | |
32 | 51 | (if (null? graph) | |
33 | - | #t | |
52 | + | (null? | |
53 | + | (filter | |
54 | + | (lambda (t) | |
55 | + | (not (compatible (cdr t)))) | |
56 | + | type-mappings)) | |
34 | 57 | (let* ((t (car graph))) | |
35 | 58 | (if (equal? (rdf-triple-predicate t) (rdf-iri "type")) | |
36 | - | (if (assoc-ref type-mappings (rdf-triple-subject t)) | |
37 | - | #f | |
38 | - | (loop (cdr graph) | |
39 | - | (cons | |
40 | - | (cons (rdf-triple-subject t) (rdf-triple-object t)) | |
41 | - | type-mappings))) | |
42 | - | (loop (cdr graph) type-mappings)))))) | |
59 | + | (loop | |
60 | + | (cdr graph) | |
61 | + | (alist-set type-mappings (rdf-triple-subject t) | |
62 | + | (cons (rdf-triple-object t) | |
63 | + | (or | |
64 | + | (assoc-ref type-mappings | |
65 | + | (rdf-triple-subject t)) | |
66 | + | '()))))))))) | |
43 | 67 | (and (d:consistent-graph? graph) | |
44 | - | (non-overlapping-types? graph))) | |
68 | + | (compatible-types? graph))) | |
45 | 69 | ||
46 | 70 | ;; G entails E if E has an instance (where blank nodes are replaced by literals | |
47 | 71 | ;; or IRIs) that is a subgraph of G. | |
… | |||
164 | 188 | g2))) | |
165 | 189 | (validate-mapping mapping g1 g2))))) | |
166 | 190 | ||
167 | - | (define (entails? g e) | |
191 | + | (define (entails? g e vocabulary) | |
168 | 192 | "Return true if g entails e" | |
169 | - | (or (not (consistent-graph? g)) | |
170 | - | (d:entails? (augment g) e))) | |
193 | + | (let ((g (recognize g vocabulary))) | |
194 | + | (or (not (consistent-graph? g vocabulary)) | |
195 | + | (d:entails? (augment g) e)))) |
rdf/entailment/rdfs.scm
31 | 31 | (define (rdfs-iri name) | |
32 | 32 | (string-append "http://www.w3.org/2000/01/rdf-schema#" name)) | |
33 | 33 | ||
34 | - | (define (consistent-graph? graph) | |
34 | + | (define (consistent-graph? graph vocabulary) | |
35 | 35 | (define (valid-subclasses? graph) | |
36 | 36 | (match graph | |
37 | 37 | (() #t) | |
38 | 38 | ((($ rdf-triple (? rdf-datatype? s) p (? rdf-datatype? o)) graph ...) | |
39 | 39 | (if (is-iri? p (rdfs-iri "subClassOf")) | |
40 | - | (and (xsd:order s o) | |
40 | + | (and ((rdf-vocabulary-order vocabulary) s o) | |
41 | 41 | (valid-subclasses? graph)) | |
42 | 42 | (valid-subclasses? graph))) | |
43 | 43 | ((_ graph ...) | |
44 | 44 | (valid-subclasses? graph)))) | |
45 | 45 | (and (valid-subclasses? graph) | |
46 | - | (rdf:consistent-graph? graph))) | |
46 | + | (rdf:consistent-graph? graph vocabulary))) | |
47 | 47 | ||
48 | 48 | ;; G entails E if E has an instance (where blank nodes are replaced by literals | |
49 | 49 | ;; or IRIs) that is a subgraph of G. | |
… | |||
311 | 311 | (inner-loop (cons t graph) subclasses subprops | |
312 | 312 | ranges domains types #t augment-set))))))))))) | |
313 | 313 | ||
314 | - | (define (entails? g e d) | |
314 | + | (define (entails? g e vocabulary) | |
315 | 315 | "Return true if g entails e recognizing d" | |
316 | - | (let* ((g (recognize g d))) | |
317 | - | (or (not (consistent-graph? g)) | |
318 | - | (d:entails? (augment g d) (recognize e d))))) | |
316 | + | (let* ((g (recognize g vocabulary))) | |
317 | + | (or (not (consistent-graph? g vocabulary)) | |
318 | + | (d:entails? (augment g (rdf-vocabulary-datatypes vocabulary)) | |
319 | + | (recognize e vocabulary))))) |
rdf/rdf.scm
33 | 33 | rdf:langString | |
34 | 34 | rdf:XMLLiteral | |
35 | 35 | ||
36 | + | rdf-vocabulary | |
37 | + | make-rdf-vocabulary | |
38 | + | rdf-vocabulary? | |
39 | + | rdf-vocabulary-datatypes | |
40 | + | rdf-vocabulary-order | |
41 | + | rdf-vocabulary-compatible? | |
42 | + | ||
36 | 43 | rdf-dataset | |
37 | 44 | make-rdf-dataset | |
38 | 45 | rdf-dataset? | |
… | |||
120 | 127 | xml->sxml | |
121 | 128 | sxml->xml)) | |
122 | 129 | ||
130 | + | ;; In addition to the specification, we define a vocabulary, which will be | |
131 | + | ;; passed to entailments that need one. | |
132 | + | ;; | |
133 | + | ;; datatypes: a list of <rdf-datatype> records. | |
134 | + | ;; order: a procedure that takes two arguments and returns whether the value | |
135 | + | ;; space of the firts is included in the value space of the second | |
136 | + | ;; compatible?: a procedure that takes two arguments and returns whether the | |
137 | + | ;; intersection of their value space is not empty | |
138 | + | ||
139 | + | (define-record-type rdf-vocabulary | |
140 | + | (make-rdf-vocabulary datatypes order compatible?) | |
141 | + | rdf-vocabulary? | |
142 | + | (datatypes rdf-vocabulary-datatypes) | |
143 | + | (order rdf-vocabulary-order) | |
144 | + | (compatible? rdf-vocabulary-compatible?)) | |
145 | + | ||
123 | 146 | ;; From the specification: | |
124 | 147 | ;; An RDF dataset is a collection of RDF graphs, and comprises: | |
125 | 148 | ;; | |
… | |||
438 | 461 | predicate | |
439 | 462 | (recognize-data object datatypes))))) | |
440 | 463 | ||
441 | - | (define (recognize graph datatypes) | |
464 | + | (define (recognize graph vocabulary) | |
442 | 465 | (match graph | |
443 | 466 | (() '()) | |
444 | 467 | ((t graph ...) | |
445 | 468 | (cons | |
446 | - | (recognize-triple t datatypes) | |
447 | - | (recognize graph datatypes))))) | |
469 | + | (recognize-triple t (rdf-vocabulary-datatypes vocabulary)) | |
470 | + | (recognize graph vocabulary))))) | |
448 | 471 |
rdf/xsd.scm
18 | 18 | (define-module (rdf xsd) | |
19 | 19 | #:use-module (ice-9 match) | |
20 | 20 | #:use-module (rdf rdf) | |
21 | - | #:export (datatypes order)) | |
21 | + | #:export (datatypes order compatible?)) | |
22 | 22 | ||
23 | 23 | ;; This module implements the xsd datatypes, as presented in https://www.w3.org/TR/rdf11-concepts/#xsd-datatypes | |
24 | 24 | ||
… | |||
108 | 108 | (define (order d1 d2) | |
109 | 109 | "Return whether d1's value space is included in d2's" | |
110 | 110 | (member d1 (assoc-ref sub-classes d2))) | |
111 | + | ||
112 | + | ;; TODO: this is not entirely correct | |
113 | + | (define (compatible? d1 d2) | |
114 | + | (or (order d1 d2) (order d2 d1))) |
test-modules/online.scm
167 | 167 | (loop types)))))) | |
168 | 168 | recognized)) | |
169 | 169 | (recognized (pk 'reco (append (list xsd:string rdf:langString) recognized))) | |
170 | + | (vocabulary (make-rdf-vocabulary recognized xsd:order xsd:compatible?)) | |
170 | 171 | (expected | |
171 | 172 | (car | |
172 | 173 | (get-objects | |
… | |||
184 | 185 | (match regime | |
185 | 186 | ("simple" | |
186 | 187 | (if (if (equal? expected #f) | |
187 | - | (simple:consistent-graph? result) | |
188 | + | (not (simple:consistent-graph? result)) | |
188 | 189 | (simple:entails? result expected)) | |
189 | 190 | (if (equal? type "PositiveEntailmentTest") | |
190 | 191 | (update-test-case test #:result 'pass) | |
… | |||
198 | 199 | (update-test-case test #:result 'pass)))) | |
199 | 200 | ("RDF" | |
200 | 201 | (if (if (equal? expected #f) | |
201 | - | (rdf:consistent-graph? result) | |
202 | - | (rdf:entails? (recognize result recognized) | |
203 | - | (recognize expected recognized))) | |
202 | + | (not (rdf:consistent-graph? result vocabulary)) | |
203 | + | (rdf:entails? result expected vocabulary)) | |
204 | 204 | (if (equal? type "PositiveEntailmentTest") | |
205 | 205 | (update-test-case test #:result 'pass) | |
206 | 206 | (update-test-case test | |
… | |||
213 | 213 | (update-test-case test #:result 'pass)))) | |
214 | 214 | ("RDFS" | |
215 | 215 | (if (if (equal? expected #f) | |
216 | - | (not (rdfs:consistent-graph? result)) | |
217 | - | (rdfs:entails? result (pk 'expected expected) recognized)) | |
216 | + | (not (rdfs:consistent-graph? result vocabulary)) | |
217 | + | (rdfs:entails? result expected vocabulary)) | |
218 | 218 | (if (equal? type "PositiveEntailmentTest") | |
219 | 219 | (update-test-case test #:result 'pass) | |
220 | 220 | (update-test-case test |