use of com.hp.hpl.jena.rdf.model.impl.ResourceImpl in project goci by EBISPOT.
the class SparqlTemplate method query.
public <T> T query(String sparql, ResultSetMapper<T> rsm, Object... args) {
sparql = getPrefixString().concat(sparql);
Graph g = getJenaQueryExecutionService().getDefaultGraph();
Map<String, Object> bindingMap = new HashMap<String, Object>();
int i = 0;
for (Object o : args) {
String argName = "?_arg" + i++;
sparql = sparql.replaceFirst("\\?\\?", argName);
bindingMap.put(argName, o);
}
Query q1 = QueryFactory.create(sparql, Syntax.syntaxARQ);
QuerySolutionMap initialBinding = new QuerySolutionMap();
for (String argName : bindingMap.keySet()) {
Object argValue = bindingMap.get(argName);
RDFNode arg;
if (argValue instanceof URI) {
arg = new ResourceImpl(argValue.toString());
} else {
arg = getLiteralNode(argValue);
}
initialBinding.add(argName, arg);
}
ParameterizedSparqlString queryString = new ParameterizedSparqlString(q1.toString(), initialBinding);
QueryExecution execute = null;
try {
execute = getJenaQueryExecutionService().getQueryExecution(g, queryString.asQuery(), false);
ResultSet results = execute.execSelect();
return rsm.mapResultSet(results);
} catch (LodeException e) {
throw new SparqlQueryException("Failed to execute query '" + sparql + "'", e);
} finally {
if (execute != null) {
execute.close();
if (g != null) {
g.close();
}
}
}
}
Aggregations