Last active
December 10, 2015 07:48
-
-
Save AlBaker/4403404 to your computer and use it in GitHub Desktop.
Two lines of Stardog
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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"])