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