public static RDFNode fetchScalar(Dataset m,Query query) throws Exception {
return fetchScalar(m,query,null);
}
public static RDFNode fetchScalar(Dataset m,Query query,QuerySolution bindings) throws Exception {
QueryExecution qe=QueryExecutionFactory.create(query,m);
try {
if(null!=bindings) {
qe.setInitialBinding(bindings);
}
ResultSet results=qe.execSelect();
if(!results.hasNext())
return null;
List<String> vars=results.getResultVars();
if(vars.size()!=1) {
throw new Exception("Scalar query returns other than one column");
}
QuerySolution row=results.nextSolution();
RDFNode value=row.get(vars.get(0));
if (results.hasNext()) {
throw new Exception("Scalar query returns more than one row");
}
return value;
} finally { qe.close(); }
}