summaryrefslogtreecommitdiff
path: root/schemantic/interop/guile-rdf.scm
blob: d31d94928b333a992896ffae0683f99e709ffd6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
; SPDX-FileCopyrightText: 2020 pukkamustard <pukkamustard@posteo.net>
;
; SPDX-License-Identifier: GPL-3.0-or-later

(define-module (schemantic interop guile-rdf)
  #:use-module (schemantic rdf)
  #:use-module (schemantic blank-node)
  #:use-module ((schemantic ns) #:select (xsd rdf))

  #:use-module (rdf rdf)

  #:export (rdf-triple->triple))

;; Interop to the guile-rdf library

;; NOTE maybe it makes sense to keep the mapping of datatype+lexical-form to
;; literal in some other place as this is something that would be used by other
;; serializations... literals are tricky
(define (rdf-literal->literal literal)
  (let ((datatype (make-iri (rdf-literal-type literal))))
    (cond

     ((equal? (rdf "langString") datatype)
      (make-lang-string
       (rdf-literal-lexical-form literal)
       #:language (rdf-literal-langtag literal)))

     ((equal? (xsd "string") datatype)
      (make-literal (rdf-literal-lexical-form literal)))

     ((equal? (xsd "integer") datatype)
      (make-literal (string->number (rdf-literal-lexical-form literal))))

     (else (make-generic-literal (rdf-literal-lexical-form literal)
                                 #:datatype datatype)))))

(define (rdf-term->term term)
  (cond
   ((string? term) (make-iri term))
   ((integer? term) (make-blank-node term))
   ((rdf-literal? term) (rdf-literal->literal term))))

(define (rdf-triple->triple t)
  (make-triple
   (rdf-term->term (rdf-triple-subject t))
   (rdf-term->term (rdf-triple-predicate t))
   (rdf-term->term (rdf-triple-object t))))