-
Hello, I was asking if it is possible to define default SPARQL prefixes without having to declare them in the query? Like I want to be able to run the query SELECT * {ex:aaa ?p ?o} without having to specify For now I was concatenating I've also tried EDIT: To answer, I've added in the PR #3993 these methods to set custom prefixes // SPARQLParser
SPARQLParser(Set<Namespace> customDefaultPrefixes)
// SPARQLParserFactory
QueryParser getParser(Set<Namespace> customDefaultPrefixes); Examples // create a set with the ex namespace
Set<Namespace> customDefaultPrefixes = new HashSet<>();
customDefaultPrefixes.add(Values.namespace("ex", "http://example.org/"));
// create a parser with the default prefixes
SPARQLParser parser = new SPARQLParser(customDefaultPrefixes);
// we can use the default prefix without having to specify it
parser.parseQuery("SELECT ?s {?s ex:aaa ex:ooo}", null);
// we can redefine the prefix in the query if we want another one (this is not an error)
parser.parseQuery("PREFIX ex: <http://example2.org/> SELECT ?s {?s ex:aaa ex:ooo}", null);
// we cannot redefine a prefix in the same request (this is still an excepted error)
parser.parseQuery("PREFIX ex: <http://example.org/> PREFIX ex: <http://example2.org/> SELECT ?s {?s ex:aaa ex:ooo}", null); |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I came across this question a couple days ago in search of a solution to this exact situation, and from what I can tell, there is no API to apply prefixes to a SPARQL query or parser. It is possible for RDF parsers by calling I implemented it in my own project by lexing SPARQL queries for their prefixes and tacking on prefixes that don't exist which is the same approach you mentioned plus some logic to work around duplicate prefixes. It's not ideal, but it works. https://github.com/rdfpub/generator/blob/main/init/src/main/java/pub/rdf/builder/DatabaseBuilder.java#L163-L183 As far as It would be nice to have an actual API to apply prefixes to SPARQL queries. |
Beta Was this translation helpful? Give feedback.
I came across this question a couple days ago in search of a solution to this exact situation, and from what I can tell, there is no API to apply prefixes to a SPARQL query or parser. It is possible for RDF parsers by calling
RDFParser.getParserConfig().set(BasicParserSettings.NAMESPACES,namespaces)
, but no analogue exists for SPARQL parsers that I could find.I implemented it in my own project by lexing SPARQL queries for their prefixes and tacking on prefixes that don't exist which is the same approach you mentioned plus some logic to work around duplicate prefixes. It's not ideal, but it works. https://github.com/rdfpub/generator/blob/main/init/src/main/java/pub/rdf/builder/DatabaseBui…