除非您将整个 DBpedia 数据集加载到本地模型中,否则要获得与在 DBpedia SPARQL 表单中运行查询相同的效果,您必须将查询发送到 DBpedia SPARQL 端点。我对您的程序进行了以下修改:
package example;
import com.hp.hpl.jena.query.*;
public class AshTest
{
public static void main( String[] args ) {
String s2 = "PREFIX g: <http://www.w3.org/2003/01/geo/wgs84_pos#>\n" +
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" +
"PREFIX onto: <http://dbpedia.org/ontology/>\n" +
"\n" +
"SELECT ?subject ?stadium ?lat ?long\n" +
"WHERE\n" +
" { ?subject g:lat ?lat .\n" +
" ?subject g:long ?long .\n" +
" ?subject <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> onto:Stadium .\n" +
" ?subject rdfs:label ?stadium\n" +
" FILTER ( ( ( ( ( ?lat >= 52.4814 ) && ( ?lat <= 57.4814 ) ) && ( ?long >= -1.89358 ) ) && ( ?long <= 3.10642 ) ) && ( lang(?stadium) = \"en\" ) )\n" +
" }\n" +
"LIMIT 5\n" +
"";
Query query = QueryFactory.create(s2); //s2 = the query above
QueryExecution qExe = QueryExecutionFactory.sparqlService( "http://dbpedia.org/sparql", query );
ResultSet results = qExe.execSelect();
ResultSetFormatter.out(System.out, results, query) ;
}
}
并得到以下结果:
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| subject | stadium | lat | long |
======================================================================================================================================================================================================================
| <http://dbpedia.org/resource/Welford_Road_Stadium> | "Welford Road Stadium"@en | "52.6242"^^<http://www.w3.org/2001/XMLSchema#float> | "-1.13306"^^<http://www.w3.org/2001/XMLSchema#float> |
| <http://dbpedia.org/resource/Hillsborough_Stadium> | "Hillsborough Stadium"@en | "53.4114"^^<http://www.w3.org/2001/XMLSchema#float> | "-1.50056"^^<http://www.w3.org/2001/XMLSchema#float> |
| <http://dbpedia.org/resource/Gateshead_International_Stadium> | "Gateshead International Stadium"@en | "54.9611"^^<http://www.w3.org/2001/XMLSchema#float> | "-1.57972"^^<http://www.w3.org/2001/XMLSchema#float> |
| <http://dbpedia.org/resource/Filbert_Street> | "Filbert Street"@en | "52.6236"^^<http://www.w3.org/2001/XMLSchema#float> | "-1.14056"^^<http://www.w3.org/2001/XMLSchema#float> |
| <http://dbpedia.org/resource/Craven_Park,_Hull> | "Craven Park, Hull"@en | "53.7539"^^<http://www.w3.org/2001/XMLSchema#float> | "-0.264722"^^<http://www.w3.org/2001/XMLSchema#float> |
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------