Query RDF using SPARQL / Sesame
Asked Answered
A

1

6

I´m trying to query a repository using SPARQL and Sesame 2.7 but when I run my code I get the following error

org.openrdf.http.client.SesameHTTPClient - Server reports problem: org.openrdf.query.parser.sparql.ast.VisitorException: QName 'viagem:nome' uses an undefined prefix

The problem is that, I have the prefix "viagem" under the Namespaces tab for that repository on openrdf-workbench, also when I use the method getNamespaces() it shows up...

The only way I get the query to run is to add the PREFIX manually on every query, but that sounds wrong...

Is there anything that I´m missing on how to use this properly?

--- Edited with more information

Code not working:

String queryString = "SELECT ?name \n" +
"WHERE {?Aeroporto viagem:nome ?name.\n" +
"?Aeroporto rdf:type viagem:Aeroporto}";
        TupleQuery tupleQuery = con.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
        TupleQueryResult result = tupleQuery.evaluate();
        try {
            List<String> bindingNames = result.getBindingNames();
            while (result.hasNext()) {
                BindingSet bindingSet = result.next();
                Value firstValue = bindingSet.getValue(bindingNames.get(0));
                System.out.println(firstValue);
        }
        } finally {
            result.close();

        }
...

This code work if I change queryString to

 String queryString = "PREFIX viagem:<http://teste.com.br/tut/Viagem.owl#> SELECT ?name \n" +
"WHERE {?Aeroporto viagem:nome ?name.\n" +
"?Aeroporto rdf:type viagem:Aeroporto}";

I was not sure if I should add the PREFIX for every query that I'm going to execute (if that it´s the normal behavior it´s ok...)

Also if I run the following code I get the prefix and the name correctly

RepositoryResult<Namespace> listaNamespace = meuRepositorio.getConnection().getNamespaces();

    while(listaNamespace.hasNext()){
        Namespace atual = listaNamespace.next();
        System.out.println("Name " + atual.getName() + " Prefix " + atual.getPrefix());
    }

the output is:

Name http://www.w3.org/2000/01/rdf-schema# Prefix rdfs
Name http://www.w3.org/2003/11/swrl# Prefix swrl
...
Name http://www.w3.org/1999/02/22-rdf-syntax-ns# Prefix rdf
Name http://teste.com.br/tut/Viagem.owl# Prefix viagem
Aluminium answered 12/8, 2013 at 21:11 Comment(5)
What's your code? What's the query? There's not enough here to diagnose anything yet. What do you mean "I have the prefix "viagem" under the Namespaces tab for that repository"? Is that for a web-based SPARQL interface? The namespaces available for it there might not be defined for queries that you execute in code.Reside
For what it's worth, it's typically a good idea to define all the prefixes that you use, and not to depend on the server doing something to add them in. It shouldn't be difficult to define a a string that defines a bunch of prefixes, and to prepend that to your queries before you send them out.Reside
This ticket may be relevant: openvest.com/trac/ticket/34 (but I'm not absolutely sure). Someone makes the point that if the predefined namespaces on the server are changed, that has the potential to break the code of anyone making queries, which seems like a Bad Thing™, and that the prefixes for queries should be defined explicitly.Reside
@JoshuaTaylor Thanks for the reply. I added more information. So if a understood correctly this is the normal behavior, right? I need to explicity add the prefix on all queries?Aluminium
Well, I'm not a Sesame user, so I don't know for sure that Sesame doesn't try to provide you some way to use predefined namespaces, or something like that, but I consider a query sort of like source code: sure, you could make a compiler in which you can predefine some automatic imports/includes, but then no one else will be able to compile your code until they duplicate your environment, and that's not particularly useful. My advice, which isn't a definite answer to this question, is to always explicitly define your prefixes so that there's absolutely no ambiguity about your queries.Reside
T
4

Although Sesame stores namespace declarations in the repository, there is no mechanism in place to automatically add these namespaces to a SPARQL query. It is up to you as a user to make sure the SPARQL query is correct and complete.

However, the Workbench application has an advanced SPARQL editor with autocomplete support, which automatically adds namespace declarations when you use a prefix. So you do not have to type them in manually when using Workbench. Note that this is simply a convenience of the client application, not of the actual SPARQL query engine.

Update although, as stated above, Sesame does not read namespace definitions from your Repository when parsing/executing a query, it does allow you to use prefixed names for a limited number of standard vocabularies without explicitly declaring them. These are the 'rdf', 'rdfs', 'owl', 'xsd', 'fn', and 'sesame' prefixes. If you use those in a SPARQL query without declaring them, Sesame's SPARQL engine automatically replaces them with the standard namespace to which those prefixes map (note that it does not use the namespaces in your repository for this, it uses predefined constants).

However, having said all that, it's still good practice as a writer of a SPARQL query to make sure your query is complete. Prefix declarations are an integral part of a SPARQL query, without them your query is simply not syntactically valid, and therefore not portable.

Tojo answered 17/8, 2013 at 11:38 Comment(5)
You say it doesn't do it automatically. Is there a way to make it replace them? What use is there having namespaces if you can't directly use them?Genitals
@Genitals well in Java, you just read the namespaces from the store, iterate over them and prepend them to your query string before executing it. But the point of having namespaces is not to allow you to write incomplete queries. They are mostly there to allow their use in serialization when writing store contents to a file (e.g. in turtle-syntax) - and of course to be queried and reused by you as a dev. And FWIW: the latest version of the Workbench tool has an advanced SPARQL editor with autocomplete that automatically adds namespace declarations for you.Tojo
Also, my statement that Sesame does not do anything to automatically add namespaces is not 100% true. I'll update my answer.Tojo
In your update you mentioned some predefined prefixes. Is that all of them, and if not where could I find a complete list?Genitals
@Genitals that is the complete list. It's an undocumented feature, the only place you can see the list is the codebase itself (well, and now in this SO answer I guess).Tojo

© 2022 - 2024 — McMap. All rights reserved.