2

Firstly, I found this relevant question and answer:

If I add the PREFIX in the SPARQL query it works. However, I don't want to duplicate all the prefixes in all my SPARQL queries, but to define them only once. I tried to do it programmatically for rdfs::

model.setNsPrefix("rdfs", "http://www.w3.org/2000/01/rdf-schema#");
query.setPrefix("rdfs", "http://www.w3.org/2000/01/rdf-schema#");

and it works, but if I try to do it with my own ontologies it doesn't work:

model.setNsPrefix("myOnt", "http://example.com/ontologies/myOnt#");
query.setPrefix("myOnt", "http://example.com/ontologies/myOnt#");
Melou
  • 21
  • 3
  • 2
    Try printing out the query to see why there is an error. – AndyS Sep 15 '13 at 10:31
  • Did you end up getting this resolved? – Joshua Taylor Sep 26 '13 at 20:58
  • Sorry, I did not receive any notifications concerning these answers. I kept the PREFIX in the SPARQL queries, but I do not like this solution since we have to rewrite them each time, I wanted to find a better solution. – Melou Apr 25 '14 at 10:07

1 Answers1

1

You can set up a Prologue to contain you prefix mappings and then pass that into the QueryFactory with your specific SPARQL query.

Something like the following worked for me using Apache Jena 3.0.0:

Prologue queryPrologue = new Prologue();
queryPrologue.setPrefix("skos", "http://www.w3.org/2004/02/skos/core#");

String sparql = "SELECT (COUNT ?s) WHERE { ?s a skos:Concept . }"
Query query = QueryFactory.parse(new Query(queryPrologue), sparql, null, null);

try(QueryExecution queryExec = QueryExecutionFactory.create(query, dataset)) {
    // ...
}
abargnesi
  • 341
  • 4
  • 13