Search code examples
javasparqlrdfsemantic-web

SPARQL Query does not retrieve result in java


Hi friends sorry I am asking question which is similar to my previous question.I have run code in SPARQL web service [http://drugbank.bio2rdf.org/sparql] and return result which is as follows:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX drugbank: <http://www4.wiwiss.fu-berlin.de/drugbank/resource/drugbank/>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT  distinct ?resource WHERE { ?resource dcterms:identifier "drugbank:DB01051"^^xsd:string} 

However, I tried to get the result in java and it does not return a result with the same query. The java code is as follows:

import java.io.IOException;
import org.apache.jena.query.ParameterizedSparqlString;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryExecutionFactory;
import org.apache.jena.query.ResultSetFactory;
import org.apache.jena.query.ResultSetFormatter;
import org.apache.jena.query.ResultSetRewindable;

public class DrugbankResourceProperty {

    public static void main(String[] args) throws IOException {

        ParameterizedSparqlString pss = new ParameterizedSparqlString(""
                + "PREFIX  rdf:     <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"
                + "PREFIX  xsd:     <http://www.w3.org/2001/XMLSchema#>\n"
                + "PREFIX  rdfs:    <http://www.w3.org/2000/01/rdf-schema#>\n"
                + "PREFIX  dcterms: <http://purl.org/dc/terms/>\n"
                + "PREFIX  drugbank: <http://www4.wiwiss.fu-berlin.de/drugbank/resource/drugbank/>"
                + "\n"
                + "SELECT  distinct ?resource WHERE { ?resource dcterms:identifier \"drugbank:DB01051\"^^xsd:string}");

        QueryExecution exec = QueryExecutionFactory.sparqlService("http://drugbank.bio2rdf.org/sparql", pss.asQuery());

        ResultSetRewindable results = ResultSetFactory.makeRewindable(exec.execSelect());

        while (results.hasNext()) {

            System.out.println(ResultSetFormatter.asText(results));
        }

    }

}

I added "xsd" and "drugbank" prefixes after first run and thought it does not return because of these missing prefixes. However, it is not still working after that.I really do not know why it does not return result.If possible could you tell me where I make a mistake please ? Your help is greatly appreciated!


Solution

  • The problem is that RDF 1.1 since Jena 3.x. With RDF 1.1, the semantics and handling of string literals have changed. Thus, the query will be parsed to

       PREFIX  drugbank: <http://www4.wiwiss.fu-berlin.de/drugbank/resource/drugbank/>
       PREFIX  xsd:  <http://www.w3.org/2001/XMLSchema#>
       PREFIX  dcterms: <http://purl.org/dc/terms/>
       PREFIX  rdfs: <http://www.w3.org/2000/01/rdf-schema#>
       PREFIX  rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
    
    SELECT DISTINCT  ?resource
    WHERE
      { ?resource  dcterms:identifier  "drugbank:DB01051" }
    

    See the missing datatype for the literal. But, and here is the problem, the SPARQL endpoint doesn't work on the new RDF 1.1 semantics and handles the literal as plain literal, which in fact does not match with the xsd:string literals.

    I don't know if there is a config option in JENA, but you should probably ask for it on the mailing list and then post the answer here.

    The only workaround I see is to use QueryEngineHTTP which can avoid internal parsing, e.g.

    QueryEngineHTTP qe = new QueryEngineHTTP("http://drugbank.bio2rdf.org/sparql", pss.toString());
    System.out.println(ResultSetFormatter.asText(qe.execSelect()));
    qe.close();
    

    Update

    According to the comment below from @RobV, the config option to change to the old behaviour in Apache JENA (javadoc) is:

    JenaRuntime.isRDF11 = false;