summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpukkamustard <pukkamustard@posteo.net>2020-09-04 16:39:56 +0200
committerpukkamustard <pukkamustard@posteo.net>2020-09-04 16:39:56 +0200
commite65ac1caf77e8ef6ca63ab0f67efb6e2884e8196 (patch)
treecddffa353dd25e943bced0c4294c5a9a85150d2d
parentf3a132c2943d05c09c86b94638ae50e64c1985bb (diff)
(schematic rdf): add <graph-query>
-rw-r--r--schemantic/graph/vhash.scm6
-rw-r--r--schemantic/rdf.scm76
-rw-r--r--tests/schemantic/rdf.scm35
3 files changed, 104 insertions, 13 deletions
diff --git a/schemantic/graph/vhash.scm b/schemantic/graph/vhash.scm
index fa6550a..8935029 100644
--- a/schemantic/graph/vhash.scm
+++ b/schemantic/graph/vhash.scm
@@ -12,7 +12,8 @@
#:use-module (ice-9 vlist)
#:use-module (ice-9 match)
- #:export (<vhash-graph>))
+ #:export (<vhash-graph>
+ make-vhash-graph))
(define-class <vhash-graph> (<graph>)
;; NOTE more indices (ops and pso) could be added to increase performance of certain queries
@@ -21,6 +22,9 @@
(define-method (initialize (g <vhash-graph>) initargs)
(set! (spo g) vlist-null))
+(define (make-vhash-graph)
+ (make <vhash-graph>))
+
(define-method (graph-add (g <vhash-graph>) (t <triple>))
(let ((s (triple-subject t))
(p (triple-predicate t))
diff --git a/schemantic/rdf.scm b/schemantic/rdf.scm
index cc8a0a9..5253572 100644
--- a/schemantic/rdf.scm
+++ b/schemantic/rdf.scm
@@ -10,6 +10,8 @@
#:use-module (schemantic xsd)
#:use-module (schemantic rdf lang-string)
+ #:use-module (ice-9 format)
+
#:export (<triple>
make-triple
triple?
@@ -27,11 +29,9 @@
graph-radd
graph-match
- <description>
- description?
- description-subject
-
- rdf-ref)
+ <graph-query>
+ graph-query?
+ graph-query)
#:re-export (<term>
@@ -88,6 +88,9 @@
(define-class <lvar> (<term>))
+(define-method (write (self <lvar>) port)
+ (format port "<lvar ~x>" (object-address self)))
+
(define (lvar)
(make <lvar>))
@@ -116,10 +119,59 @@
(define-method (graph-match (g <graph>) (t <triple>))
(graph-match g (triple-subject t) (triple-predicate t) (triple-object t)))
-;; Description
-
-;; A description is a pointer to a subject in a graph. It is also a container of triples and thus also a graph.
-
-(define-class <description> (<graph>))
-
-(define-generic description-subject)
+;; Graph Query
+
+;; This is a helper to successively build up a query (in spo order). Usefulness
+;; of this remains to be seen...
+
+(define-class <graph-query> ()
+ (graph #:init-keyword #:graph #:getter graph-query-graph)
+ (s #:init-keyword #:s #:init-thunk lvar #:getter graph-query-s)
+ (p #:init-keyword #:p #:init-thunk lvar #:getter graph-query-p)
+ (o #:init-keyword #:o #:init-thunk lvar #:getter graph-query-o))
+
+(define (graph-query? x)
+ (is-a? x <graph-query>))
+
+(define-method (write (self <graph-query>) port)
+ (format port "<graph-query ~a ~a ~a ~a>"
+ (graph-query-graph self)
+ (graph-query-s self)
+ (graph-query-p self)
+ (graph-query-o self)))
+
+(define-method (graph-query (graph <graph>) (s <iri>))
+ (make <graph-query> #:graph graph #:s s))
+
+(define-method (graph-query (q <graph-query>) (iri <iri>))
+ "Build up a query with successive (graph-query) calls."
+ (cond
+ ((lvar? (graph-query-s q))
+ (make <graph-query>
+ #:graph (graph-query-graph q)
+ #:s iri
+ #:p (graph-query-p q)
+ #:o (graph-query-o q)))
+
+ ((lvar? (graph-query-p q))
+ (make <graph-query>
+ #:graph (graph-query-graph q)
+ #:s (graph-query-s q)
+ #:p iri
+ #:o (graph-query-o q)))
+
+ ((lvar? (graph-query-o q))
+ (make <graph-query>
+ #:graph (graph-query-graph q)
+ #:s (graph-query-s q)
+ #:p (graph-query-p q)
+ #:o iri))
+
+ ;; too far
+ (else q)))
+
+(define-method (graph-match (q <graph-query>))
+ (graph-match (graph-query-graph q)
+ (graph-query-s q)
+ (graph-query-p q)
+ (graph-query-o q)))
diff --git a/tests/schemantic/rdf.scm b/tests/schemantic/rdf.scm
new file mode 100644
index 0000000..3cba3fb
--- /dev/null
+++ b/tests/schemantic/rdf.scm
@@ -0,0 +1,35 @@
+; SPDX-FileCopyrightText: 2020 pukkamustard <pukkamustard@posteo.net>
+;
+; SPDX-License-Identifier: GPL-3.0-or-later
+
+(define-module (tests schemantic rdf)
+ #:use-module (schemantic rdf)
+ #:use-module ((schemantic ns) #:select (rdf))
+ #:use-module (schemantic graph vhash)
+ #:use-module (schemantic serialization turtle)
+
+ #:use-module (srfi srfi-171)
+
+ #:use-module (ice-9 textual-ports))
+
+(define g (make-vhash-graph))
+
+(define clowns (call-with-input-file "tests/data/clowns.ttl" get-string-all))
+
+(turtle-transduce (tmap identity)
+ (graph-radd g)
+ clowns
+ (make-iri "urn:base"))
+
+(test-begin "graph-query")
+
+(test-assert "graph-query on graph returns graph-query"
+ (graph-query? (graph-query g (make-iri "https://test.example/alice"))))
+
+(test-equal "graph-match on graph-query"
+ 1 (length
+ (graph-match
+ (graph-query (graph-query g (make-iri "https://test.example/alice"))
+ (rdf "type")))))
+
+(test-end "graph-query")