This is a small library to make it easier to work with RDF and SPARQL in Clojure 1.4. Uploaded to Clojars at http://clojars.org/seabass
Add the following line to your dependencies:
[seabass "2.1.1"]
- xsd (http://www.w3.org/2001/XMLSchema#)
- rdf (http://www.w3.org/1999/02/22-rdf-syntax-ns#)
- rdfs (http://www.w3.org/2000/01/rdf-schema#)
- owl (http://www.w3.org/2002/07/owl#)
Takes n-many arguments and returns an RDF model. These arguments can be
- Strings for relative or absolute pathnames for RDF files
- Strings for URI's that resolve to an RDF file online
- Vectors for RDF files where the first element is the URI/pathname, and the
second element is the language (when the file suffix isn't sufficient).
Valid language strings (following Jena) are:
- "RDF/XML"
- "N-TRIPLES"
- "TTL"
- "N3"
- Strings for Jena Rules files (must end with ".rules" in the filename)
- RDF models previously created with the build function
- A Jena model
This executes a SELECT query against an RDF model, returning a map with the following keys:
- :vars - the list of variables used in the select query's 'projection' clause
- :data - a list of maps whose keys are the variables listed in :vars
The arguments are:
- query: a SPARQL Select query string
- target: either a URI string for a Sparql Endpoint or an RDF model
This executes an ASK query against an RDF model, returning a Boolean value. The arguments are:
- query: a SPARQL Ask query string
- target: either a URI string for a Sparql Endpoint or an RDF model
This executes a CONSTRUCT query against an RDF model, returning a new RDF model. The arguments are:
- query: a SPARQL Construct query string
- target: either a URI string for a Sparql Endpoint or an RDF model
Writes the contents of a model to the file specified by the target string.
The resulting file is encoded in n-triples. Only RDF facts are written
(i.e. not rules). Returns the path name of the written file (ie the
provided 'target' parameter).
- model: an RDF model previously constructed (via build or pull)
- target: a string for a relative or absolute pathname for the file to write to
These functions return Jena triples, which can be pushed into a model.
- subject: A string that is a valid uri or a string starting with '_:', indicating a blank node
- predicate: A string that is a valid uri
- object: A string that is a valid uri, or a string starting with '_:', indicating a blank node. Will be interpreted as an RDF resource.
These functions return Jena triples, which can be pushed into a model.
- subject: A string that is a valid uri or a string starting with '_:', indicating a blank node
- predicate: A string that is a valid uri
- object: Either a string, integer, long, double, boolean, or date (java.util.Date). The value will be converted to an appropriate RDF datatype. If the conversion fails, either an exception will be thrown or a nil will be returned (depending on how crazy the submitted value is).
Asserts a collection of triples into the target, which can either be a Jena model or a remote endpoint uri string.
- Build an RDF model with a local TTL file, a remote NT file, and a model pulled from an Endpoint
(def c "construct {?x <http://seabass.foo/bar> ?y}
{ ?y <http://example.org/baz> ?x }")
(def m (build ["data/my-ontology.rdf" "TTL"]
"http://way.out.there/my-data.nt"
(pull c "http://my-endpoint/sparql")))
- Push three facts into a locally-hosted Sesame repo called 'test'
(def endpoint "http://localhost:8080/openrdf-sesame/repositories/test/statements")
(def r1 (resource-fact "http://foo/luke" "http://foo/sibling" "http://foo/leia"))
(def r2 (resource-fact "_:v" "http://foo/father" "http://foo/luke"))
(def l1 (literal-fact "_:v" "http://foo/wears" "http://foo/cape"))
(push endpoint [r1 r2 l1])
- Ask whether a Sparql endpoint is up
(ask "ask {}" "http://my-endpoint/sparql")
- Execute a Select query
(def q "select ?x ?y ?z
{?x <http://ex.org/foo> ?y .
?z <http://ex.org/bar ?y . }")
(def m (build "data/my-ont.ttl" "data/your-ont.owl"))
(bounce q m)
All built-ins supported in Jena 2.9.2 (the current release as of April, 2011) can be used. The following built-ins have been added for Jena Rules (i.e. in .rules files, and not in Sparql queries):
- diff-second: Returns the difference between two times, dates, or datetimes in seconds.
- diff-minute: Returns the difference between two times, dates, or datetimes in minutes.
- diff-hour: Returns the difference between two times, dates, or datetimes in hours.
- diff-day: Returns the difference between two times, dates, or datetimes in days (1 day = 24 hours).
Copyright (C) 2012 Ryan Kohl
Distributed under the Eclipse Public License, the same as Clojure.