I'm developing a graphdb plugin using graphdb's Plugin API.
I need my plugin to retrieve some statements from the current active graphdb repository to process them further. I want to make a query with this format:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
prefix proof: <http://www.ontotext.com/proof/>
select ?rule ?s ?p ?o ?context where {
values (?subject ?predicate ?object) {(<urn:Mary> <urn:hasChild> <urn:John>)}
?ctx proof:explain (?subject ?predicate ?object) .
?ctx proof:rule ?rule .
?ctx proof:subject ?s .
?ctx proof:predicate ?p .
?ctx proof:object ?o .
?ctx proof:context ?context .
}
If it's relevant, the stated query should be handled by another plugin: the proof plugin.
This should happen automatically if the query is formulated correctly and the plugin is active and running on the repository.
RDF4J offers some classes to formulate and send queries to a RepositoryConnection
object.
Repository db = new SailRepository(new MemoryStore());
// Open a connection to the database
try (RepositoryConnection conn = db.getConnection()) {
String filename = "example-data-artists.ttl";
try (InputStream input = Example15SimpleSPARQLQuery.class.getResourceAsStream("/" + filename)) {
// add the RDF data from the inputstream directly to our database
conn.add(input, "", RDFFormat.TURTLE);
}
// We do a simple SPARQL SELECT-query that retrieves all resources of type `ex:Artist`,
// and their first names.
String queryString = "PREFIX ex: <http://example.org/> \n";
queryString += "PREFIX foaf: <" + FOAF.NAMESPACE + "> \n";
queryString += "SELECT ?s ?n \n";
queryString += "WHERE { \n";
queryString += " ?s a ex:Artist; \n";
queryString += " foaf:firstName ?n .";
queryString += "}";
TupleQuery query = conn.prepareTupleQuery(queryString);
// A QueryResult is also an AutoCloseable resource, so make sure it gets closed when done.
try (TupleQueryResult result = query.evaluate()) {
// we just iterate over all solutions in the result...
for (BindingSet solution : result) {
// ... and print out the value of the variable binding for ?s and ?n
System.out.println("?s = " + solution.getValue("s"));
System.out.println("?n = " + solution.getValue("n"));
}
}
} finally {
// Before our program exits, make sure the database is properly shut down.
db.shutDown();
}
The first issue is that I haven't found a way to get an object of the correct type pointing to the current working repository for graphdb.
The plugin API works with a PluginConnection
object (documented here) offering some utilities, but which does not seem to allow this kind of interfacing.
I was able to list all repositories present at localhost:7200, but I don't know how to distinguish the current working repository; if I want access to a repository I must know its id/name which is user-defined.
The docs show a possible solution using configuration files.
I don't think I can rely on those. I can't be sure of where they are placed in a graphdb installation and it seems impractical in the context of a plugin.
Also, they don't seem to show which repository is currently being queried.
I have considered using the getStatements()
function offered by PluginConnection
. This allows for some basic filtering based on subject, predicate and object.
I find this insufficient for 2 reasons:
- I don't know how to use it to reproduce the behavior of more complex queries like the one above
- I can't use rdf lists in the object like this
?ctx proof:explain (?subject ?predicate ?object) .