guile-jsonld/jsonld/flattening.scm

flattening.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 flattening)
19
  #:use-module (jsonld generate-blank-node-identifier)
20
  #:use-module (jsonld json)
21
  #:use-module (jsonld node-map-generation)
22
  #:export (flattening))
23
24
(define* (flattening element #:key ordered?)
25
  ;; 1
26
  (let ((node-map `(("@default" . ())))
27
        (default-graph '())
28
        (node-map-generation (get-node-map-generation
29
                               (get-generate-blank-node-identifier))))
30
    ;; 2
31
    (set! node-map
32
      (assoc-ref (node-map-generation element node-map) "node-map"))
33
    ;; 3
34
    (set! default-graph (assoc-ref node-map "@default"))
35
    ;; 4
36
    (for-each-pair
37
      (lambda (graph-name graph)
38
        (unless (equal? graph-name "@default")
39
          ;; 4.1
40
          (unless (json-has-key? default-graph graph-name)
41
            (set! default-graph
42
              (alist-set default-graph graph-name `(("@id" . ,graph-name)))))
43
          ;; 4.2
44
          (let ((entry (assoc-ref default-graph graph-name))
45
                (graph-entry '()))
46
            ;; 4.3
47
            (set! entry
48
              (alist-set entry "@graph" '()))
49
            ;; 4.4
50
            (for-each-pair
51
              (lambda (id node)
52
                (unless (null?
53
                          (filter
54
                            (lambda (kv)
55
                              (not (equal? (car kv) "@id")))
56
                            node))
57
                  (set! graph-entry (cons node graph-entry))))
58
              (if ordered?
59
                  (alist-sort-by-key graph)
60
                  graph))
61
            (set! entry
62
              (alist-set entry "@graph" (list->array 1 (reverse graph-entry))))
63
            (set! default-graph
64
              (alist-set default-graph graph-name entry)))))
65
      (if ordered?
66
          (alist-sort-by-key node-map)
67
          node-map))
68
    ;; 5
69
    (let ((flattened '()))
70
      ;; 6
71
      (for-each-pair
72
        (lambda (id node)
73
          (unless (null? (filter (lambda (kv) (not (equal? (car kv) "@id")))
74
                                 node))
75
            (set! flattened (cons node flattened))))
76
        (if ordered?
77
            (alist-sort-by-key default-graph)
78
            default-graph))
79
      ;; 7
80
      (list->array 1 (reverse flattened)))))
81