I have resolved my problem using this code:
public QueryExecution query(){
String stringa = "http://dbpedia.org/resource/Fred_Guy";
ParameterizedSparqlString qs = new ParameterizedSparqlString( "" +
"prefix dbpediaont: <http://dbpedia.org/ontology/>
" +
"prefix dbpedia: <http://dbpedia.org/resource/>
" +
"prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
" +
"
" +
"select ?resource where {
" +
"?mat rdf:type ?resource
" +
"filter strstarts(str(?resource), dbpediaont:)
" +
"}" );
Resource risorsa = ResourceFactory.createResource(stringa);
qs.setParam( "mat", risorsa );
//System.out.println( qs );
QueryExecution exec = QueryExecutionFactory.sparqlService( "http://dbpedia.org/sparql", qs.asQuery() );
ResultSet results = ResultSetFactory.copyResults( exec.execSelect() );
while ( results.hasNext() ) {
System.out.println( results.next().get( "resource" ));
}
// A simpler way of printing the results.
ResultSetFormatter.out( results );
return exec;
}
In particular, in FILTER as the second arguments, I have written the "dbpediaont" label.
This code, however, still produces an error:
Exception in thread "main" HttpException: 500
at com.hp.hpl.jena.sparql.engine.http.HttpQuery.execGet(HttpQuery.java:340)
at com.hp.hpl.jena.sparql.engine.http.HttpQuery.exec(HttpQuery.java:276)
at com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP.execSelect(QueryEngineHTTP.java:345)
This error is due to the fact that the code need to write
dbpediaont:
with
str()
since it's an IRI, not a string:
filter strstarts(str(?resource), str(dbpediaont:))
The latter problem is reported on HttpException error when I call SPARQL query (on DBPedia) in Java Code
I hope this answer can help someone.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…