guile-jsonld/jsonld/term-selection.scm

term-selection.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 term-selection)
19
  #:use-module (jsonld context)
20
  #:use-module (jsonld inverse-context-creation)
21
  #:use-module (jsonld json)
22
  #:export (term-selection))
23
24
(define* (term-selection active-context var containers type/language
25
                         preferred-values)
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))
33
        (result json-null))
34
    ;; 4
35
    (for-each
36
      (lambda (container)
37
        ;; 4.1
38
        (when (json-has-key? container-map container)
39
          ;; 4.2
40
          (let* ((type/language-map (assoc-ref container-map container))
41
                 ;; 4.3
42
                 (value-map (assoc-ref type/language-map type/language)))
43
            (for-each
44
              (lambda (item)
45
                (when (json-has-key? value-map item)
46
                  (unless (not-null-or-false result)
47
                    (set! result (assoc-ref value-map item)))))
48
              preferred-values))))
49
      containers)
50
    ;; 5
51
    result))
52