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