guile-jsonld/jsonld/context.scm

context.scm

1
;;;; Copyright (C) 2019, 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 context)
19
  #:use-module (ice-9 match)
20
  #:use-module (jsonld json)
21
  #:use-module (srfi srfi-9)
22
  #:export (make-active-context
23
            active-context?
24
            active-context-definitions
25
            active-context-base
26
            active-context-original-base
27
            active-context-vocab
28
            active-context-language
29
            active-context-direction
30
            active-context-previous
31
            new-active-context
32
            update-active-context
33
34
            make-term-definition
35
            term-definition?
36
            term-definition-iri
37
            term-definition-base-url
38
            term-definition-reverse?
39
            term-definition-type
40
            term-definition-language
41
            term-definition-direction
42
            term-definition-context
43
            term-definition-nest
44
            term-definition-prefix?
45
            term-definition-index
46
            term-definition-protected?
47
            term-definition-container
48
            term-definition-equal?
49
            new-term-definition
50
            update-term-definition
51
            term-definition-ref))
52
53
;;; Section 4.1: definitions of an Active Context and a Term Definition
54
55
(define-record-type active-context
56
  (make-active-context definitions base original-base vocab language direction
57
                       previous)
58
  active-context?
59
  (definitions   active-context-definitions)
60
  (base          active-context-base)
61
  (original-base active-context-original-base)
62
  (vocab         active-context-vocab)
63
  (language      active-context-language)
64
  (direction     active-context-direction)
65
  (previous      active-context-previous))
66
67
(define* (update-active-context
68
           active-context
69
           #:key (definitions   (active-context-definitions   active-context))
70
                 (base          (active-context-base          active-context))
71
                 (original-base (active-context-original-base active-context))
72
                 (vocab         (active-context-vocab         active-context))
73
                 (language      (active-context-language      active-context))
74
                 (direction     (active-context-direction     active-context))
75
                 (previous      (active-context-previous      active-context)))
76
  (make-active-context definitions base original-base vocab language direction
77
                       previous))
78
79
(define* (new-active-context
80
           #:key (definitions   '())
81
                 (base          #f)
82
                 (original-base #f)
83
                 (vocab         #f)
84
                 (language      #f)
85
                 (direction     #f)
86
                 (previous      json-null))
87
  (make-active-context definitions base original-base vocab language direction
88
                       previous))
89
90
(define-record-type term-definition
91
  (make-term-definition iri prefix? protected? reverse? base-url context container
92
                        direction index language nest type)
93
  term-definition?
94
  (iri        term-definition-iri)
95
  (prefix?    term-definition-prefix?)
96
  (protected? term-definition-protected?)
97
  (reverse?   term-definition-reverse?)
98
  (base-url   term-definition-base-url)
99
  (context    term-definition-context)
100
  (container  term-definition-container)
101
  (direction  term-definition-direction)
102
  (index      term-definition-index)
103
  (language   term-definition-language)
104
  (nest       term-definition-nest)
105
  (type       term-definition-type))
106
107
(define* (update-term-definition
108
           term-definition
109
           #:key (iri        (term-definition-iri        term-definition))
110
                 (prefix?    (term-definition-prefix?    term-definition))
111
                 (protected? (term-definition-protected? term-definition))
112
                 (reverse?   (term-definition-reverse?   term-definition))
113
                 (base-url   (term-definition-base-url   term-definition))
114
                 (context    (term-definition-context    term-definition))
115
                 (container  (term-definition-container  term-definition))
116
                 (direction  (term-definition-direction  term-definition))
117
                 (index      (term-definition-index      term-definition))
118
                 (language   (term-definition-language   term-definition))
119
                 (nest       (term-definition-nest       term-definition))
120
                 (type       (term-definition-type       term-definition)))
121
  (make-term-definition iri prefix? protected? reverse? base-url context container
122
                        direction index language nest type))
123
124
(define* (new-term-definition
125
           #:key (iri        #f)
126
                 (prefix?    #f)
127
                 (protected? #f)
128
                 (reverse?   #f)
129
                 (base-url   #f)
130
                 (context    #f)
131
                 (container  #f)
132
                 (direction  #f)
133
                 (index      #f)
134
                 (language   #f)
135
                 (nest       #f)
136
                 (type       #f))
137
  (make-term-definition iri prefix? protected? reverse? base-url context container
138
                        direction index language nest type))
139
140
(define (term-definition-equal? a b)
141
  (match a
142
    (($ term-definition iri prefix? protected? reverse? base-url context
143
        container direction index language nest type)
144
     (and (term-definition? b)
145
          (equal? iri        (term-definition-iri        b))
146
          (equal? prefix?    (term-definition-prefix?    b))
147
          (equal? protected? (term-definition-protected? b))
148
          (equal? reverse?   (term-definition-reverse?   b))
149
          (equal? base-url   (term-definition-base-url   b))
150
          (equal? context    (term-definition-context    b))
151
          (equal? container  (term-definition-container  b))
152
          (equal? direction  (term-definition-direction  b))
153
          (equal? index      (term-definition-index      b))
154
          (equal? language   (term-definition-language   b))
155
          (equal? nest       (term-definition-nest       b))
156
          (equal? type       (term-definition-type       b))))
157
    (_ #f)))
158
159
(define (term-definition-ref context term)
160
  (let ((definitions (active-context-definitions context)))
161
    (assoc-ref definitions term)))
162