summaryrefslogtreecommitdiff
path: root/schemantic/rdf.scm
blob: 68329a735213b3a685fb3e34654cc568317bc371 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
; SPDX-FileCopyrightText: 2020 pukkamustard <pukkamustard@posteo.net>
;
; SPDX-License-Identifier: GPL-3.0-or-later

(define-module (schemantic rdf)
  #:use-module (oop goops)
 
  #:use-module (oop goops describe)     ; only for dev/debug

  #:use-module (web uri)
  #:use-module (ice-9 optargs)
  #:use-module (ice-9 exceptions)

  #:export (<iri>
            make-iri
            iri?
            iri-value

            <literal>
            literal?
            literal-value
            literal-lexical
            literal-canonical
            literal-datatype

            <triple>
            make-triple
            triple?
            triple-subject
            triple-predicate
            triple-object

            define-namespace))

;; IRI

(define-class <iri> ()
  (value #:init-keyword #:value #:getter iri-value))

(define-method (initialize (self <iri>) initargs)
  (let-keywords initargs #f (value)
                (cond
                 ((string? value) (next-method self (list #:value value)))
                 ((uri? value) (next-method self (list #:value (uri->string value))))
                 ((iri? value) (next-method self (list #:value (iri-value value))))
                 (else (raise-exception
                        (make-exception
                         (make-programming-error)
                         (make-exception-with-message "can not cast to iri")))))))

(define-method (equal? (x <iri>) (y <iri>))
  (equal? (iri-value x) (iri-value y)))

(define (make-iri value)
  "Returns a new iri with value"
  (make <iri> #:value value))

(define (iri? x)
  "Returns true if x is an iri"
  (is-a? x <iri>))

;; Literal

(define-class <literal> ())

(define (literal? x)
  (is-a? x <literal>))

(define-generic literal-value)
(define-generic literal-lexical)
(define-generic literal-canonical)
(define-generic literal-datatype)


;; Triple

(define-class <triple> ()
  (subject #:init-keyword #:s #:getter triple-subject)
  (predicate #:init-keyword #:p #:getter triple-predicate)
  (object #:init-keyword '#:o #:getter triple-object))

(define-method (initialize (t <triple>) initargs)
  (next-method))

(define (make-triple s p o)
  (make <triple> #:s s #:p p #:o o))

(define (triple? x)
  (is-a? x <triple>))

;; Syntax for defining new namespace

(define-syntax-rule (define-namespace name uri)
  (define (name id)
    (make-iri (string-append uri id))))