use of org.openrdf.query.Binding in project blueprints by tinkerpop.
the class SailGraph method executeSparql.
/**
* Evaluate a SPARQL query against the SailGraph (http://www.w3.org/TR/rdf-sparql-query/). The result is a mapping between the ?-bindings and the bound URI, blank node, or literal represented as a Vertex.
*
* @param sparqlQuery the SPARQL query to evaluate
* @return the mapping between a ?-binding and the URI, blank node, or literal as a Vertex
* @throws RuntimeException if an error occurs in the SPARQL query engine
*/
public List<Map<String, Vertex>> executeSparql(String sparqlQuery) throws RuntimeException {
try {
sparqlQuery = getPrefixes() + sparqlQuery;
final SPARQLParser parser = new SPARQLParser();
final ParsedQuery query = parser.parseQuery(sparqlQuery, null);
boolean includeInferred = false;
final CloseableIteration<? extends BindingSet, QueryEvaluationException> results = this.sailConnection.get().evaluate(query.getTupleExpr(), query.getDataset(), new MapBindingSet(), includeInferred);
final List<Map<String, Vertex>> returnList = new ArrayList<Map<String, Vertex>>();
try {
while (results.hasNext()) {
BindingSet bs = results.next();
Map<String, Vertex> returnMap = new HashMap<String, Vertex>();
for (Binding b : bs) {
returnMap.put(b.getName(), this.getVertex(b.getValue().toString()));
}
returnList.add(returnMap);
}
} finally {
results.close();
}
return returnList;
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
Aggregations