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/>\n" +
"prefix dbpedia: <http://dbpedia.org/resource/>\n" +
"prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" +
"\n" +
"select ?resource where {\n" +
"?mat rdf:type ?resource\n" +
"filter strstarts(str(?resource), dbpediaont:)\n" +
"}" );
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.
6
solved To write a query SPARQL in Java code using strstarts filter