summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpukkamustard <pukkamustard@posteo.net>2020-09-27 11:52:03 +0200
committerpukkamustard <pukkamustard@posteo.net>2020-09-27 11:52:03 +0200
commita7c30b2235bf3cf345b7daaaaa38e8b7b3190e04 (patch)
tree7d7a1fa2eef7d96bd3fbb5510001738f58c9d231
parente8f4af287a327cf8e9954b2d1ba70f9425fb7e70 (diff)
(schemantic datalog rdf): include RDF Graph into the EDB of a Datlog program
-rw-r--r--Makefile.am10
-rw-r--r--hall.scm20
-rw-r--r--schemantic/datalog.scm10
-rw-r--r--schemantic/datalog/rdf.scm28
-rw-r--r--tests/data/clowns.ttl2
-rw-r--r--tests/schemantic/datalog/rdf.scm49
6 files changed, 102 insertions, 17 deletions
diff --git a/Makefile.am b/Makefile.am
index bd6a8ca..24dbe41 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -46,11 +46,14 @@ SOURCES = schemantic.scm \
schemantic/blank-node.scm \
schemantic/xsd.scm \
schemantic/rdf.scm \
- schemantic/datalog.scm \
schemantic/fragment-graph.scm \
schemantic/fragment-graph/csexp.scm \
schemantic/fragment-graph/radix-sort.scm \
- schemantic/ns.scm
+ schemantic/ns.scm \
+ schemantic/datalog.scm \
+ schemantic/datalog/vhash-set.scm \
+ schemantic/datalog/relational-algebra.scm \
+ schemantic/datalog/rdf.scm
TESTS = tests/schemantic/graph/vhash.scm \
tests/schemantic/serializaton/turtle.scm \
@@ -59,7 +62,8 @@ TESTS = tests/schemantic/graph/vhash.scm \
tests/schemantic/lvar.scm \
tests/schemantic/fragment-graph.scm \
tests/schemantic/datalog.scm \
- tests/schemantic/datalog/relational-algebra.scm
+ tests/schemantic/datalog/relational-algebra.scm \
+ tests/schemantic/datalog/rdf.scm
TEST_EXTENSIONS = .scm
SCM_LOG_DRIVER = \
diff --git a/hall.scm b/hall.scm
index 8001005..4b4469c 100644
--- a/hall.scm
+++ b/hall.scm
@@ -16,12 +16,12 @@
(directory
"schemantic"
((directory
- "serialization"
- ((scheme-file "turtle")))
+ "serialization"
+ ((scheme-file "turtle")))
(directory
- "graph"
- ((directory "vhash" ((scheme-file "index")))
- (scheme-file "vhash")))
+ "graph"
+ ((directory "vhash" ((scheme-file "index")))
+ (scheme-file "vhash")))
(directory "interop" ((scheme-file "guile-rdf")))
(directory "rdf" ((scheme-file "lang-string")))
(scheme-file "iri")
@@ -30,11 +30,14 @@
(scheme-file "blank-node")
(scheme-file "xsd")
(scheme-file "rdf")
- (scheme-file "datalog")
(scheme-file "fragment-graph")
(directory "fragment-graph" ((scheme-file "csexp")
(scheme-file "radix-sort")))
- (scheme-file "ns")))))
+ (scheme-file "ns")
+ (scheme-file "datalog")
+ (directory "datalog" ((scheme-file "vhash-set")
+ (scheme-file "relational-algebra")
+ (scheme-file "rdf")))))))
(tests ((directory
"tests"
((directory "data" ())
@@ -50,7 +53,8 @@
(scheme-file "fragment-graph")
(scheme-file "datalog")
(directory "datalog"
- ((scheme-file "relational-algebra")))))))))
+ ((scheme-file "relational-algebra")
+ (scheme-file "rdf")))))))))
(programs ((directory "scripts" ())))
(documentation
((org-file "README")
diff --git a/schemantic/datalog.scm b/schemantic/datalog.scm
index a75182d..57ee711 100644
--- a/schemantic/datalog.scm
+++ b/schemantic/datalog.scm
@@ -297,8 +297,8 @@
vlist-null
clauses))
-(define (make-evaluation-context clauses)
- "Creates a VHash from predicate symbols in list of clauses to empty sets."
+(define (edb->evaluation-context edb clauses)
+ "Extend the Extensional Database into a evaluation context that includes empty sets for all intrinsical predicate symbols."
(fold
(lambda (clause result)
(let ((head (clause-head clause)))
@@ -306,15 +306,15 @@
(atom-predicate head)
(set)
(vhash-delete (atom-predicate head) result))))
- vlist-null
+ edb
clauses))
;; Algebraic Naive Evaluation (Section 9.1.1 of Logic Programming and Databases)
-(define (datalog-eval clauses)
+(define* (datalog-eval clauses #:key (edb vlist-null))
"Evaluate a Datalog program"
(let ((relational-exprs (clauses->relational-expr (sort-clauses clauses))))
- (let loop ((context (make-evaluation-context clauses)))
+ (let loop ((context (edb->evaluation-context edb clauses)))
(let ((context+changes?
(vhash-fold (λ (predicate relational-expr context+changes?)
(let* ((new-values (ra:evaluate (car context+changes?) relational-expr))
diff --git a/schemantic/datalog/rdf.scm b/schemantic/datalog/rdf.scm
new file mode 100644
index 0000000..23a399e
--- /dev/null
+++ b/schemantic/datalog/rdf.scm
@@ -0,0 +1,28 @@
+(define-module (schemantic datalog rdf)
+ #:use-module (schemantic rdf)
+
+ #:use-module (schemantic datalog vhash-set)
+
+ #:use-module (ice-9 vlist)
+
+ #:use-module (srfi srfi-1)
+
+ #:export (add-rdf-graph-to-edb))
+
+(define (add-rdf-graph-to-edb predicate-symbol graph context)
+ (vhash-cons
+ predicate-symbol
+ (lambda (attributes)
+ (fold
+ (λ (triple result)
+ (set-adjoin
+ result
+ (vector (triple-subject triple)
+ (triple-predicate triple)
+ (triple-object triple))))
+ vlist-null
+ (graph-match graph
+ (vector-ref attributes 0)
+ (vector-ref attributes 1)
+ (vector-ref attributes 2))))
+ context))
diff --git a/tests/data/clowns.ttl b/tests/data/clowns.ttl
index 44fe9e3..ddfde2f 100644
--- a/tests/data/clowns.ttl
+++ b/tests/data/clowns.ttl
@@ -1,6 +1,6 @@
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix schema: <https://schema.org/> .
-@prefix as: <https://www.w3.org/ns/activitystreams> .
+@prefix as: <https://www.w3.org/ns/activitystreams#> .
<https://test.example/notes/1>
rdf:type as:Note ;
diff --git a/tests/schemantic/datalog/rdf.scm b/tests/schemantic/datalog/rdf.scm
new file mode 100644
index 0000000..c811662
--- /dev/null
+++ b/tests/schemantic/datalog/rdf.scm
@@ -0,0 +1,49 @@
+(define-module (tests schemantic datalog rdf)
+ #:use-module (schemantic rdf)
+ #:use-module ((schemantic ns) #:select (rdf))
+ #:use-module (schemantic serialization turtle)
+ #:use-module (schemantic graph vhash)
+
+ #:use-module (schemantic datalog)
+ #:use-module (schemantic datalog rdf)
+ #:use-module (schemantic 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)