Add type recognition procedure
rdf/rdf.scm
29 | 29 | rdf-datatype-lexical->value | |
30 | 30 | rdf-datatype-value->lexical | |
31 | 31 | ||
32 | + | rdf:langString | |
33 | + | ||
32 | 34 | rdf-dataset | |
33 | 35 | make-rdf-dataset | |
34 | 36 | rdf-dataset? | |
… | |||
53 | 55 | rdf-graph? | |
54 | 56 | ||
55 | 57 | merge-graphs | |
56 | - | rdf-isomorphic?)) | |
58 | + | rdf-isomorphic? | |
59 | + | recognize)) | |
57 | 60 | ||
58 | 61 | ;; From the specification: | |
59 | 62 | ;; Datatypes are used with RDF literals to represent values such as strings, | |
… | |||
90 | 93 | (lexical->value rdf-datatype-lexical->value) | |
91 | 94 | (value->lexical rdf-datatype-value->lexical)) | |
92 | 95 | ||
96 | + | (define rdf:langString | |
97 | + | (make-rdf-datatype | |
98 | + | '("http://www.w3.org/1999/02/22-rdf-syntax-ns#langString") | |
99 | + | "A literal is a language-tagged string if the third element is present. | |
100 | + | Lexical representations of language tags MAY be converted to lower case. The | |
101 | + | value space of language tags is always in lower case." | |
102 | + | string? | |
103 | + | string? | |
104 | + | string-downcase | |
105 | + | identity)) | |
106 | + | ||
93 | 107 | ;; From the specification: | |
94 | 108 | ;; An RDF dataset is a collection of RDF graphs, and comprises: | |
95 | 109 | ;; | |
… | |||
134 | 148 | ;; be well-formed according to section 2.2.9 of [BCP47]. | |
135 | 149 | ||
136 | 150 | (define-record-type rdf-literal | |
137 | - | (make-rdf-literal lexical-form datatype language-tag) | |
151 | + | (make-rdf-literal lexical-form type language-tag) | |
138 | 152 | rdf-literal? | |
139 | 153 | (lexical-form rdf-literal-lexical-form) | |
140 | - | (datatype rdf-literal-datatype) | |
154 | + | (type rdf-literal-type) | |
141 | 155 | (language-tag rdf-literal-language-tag)) | |
142 | 156 | ||
143 | 157 | ;; From the specification: | |
… | |||
157 | 171 | (define (rdf-graph? graph) | |
158 | 172 | (and (list? graph) (null? (filter (lambda (t) (not (rdf-triple? t))) graph)))) | |
159 | 173 | ||
174 | + | ;; The following is for a merging procedure, where we rename blank nodes to ensure | |
175 | + | ;; we are not merging blank nodes that have the same name | |
176 | + | ||
160 | 177 | (define (last-blank g) | |
161 | 178 | "Retun the biggest blank node identifier in g" | |
162 | 179 | (let loop ((g g) (m 0)) | |
… | |||
373 | 390 | (validate-mapping (reverse-mapping mapping) g2 g1)) | |
374 | 391 | #t | |
375 | 392 | (loop disjunctions))))))) | |
393 | + | ||
394 | + | ;; Recognizing datatypes is a transformation on the graph to add the proper | |
395 | + | ;; datatype to literals, and replace IRIs that represent a datatype with the | |
396 | + | ;; datatype it represents. This is useful for some entailment regimes, such | |
397 | + | ;; as the RDF or RDFS entailment regimes. | |
398 | + | ||
399 | + | (define (recognize-data d datatypes) | |
400 | + | (match d | |
401 | + | ((? string? iri) | |
402 | + | (let loop ((datatypes datatypes)) | |
403 | + | (if (null? datatypes) | |
404 | + | iri | |
405 | + | (if (member iri (rdf-datatype-iris (car datatypes))) | |
406 | + | (car datatypes) | |
407 | + | (loop (cdr datatypes)))))) | |
408 | + | (($ rdf-literal literal-form type langtag) | |
409 | + | (let loop ((datatypes datatypes)) | |
410 | + | (if (null? datatypes) | |
411 | + | (make-rdf-literal literal-form type langtag) | |
412 | + | (if (member type (rdf-datatype-iris (car datatypes))) | |
413 | + | (make-rdf-literal literal-form (car datatypes) langtag) | |
414 | + | (loop (cdr datatypes)))))) | |
415 | + | (_ d))) | |
416 | + | ||
417 | + | (define (recognize-triple t datatypes) | |
418 | + | (match t | |
419 | + | (($ rdf-triple subject predicate object) | |
420 | + | (make-rdf-triple | |
421 | + | (recognize-data subject datatypes) | |
422 | + | predicate | |
423 | + | (recognize-data object datatypes))))) | |
424 | + | ||
425 | + | (define (recognize graph datatypes) | |
426 | + | (match graph | |
427 | + | (() '()) | |
428 | + | ((t graph ...) | |
429 | + | (cons | |
430 | + | (recognize-triple t datatypes) | |
431 | + | (recognize graph datatypes))))) | |
432 | + |