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);
}
}
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);
}
}
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;
}
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;
}
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);
}
Aggregations