Add rdf vocabulary type and use it in entailments

Julien LepillerSun Apr 05 03:59:21+0200 2020

d19e12a

Add rdf vocabulary type and use it in entailments

rdf/entailment/rdf.scm

2626
(define (rdf-iri name)
2727
  (string-append "http://www.w3.org/1999/02/22-rdf-syntax-ns#" name))
2828
29-
(define (consistent-graph? graph)
30-
  (define (non-overlapping-types? graph)
29+
(define (consistent-graph? graph vocabulary)
30+
  (define (alist-set lst key val)
31+
    (match lst
32+
      (() (list (cons key val)))
33+
      (((k . v) lst ...)
34+
       (if (equal? k key)
35+
           (cons (cons key val) lst)
36+
           (cons (cons k v) (alist-set lst key val))))))
37+
38+
  (define (compatible? types)
39+
    (match types
40+
      (() #t)
41+
      ((_) #t)
42+
      ((a b ...)
43+
       (and (null? (filter
44+
                     (lambda (t)
45+
                       ((rdf-vocabulary-compatible? vocabulary) a t))
46+
                     b))
47+
            (compatible? b)))))
48+
49+
  (define (compatible-types? graph)
3150
    (let loop ((graph graph) (type-mappings '()))
3251
      (if (null? graph)
33-
          #t
52+
          (null?
53+
            (filter
54+
              (lambda (t)
55+
                (not (compatible (cdr t))))
56+
              type-mappings))
3457
          (let* ((t (car graph)))
3558
            (if (equal? (rdf-triple-predicate t) (rdf-iri "type"))
36-
                (if (assoc-ref type-mappings (rdf-triple-subject t))
37-
                    #f
38-
                    (loop (cdr graph)
39-
                          (cons
40-
                            (cons (rdf-triple-subject t) (rdf-triple-object t))
41-
                            type-mappings)))
42-
                (loop (cdr graph) type-mappings))))))
59+
                (loop
60+
                  (cdr graph)
61+
                  (alist-set type-mappings (rdf-triple-subject t)
62+
                             (cons (rdf-triple-object t)
63+
                                   (or
64+
                                     (assoc-ref type-mappings
65+
                                                (rdf-triple-subject t))
66+
                                     '())))))))))
4367
  (and (d:consistent-graph? graph)
44-
       (non-overlapping-types? graph)))
68+
       (compatible-types? graph)))
4569
4670
;; G entails E if E has an instance (where blank nodes are replaced by literals
4771
;; or IRIs) that is a subgraph of G.

164188
                        g2)))
165189
          (validate-mapping mapping g1 g2)))))
166190
167-
(define (entails? g e)
191+
(define (entails? g e vocabulary)
168192
  "Return true if g entails e"
169-
  (or (not (consistent-graph? g))
170-
      (d:entails? (augment g) e)))
193+
  (let ((g (recognize g vocabulary)))
194+
    (or (not (consistent-graph? g vocabulary))
195+
        (d:entails? (augment g) e))))

rdf/entailment/rdfs.scm

3131
(define (rdfs-iri name)
3232
  (string-append "http://www.w3.org/2000/01/rdf-schema#" name))
3333
34-
(define (consistent-graph? graph)
34+
(define (consistent-graph? graph vocabulary)
3535
  (define (valid-subclasses? graph)
3636
    (match graph
3737
      (() #t)
3838
      ((($ rdf-triple (? rdf-datatype? s) p (? rdf-datatype? o)) graph ...)
3939
       (if (is-iri? p (rdfs-iri "subClassOf"))
40-
           (and (xsd:order s o)
40+
           (and ((rdf-vocabulary-order vocabulary) s o)
4141
                (valid-subclasses? graph))
4242
           (valid-subclasses? graph)))
4343
      ((_ graph ...)
4444
       (valid-subclasses? graph))))
4545
  (and (valid-subclasses? graph)
46-
       (rdf:consistent-graph? graph)))
46+
       (rdf:consistent-graph? graph vocabulary)))
4747
4848
;; G entails E if E has an instance (where blank nodes are replaced by literals
4949
;; or IRIs) that is a subgraph of G.

311311
                     (inner-loop (cons t graph) subclasses subprops
312312
                                 ranges domains types #t augment-set)))))))))))
313313
314-
(define (entails? g e d)
314+
(define (entails? g e vocabulary)
315315
  "Return true if g entails e recognizing d"
316-
  (let* ((g (recognize g d)))
317-
    (or (not (consistent-graph? g))
318-
        (d:entails? (augment g d) (recognize e d)))))
316+
  (let* ((g (recognize g vocabulary)))
317+
    (or (not (consistent-graph? g vocabulary))
318+
        (d:entails? (augment g (rdf-vocabulary-datatypes vocabulary))
319+
                    (recognize e vocabulary)))))

rdf/rdf.scm

3333
            rdf:langString
3434
            rdf:XMLLiteral
3535
36+
            rdf-vocabulary
37+
            make-rdf-vocabulary
38+
            rdf-vocabulary?
39+
            rdf-vocabulary-datatypes
40+
            rdf-vocabulary-order
41+
            rdf-vocabulary-compatible?
42+
3643
            rdf-dataset
3744
            make-rdf-dataset
3845
            rdf-dataset?

120127
    xml->sxml
121128
    sxml->xml))
122129
130+
;; In addition to the specification, we define a vocabulary, which will be
131+
;; passed to entailments that need one.
132+
;;
133+
;; datatypes: a list of <rdf-datatype> records.
134+
;; order: a procedure that takes two arguments and returns whether the value
135+
;;        space of the firts is included in the value space of the second
136+
;; compatible?: a procedure that takes two arguments and returns whether the
137+
;;              intersection of their value space is not empty
138+
139+
(define-record-type rdf-vocabulary
140+
  (make-rdf-vocabulary datatypes order compatible?)
141+
  rdf-vocabulary?
142+
  (datatypes   rdf-vocabulary-datatypes)
143+
  (order       rdf-vocabulary-order)
144+
  (compatible? rdf-vocabulary-compatible?))
145+
123146
;; From the specification:
124147
;;   An RDF dataset is a collection of RDF graphs, and comprises:
125148
;;

438461
       predicate
439462
       (recognize-data object datatypes)))))
440463
441-
(define (recognize graph datatypes)
464+
(define (recognize graph vocabulary)
442465
  (match graph
443466
    (() '())
444467
    ((t graph ...)
445468
     (cons
446-
       (recognize-triple t datatypes)
447-
       (recognize graph datatypes)))))
469+
       (recognize-triple t (rdf-vocabulary-datatypes vocabulary))
470+
       (recognize graph vocabulary)))))
448471

rdf/xsd.scm

1818
(define-module (rdf xsd)
1919
  #:use-module (ice-9 match)
2020
  #:use-module (rdf rdf)
21-
  #:export (datatypes order))
21+
  #:export (datatypes order compatible?))
2222
2323
;; This module implements the xsd datatypes, as presented in https://www.w3.org/TR/rdf11-concepts/#xsd-datatypes
2424

108108
(define (order d1 d2)
109109
  "Return whether d1's value space is included in d2's"
110110
  (member d1 (assoc-ref sub-classes d2)))
111+
112+
;; TODO: this is not entirely correct
113+
(define (compatible? d1 d2)
114+
  (or (order d1 d2) (order d2 d1)))

test-modules/online.scm

167167
                             (loop types))))))
168168
                  recognized))
169169
              (recognized (pk 'reco (append (list xsd:string rdf:langString) recognized)))
170+
              (vocabulary (make-rdf-vocabulary recognized xsd:order xsd:compatible?))
170171
              (expected
171172
                (car
172173
                  (get-objects

184185
         (match regime
185186
           ("simple"
186187
            (if (if (equal? expected #f)
187-
                    (simple:consistent-graph? result)
188+
                    (not (simple:consistent-graph? result))
188189
                    (simple:entails? result expected))
189190
                (if (equal? type "PositiveEntailmentTest")
190191
                    (update-test-case test #:result 'pass)

198199
                    (update-test-case test #:result 'pass))))
199200
           ("RDF"
200201
            (if (if (equal? expected #f)
201-
                    (rdf:consistent-graph? result)
202-
                    (rdf:entails? (recognize result recognized)
203-
                                  (recognize expected recognized)))
202+
                    (not (rdf:consistent-graph? result vocabulary))
203+
                    (rdf:entails? result expected vocabulary))
204204
                (if (equal? type "PositiveEntailmentTest")
205205
                    (update-test-case test #:result 'pass)
206206
                    (update-test-case test

213213
                    (update-test-case test #:result 'pass))))
214214
           ("RDFS"
215215
            (if (if (equal? expected #f)
216-
                    (not (rdfs:consistent-graph? result))
217-
                    (rdfs:entails? result (pk 'expected expected) recognized))
216+
                    (not (rdfs:consistent-graph? result vocabulary))
217+
                    (rdfs:entails? result expected vocabulary))
218218
                (if (equal? type "PositiveEntailmentTest")
219219
                    (update-test-case test #:result 'pass)
220220
                    (update-test-case test