Search in sources :

Example 6 with Binding

use of org.eclipse.rdf4j.query.Binding in project rdf4j by eclipse.

the class RepositoryFederatedService method select.

/**
 * Evaluate the provided sparqlQueryString at the initialized {@link Repository} of this
 * {@link FederatedService}. Insert bindings into SELECT query and evaluate
 */
@Override
public CloseableIteration<BindingSet, QueryEvaluationException> select(Service service, Set<String> projectionVars, BindingSet bindings, String baseUri) throws QueryEvaluationException {
    try {
        String sparqlQueryString = service.getSelectQueryString(projectionVars);
        TupleQuery query = getConnection().prepareTupleQuery(QueryLanguage.SPARQL, sparqlQueryString, baseUri);
        Iterator<Binding> bIter = bindings.iterator();
        while (bIter.hasNext()) {
            Binding b = bIter.next();
            if (service.getServiceVars().contains(b.getName()))
                query.setBinding(b.getName(), b.getValue());
        }
        TupleQueryResult res = query.evaluate();
        // insert original bindings again
        InsertBindingSetCursor result = new InsertBindingSetCursor(res, bindings);
        if (service.isSilent())
            return new SilentIteration(result);
        else
            return result;
    } catch (MalformedQueryException e) {
        throw new QueryEvaluationException(e);
    } catch (RepositoryException e) {
        throw new QueryEvaluationException("Repository for endpoint " + rep.toString() + " could not be initialized.", e);
    }
}
Also used : Binding(org.eclipse.rdf4j.query.Binding) InsertBindingSetCursor(org.eclipse.rdf4j.repository.sparql.query.InsertBindingSetCursor) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) TupleQuery(org.eclipse.rdf4j.query.TupleQuery) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) TupleQueryResult(org.eclipse.rdf4j.query.TupleQueryResult)

Example 7 with Binding

use of org.eclipse.rdf4j.query.Binding in project rdf4j by eclipse.

the class RepositoryFederatedService method ask.

/**
 * Evaluate the provided sparqlQueryString at the initialized {@link Repository} of this
 * {@link FederatedService}. Insert bindings, send ask query and return final result
 */
@Override
public boolean ask(Service service, BindingSet bindings, String baseUri) throws QueryEvaluationException {
    try {
        String sparqlQueryString = service.getAskQueryString();
        BooleanQuery query = getConnection().prepareBooleanQuery(QueryLanguage.SPARQL, sparqlQueryString, baseUri);
        Iterator<Binding> bIter = bindings.iterator();
        while (bIter.hasNext()) {
            Binding b = bIter.next();
            if (service.getServiceVars().contains(b.getName()))
                query.setBinding(b.getName(), b.getValue());
        }
        return query.evaluate();
    } catch (MalformedQueryException e) {
        throw new QueryEvaluationException(e);
    } catch (RepositoryException e) {
        throw new QueryEvaluationException("Repository for endpoint " + rep.toString() + " could not be initialized.", e);
    }
}
Also used : Binding(org.eclipse.rdf4j.query.Binding) BooleanQuery(org.eclipse.rdf4j.query.BooleanQuery) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException)

Example 8 with Binding

use of org.eclipse.rdf4j.query.Binding in project rdf4j by eclipse.

the class InsertBindingSetCursor method next.

@Override
public BindingSet next() throws QueryEvaluationException {
    BindingSet next = super.next();
    if (next == null) {
        return null;
    }
    int size = bindings.size() + next.size();
    SPARQLQueryBindingSet set = new SPARQLQueryBindingSet(size);
    set.addAll(bindings);
    for (Binding binding : next) {
        set.setBinding(binding);
    }
    return set;
}
Also used : Binding(org.eclipse.rdf4j.query.Binding) BindingSet(org.eclipse.rdf4j.query.BindingSet)

Example 9 with Binding

use of org.eclipse.rdf4j.query.Binding in project rdf4j by eclipse.

the class AbstractHTTPQuery method getBindingsArray.

public Binding[] getBindingsArray() {
    BindingSet bindings = this.getBindings();
    Binding[] bindingsArray = new Binding[bindings.size()];
    Iterator<Binding> iter = bindings.iterator();
    for (int i = 0; i < bindings.size(); i++) {
        bindingsArray[i] = iter.next();
    }
    return bindingsArray;
}
Also used : Binding(org.eclipse.rdf4j.query.Binding) BindingSet(org.eclipse.rdf4j.query.BindingSet)

Example 10 with Binding

use of org.eclipse.rdf4j.query.Binding in project rdf4j by eclipse.

the class TransactionWriter method serialize.

protected void serialize(SPARQLUpdateOperation op, XMLWriter xmlWriter) throws IOException {
    String baseURI = op.getBaseURI();
    if (baseURI != null) {
        xmlWriter.setAttribute(TransactionXMLConstants.BASE_URI_ATT, baseURI);
    }
    xmlWriter.setAttribute(TransactionXMLConstants.INCLUDE_INFERRED_ATT, op.isIncludeInferred());
    xmlWriter.startTag(TransactionXMLConstants.SPARQL_UPDATE_TAG);
    // serialize update string
    String updateString = op.getUpdateString();
    xmlWriter.textElement(TransactionXMLConstants.UPDATE_STRING_TAG, updateString);
    // serialize dataset definition (if any)
    Dataset dataset = op.getDataset();
    if (dataset != null) {
        xmlWriter.startTag(TransactionXMLConstants.DATASET_TAG);
        xmlWriter.startTag(TransactionXMLConstants.DEFAULT_GRAPHS_TAG);
        for (IRI defaultGraph : dataset.getDefaultGraphs()) {
            xmlWriter.textElement(TransactionXMLConstants.GRAPH_TAG, defaultGraph.stringValue());
        }
        xmlWriter.endTag(TransactionXMLConstants.DEFAULT_GRAPHS_TAG);
        xmlWriter.startTag(TransactionXMLConstants.NAMED_GRAPHS_TAG);
        for (IRI namedGraph : dataset.getNamedGraphs()) {
            xmlWriter.textElement(TransactionXMLConstants.GRAPH_TAG, namedGraph.stringValue());
        }
        xmlWriter.endTag(TransactionXMLConstants.NAMED_GRAPHS_TAG);
        xmlWriter.startTag(TransactionXMLConstants.DEFAULT_REMOVE_GRAPHS_TAG);
        for (IRI defaultRemoveGraph : dataset.getDefaultRemoveGraphs()) {
            xmlWriter.textElement(TransactionXMLConstants.GRAPH_TAG, defaultRemoveGraph.stringValue());
        }
        xmlWriter.endTag(TransactionXMLConstants.DEFAULT_REMOVE_GRAPHS_TAG);
        if (dataset.getDefaultInsertGraph() != null) {
            xmlWriter.textElement(TransactionXMLConstants.DEFAULT_INSERT_GRAPH, dataset.getDefaultInsertGraph().stringValue());
        }
        xmlWriter.endTag(TransactionXMLConstants.DATASET_TAG);
    }
    if (op.getBindings() != null && op.getBindings().length > 0) {
        xmlWriter.startTag(TransactionXMLConstants.BINDINGS);
        for (Binding binding : op.getBindings()) {
            if (binding.getName() != null && binding.getValue() != null && binding.getValue().stringValue() != null) {
                if (binding.getValue() instanceof IRI) {
                    xmlWriter.setAttribute(TransactionXMLConstants.NAME_ATT, binding.getName());
                    xmlWriter.textElement(TransactionXMLConstants.BINDING_URI, binding.getValue().stringValue());
                }
                if (binding.getValue() instanceof BNode) {
                    xmlWriter.setAttribute(TransactionXMLConstants.NAME_ATT, binding.getName());
                    xmlWriter.textElement(TransactionXMLConstants.BINDING_BNODE, binding.getValue().stringValue());
                }
                if (binding.getValue() instanceof Literal) {
                    xmlWriter.setAttribute(TransactionXMLConstants.NAME_ATT, binding.getName());
                    Literal literal = (Literal) binding.getValue();
                    if (Literals.isLanguageLiteral(literal)) {
                        xmlWriter.setAttribute(TransactionXMLConstants.LANGUAGE_ATT, literal.getLanguage().get());
                    } else {
                        xmlWriter.setAttribute(TransactionXMLConstants.DATA_TYPE_ATT, literal.getDatatype().stringValue());
                    }
                    xmlWriter.textElement(TransactionXMLConstants.BINDING_LITERAL, binding.getValue().stringValue());
                }
            }
        }
        xmlWriter.endTag(TransactionXMLConstants.BINDINGS);
    }
    xmlWriter.endTag(TransactionXMLConstants.SPARQL_UPDATE_TAG);
}
Also used : Binding(org.eclipse.rdf4j.query.Binding) IRI(org.eclipse.rdf4j.model.IRI) BNode(org.eclipse.rdf4j.model.BNode) Dataset(org.eclipse.rdf4j.query.Dataset) Literal(org.eclipse.rdf4j.model.Literal)

Aggregations

Binding (org.eclipse.rdf4j.query.Binding)18 Value (org.eclipse.rdf4j.model.Value)6 BindingSet (org.eclipse.rdf4j.query.BindingSet)5 BNode (org.eclipse.rdf4j.model.BNode)4 TupleQueryResultHandlerException (org.eclipse.rdf4j.query.TupleQueryResultHandlerException)4 IRI (org.eclipse.rdf4j.model.IRI)3 MalformedQueryException (org.eclipse.rdf4j.query.MalformedQueryException)3 QueryEvaluationException (org.eclipse.rdf4j.query.QueryEvaluationException)3 SimpleBinding (org.eclipse.rdf4j.query.impl.SimpleBinding)3 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Literal (org.eclipse.rdf4j.model.Literal)2 QueryResultHandlerException (org.eclipse.rdf4j.query.QueryResultHandlerException)2 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)2 SAXException (org.xml.sax.SAXException)2 ClearNamespacesOperation (org.eclipse.rdf4j.http.protocol.transaction.operations.ClearNamespacesOperation)1 RemoveNamespaceOperation (org.eclipse.rdf4j.http.protocol.transaction.operations.RemoveNamespaceOperation)1 SPARQLUpdateOperation (org.eclipse.rdf4j.http.protocol.transaction.operations.SPARQLUpdateOperation)1 SetNamespaceOperation (org.eclipse.rdf4j.http.protocol.transaction.operations.SetNamespaceOperation)1