value-compaction.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 value-compaction) |
19 | #:use-module (jsonld context) |
20 | #:use-module (jsonld iri-compaction) |
21 | #:use-module (jsonld json) |
22 | #:export (value-compaction |
23 | container-mapping)) |
24 | |
25 | (define (container-mapping active-context active-property) |
26 | (let* ((def (term-definition-ref active-context active-property)) |
27 | (container-mapping |
28 | (if (term-definition? def) |
29 | (or (not-null-or-false (term-definition-container def)) #()) |
30 | #()))) |
31 | (if (json-array? container-mapping) |
32 | (array->list container-mapping) |
33 | (list container-mapping)))) |
34 | |
35 | (define (value-compaction active-context inverse-context active-property value |
36 | processing-mode) |
37 | (let* ((result value) |
38 | (active-def (term-definition-ref active-context active-property)) |
39 | ;; 2 |
40 | (language (if (term-definition? active-def) |
41 | (term-definition-language active-def) |
42 | #f)) |
43 | ;; 3 |
44 | (direction (if (term-definition? active-def) |
45 | (term-definition-direction active-def) |
46 | #f))) |
47 | (when (equal? language #f) |
48 | (set! language (active-context-language active-context))) |
49 | (when (equal? direction #f) |
50 | (set! direction (active-context-direction active-context))) |
51 | (cond |
52 | ;; 4 |
53 | ((and (json-has-key? value "@id") |
54 | (null? (filter |
55 | (lambda (kv) |
56 | (not (member (car kv) '("@id" "@index")))) |
57 | value))) |
58 | (cond |
59 | ((and (term-definition? active-def) |
60 | (equal? (term-definition-type active-def) "@id")) |
61 | (set! result (iri-compaction active-context inverse-context |
62 | (assoc-ref value "@id") #:vocab? #f |
63 | #:processing-mode processing-mode))) |
64 | ((and (term-definition? active-def) |
65 | (equal? (term-definition-type active-def) "@vocab")) |
66 | (set! result (iri-compaction active-context inverse-context |
67 | (assoc-ref value "@id") #:vocab? #t |
68 | #:processing-mode processing-mode))))) |
69 | ;; 5 |
70 | ((and (json-has-key? value "@type") |
71 | (term-definition? active-def) |
72 | (equal? (assoc-ref value "@type") |
73 | (term-definition-type active-def))) |
74 | (set! result (assoc-ref value "@value"))) |
75 | ;; 6 |
76 | ((or (and (term-definition? active-def) |
77 | (equal? (term-definition-type active-def) "@none")) |
78 | (json-has-key? value "@type")) |
79 | (when (json-has-key? result "@type") |
80 | (let ((type (iri-compaction active-context inverse-context |
81 | (assoc-ref result "@type") |
82 | #:vocab? #t |
83 | #:processing-mode |
84 | processing-mode))) |
85 | (set! result |
86 | (alist-set result "@type" |
87 | (if (json-array? type) |
88 | (if (= (array-length type) 1) |
89 | (car (array->list type)) |
90 | type) |
91 | type)))))) |
92 | ;; 7 |
93 | ((and |
94 | (json-has-key? value "@value") |
95 | (not (string? (assoc-ref value "@value")))) |
96 | (when (or (and (json-has-key? value "@index") |
97 | (term-definition? active-def) |
98 | (member "@index" (container-mapping active-context |
99 | active-property))) |
100 | (not (json-has-key? value "@index"))) |
101 | (set! result (assoc-ref value "@value")))) |
102 | ;; 8 |
103 | ((and (or (and (not-null-or-false language) |
104 | (equal? language (assoc-ref value "@language"))) |
105 | (and (not (not-null-or-false language)) |
106 | (not (json-has-key? value "@language")))) |
107 | (or (and (not-null-or-false direction) |
108 | (equal? direction (assoc-ref value "@direction"))) |
109 | (and (not (not-null-or-false direction)) |
110 | (not (json-has-key? value "@direction"))))) |
111 | (when (or (and (json-has-key? value "@index") |
112 | (term-definition? active-def) |
113 | (member "@index" (container-mapping |
114 | active-context active-property))) |
115 | (not (json-has-key? value "@index"))) |
116 | (when (json-has-key? value "@value") |
117 | (set! result (assoc-ref value "@value")))))) |
118 | ;; 9 |
119 | (when (json-object? result) |
120 | (set! result |
121 | (map |
122 | (lambda (kv) |
123 | (cons |
124 | (iri-compaction active-context inverse-context (car kv) |
125 | #:vocab? #t) |
126 | (cdr kv))) |
127 | result))) |
128 | ;; 10 |
129 | result)) |
130 |