javasparqljenaapache-jena

Cannot run particular SPARQL query using Apache Jena


I'm trying to run the following query using Apache Jena

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX vrank:<http://purl.org/voc/vrank#>
PREFIX dbp-ont:<http://dbpedia.org/ontology/>
PREFIX dbp-prop:<http://dbpedia.org/property/>
SELECT distinct (SAMPLE(?slabel) AS ?sublabel) (SAMPLE (?plabel) AS ?predlabel) (SAMPLE(?olabel) AS ?oblabel) ?v 
FROM <http://dbpedia.org/> 
FROM <http://people.aifb.kit.edu/ath/#DBpedia_PageRank> 
WHERE {
    {   <http://dbpedia.org/resource/Yao_Ming> ?p ?o.
        FILTER regex(str(?o),"http://dbpedia.org/resource","i").
        FILTER (?p != dbp-ont:wikiPageWikiLink && ?p != <http://purl.org/dc/terms/subject> 
                && ?p != dbp-prop:wikiPageUsesTemplate && ?p != rdfs:seeAlso 
                && ?p != <http://www.w3.org/2002/07/owl#differentFrom> 
                && ?p != <http://dbpedia.org/ontology/wikiPageDisambiguates> && ?p != <http://dbpedia.org/ontology/wikiPageRedirects> ).
        OPTIONAL {?o rdfs:label ?olabel. FILTER langmatches( lang(?olabel), "EN" ). }.
        OPTIONAL {?p rdfs:label ?plabel. FILTER langmatches( lang(?plabel), "EN" ).}.
        OPTIONAL {<http://dbpedia.org/resource/Yao_Ming> rdfs:label ?slabel. FILTER langmatches( lang(?slabel), "EN" ).}.
        OPTIONAL {?o vrank:hasRank ?r. ?r vrank:rankValue ?v}.
    } 
UNION
    {   ?s ?p <http://dbpedia.org/resource/Yao_Ming>.
        FILTER regex(str(?s),"http://dbpedia.org/resource","i").
        FILTER (?p != dbp-ont:wikiPageWikiLink && ?p != <http://purl.org/dc/terms/subject> 
                && ?p != dbp-prop:wikiPageUsesTemplate && ?p != rdfs:seeAlso 
                && ?p != <http://www.w3.org/2002/07/owl#differentFrom> 
                && ?p != <http://dbpedia.org/ontology/wikiPageDisambiguates> && ?p != <http://dbpedia.org/ontology/wikiPageRedirects> ).
        OPTIONAL {?s rdfs:label ?slabel.   FILTER langmatches( lang(?slabel), "EN" ). }.
        OPTIONAL {?p rdfs:label ?plabel.  FILTER langmatches( lang(?plabel), "EN" ).}.
        OPTIONAL {<http://dbpedia.org/resource/Yao_Ming> rdfs:label ?olabel. FILTER langmatches( lang(?olabel), "EN" ).}.
        OPTIONAL {?s vrank:hasRank ?r. ?r vrank:rankValue ?v}.
    }
} group by ?v order by desc (?v)

This query is taken from the LinkSUM project. It runs fine(results) on the dbpedia sparql endpoint but no rows are returned by Jena.

This is the code

import org.apache.jena.query.QueryExecutionFactory;
import org.apache.jena.query.QuerySolution;
import org.apache.jena.query.ResultSet;
import org.apache.jena.query.ResultSetFactory;
import org.apache.jena.sparql.engine.http.QueryEngineHTTP;

        String query = "..."; // The previously mentioned query
        String DBPEDIA_SPARQL_SERVICE = "http://dbpedia.org/sparql/";
        QueryEngineHTTP qExec = (QueryEngineHTTP) QueryExecutionFactory.sparqlService(DBPEDIA_SPARQL_SERVICE , query);
        ResultSet resultSet = null;
        try {
            resultSet = qExec.execSelect();
            resultSet = ResultSetFactory.copyResults(resultSet);
        } catch (Exception e) {
            // Report exception
        } finally {
            qExec.close();
        }

        String subLabel = "sublabel";
        String predLabel = "predlabel";
        String obLabel = "oblabel";
        String vRank = "v";

        if (resultSet != null) {
            while (resultSet.hasNext()) {
                QuerySolution result = resultSet.next();
                if (result != null) {
                    System.out.println(subLabel);
                    System.out.println(predLabel);
                    System.out.println(obLabel);
                    System.out.println(vRank);
                }
            }
        }

I have run several queries using the same code but this one fails to return any results.


Solution

  • DBpedia requires ?default-graph-uri=http%3A%2F%2Fdbpedia.org in the servce URL or alternatively qExec.addDefaultGraph("http://dbpedia.org"); sometimes. This seems to be the case here when using FROM in the query.