guile-jsonld/jsonld/list-to-rdf.scm

list-to-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 (jsonld list-to-rdf)
19
  #:use-module (jsonld deserialize-jsonld)
20
  #:use-module (jsonld iri)
21
  #:use-module (jsonld json)
22
  #:use-module (jsonld object-to-rdf)
23
  #:use-module (rdf rdf)
24
  #:export (list-to-rdf))
25
26
(define* (list-to-rdf generate-blank-node rdf-direction lst list-triples)
27
  (let ((result #nil))
28
    (if (null? lst)
29
        ;; 1
30
        (set! result (rdf-iri "nil"))
31
        ;; 2
32
        (let ((bnodes (map
33
                        (lambda _
34
                          (blank-node->rdf-blank-node (generate-blank-node #nil)))
35
                        lst)))
36
          ;; 3
37
          (let loop ((bnodes bnodes) (lst lst))
38
            (unless (null? bnodes)
39
              ;; 3.2
40
              (let* ((subject (car bnodes))
41
                     (item (car lst))
42
                     (rest (cdr bnodes))
43
                     (rest (if (null? rest) (rdf-iri "nil") (car rest)))
44
                     (res (object-to-rdf generate-blank-node rdf-direction
45
                                         item '()))
46
                     (object (assoc-ref res "result"))
47
                     (embedded-triples (assoc-ref res "list-triples")))
48
                ;; 3.3
49
                (unless (equal? object #nil)
50
                  (set! list-triples
51
                    (cons (make-rdf-triple subject (rdf-iri "first") object)
52
                          list-triples)))
53
                ;; 3.4
54
                (set! list-triples
55
                  (cons (make-rdf-triple subject (rdf-iri "rest") rest)
56
                        list-triples))
57
                ;; 3.5
58
                (set! list-triples
59
                  (append list-triples embedded-triples)))
60
              (loop (cdr bnodes) (cdr lst))))
61
          ;; 4
62
          (if (null? bnodes)
63
              (set! result (rdf-iri "nil"))
64
              (set! result (car bnodes)))))
65
    `(("result" . ,result)
66
      ("list-triples" . ,list-triples))))
67