Update compaction algorithm.

Julien LepillerMon Feb 28 23:41:27+0100 2022

7f94de5

Update compaction algorithm. This fixes two new test failures.

jsonld.scm

2525
  #:use-module (jsonld expansion)
2626
  #:use-module (jsonld flattening)
2727
  #:use-module (jsonld generate-blank-node-identifier)
28-
  #:use-module (jsonld inverse-context-creation)
2928
  #:use-module (jsonld iri-compaction)
3029
  #:use-module (jsonld json)
3130
  #:use-module (jsonld node-map-generation)

6160
                                                   #:base base-iri)
6261
                                                 context context-base
6362
                                                 #:options options))
64-
             (inverse-context (inverse-context-creation active-context))
6563
             (compacted-output
66-
               (compaction active-context inverse-context
64+
               (compaction active-context
6765
                           json-null ;; active-property
6866
                           expanded-input ;; element
6967
                           #:compact-arrays? (jsonld-options-compact-arrays? options)

7472
            (set! compacted-output '())
7573
            (when (json-array? compacted-output)
7674
              (set! compacted-output `((,(iri-compaction active-context
77-
                                                         inverse-context
7875
                                                         "@graph"
7976
                                                         #:vocab? #t
8077
                                                         #:reverse? #f) .

141138
                                              (jsonld-options-base options))
142139
                                          (jsonld-options-base options))
143140
                                      #:options options)))
141+
144142
      (when (and
145143
              (json-object? expanded-output)
146144
              (json-has-key? expanded-output "@graph")

jsonld/compaction.scm

1818
(define-module (jsonld compaction)
1919
  #:use-module (jsonld context)
2020
  #:use-module (jsonld context-processing)
21-
  #:use-module (jsonld inverse-context-creation)
2221
  #:use-module (jsonld iri-compaction)
2322
  #:use-module (jsonld iri-expansion)
2423
  #:use-module (jsonld json)

5049
               (list->array 1 new-value))))
5150
    (alist-set object key new-value)))
5251
53-
(define* (compaction active-context inverse-context active-property element
52+
(define* (compaction active-context active-property element
5453
                     #:key (compact-arrays? #f) (ordered? #f) processing-mode)
55-
  (pk 'compaction element)
5654
  ;; 1
5755
  (let ((type-scoped-context active-context)
5856
        (def (term-definition-ref active-context active-property)))

6866
         (for-each
6967
           (lambda (item)
7068
             ;; 3.2.1
71-
             (let ((compacted-item (compaction active-context inverse-context
69+
             (let ((compacted-item (compaction active-context
7270
                                               active-property item
7371
                                               #:compact-arrays? compact-arrays?
7472
                                               #:ordered? ordered?

9896
                      (and (json-has-key? element "@id")
9997
                           (null? (filter (lambda (kp) (not (equal? (car kp) "@id")))
10098
                                          element))))
101-
            (set! active-context (active-context-previous active-context))
102-
            (set! inverse-context (inverse-context-creation active-context))))
99+
            (set! active-context (active-context-previous active-context))))
103100
        ;; 6
104101
        (when (and (term-definition? def)
105102
                   (not-null-or-false (term-definition-context def)))
106-
          ;; 6.1
107103
          (set! active-context
108104
            (context-processing active-context (term-definition-context def)
109105
                                (term-definition-base-url def)
110-
                                #:override-protected? #t))
111-
          ;; 6.2
112-
          (set! inverse-context (inverse-context-creation active-context)))
106+
                                #:override-protected? #t)))
113107
        ;; 7
114108
        (cond
115109
          ((and (or (json-has-key? element "@value")
116110
                    (json-has-key? element "@id"))
117111
                (let ((compact
118-
                      (value-compaction active-context inverse-context
119-
                                        active-property element processing-mode)))
112+
                      (value-compaction active-context active-property element
113+
                                        processing-mode)))
120114
                  (or (scalar? compact)
121115
                      (and (term-definition? def)
122116
                           (equal? (term-definition-type def) "@json")))))
123-
           (value-compaction active-context inverse-context active-property
124-
                             element processing-mode))
117+
           (value-compaction active-context  active-property element
118+
                             processing-mode))
125119
          ;; 8
126120
          ((and (json-has-key? element "@list")
127121
                (member "@list" (container-mapping active-context active-property)))
128-
           (compaction active-context inverse-context active-property
122+
           (compaction active-context active-property
129123
                       (assoc-ref element "@list")
130124
                       #:compact-arrays? compact-arrays?
131125
                       #:ordered? ordered?))

142136
                       (compacted-types
143137
                         (map
144138
                           (lambda (type)
145-
                             (iri-compaction active-context inverse-context type
146-
                                             #:vocab? #t #:processing-mode processing-mode))
139+
                             (iri-compaction active-context type
140+
                                             #:vocab? #t
141+
                                             #:processing-mode processing-mode))
147142
                           types)))
148-
                  (pk 'compacted-types compacted-types)
149143
                  (for-each
150144
                    (lambda (term)
151-
                      ;; 11.1
152145
                      (when (and
153146
                              (term-definition? (term-definition-ref type-scoped-context term))
154147
                              (term-definition-context
155148
                                (term-definition-ref type-scoped-context term)))
156-
                        ;; 11.1.1
157149
                        (set! active-context
158150
                          (context-processing
159151
                            active-context

161153
                              (term-definition-ref type-scoped-context term))
162154
                            (term-definition-base-url
163155
                              (term-definition-ref type-scoped-context term))
164-
                            #:propagate? #f))
165-
                        ;; 11.1.2
166-
                        (set! inverse-context
167-
                          (inverse-context-creation active-context))))
156+
                            #:propagate? #f))))
168157
                    (sort compacted-types string<=?))))
169158
              ;; 12
170159
              (for-each-pair
171160
                (lambda (expanded-property expanded-value)
172161
                  (set! result
173-
                    (step-12 active-context inverse-context expanded-property
162+
                    (step-12 active-context expanded-property
174163
                             expanded-value result compact-arrays? ordered?
175164
                             inside-reverse? type-scoped-context active-property
176165
                             processing-mode)))

180169
              ;; 13
181170
              result)))))))
182171
183-
(define (step-12 active-context inverse-context expanded-property expanded-value
172+
(define (step-12 active-context expanded-property expanded-value
184173
                 result compact-arrays? ordered? inside-reverse? type-scoped-context
185174
                 active-property processing-mode)
186175
  (cond
187176
    ;; 12.1
188177
    ((equal? expanded-property "@id")
189-
     (pk 'expanded-value expanded-value)
190178
     ;; XXX: not clear what to do if expanded-value is not a
191179
     ;; string, make sure there is a test
192180
     (let ((compacted-value
193181
             (if (string? expanded-value)
194-
                 (iri-compaction active-context inverse-context expanded-value
182+
                 (iri-compaction active-context expanded-value
195183
                                 #:vocab? #f #:processing-mode processing-mode)
196184
                 expanded-value))
197-
           (alias (iri-compaction active-context inverse-context expanded-property
185+
           (alias (iri-compaction active-context expanded-property
198186
                                  #:vocab? #t #:processing-mode processing-mode)))
199-
       (pk 'compacted-value compacted-value)
200-
       (pk 'alias alias)
201187
       (set! result
202188
         (alist-set result alias compacted-value))))
203189
    ;; 12.2
204190
    ((equal? expanded-property "@type")
205191
     (let* ((compacted-value #f)
206-
            (alias (iri-compaction active-context inverse-context expanded-property
192+
            ;; 12.2.3
193+
            (alias (iri-compaction active-context expanded-property
207194
                                   #:vocab? #t #:processing-mode processing-mode))
195+
            ;; 12.2.4
208196
            (as-array?
209197
              (or 
210198
                (and
211-
                  (not (processing-mode-1.0? processing-mode))
199+
                  (processing-mode-1.1? processing-mode)
212200
                  (member "@set" (container-mapping active-context alias)))
213-
                (not compact-arrays?)))
214-
            (type-scoped-inverse-context
215-
              (inverse-context-creation type-scoped-context)))
216-
       (pk 'alias alias)
201+
                (not compact-arrays?))))
217202
       (if (string? expanded-value)
203+
           ;; 12.1
218204
           (set! compacted-value
219-
             (iri-compaction type-scoped-context type-scoped-inverse-context
205+
             (iri-compaction type-scoped-context
220206
                             expanded-value
221207
                             #:vocab? #t #:processing-mode processing-mode))
208+
           ;; 12.2
222209
           (begin
223210
             (set! expanded-value (array->list expanded-value))
224211
             (set! compacted-value
225212
               (list->array 1
226213
                 (map
227-
                   (lambda (v)
228-
                     (pk 'v (iri-compaction type-scoped-context type-scoped-inverse-context
229-
                                     v
230-
                                     #:vocab? #t #:processing-mode processing-mode)))
214+
                   (lambda (expanded-type)
215+
                     (iri-compaction type-scoped-context expanded-type
216+
                                     #:vocab? #t
217+
                                     #:processing-mode processing-mode))
231218
                   expanded-value)))))
232-
       (pk 'compacted-value compacted-value)
219+
       ;; XXX: not in spec
233220
       (when (and (json-array? compacted-value) (= (array-length compacted-value) 1))
234221
         (set! compacted-value (car (array->list compacted-value))))
235222
       (set! result
236223
         (add-value result alias compacted-value as-array?))))
237224
    ;; 12.3
238225
    ((equal? expanded-property "@reverse")
226+
     ;; 12.3.1
239227
     (let ((compacted-value
240-
             (compaction active-context inverse-context "@reverse" expanded-value
228+
             (compaction active-context "@reverse" expanded-value
241229
                         #:compact-arrays? compact-arrays? #:ordered? ordered?)))
242230
       ;; 12.3.2
243231
       (for-each-pair
244232
         (lambda (property value)
245233
           (let ((def (term-definition-ref active-context property)))
246234
             (when (and (term-definition? def) (term-definition-reverse? def))
235+
               ;; 12.3.2.1.1
247236
               (let ((as-array? (or
248237
                                  (member "@set"
249238
                                          (container-mapping active-context property))
250239
                                  (not compact-arrays?))))
240+
                 ;; 12.3.2.1.2
251241
                 (set! result
252242
                   (add-value result property value as-array?)))
243+
               ;; 12.3.2.1.3
253244
               (set! compacted-value
254245
                 (alist-remove compacted-value property)))))
255246
         compacted-value)
256247
       ;; 12.3.3
257248
       (unless (null? compacted-value)
258-
         (let ((alias (iri-compaction active-context inverse-context "@reverse"
249+
         (let ((alias (iri-compaction active-context "@reverse"
259250
                                      #:vocab? #t #:processing-mode processing-mode)))
260-
           (pk 'alias alias)
261251
           (set! result (alist-set result alias compacted-value))))))
262252
    ;; 12.4
263253
    ((equal? expanded-property "@preserve")
264254
     (let ((compacted-value
265-
             (compaction active-context inverse-context active-property
266-
                         expanded-value #:compact-arrays? compact-arrays?
255+
             (compaction active-context active-property expanded-value
256+
                         #:compact-arrays? compact-arrays?
267257
                         #:ordered? ordered?)))
268258
       (unless (equal? expanded-value #())
269259
         (set! result (alist-set result "@preserve" compacted-value)))))

277267
         (equal? expanded-property "@index")
278268
         (equal? expanded-property "@language")
279269
         (equal? expanded-property "@value"))
280-
      (let ((alias (iri-compaction active-context inverse-context expanded-property
270+
      (let ((alias (iri-compaction active-context expanded-property
281271
                                   #:vocab? #t #:processing-mode processing-mode)))
282-
        (pk 'alias alias)
283272
        (set! result
284273
          (alist-set result alias expanded-value))))
285274
    (else
286275
      ;; 12.7
287276
      (when (equal? expanded-value #())
288277
        (let* ((item-active-property
289-
                 (iri-compaction active-context inverse-context expanded-property
278+
                 (iri-compaction active-context expanded-property
290279
                                 #:value expanded-value
291280
                                 #:vocab? #t
292281
                                 #:reverse? inside-reverse?

296285
               (nest-term (if (term-definition? def) (term-definition-nest def) #f))
297286
               ;; 12.7.4
298287
               (nest-result result))
299-
          (pk 'item-active-property item-active-property)
300288
          ;; 12.7.2
301289
          (when nest-term
302290
            (unless (or (equal? nest-term "@nest")

316304
        (lambda (expanded-item)
317305
          (let* ((item-active-property
318306
                   (iri-compaction
319-
                     active-context inverse-context expanded-property
307+
                     active-context expanded-property
320308
                     #:value expanded-item
321309
                     #:vocab? #t
322310
                     #:reverse? inside-reverse?

334322
                                (not compact-arrays?)))
335323
                 ;; 12.8.6
336324
                 (compacted-item
337-
                   (compaction active-context inverse-context item-active-property
325+
                   (compaction active-context item-active-property
338326
                               (if (json-has-key? expanded-item  "@list")
339327
                                   (assoc-ref expanded-item "@list")
340328
                                   (if (graph-object? expanded-item)

342330
                                       expanded-item))
343331
                               #:compact-arrays? compact-arrays?
344332
                               #:ordered? ordered?)))
345-
            (pk 'item-active-property item-active-property)
346333
            ;; 12.8.2
347334
            (when (not-null-or-false nest-term)
348335
              (unless (or (equal? nest-term "@nest")

365352
                 ;; 12.8.7.2
366353
                 (begin
367354
                   (set! compacted-item
368-
                     `((,(iri-compaction active-context inverse-context
369-
                                         "@list" #:vocab? #t
355+
                     `((,(iri-compaction active-context "@list" #:vocab? #t
370356
                                         #:processing-mode processing-mode) .
371357
                         ,compacted-item)))
372358
                   (when (json-has-key? expanded-item "@index")
373359
                     (set! compacted-item
374360
                       (alist-set
375361
                         compacted-item
376-
                         (iri-compaction active-context inverse-context
377-
                                         "@index" #:vocab? #t
362+
                         (iri-compaction active-context "@index" #:vocab? #t
378363
                                         #:processing-mode processing-mode)
379364
                         (assoc-ref expanded-item "@index"))))
380365
                   (set! nest-result

395380
                               "@none"))
396381
                         (map-key
397382
                           (iri-compaction
398-
                             active-context inverse-context map-key
383+
                             active-context map-key
399384
                             #:vocab? (not (json-has-key? expanded-item "@id"))
400385
                             #:processing-mode processing-mode)))
401386
                    ;; 12.8.8.1.3

426411
                  (when (and (json-array? compacted-item)
427412
                             (> (array-length compacted-item) 1))
428413
                    (set! compacted-item
429-
                      `((,(iri-compaction active-context inverse-context "@included"
430-
                                          #:vocab? #t #:processing-mode processing-mode) .
414+
                      `((,(iri-compaction active-context "@included"
415+
                                          #:vocab? #t
416+
                                          #:processing-mode processing-mode) .
431417
                         ,compacted-item))))
432418
                  (set! nest-result
433419
                    (add-value nest-result item-active-property compacted-item

437423
                 (else
438424
                   ;; 12.8.8.4.1
439425
                   (set! compacted-item
440-
                     `((,(iri-compaction active-context inverse-context "@graph"
441-
                                         #:vocab? #t #:processing-mode processing-mode) .
426+
                     `((,(iri-compaction active-context "@graph"
427+
                                         #:vocab? #t
428+
                                         #:processing-mode processing-mode) .
442429
                        ,compacted-item)))
443430
                   ;; 12.8.8.4.2
444431
                   (when (json-has-key? expanded-item "@id")
445432
                     (set! compacted-item
446433
                       (alist-set
447434
                         compacted-item
448-
                         (iri-compaction active-context inverse-context "@id"
435+
                         (iri-compaction active-context "@id"
449436
                                         #:vocab? #t
450437
                                         #:processing-mode processing-mode)
451-
                         (iri-compaction active-context inverse-context
438+
                         (iri-compaction active-context
452439
                                         (assoc-ref expanded-item "@id")
453440
                                         #:vocab? #f
454441
                                         #:processing-mode processing-mode))))

457444
                     (set! compacted-item
458445
                       (alist-set
459446
                         compacted-item
460-
                         (iri-compaction active-context inverse-context "@index"
447+
                         (iri-compaction active-context "@index"
461448
                                         #:vocab? #t
462449
                                         #:processing-mode processing-mode)
463450
                         (assoc-ref expanded-item "@index"))))

476463
               (let* ((map-object (or (assoc-ref nest-result item-active-property)
477464
                                      '()))
478465
                      ;; 12.8.9.2
479-
                      (container-key (iri-compaction active-context inverse-context
466+
                      (container-key (iri-compaction active-context
480467
                                                     (cond
481468
                                                       ((member "@language" container) "@language")
482469
                                                       ((member "@index" container) "@index")

505492
                   ((member "@index" container)
506493
                    ;; 12.8.9.6.1
507494
                    (set! container-key
508-
                      (iri-compaction active-context inverse-context index-key
495+
                      (iri-compaction active-context
496+
                                      (assoc-ref
497+
                                        (iri-expansion active-context index-key
498+
                                                       #:vocab? #t)
499+
                                        "iri")
509500
                                      #:vocab? #t))
510501
                    ;; 12.8.9.6.2
511502
                    (let* ((keys (assoc-ref compacted-item container-key))

561552
                                   "@id"))
562553
                        (set! compacted-item
563554
                          (compaction
564-
                            active-context inverse-context item-active-property
555+
                            active-context item-active-property
565556
                            `(("@id" . ,(assoc-ref expanded-item "@id")))))))))
566557
                 ;; 12.8.9.9
567558
                 (when (json-null? map-key)
568-
                   (set! map-key (iri-compaction active-context inverse-context
569-
                                                 "@none" #:vocab? #t)))
559+
                   (set! map-key (iri-compaction active-context "@none"
560+
                                                 #:vocab? #t)))
570561
                 ;; 12.8.9.10
571562
                 (set! map-object
572563
                   (add-value map-object map-key compacted-item as-array?))

jsonld/context-processing.scm

3838
  "Process a context.  This is an implementation of the context processing
3939
algorithm defined in the JsonLD API specification.
4040
41-
See @url{https://www.w3.org/TR/2014/REC-json-ld-api-20140116}."
41+
See @url{https://w3c.github.io/json-ld-api/#context-processing-algorithm}."
4242
  ;; 1
43-
  (define result active-context)
43+
  (define result (update-active-context active-context #:inverse-context json-null))
4444
  ;; 2
4545
  (when (json-has-key? local-context "@propagate")
4646
    (set! propagate? (assoc-ref local-context "@propagate")))

jsonld/context.scm

2424
            active-context-definitions
2525
            active-context-base
2626
            active-context-original-base
27+
            active-context-inverse-context
2728
            active-context-vocab
2829
            active-context-language
2930
            active-context-direction

5354
;;; Section 4.1: definitions of an Active Context and a Term Definition
5455
5556
(define-record-type active-context
56-
  (make-active-context definitions base original-base vocab language direction
57-
                       previous)
57+
  (make-active-context definitions base original-base inverse-context vocab
58+
                       language direction previous)
5859
  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))
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))
6668
6769
(define* (update-active-context
6870
           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))
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))
7881
7982
(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))
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))
8993
9094
(define-record-type term-definition
9195
  (make-term-definition iri prefix? protected? reverse? base-url context container

jsonld/deserialize-jsonld.scm

5454
(define* (deserialize-jsonld generate-blank-node node-map dataset
5555
                             #:key produce-generalized-rdf? rdf-direction)
5656
  ;; 1
57-
  (pk 'node-map node-map)
5857
  (for-each-pair
5958
    (lambda (graph-name graph)
6059
      ;; 1.1

jsonld/iri-compaction.scm

1818
(define-module (jsonld iri-compaction)
1919
  #:use-module (iri iri)
2020
  #:use-module (jsonld context)
21+
  #:use-module (jsonld inverse-context-creation)
2122
  #:use-module (jsonld json)
2223
  #:use-module (jsonld term-selection)
2324
  #:export (iri-compaction))

2829
     (unless var
2930
       (set! var val)))))
3031
31-
(define* (iri-compaction active-context inverse-context var
32+
(define* (iri-compaction active-context var
3233
                         #:key value vocab? reverse? processing-mode)
3334
  ;; not specified, but sometimes the spec leads to var being an array, so
3435
  ;; in that case we iri-compact each part of it and return an array.
3536
  (cond
36-
    ;; 2
37+
    ;; 1
3738
    ((json-null? var) json-null)
39+
    ;; XXX: addition to spec
3840
    ((json-array? var)
3941
     (list->array 1
4042
       (map
4143
         (lambda (val)
42-
           (iri-compaction active-context inverse-context val
44+
           (iri-compaction active-context val
4345
                           #:value value
4446
                           #:vocab? vocab?
4547
                           #:reverse? reverse?

4749
         (array->list var))))
4850
    (else
4951
     (begin
50-
       (let ((result #f))
51-
         (pk 'inverse (assoc-ref inverse-context var))
52-
         ;; 2
52+
       ;; 2
53+
       (when (json-null? (active-context-inverse-context active-context))
54+
         (set! active-context
55+
           (update-active-context active-context
56+
                                  #:inverse-context
57+
                                  (inverse-context-creation active-context))))
58+
       ;; 3
59+
       (let* ((inverse-context (active-context-inverse-context active-context))
60+
              (result #f))
61+
         ;; 4
5362
         (when (and vocab? (json-has-key? inverse-context var))
5463
           (let ((default-language
55-
                   ;; 2.1
56-
                   (if (or (not-null-or-false (active-context-direction active-context))
57-
                           (not-null-or-false (active-context-language active-context)))
64+
                   ;; 4.1
65+
                   (if (not-null-or-false (active-context-direction active-context))
5866
                       (string-append
59-
                         (or (not-null-or-false (active-context-language active-context))
60-
                             "")
67+
                         (string-downcase
68+
                           (or (not-null-or-false (active-context-language active-context))
69+
                               ""))
6170
                         "_"
62-
                         (or (not-null-or-false (active-context-direction active-context))
63-
                             ""))
64-
                       "@none"))
65-
                 ;; 2.3
71+
                         (active-context-direction active-context))
72+
                       (string-downcase
73+
                         (or (not-null-or-false (active-context-language active-context))
74+
                             "@none"))))
75+
                 ;; 4.3
6676
                 (containers '())
67-
                 ;; 2.4
77+
                 ;; 4.4
6878
                 (type/language "@language")
6979
                 (type/language-value "@null")
70-
                 ;; 2.14
80+
                 ;; 4.14
7181
                 (preferred-values '()))
72-
             ;; 2.2
82+
             ;; 4.2
7383
             (when (json-has-key? value "@preserve")
74-
               (set! value (assoc-ref value "@preserve")))
75-
             ;; 2.5
84+
               (let ((preserve (assoc-ref value "@preserve")))
85+
                 (set! value
86+
                   (if (json-array? preserve)
87+
                       (car (array->list preserve))
88+
                       preserve))))
89+
             ;; 4.5
7690
             (when (and (json-has-key? value "@index")
7791
                        (not (graph-object? value)))
7892
               (set! containers
7993
                 (append containers '("@index" "@index@set"))))
8094
             (cond
81-
               ;; 2.6
95+
               ;; 4.6
8296
               (reverse?
8397
                 (set! type/language "@type")
8498
                 (set! type/language-value "@reverse")
8599
                 (set! containers (append containers '("@set"))))
86-
               ;; 2.7
100+
               ;; 4.7
87101
               ((list-object? value)
88-
                ;; 2.7.1
102+
                ;; 4.7.1
89103
                (unless (json-has-key? value "@index")
90104
                  (set! containers (append containers '("@list"))))
91-
                ;; 2.7.2
105+
                ;; 4.7.2
92106
                (let ((lst (array->list (assoc-ref value "@list")))
93-
                      ;; 2.7.3
107+
                      ;; 4.7.3
94108
                      (common-type json-null)
95109
                      (common-language json-null))
96110
                  (when (null? lst)
97111
                    (set! common-language default-language))
98-
                  ;; 2.7.4
112+
                  ;; 4.7.4
99113
                  (for-each
100114
                    (lambda (item)
101-
                      ;; 2.7.4.1
115+
                      ;; 4.7.4.1
102116
                      (let ((item-language "@none")
103117
                            (item-type "@none"))
104118
                        (if (json-has-key? item "@value")
105-
                            ;; 2.7.4.2
119+
                            ;; 4.7.4.2
106120
                            (cond
107121
                              ((json-has-key? item "@direction")
108122
                               (set! item-language

116130
                               (set! item-type (assoc-ref item "@type")))
117131
                              (else
118132
                                (set! item-language "@null")))
119-
                            ;; 2.7.4.3
133+
                            ;; 4.7.4.3
120134
                            (set! item-type "@id"))
121-
                        ;; 2.7.4.4
135+
                        ;; 4.7.4.4
122136
                        (if (json-null? common-language)
123137
                            (set! common-language item-language)
124138
                            (unless (or (equal? common-language item-language)
125139
                                        (not (json-has-key? item "@value")))
126140
                              (set! common-language "@none")))
127-
                        ;; 2.7.4.6
141+
                        ;; 4.7.4.6
128142
                        (if (json-null? common-type)
129143
                            (set! common-type item-type)
130144
                            (unless (equal? common-type item-type)
131145
                              (set! common-type "@none")))))
132146
                    lst)
133-
                  ;; 2.7.5
147+
                  ;; 4.7.5
134148
                  (when (json-null? common-language)
135149
                    (set! common-language "@none"))
136150
                  (when (json-null? common-type)

140154
                        (set! type/language "@type")
141155
                        (set! type/language-value common-type))
142156
                      (set! type/language-value common-language))))
143-
               ;; 2.8
157+
               ;; 4.8
144158
               ((graph-object? value)
145-
                ;; 2.8.1
159+
                ;; 4.8.1
146160
                (when (json-has-key? value "@index")
147161
                  (set! containers
148162
                    (append containers '("@graph@index" "@graph@index@set"))))
149-
                ;; 2.8.2
163+
                ;; 4.8.2
150164
                (when (json-has-key? value "@id")
151165
                  (set! containers
152166
                    (append containers '("@graph@id" "@graph@id@set"))))
153-
                ;; 2.8.3
167+
                ;; 4.8.3
154168
                (set! containers
155169
                  (append containers '("@graph" "@graph@set" "@set")))
156-
                ;; 2.8.4
170+
                ;; 4.8.4
157171
                (unless (json-has-key? value "@index")
158172
                  (set! containers
159173
                    (append containers '("@graph@index" "@graph@index@set"))))
160-
                ;; 2.8.5
174+
                ;; 4.8.5
161175
                (unless (json-has-key? value "@id")
162176
                  (set! containers
163177
                    (append containers '("@graph@id" "@graph@id@set"))))
164-
                ;; 2.8.6
178+
                ;; 4.8.6
165179
                (set! containers
166180
                  (append containers '("@index" "@index@set")))
167-
                ;; 2.8.7
181+
                ;; 4.8.7
168182
                (set! type/language "@type")
169183
                (set! type/language-value "@id"))
170-
               ;; 2.9.1 and 2.9.3
184+
               ;; 4.9.1 and 4.9.3
171185
               ((json-has-key? value "@value")
172186
                (cond
173187
                  ((and (json-has-key? value "@direction")

188202
                   (set! type/language-value (assoc-ref value "@type"))
189203
                   (set! type/language "@type")))
190204
                (set! containers (append containers '("@set"))))
191-
               ;; 2.9.2 and 2.9.3
205+
               ;; 4.9.2 and 4.9.3
192206
               (else
193207
                (set! type/language "@type")
194208
                (set! type/language-value "@id")
195209
                (set! containers
196210
                  (append containers '("@id" "@id@set" "@type" "@set@type" "@set")))))
197-
             ;;2.10
211+
             ;; 4.10
198212
             (set! containers (append containers '("@none")))
199-
             ;; 2.11
213+
             ;; 4.11
200214
             (unless (processing-mode-1.0? processing-mode)
201215
               (unless (json-has-key? value "@index")
202216
                 (set! containers (append containers '("@index" "@index@set"))))
203-
               ;; 2.12
217+
               ;; 4.12
204218
               (when (and (json-has-key? value "@value")
205219
                          (null? (filter (lambda (kp) (not (equal? (car kp) "@value")))
206220
                                         value)))
207221
                 (set! containers (append containers '("@language" "@language@set")))))
208-
             ;; 2.13
222+
             ;; 4.13
209223
             (when (equal? type/language-value json-null)
210224
               (set! type/language-value "@null"))
211-
             ;; 2.15
225+
             ;; 4.15
212226
             (when (equal? type/language-value "@reverse")
213227
               (set! preferred-values (append preferred-values '("@reverse"))))
214-
             ;; 2.16
228+
             ;; 4.16
215229
             (if (and (or (equal? type/language-value "@reverse")
216230
                          (equal? type/language-value "@id"))
217231
                      (json-has-key? value "@id"))
218-
               (let* ((compacted-iri (iri-compaction active-context inverse-context
232+
               (let* ((compacted-iri (iri-compaction active-context
219233
                                                     (assoc-ref value "@id")
220234
                                                     #:vocab? #t
221235
                                                     #:processing-mode processing-mode))

228242
                       (append preferred-values '("@vocab" "@id" "@none")))
229243
                     (set! preferred-values
230244
                       (append preferred-values '("@id" "@vocab" "@none")))))
231-
               ;; 2.17
245+
               ;; 4.17
232246
               (begin
233247
                 (when (equal? (assoc-ref value "@list") #())
234248
                   (set! type/language "@any"))
235249
                 (set! preferred-values
236250
                   (append preferred-values (list type/language-value "@none")))))
237-
             ;; 2.18
251+
             ;; 4.18
238252
             (set! preferred-values (append preferred-values '("@any")))
239-
             ;; 2.19
253+
             ;; 4.19
240254
             (let* ((underscore-vals (filter
241255
                                       (lambda (s)
242256
                                         (string-index s #\_))

249263
                     (list
250264
                       (string-join
251265
                         (cons "" (cdr (string-split underscore #\_))) "_"))))))
252-
             ;; 2.20
253-
             (let ((term (term-selection inverse-context var containers type/language preferred-values)))
266+
             ;; 4.20
267+
             (let ((term (term-selection active-context var containers
268+
                                         type/language preferred-values)))
254269
               (when (not-null-or-false term)
255270
                 (set-cond! result term)))))
256-
         ;; 3
271+
         ;; 5
257272
         (when (and vocab? (not-null-or-false (active-context-vocab active-context)))
258273
           (let ((vocab (active-context-vocab active-context)))
259-
             (pk 'vocab vocab var)
260274
             (when (and (>= (string-length var) (string-length vocab))
261275
                        (equal? (substring var 0 (string-length vocab)) vocab))
262276
               (let ((suffix (substring var (string-length vocab))))
263277
                 (when (not (not-null-or-false (term-definition-ref active-context suffix)))
264278
                   (set-cond! result suffix))))))
265-
         ;; 4
279+
         ;; 6
266280
         (let ((compact-iri json-null))
267-
           ;; 5
281+
           ;; 7
268282
           (for-each-pair
269283
             (lambda (term def)
270-
               ;; 5.1
284+
               ;; 7.1
271285
               (unless (or (json-null? (term-definition-iri def))
272286
                           (equal? (term-definition-iri def) var)
273287
                           (not (string? var))

278292
                                    (string-length (term-definition-iri def)))
279293
                                  (term-definition-iri def)))
280294
                           (not (term-definition-prefix? def)))
281-
                 ;; 5.2
295+
                 ;; 7.2
282296
                 (let ((candidate (string-append term ":"
283297
                                                 (substring
284298
                                                   var
285299
                                                   (string-length
286300
                                                     (term-definition-iri def))))))
287-
                   ;; 5.3
301+
                   ;; 7.3
288302
                   (when (or (json-null? compact-iri)
289303
                             (< (string-length candidate) (string-length compact-iri))
290304
                             (and (= (string-length candidate)

296310
                                      (equal? (term-definition-iri def) var)))
297311
                         (set! compact-iri candidate)))))))
298312
             (active-context-definitions active-context))
299-
           ;; 6
313+
           ;; 8
300314
           (when (not-null-or-false compact-iri)
301315
             (set-cond! result compact-iri)))
302-
         ;; 7
316+
         ;; 9
303317
         (unless result
304318
           (let* ((components (string-split var #\:))
305319
                  (prefix (car components))

313327
               (unless (and (> (string-length suffix) 2)
314328
                            (equal? (substring suffix 0 2) "//"))
315329
                 (throw 'iri-confused-with-prefix)))))
316-
         ;; 8
330+
         ;; 10
317331
         (unless vocab?
318332
           (when (and (not-null-or-false (active-context-base active-context))
319333
                      (absolute-iri? var))
320-
             (set-cond! result
321-
               (make-relative-iri var (active-context-base active-context)))))
322-
         ;; 9
334+
             (let ((iri (make-relative-iri var (active-context-base active-context))))
335+
               (if (keyword-form? iri)
336+
                   (set-cond! result (string-append "./" iri))
337+
                   (set-cond! result iri)))))
338+
         ;; 11
323339
         (set-cond! result var)
324340
         result)))))

jsonld/json.scm

5050
            node-object?
5151
            not-null-or-false
5252
            processing-mode-1.0?
53+
            processing-mode-1.1?
5354
            relative-iri?
5455
            same-json?
5556
            identical-json?

213214
(define (processing-mode-1.0? mode)
214215
  (member mode '("jsonld-1.0" "json-ld-1.0")))
215216
217+
(define (processing-mode-1.1? mode)
218+
  (member mode '("jsonld-1.1" "json-ld-1.1")))
219+
216220
(define (node-object? o)
217221
  (and (json-object? o)
218222
       (not (json-has-key? o "@value"))

jsonld/options.scm

7373
                             (extract-all-scripts? #f)
7474
                             (frame-expansion? #f)
7575
                             (ordered? #f)
76-
                             (processing-mode "jsonld-1.1")
76+
                             (processing-mode "json-ld-1.1")
7777
                             (produce-generalized-rdf? #t)
7878
                             (rdf-direction #f)
7979
                             (use-native-types? #f)

jsonld/term-selection.scm

1616
;;;; 
1717
1818
(define-module (jsonld term-selection)
19+
  #:use-module (jsonld context)
20+
  #:use-module (jsonld inverse-context-creation)
1921
  #:use-module (jsonld json)
2022
  #:export (term-selection))
2123
22-
(define* (term-selection inverse-context var containers type/language
24+
(define* (term-selection active-context var containers type/language
2325
                         preferred-values)
24-
  (let ((container-map (assoc-ref inverse-context var))
26+
  ;; 1
27+
  (when (json-null? (active-context-inverse-context active-context))
28+
    (set! active-context
29+
      (update-active-context active-context
30+
                             #:inverse-context
31+
                             (inverse-context-creation active-context))))
32+
  (let ((container-map (assoc-ref (active-context-inverse-context active-context) var))
2533
        (result json-null))
26-
    ;; 2
34+
    ;; 4
2735
    (for-each
2836
      (lambda (container)
29-
        ;; 2.1
37+
        ;; 4.1
3038
        (when (json-has-key? container-map container)
31-
          ;; 2.2
39+
          ;; 4.2
3240
          (let* ((type/language-map (assoc-ref container-map container))
33-
                 ;; 2.3
41+
                 ;; 4.3
3442
                 (value-map (assoc-ref type/language-map type/language)))
3543
            (for-each
3644
              (lambda (item)

3947
                    (set! result (assoc-ref value-map item)))))
4048
              preferred-values))))
4149
      containers)
42-
    ;; 3
50+
    ;; 5
4351
    result))

jsonld/value-compaction.scm

3232
        (array->list container-mapping)
3333
        (list container-mapping))))
3434
35-
(define (value-compaction active-context inverse-context active-property value
36-
                          processing-mode)
35+
(define (value-compaction active-context active-property value processing-mode)
3736
  (let* ((result value)
3837
         (active-def (term-definition-ref active-context active-property))
3938
         ;; 2

5857
       (cond
5958
         ((and (term-definition? active-def)
6059
               (equal? (term-definition-type active-def) "@id"))
61-
          (set! result (iri-compaction active-context inverse-context
60+
          (set! result (iri-compaction active-context
6261
                                       (assoc-ref value "@id") #:vocab? #f
6362
                                       #:processing-mode processing-mode)))
6463
         ((and (term-definition? active-def)
6564
               (equal? (term-definition-type active-def) "@vocab"))
66-
          (set! result (iri-compaction active-context inverse-context
65+
          (set! result (iri-compaction active-context
6766
                                       (assoc-ref value "@id") #:vocab? #t
6867
                                       #:processing-mode processing-mode)))))
6968
      ;; 5

7776
                (equal? (term-definition-type active-def) "@none"))
7877
           (json-has-key? value "@type"))
7978
       (when (json-has-key? result "@type")
80-
         (let ((type (iri-compaction active-context inverse-context
79+
         (let ((type (iri-compaction active-context
8180
                                                    (assoc-ref result "@type")
8281
                                                    #:vocab? #t
8382
                                                    #:processing-mode

121120
        (map
122121
          (lambda (kv)
123122
            (cons
124-
              (iri-compaction active-context inverse-context (car kv)
125-
                             #:vocab? #t)
123+
              (iri-compaction active-context (car kv) #:vocab? #t)
126124
              (cdr kv)))
127125
          result)))
128126
    ;; 10

tests/tordf.scm unknown status 2

1-
#!/data/tyreunom/projects/guile-jsonld/pre-inst-env guile
2-
!#
3-
;;;; Copyright (C) 2020 Julien Lepiller <julien@lepiller.eu>
4-
;;;; 
5-
;;;; This library is free software; you can redistribute it and/or
6-
;;;; modify it under the terms of the GNU Lesser General Public
7-
;;;; License as published by the Free Software Foundation; either
8-
;;;; version 3 of the License, or (at your option) any later version.
9-
;;;; 
10-
;;;; This library is distributed in the hope that it will be useful,
11-
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12-
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13-
;;;; Lesser General Public License for more details.
14-
;;;; 
15-
;;;; You should have received a copy of the GNU Lesser General Public
16-
;;;; License along with this library; if not, write to the Free Software
17-
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18-
;;;; 
19-
20-
(use-modules (test-modules online))
21-
(use-modules (test-modules result))
22-
(use-modules (test-modules testsuite))
23-
24-
(run-test-suite tordf-test-url expected-failures tap-driver)