0

I'm using Jena to write a SPARQL query to get the rdfs:label property from a URI received as a method parameter. That method only receives URIs like: http://pt.dbpedia.org/.. It should return me the rdfs:label, but it doesn't return me anything. I checked and it doesn't enter the while block supposed to iterate the results. I even made a test with the URI: <http://pt.dbpedia.org/resource/Brasil> , but it didn't work.

What may be the problem?

 public String getLabel(String uri, String label) {
              Model model = ModelFactory.createDefaultModel().read( uri );
              RDFNode node;

 String queryString = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> " +
              "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> " +
                  "PREFIX owl: <http://www.w3.org/2002/07/owl#>" +
              "SELECT distinct ?label WHERE { " +
             "?resource owl:sameAs <" + uri + "> ;" +
                  "rdfs:label ?label ." +
                  "filter( langMatches(lang(?label),'pt'))  }";

             Query query = QueryFactory.create(queryString);

                QueryExecution qe = QueryExecutionFactory.create(query, model);
                ResultSet r =  qe.execSelect();

                while( r.hasNext() ) {
                       QuerySolution querySolution = r.next();
                       node = querySolution.get("label");
                       label = node.toString();
                    }
                return label;
      }

The SPARQL query is like that:

SELECT distinct ?label WHERE { 
  ?brasil owl:sameAs <http://pt.dbpedia.org/resource/Brasil> ;
          rdfs:label ?label .
  filter( langMatches(lang(?label),"pt") )
}

Thank you!

Luciane
  • 259
  • 4
  • 23
  • I don't know if it's the problem that you're running into or not, (it doesn't look like it is), but rather that using string concatenation to make your query, you really should consider a parameterized sparql string. This answer (http://stackoverflow.com/a/16739846/1281433) has an example. – Joshua Taylor Dec 05 '13 at 20:51

1 Answers1

1

I understand that this is a continuation of your earlier question, Should queries with URIs like http://pt.dbpedia.org/resource/.. be different from the ones with URIs like http://dbpedia.org/resource/..?. If you're getting the query:

SELECT distinct ?label WHERE { 
  ?brasil owl:sameAs <http://pt.dbpedia.org/resource/Brasil> ;
          rdfs:label ?label .
  filter( langMatches(lang(?label),"pt") )
}

then your uri must have been http://pt.dbpedia.org/resource/Brasil, so you would have been (trying) to retrieve data with

Model model = ModelFactory.createDefaultModel().read( uri );

and then you're trying to run a SPARQL query against the local data that you've downloaded. As I mentioned in the previous (linked) question, the queries that I provided were meant to be run across the SPARQL endpoints; they weren't based on downloading the data and querying locally.

Trying to download the data locally like this doesn't work, as the following program and its output show:

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;

public class BrasilExample {
    public static void main(String[] args) {
        final Model model = ModelFactory.createDefaultModel().read( "http://pt.dbpedia.org/resource/Brasil" );
        model.write( System.out );
    }
}
<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" > 
</rdf:RDF>

If you want to download a little bit of data and query against it, then note that

and that latter page has links at the bottom to download the data, e.g.,

If you were to download that file, then your query might work (but of course the uri would no longer be the same).

The query you're using from my earlier answer, though, was designed for the main DBpedia endpoint, not the Portuguese endpoint. You might be able to download the data for Brasil from the main DBpedia by going to http://dbpedia.org/resource/Brazil and following the same redirect and download link as described above, but a better choice would be to actually run the query against the main DBpedia endpoint, http://dbpedia.org/sparql, as shown in the following code and its results.

import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;

public class BrasilExample {
    public static void main(String[] args) {

        final String QUERY = 
                "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" +
                "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" +
                "PREFIX owl: <http://www.w3.org/2002/07/owl#>\n" +
                "\n" +
                "SELECT distinct ?label WHERE {\n" +
                "  ?brasil owl:sameAs <http://pt.dbpedia.org/resource/Brasil> ;\n" +
                "          rdfs:label ?label .\n" +
                "  filter( langMatches(lang(?label),\"pt\") )\n" +
                "}";

        final String ENDPOINT = "http://dbpedia.org/sparql";
        final ResultSet rs = QueryExecutionFactory.sparqlService( ENDPOINT, QUERY ).execSelect();
        ResultSetFormatter.out( rs );
    }
}
---------------
| label       |
===============
| "Brasil"@pt |
| "Brazil"@pt |
---------------
Community
  • 1
  • 1
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
  • Thank you! Will I always get more than one result? Because if I open on the browser, I can see that the value for "rdfs:label" there is only "Brasil". Why do I get the result "Brazil" here too? – Luciane Dec 05 '13 at 22:29
  • @Luciane You'll always get as many results as the RDF store has that matches. Evidently, the PT DBpedia has one result about PT Dbpedia's Brasil. The main DBpedia has two results. Different databases contain different data. It's just the way of the world, I suppose. – Joshua Taylor Dec 06 '13 at 04:12
  • Ok. But what if I want only to get the result from the PT Dbpedia's Brasil? – Luciane Dec 06 '13 at 10:34
  • @Luciane Query PT DBpedia (http://pt.dbpedia.org/sparql) with the query for PT DBpedia from the previous question (`SELECT ?label WHERE { rdfs:label ?label. }`). That's why in that answer I pointed out a number of queries and _what endpoints to run them on_ and what results they produced. You just need to use the right query on the right endpoint. **In general:** If you want PT DBpedia, use http://pt.dbpedia.org/sparql; if you want Main DBpedia, use http://dbpedia.org/sparql, and since they have different data, you might need different queries. – Joshua Taylor Dec 06 '13 at 13:28