Reputation: 19
hello ive this query in this site working http://dbpedia.org/snorql/
PREFIX ontology: <http://dbpedia.org/ontology/>
SELECT DISTINCT ?author ?name ?wiki ?desc ?thumb WHERE {
?author a ontology:Book;
rdfs:label ?name;
ontology:wikiPageID ?wiki;
ontology:abstract ?desc.
OPTIONAL {?author <http://dbpedia.org/ontology/thumbnail> ?thumb }.
FILTER (lang(?name) = 'en')
FILTER (regex(?name, "lo") || regex(?desc, "lo"))
FILTER (lang(?desc) = 'en').
}ORDER BY ?name LIMIT 100
but in my jena queryFactory java class it only works if i remove Optional filter for thumbnail at this line :
OPTIONAL {?author <http://dbpedia.org/ontology/thumbnail> ?thumb }.
here is my jena java method that works :
private String authorQuery(String entity, String keyWord, String language) {
return addPrefix("rdfs: <http://www.w3.org/2000/01/rdf-schema#>") +
addPrefix("rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>") +
addPrefix("dbpprop: <http://dbpedia.org/property/>") +
addPrefix("ontology: <http://dbpedia.org/ontology/>") +
addQuery("SELECT DISTINCT ?author ?name ?wiki ?desc ?thumb WHERE {\n" +
"?author a ontology:" + entity + ";\n" +
"rdfs:label ?name;\n" +
"ontology:wikiPageID ?wiki;\n" +
"ontology:abstract ?desc;\n" +
"<http://dbpedia.org/ontology/thumbnail> ?thumb.\n" +
"FILTER (lang(?name) = '" + language + "') " +
"FILTER (regex(?name, \"" + keyWord + "\") || regex(?desc, \"" + keyWord + "\"))\n" +
" FILTER (lang(?desc) = '" + language + "')." +
"}\n" +
"ORDER BY ?name\n" +
"LIMIT 40000");
}
but as soon as i add optionnal keyword in this line :
"<http://dbpedia.org/ontology/thumbnail> ?thumb.\n" +
No result are returned, can someone tell me why? :(
PS : it work well without OPTIONAL FILTER
Edit : whole DbpediaQuery class
public class DbPediaQuery {
//array of authors
private DbPediaQuery() {
}
public static DbPediaQuery createDbPediaQuery() {
return new DbPediaQuery();
}
public LinkedList<Entity> queryAuthor(String entity, String keyWord, String language) {
LinkedList<Entity> temp = new LinkedList<>();
Query query = QueryFactory.create(authorQuery(entity, keyWord, language));
QueryExecution qexec = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", query);
try {
temp.addAll(collectAuthors(qexec.execSelect()));
} catch (Exception e) {
e.printStackTrace();
}
return temp;
}
private String authorQuery(String entity, String keyWord, String language) {
return addPrefix("rdfs: <http://www.w3.org/2000/01/rdf-schema#>") +
addPrefix("rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>") +
addPrefix("dbpprop: <http://dbpedia.org/property/>") +
addPrefix("ontology: <http://dbpedia.org/ontology/>") +
addQuery("SELECT DISTINCT ?author ?name ?wiki ?desc ?thumb WHERE {\n" +
"?author a ontology:" + entity + ";\n" +
"rdfs:label ?name;\n" +
"ontology:wikiPageID ?wiki;\n" +
"ontology:abstract ?desc;\n" +
"<http://dbpedia.org/ontology/thumbnail> ?thumb.\n" +
"FILTER (lang(?name) = '" + language + "') " +
"FILTER (regex(?name, \"" + keyWord + "\") || regex(?desc, \"" + keyWord + "\"))\n" +
" FILTER (lang(?desc) = '" + language + "')." +
"}\n" +
"ORDER BY ?name\n" +
"LIMIT 40000");
}
private LinkedList<Entity> collectAuthors(ResultSet results) {
LinkedList<Entity> temp = new LinkedList<>();
while (results.hasNext()) {
Entity a = new Entity();
QuerySolution row = results.next();
String fullName = row.get("name").toString().substring(0, row.get("name").toString().indexOf("@"));
String biography = row.get("desc").toString().substring(0, row.get("desc").toString().indexOf("@"));
a.setTitle(fullName);
a.setWikiID(Integer.parseInt(row.get("wiki").toString().substring(0, row.get("wiki").toString().indexOf("^"))));
if (!row.get("thumb").toString().isEmpty())
a.setPictureURL(row.get("thumb").toString());
else
a.setPictureURL("http://www.google.fr/imgres?imgurl=http%3A%2F%2Fwww.elated.com%2Fres%2FImage%2Farticles%2Fmanagement%2Fapache%2Fmaking-a-custom-error-page%2Fapache_404_default.gif&imgrefurl=https%3A%2F%2Fgithub.com%2Fjdorn%2Fphp-reports%2Fissues%2F43&h=241&w=400&tbnid=KQI5AbkkVp3-uM%3A&zoom=1&docid=6Bd7CTaQ291_UM&ei=5AU0VceoI87WPYOvgCg&tbm=isch&iact=rc&uact=3&dur=3255&page=1&start=0&ndsp=20&ved=0CDYQrQMwBw");
a.setBiography(biography);
temp.add(a);
System.out.println("FAAT" + a.getTitle());
}
return temp;
}
private String addPrefix(String prefix) { return "PREFIX " + prefix + "\n"; }
private String addQuery(String query) { return query; }
}
Upvotes: 0
Views: 538
Reputation: 19
ok i figured it out after logging several things... First ive loged the resultset size, with the optional filter it gave me more results than if i put no optional filter... 2383 vs 2103(without optional).. then i noticied that if
a.setPictureURL(row.get("thumb2").toString());
is empty or null the loop dont continue... it stoped after 14 results and gave me no return for this method :
private LinkedList<Entity> collectAuthors(ResultSet results) {
LinkedList<Entity> temp = new LinkedList<>();
while (results.hasNext()) {
Entity a = new Entity();
QuerySolution row = results.next();
String fullName = row.get("name").toString().substring(0, row.get("name").toString().indexOf("@"));
String biography = row.get("desc").toString().substring(0, row.get("desc").toString().indexOf("@"));
a.setTitle(fullName);
a.setWikiID(Integer.parseInt(row.get("wiki").toString().substring(0, row.get("wiki").toString().indexOf("^"))));
if (!row.get("thumb").toString().isEmpty())
a.setPictureURL(row.get("thumb").toString());
a.setBiography(biography);
temp.add(a);
System.out.println("FAAT" + a.getTitle());
}
return temp;
}
so i just checked with if(row.getResource("thumb2")!=null)
like that
if(row.getResource("thumb")!=null)
a.setPictureURL(row.get("thumb").toString());
and then the loop continued ---> problem solved
Thanks everyone for your hints and be carefull of empty reslutset columns with Jena
Upvotes: 0