Search code examples
javardfsparqldbpediasesame

OpenRdf Exception when parsing data from DBPedia


I use OpenRdf with Sparql to gather data from DBPedia but I encounter some errors on the following query ran against the DBPedia Sparql endpoint:

CONSTRUCT{ 
    ?battle ?relation ?data . 
} 
WHERE{
  ?battle   rdf:type    yago:Battle100953559 ;  
            ?relation   ?data   .  
  FILTER(?relation != owl:sameAs)
}
LIMIT 1 
OFFSET 18177

I modified the LIMIT and OFFSET to point out the specific result that provokes the problem.

The response is this one :

@prefix foaf:   <http://xmlns.com/foaf/0.1/> .
@prefix ns1:    <http://en.wikipedia.org/wiki/> .
<http://dbpedia.org/resource/Mongol%E2%80%93Jin_Dynasty_War>    foaf:isPrimaryTopicOf   ns1:Mongol–Jin_Dynasty_War .

The problem is that the ns1:Mongol–Jin_Dynasty_War entity contains a minus sign, therefore I get the following exception when running this query inside a Java application using OpenRdf :

org.openrdf.query.QueryEvaluationException: org.openrdf.rio.RDFParseException: Expected '.', found '–' [line 3]

Is there any way to circumvent this problem ?

Thanks !


Solution

  • To help other users who might encounter the same problem, I'll post here the way to set the preferred output format for Graph Queries using OpenRDF v2.7.x.

    You need to creat a subclass of SPARQLRepository to access the HTTPClient (for some reason, the field is protected.

    public class NtripleSPARQLRepository extends SPARQLRepository {
        public NtripleSPARQLRepository(String endpointUrl) {
            super(endpointUrl);
            this.getHTTPClient().setPreferredRDFFormat(RDFFormat.NTRIPLES);
        }
    }
    

    The you just need to create a new Instance of this class :

    NtripleSPARQLRepository repository = new NtripleSPARQLRepository(service);
    RepositoryConnection connection = new SPARQLConnection(repository);
    Query query = connection.prepareQuery(QueryLanguage.SPARQL, "YOUR_QUERY");