org.eclipse.rdf4j.query.Binding Java Examples
The following examples show how to use
org.eclipse.rdf4j.query.Binding.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ValueMappingBindingSet.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
private Binding mapBinding(Binding binding) { return new Binding() { @Override public String getName() { return binding.getName(); } @Override public Value getValue() { return mapper.apply(binding.getValue()); } @Override public String toString() { return getName() + "=" + getValue().toString(); } }; }
Example #2
Source File: ValueMappingBindingSet.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Override public Iterator<Binding> iterator() { return new Iterator<Binding>() { Iterator<Binding> idelegate = delegate.iterator(); @Override public boolean hasNext() { return idelegate.hasNext(); } @Override public Binding next() { return mapBinding(idelegate.next()); } }; }
Example #3
Source File: SPARQLCSVTupleBackgroundTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
private static boolean bindingSetsMatch(BindingSet bs1, BindingSet bs2, Map<BNode, BNode> bNodeMapping) { if (bs1.size() != bs2.size()) { return false; } for (Binding binding1 : bs1) { Value value1 = binding1.getValue(); Value value2 = bs2.getValue(binding1.getName()); if (value1 == null && value2 != null) { return false; } else if (value1 != null && value2 == null) { return false; } else if (value1 != null && value2 != null) { if (!CSVQueryResultsComparisons.equals(value1, value2) && !value1.stringValue().equals(value2.stringValue())) { return false; } } } return true; }
Example #4
Source File: DAWGTestResultSetParser.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
private void reportSolution(Resource solutionNode, List<String> bindingNames) throws RDFHandlerException { MapBindingSet bindingSet = new MapBindingSet(bindingNames.size()); for (Value bindingNode : Models.getProperties(graph, solutionNode, BINDING)) { if (bindingNode instanceof Resource) { Binding binding = getBinding((Resource) bindingNode); bindingSet.addBinding(binding); } else { throw new RDFHandlerException("Value for " + BINDING + " is not a resource: " + bindingNode); } } try { tqrHandler.handleSolution(bindingSet); } catch (TupleQueryResultHandlerException e) { throw new RDFHandlerException(e.getMessage(), e); } }
Example #5
Source File: TimeAwareHBaseSail.java From Halyard with Apache License 2.0 | 6 votes |
@Override public void removeStatement(UpdateContext op, Resource subj, IRI pred, Value obj, Resource... contexts) throws SailException { BindingSet bs; Binding b; Value v; long timestamp = (op != null && (bs = op.getBindingSet()) != null && (b = bs.getBinding(TIMESTAMP_CALLBACK_BINDING_NAME)) != null) ? longValueOfTimeStamp((Literal) b.getValue()) : System.currentTimeMillis(); try { for (Resource ctx : normalizeContexts(contexts)) { for (KeyValue kv : HalyardTableUtils.toKeyValues(subj, pred, obj, ctx, true, timestamp)) { //calculate the kv's corresponding to the quad (or triple) delete(kv); } } } catch (IOException e) { throw new SailException(e); } }
Example #6
Source File: SPARQLUpdateOperation.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Override public void execute(RepositoryConnection con) throws RepositoryException { try { Update preparedUpdate = con.prepareUpdate(QueryLanguage.SPARQL, getUpdateString(), getBaseURI()); preparedUpdate.setIncludeInferred(isIncludeInferred()); preparedUpdate.setDataset(getDataset()); if (getBindings() != null) { for (Binding binding : getBindings()) { preparedUpdate.setBinding(binding.getName(), binding.getValue()); } } preparedUpdate.execute(); } catch (MalformedQueryException | UpdateExecutionException e) { throw new RepositoryException(e); } }
Example #7
Source File: SAILFederatedService.java From CostFed with GNU Affero General Public License v3.0 | 6 votes |
@Override public CloseableIteration<BindingSet, QueryEvaluationException> select(Service service, Set<String> projectionVars, BindingSet bindings, String baseUri) throws QueryEvaluationException { RepositoryConnection conn = endpoint.getConn(); try { TupleQuery query = conn.prepareTupleQuery(QueryLanguage.SPARQL, service.getSelectQueryString(projectionVars), 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 qRes = query.evaluate(); return new InsertBindingsIteration(qRes, bindings); } catch(Throwable e) { throw new QueryEvaluationException(e); } finally { conn.close(); } }
Example #8
Source File: SPARQLCSVTupleTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
private static boolean bindingSetsMatch(BindingSet bs1, BindingSet bs2, Map<BNode, BNode> bNodeMapping) { if (bs1.size() != bs2.size()) { return false; } for (Binding binding1 : bs1) { Value value1 = binding1.getValue(); Value value2 = bs2.getValue(binding1.getName()); if (value1 == null && value2 != null) { return false; } else if (value1 != null && value2 == null) { return false; } else if (value1 != null && value2 != null) { if (!CSVQueryResultsComparisons.equals(value1, value2) && !value1.stringValue().equals(value2.stringValue())) { return false; } } } return true; }
Example #9
Source File: SolrSailExample.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
private static void tupleQuery(String queryString, RepositoryConnection connection) throws QueryEvaluationException, RepositoryException, MalformedQueryException { System.out.println("Running query: \n" + queryString); TupleQuery query = connection.prepareTupleQuery(QueryLanguage.SPARQL, queryString); try (TupleQueryResult result = query.evaluate()) { // print the results System.out.println("Query results:"); while (result.hasNext()) { BindingSet bindings = result.next(); System.out.println("found match: "); for (Binding binding : bindings) { System.out.println("\t" + binding.getName() + ": " + binding.getValue()); } } } }
Example #10
Source File: ElasticsearchSailExample.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
private static void tupleQuery(String queryString, RepositoryConnection connection) throws QueryEvaluationException, RepositoryException, MalformedQueryException { System.out.println("Running query: \n" + queryString); TupleQuery query = connection.prepareTupleQuery(QueryLanguage.SPARQL, queryString); try (TupleQueryResult result = query.evaluate()) { // print the results System.out.println("Query results:"); while (result.hasNext()) { BindingSet bindings = result.next(); System.out.println("found match: "); for (Binding binding : bindings) { System.out.println("\t" + binding.getName() + ": " + binding.getValue()); } } } }
Example #11
Source File: SPARQLProtocolSession.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
protected HttpUriRequest getUpdateMethod(QueryLanguage ql, String update, String baseURI, Dataset dataset, boolean includeInferred, int maxQueryTime, Binding... bindings) { HttpPost method = new HttpPost(getUpdateURL()); method.setHeader("Content-Type", Protocol.FORM_MIME_TYPE + "; charset=utf-8"); List<NameValuePair> queryParams = getUpdateMethodParameters(ql, update, baseURI, dataset, includeInferred, maxQueryTime, bindings); method.setEntity(new UrlEncodedFormEntity(queryParams, UTF8)); if (this.additionalHttpHeaders != null) { for (Map.Entry<String, String> additionalHeader : additionalHttpHeaders.entrySet()) { method.addHeader(additionalHeader.getKey(), additionalHeader.getValue()); } } return method; }
Example #12
Source File: BoundJoinVALUESConversionIteration.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Override protected BindingSet convert(BindingSet bIn) throws QueryEvaluationException { QueryBindingSet res = new QueryBindingSet(); int bIndex = Integer.parseInt(bIn.getBinding(INDEX_BINDING_NAME).getValue().stringValue()); Iterator<Binding> bIter = bIn.iterator(); while (bIter.hasNext()) { Binding b = bIter.next(); if (b.getName().equals(INDEX_BINDING_NAME)) { continue; } res.addBinding(b); } for (Binding bs : bindings.get(bIndex)) { res.setBinding(bs); } return res; }
Example #13
Source File: RDF4JProtocolSession.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override protected List<NameValuePair> getQueryMethodParameters(QueryLanguage ql, String query, String baseURI, Dataset dataset, boolean includeInferred, int maxQueryTime, Binding... bindings) { Objects.requireNonNull(ql, "QueryLanguage may not be null"); List<NameValuePair> queryParams = new ArrayList<>(); queryParams.add(new BasicNameValuePair(Protocol.QUERY_LANGUAGE_PARAM_NAME, ql.getName())); queryParams.add(new BasicNameValuePair(Protocol.QUERY_PARAM_NAME, query)); if (baseURI != null) { queryParams.add(new BasicNameValuePair(Protocol.BASEURI_PARAM_NAME, baseURI)); } queryParams .add(new BasicNameValuePair(Protocol.INCLUDE_INFERRED_PARAM_NAME, Boolean.toString(includeInferred))); if (maxQueryTime > 0) { queryParams.add(new BasicNameValuePair(Protocol.TIMEOUT_PARAM_NAME, Integer.toString(maxQueryTime))); } if (dataset != null) { for (IRI defaultGraphURI : dataset.getDefaultGraphs()) { queryParams.add( new BasicNameValuePair(Protocol.DEFAULT_GRAPH_PARAM_NAME, String.valueOf(defaultGraphURI))); } for (IRI namedGraphURI : dataset.getNamedGraphs()) { queryParams.add(new BasicNameValuePair(Protocol.NAMED_GRAPH_PARAM_NAME, String.valueOf(namedGraphURI))); } } for (int i = 0; i < bindings.length; i++) { String paramName = Protocol.BINDING_PREFIX + bindings[i].getName(); String paramValue = Protocol.encodeValue(bindings[i].getValue()); queryParams.add(new BasicNameValuePair(paramName, paramValue)); } return queryParams; }
Example #14
Source File: QueryBindingSet.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
public void addAll(BindingSet bindingSet) { if (bindingSet instanceof QueryBindingSet) { bindings.putAll(((QueryBindingSet) bindingSet).bindings); } else { for (Binding binding : bindingSet) { this.addBinding(binding); } } }
Example #15
Source File: RDF4JProtocolSession.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override protected HttpUriRequest getUpdateMethod(QueryLanguage ql, String update, String baseURI, Dataset dataset, boolean includeInferred, int maxExecutionTime, Binding... bindings) { RequestBuilder builder = null; String transactionURL = getTransactionURL(); if (transactionURL != null) { builder = RequestBuilder.put(transactionURL); builder.addHeader("Content-Type", Protocol.SPARQL_UPDATE_MIME_TYPE + "; charset=utf-8"); builder.addParameter(Protocol.ACTION_PARAM_NAME, Action.UPDATE.toString()); for (NameValuePair nvp : getUpdateMethodParameters(ql, null, baseURI, dataset, includeInferred, maxExecutionTime, bindings)) { builder.addParameter(nvp); } // in a PUT request, we carry the only actual update string as the // request body - the rest is sent as request parameters builder.setEntity(new StringEntity(update, UTF8)); pingTransaction(); } else { builder = RequestBuilder.post(getUpdateURL()); builder.addHeader("Content-Type", Protocol.FORM_MIME_TYPE + "; charset=utf-8"); builder.setEntity(new UrlEncodedFormEntity(getUpdateMethodParameters(ql, update, baseURI, dataset, includeInferred, maxExecutionTime, bindings), UTF8)); } // functionality to provide custom http headers as required by the // applications for (Map.Entry<String, String> additionalHeader : getAdditionalHttpHeaders().entrySet()) { builder.addHeader(additionalHeader.getKey(), additionalHeader.getValue()); } return builder.build(); }
Example #16
Source File: AbstractHTTPUpdate.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
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; }
Example #17
Source File: KryoVisibilityBindingSetSerializer.java From rya with Apache License 2.0 | 5 votes |
@Override public void write(final Kryo kryo, final Output output, final VisibilityBindingSet visBindingSet) { output.writeString(visBindingSet.getVisibility()); // write the number count for the reader. output.writeInt(visBindingSet.size()); for (final Binding binding : visBindingSet) { output.writeString(binding.getName()); final RyaType ryaValue = RdfToRyaConversions.convertValue(binding.getValue()); final String valueString = ryaValue.getData(); final IRI type = ryaValue.getDataType(); output.writeString(valueString); output.writeString(type.toString()); } }
Example #18
Source File: TimeAwareHBaseSail.java From Halyard with Apache License 2.0 | 5 votes |
@Override public void addStatement(UpdateContext op, Resource subj, IRI pred, Value obj, Resource... contexts) throws SailException { BindingSet bs; Binding b; Value v; long timestamp = (op != null && (bs = op.getBindingSet()) != null && (b = bs.getBinding(TIMESTAMP_CALLBACK_BINDING_NAME)) != null) ? longValueOfTimeStamp((Literal) b.getValue()) : System.currentTimeMillis(); for (Resource ctx : normalizeContexts(contexts)) { addStatementInternal(subj, pred, obj, ctx, timestamp); } }
Example #19
Source File: QueryBindingSet.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public Binding getBinding(String bindingName) { Value value = getValue(bindingName); if (value != null) { return new SimpleBinding(bindingName, value); } return null; }
Example #20
Source File: PipelineResultIteration.java From rya with Apache License 2.0 | 5 votes |
private QueryBindingSet docToBindingSet(Document result) { QueryBindingSet bindingSet = new QueryBindingSet(bindings); Document valueSet = result.get(AggregationPipelineQueryNode.VALUES, Document.class); Document typeSet = result.get(AggregationPipelineQueryNode.TYPES, Document.class); if (valueSet != null) { for (Map.Entry<String, Object> entry : valueSet.entrySet()) { String fieldName = entry.getKey(); String valueString = entry.getValue().toString(); String typeString = typeSet == null ? null : typeSet.getString(fieldName); String varName = varToOriginalName.getOrDefault(fieldName, fieldName); Value varValue; if (typeString == null || typeString.equals(XMLSchema.ANYURI.stringValue())) { varValue = VF.createIRI(valueString); } else { varValue = VF.createLiteral(valueString, VF.createIRI(typeString)); } Binding existingBinding = bindingSet.getBinding(varName); // If this variable is not already bound, add it. if (existingBinding == null) { bindingSet.addBinding(varName, varValue); } // If it's bound to something else, the solutions are incompatible. else if (!existingBinding.getValue().equals(varValue)) { return null; } } } return bindingSet; }
Example #21
Source File: DAWGTestResultSetWriter.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public void handleSolution(BindingSet bindingSet) throws TupleQueryResultHandlerException { try { BNode solutionNode = vf.createBNode(); reportStatement(resultSetNode, SOLUTION, solutionNode); for (Binding binding : bindingSet) { BNode bindingNode = vf.createBNode(); reportStatement(solutionNode, BINDING, bindingNode); reportStatement(bindingNode, VARIABLE, vf.createLiteral(binding.getName())); Value value = binding.getValue(); // Map bnodes to new bnodes to prevent collisions with the bnodes // generated for the result format if (value instanceof BNode) { BNode mappedBNode = bnodeMap.get(value); if (mappedBNode == null) { mappedBNode = vf.createBNode(); bnodeMap.put((BNode) value, mappedBNode); } value = mappedBNode; } reportStatement(bindingNode, VALUE, value); } } catch (RDFHandlerException e) { throw new TupleQueryResultHandlerException(e); } }
Example #22
Source File: SPARQLProtocolSession.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
protected List<NameValuePair> getQueryMethodParameters(QueryLanguage ql, String query, String baseURI, Dataset dataset, boolean includeInferred, int maxQueryTime, Binding... bindings) { List<NameValuePair> queryParams = new ArrayList<>(); /* * Only query, default-graph-uri, and named-graph-uri are standard parameters in SPARQL Protocol 1.1. */ if (query != null) { if (baseURI != null && !baseURI.isEmpty()) { // prepend query string with base URI declaration query = "BASE <" + baseURI + "> \n" + query; } queryParams.add(new BasicNameValuePair(Protocol.QUERY_PARAM_NAME, query)); } if (dataset != null) { for (IRI defaultGraphURI : dataset.getDefaultGraphs()) { queryParams.add( new BasicNameValuePair(Protocol.DEFAULT_GRAPH_PARAM_NAME, String.valueOf(defaultGraphURI))); } for (IRI namedGraphURI : dataset.getNamedGraphs()) { queryParams.add(new BasicNameValuePair(Protocol.NAMED_GRAPH_PARAM_NAME, String.valueOf(namedGraphURI))); } } return queryParams; }
Example #23
Source File: ServiceJoinConversionIteration.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override protected BindingSet convert(BindingSet bIn) throws QueryEvaluationException { // overestimate the capacity SPARQLQueryBindingSet res = new SPARQLQueryBindingSet(bIn.size() + bindings.size()); int bIndex = -1; Iterator<Binding> bIter = bIn.iterator(); while (bIter.hasNext()) { Binding b = bIter.next(); String name = b.getName(); if (name.equals("__rowIdx")) { bIndex = Integer.parseInt(b.getValue().stringValue()); continue; } res.addBinding(b.getName(), b.getValue()); } // should never occur: in such case we would have to create the cross product (which // is dealt with in another place) if (bIndex == -1) { throw new QueryEvaluationException( "Invalid join. Probably this is due to non-standard behavior of the SPARQL endpoint. " + "Please report to the developers."); } res.addAll(bindings.get(bIndex)); return res; }
Example #24
Source File: SimpleBinding.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public boolean equals(Object o) { if (o instanceof Binding) { Binding other = (Binding) o; return name.equals(other.getName()) && value.equals(other.getValue()); } return false; }
Example #25
Source File: OneOfVisitorTest.java From rya with Apache License 2.0 | 5 votes |
private static void assertBindingSet(final Iterator<BindingSet> bindingSetIter, final Iterator<Resource> expectedValues) { while (expectedValues.hasNext()) { final Resource expectedValue = expectedValues.next(); assertTrue(bindingSetIter.hasNext()); final BindingSet bindingSet = bindingSetIter.next(); assertTrue(bindingSet instanceof QueryBindingSet); assertEquals(1, bindingSet.getBindingNames().size()); final Binding binding = bindingSet.getBinding("s"); assertNotNull(binding); final Value actualValue = binding.getValue(); assertEquals(expectedValue, actualValue); } }
Example #26
Source File: MapBindingSet.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public Value getValue(String bindingName) { Binding binding = getBinding(bindingName); if (binding != null) { return binding.getValue(); } return null; }
Example #27
Source File: SPARQLProtocolSession.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
public GraphQueryResult sendGraphQuery(QueryLanguage ql, String query, String baseURI, Dataset dataset, boolean includeInferred, int maxQueryTime, Binding... bindings) throws IOException, RepositoryException, MalformedQueryException, UnauthorizedException, QueryInterruptedException { try { HttpUriRequest method = getQueryMethod(ql, query, baseURI, dataset, includeInferred, maxQueryTime, bindings); return getRDFBackground(method, false); } catch (RDFHandlerException e) { // Found a bug in TupleQueryResultBuilder? throw new RepositoryException(e); } }
Example #28
Source File: AbstractSPARQLJSONWriter.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override protected void handleSolutionImpl(BindingSet bindingSet) throws TupleQueryResultHandlerException { try { if (!documentOpen) { startDocument(); } if (!headerOpen) { startHeader(); } if (!headerComplete) { endHeader(); } if (!tupleVariablesFound) { throw new IllegalStateException("Must call startQueryResult before handleSolution"); } firstTupleWritten = true; jg.writeStartObject(); Iterator<Binding> bindingIter = bindingSet.iterator(); while (bindingIter.hasNext()) { Binding binding = bindingIter.next(); jg.writeFieldName(binding.getName()); writeValue(binding.getValue()); } jg.writeEndObject(); } catch (IOException | QueryResultHandlerException e) { throw new TupleQueryResultHandlerException(e); } }
Example #29
Source File: SPARQLCSVTupleTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
private boolean matchBindingSets(List<? extends BindingSet> queryResult1, Iterable<? extends BindingSet> queryResult2, Map<BNode, BNode> bNodeMapping, int idx) { boolean result = false; if (idx < queryResult1.size()) { BindingSet bs1 = queryResult1.get(idx); List<BindingSet> matchingBindingSets = findMatchingBindingSets(bs1, queryResult2, bNodeMapping); for (BindingSet bs2 : matchingBindingSets) { // Map bNodes in bs1 to bNodes in bs2 Map<BNode, BNode> newBNodeMapping = new HashMap<>(bNodeMapping); for (Binding binding : bs1) { if (binding.getValue() instanceof BNode) { newBNodeMapping.put((BNode) binding.getValue(), (BNode) bs2.getValue(binding.getName())); } } // FIXME: this recursive implementation has a high risk of // triggering a stack overflow // Enter recursion result = matchBindingSets(queryResult1, queryResult2, newBNodeMapping, idx + 1); if (result == true) { // models match, look no further break; } } } else { // All statements have been mapped successfully result = true; } return result; }
Example #30
Source File: SPARQLCSVTupleBackgroundTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
private boolean matchBindingSets(List<? extends BindingSet> queryResult1, Iterable<? extends BindingSet> queryResult2, Map<BNode, BNode> bNodeMapping, int idx) { boolean result = false; if (idx < queryResult1.size()) { BindingSet bs1 = queryResult1.get(idx); List<BindingSet> matchingBindingSets = findMatchingBindingSets(bs1, queryResult2, bNodeMapping); for (BindingSet bs2 : matchingBindingSets) { // Map bNodes in bs1 to bNodes in bs2 Map<BNode, BNode> newBNodeMapping = new HashMap<>(bNodeMapping); for (Binding binding : bs1) { if (binding.getValue() instanceof BNode) { newBNodeMapping.put((BNode) binding.getValue(), (BNode) bs2.getValue(binding.getName())); } } // FIXME: this recursive implementation has a high risk of // triggering a stack overflow // Enter recursion result = matchBindingSets(queryResult1, queryResult2, newBNodeMapping, idx + 1); if (result == true) { // models match, look no further break; } } } else { // All statements have been mapped successfully result = true; } return result; }