Skip to content

Instantly share code, notes, and snippets.

@AlBaker
Last active December 10, 2015 07:48
Show Gist options
  • Save AlBaker/4403404 to your computer and use it in GitHub Desktop.
Save AlBaker/4403404 to your computer and use it in GitHub Desktop.
Two lines of Stardog
def stardog = new Stardog([embedded:true, createIfNotPresent:true, home:"test/stardog", to:"testgroovy", username:"admin", password:"admin"])
stardog.query("select ?x ?y ?z WHERE { ?x ?y ?z } LIMIT 2", { println it } )
// in this case, it is a BindingSet, ie TupleQueryResult.next() called until exhausted and closure executed
// as of Stardog-Groovy 0.2, there is now a projection of the results into the closure's binding
// if x, y, or z are not populated in the answer, then they are still valid binidng but are null
stardog.each("select ?x ?y ?z WHERE { ?x ?y ?z } LIMIT 2", {
println x
println y
println z // may be a LiteralImpl, so you get full access to manipulate Value objects
}
)
// Variables retain their types from however the query results were populated, so URIImpls, Value,
// Literals, etc are all preserved in one heterogeneous Groovy map serving as the closure's delegate
// insert and remove
stardog.insert([["urn:test3", "urn:test:predicate", "hello world"],
["urn:test4", "urn:test:predicate", "hello world2"]])
stardog.remove(["urn:test3", "urn:test:predicate", "hello world"])
stardog.remove(["urn:test4", "urn:test:predicate", "hello world2"])
// withConnection
stardog.withConnection { con ->
def queryString = """
SELECT ?s ?p ?o
{
?s ?p ?o
}
"""
TupleQueryResult result = null;
try {
Query query = con.query(queryString);
result = query.executeSelect();
while (result.hasNext()) {
println result.next();
}
result.close();
} catch (Exception e) {
println "Caught exception ${e}"
}
}
@fraorc
Copy link

fraorc commented May 25, 2013

Hi Al, is it possible to load a DB already created by using stardog command line client or a OWL file stored in my filesystem?

I'm trying with this syntax:

def stardog = new Stardog([embedded:true, createIfNotPresent:true, home:"", to:"", username:"admin", password:"admin"])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment