summaryrefslogtreecommitdiff
path: root/tests/schemantic/rdf/datalog.scm
blob: 5526b8f44eb763a3cb6070394a66784e5f63c361 (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
(define-module (tests schemantic rdf datalog)
  #:use-module (schemantic rdf)
  #:use-module ((schemantic ns) #:select (rdf))
  #:use-module (schemantic serialization turtle)
  #:use-module (schemantic graph vhash)
  #:use-module (schemantic rdf datalog)

  #:use-module (datalog)
  #:use-module (datalog vhash-set)

  #:use-module (ice-9 vlist)
  #:use-module (ice-9 textual-ports)    ; for reading ttl file

  #:use-module (srfi srfi-64)
  #:use-module (srfi srfi-171))

(test-begin "Include RDF Graph into Datalog program")

(define clowns (call-with-input-file "tests/data/clowns.ttl" get-string-all))

;; make a new RDF graph and load the turtle file into the graph
(define graph (make-vhash-graph))
(turtle-transduce (tmap identity)
                  (graph-radd graph)
                  clowns
                  (make-iri "urn:base"))

;; Helper to use the ActivityStreams namespace
(define-namespace as "https://www.w3.org/ns/activitystreams#")

;; This program will evaluate to all the ids of Notes attributed to Alice.
(define program
  (list
   (:- (notes (lvar 's))
    (graph (lvar 's) (rdf "type") (as "Note"))
    (graph (lvar 's) (as "attributedTo") (make-iri "https://test.example/alice")))))

(define notes
  (cdr (vhash-assoc 'notes
                    (datalog-eval
                     program
                     #:edb (add-rdf-graph-to-edb 'graph graph vlist-null)))))

(test-eq "exactly one note attribtued to Alice" 1 (set-size notes))

(test-assert "Correct note is in results"
    (set-contains? notes (vector (make-iri "https://test.example/notes/1"))))

(test-end)