Search in sources :

Example 1 with Binding

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);
    }
}
Also used : Binding(org.openrdf.query.Binding) Vertex(com.tinkerpop.blueprints.Vertex) MapBindingSet(org.openrdf.query.impl.MapBindingSet) BindingSet(org.openrdf.query.BindingSet) SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) ParsedQuery(org.openrdf.query.parser.ParsedQuery) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SailException(org.openrdf.sail.SailException) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) RDFHandlerException(org.openrdf.rio.RDFHandlerException) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) MapBindingSet(org.openrdf.query.impl.MapBindingSet) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

Vertex (com.tinkerpop.blueprints.Vertex)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Binding (org.openrdf.query.Binding)1 BindingSet (org.openrdf.query.BindingSet)1 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)1 MapBindingSet (org.openrdf.query.impl.MapBindingSet)1 ParsedQuery (org.openrdf.query.parser.ParsedQuery)1 SPARQLParser (org.openrdf.query.parser.sparql.SPARQLParser)1 RDFHandlerException (org.openrdf.rio.RDFHandlerException)1 SailException (org.openrdf.sail.SailException)1