Semweb4j/filesQueries/step7
From semanticweb.org
Up | Previous step | Next step
[edit] Step 7: SPARQL ASK
As in step 5, we don't explain the basic setup again.
[edit] ASK queries
ASK queries are always executed via model.sparqlAsk which returns a boolean.
"ASK xyz" returns True if xyz evaluates to a non-empty set and False if xyz evaluates to an empty set.
Ask if max is subject or object of any statement:
System.out.println("Query 1:");
String queryString = "ASK { { "+max.toSPARQL()+" ?p ?o } UNION { ?s ?p "+max.toSPARQL()+" } }";
boolean result = model.sparqlAsk(queryString);
if(result) {
System.out.println(max+" is in the graph");
}
where UNION is a set union of these graphs in SPARQL syntax.
Ask if any resource is tagged with tagComputers:
System.out.println("Query 2:");
queryString = "ASK { ?resource <"+hasTag+"> "+tagComputers.toSPARQL()+" }";
if(!model.sparqlAsk(queryString)) {
System.out.println("nothing is tagged with "+tagComputers);
}
[edit] output
Query 1: http://xam.de/foaf.rdf.xml#i is in the graph Query 2: nothing is tagged with Computers