Java Code Examples for org.eclipse.rdf4j.query.BindingSet#size()
The following examples show how to use
org.eclipse.rdf4j.query.BindingSet#size() .
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: SailTripleSource.java From CostFed with GNU Affero General Public License v3.0 | 6 votes |
@Override public CloseableIteration<BindingSet, QueryEvaluationException> getStatements( String preparedQuery, RepositoryConnection conn, final BindingSet bindings, final FilterValueExpr filterExpr) throws RepositoryException, MalformedQueryException, QueryEvaluationException { TupleQuery query = conn.prepareTupleQuery(QueryLanguage.SPARQL, preparedQuery, null); disableInference(query); // evaluate the query CloseableIteration<BindingSet, QueryEvaluationException> res = query.evaluate(); // apply filter and/or insert original bindings if (filterExpr!=null) { if (bindings.size()>0) res = new FilteringInsertBindingsIteration(strategy, filterExpr, bindings, res); else res = new FilteringIteration(strategy, filterExpr, res); if (!res.hasNext()) return new EmptyIteration<BindingSet, QueryEvaluationException>(); } else if (bindings.size()>0) { res = new InsertBindingsIteration(res, bindings); } return res; }
Example 2
Source File: QueryStringUtil.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * Retrieve a modified queryString into which all bindings of the given argument are replaced with their value. * * @param queryString * @param bindings * @return the modified queryString */ public static String getGraphQueryString(String queryString, BindingSet bindings) { if (bindings.size() == 0) { return queryString; } String qry = queryString; for (String name : bindings.getBindingNames()) { String replacement = valueToString(bindings.getValue(name)); if (replacement != null) { String pattern = "[\\?\\$]" + name + "(?=\\W)"; // we use Matcher.quoteReplacement to make sure things like newlines // in literal values are preserved qry = qry.replaceAll(pattern, Matcher.quoteReplacement(replacement)); } } return qry; }
Example 3
Source File: QueryStringUtil.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * Retrieve a modified queryString into which all bindings of the given argument are replaced, with the binding * names included in the SELECT clause. * * @param queryString * @param bindings * @return the modified queryString */ public static String getTupleQueryString(String queryString, BindingSet bindings) { if (bindings.size() == 0) { return queryString; } String qry = queryString; int b = qry.indexOf('{'); String select = qry.substring(0, b); String where = qry.substring(b); for (String name : bindings.getBindingNames()) { String replacement = valueToString(bindings.getValue(name)); if (replacement != null) { String pattern = "[\\?\\$]" + name + "(?=\\W)"; select = select.replaceAll(pattern, "(" + Matcher.quoteReplacement(replacement) + " as ?" + name + ")"); // we use Matcher.quoteReplacement to make sure things like newlines // in literal values // are preserved where = where.replaceAll(pattern, Matcher.quoteReplacement(replacement)); } } return select + where; }
Example 4
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 5
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 6
Source File: NoReification.java From inception with Apache License 2.0 | 5 votes |
private List<Statement> listStatements(TupleQuery aQuery, boolean aIncludeInferred) { aQuery.setIncludeInferred(aIncludeInferred); try (TupleQueryResult result = aQuery.evaluate()) { ValueFactory vf = SimpleValueFactory.getInstance(); List<Statement> statements = new ArrayList<>(); while (result.hasNext()) { BindingSet bindings = result.next(); if (bindings.size() == 0) { continue; } // LOG.trace("[{}] Bindings: {}", toHexString(hashCode()), bindings); Binding subj = bindings.getBinding(VAR_SUBJECT_NAME); Binding pred = bindings.getBinding(VAR_PREDICATE_NAME); Binding obj = bindings.getBinding(VAR_OBJECT_NAME); IRI subject = vf.createIRI(subj.getValue().stringValue()); IRI predicate = vf.createIRI(pred.getValue().stringValue()); Statement stmt = vf.createStatement(subject, predicate, obj.getValue()); // Avoid duplicate statements if (!statements.contains(stmt)) { statements.add(stmt); } } return statements; } }
Example 7
Source File: StarQuery.java From rya with Apache License 2.0 | 5 votes |
public static StarQuery getConstrainedStarQuery(final StarQuery query, final BindingSet bs) { if(bs.size() == 0) { return query; } final Set<String> bindingNames = bs.getBindingNames(); final Set<String> unCommonVarNames = query.getUnCommonVars(); final Set<String> intersectVar = Sets.intersection(bindingNames, unCommonVarNames); if (!query.commonVarConstant()) { final Value v = bs.getValue(query.getCommonVarName()); if (v != null) { query.commonVar.setValue(v); } } for(final String s: intersectVar) { try { query.nodeColumnCond[query.varPos.get(s)] = query.setValue(query.nodeColumnCond[query.varPos.get(s)], bs.getValue(s)); } catch (final RyaTypeResolverException e) { e.printStackTrace(); } } return query; }
Example 8
Source File: AbstractHTTPQuery.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 9
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 10
Source File: GroupIterator.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public void processAggregate(BindingSet s) throws QueryEvaluationException { if (getArg() != null) { Value value = evaluate(s); if (value != null && distinctValue(value)) { count++; } } else { // wildcard count if (s.size() > 0 && distinctBindingSet(s)) { count++; } } }
Example 11
Source File: BinaryQueryResultWriter.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override protected void handleSolutionImpl(BindingSet bindingSet) throws TupleQueryResultHandlerException { if (!tupleVariablesFound) { throw new IllegalStateException("Must call startQueryResult before handleSolution"); } try { if (bindingSet.size() == 0) { writeEmptyRow(); } else { for (String bindingName : bindingNames) { Value value = bindingSet.getValue(bindingName); if (value == null) { writeNull(); } else if (value.equals(previousBindings.getValue(bindingName))) { writeRepeat(); } else { writeValue(value); } } previousBindings = bindingSet; } } catch (IOException e) { throw new TupleQueryResultHandlerException(e); } }
Example 12
Source File: InsertBindingsIteration.java From CostFed with GNU Affero General Public License v3.0 | 5 votes |
@Override protected BindingSet convert(BindingSet bIn) throws QueryEvaluationException { QueryBindingSet res = new QueryBindingSet(bindings.size() + bIn.size()); res.addAll(bindings); res.addAll(bIn); return res; }
Example 13
Source File: SailTripleSource.java From CostFed with GNU Affero General Public License v3.0 | 5 votes |
@SuppressWarnings("unchecked") @Override public CloseableIteration<BindingSet, QueryEvaluationException> getStatements( TupleExpr preparedQuery, RepositoryConnection conn, BindingSet bindings, FilterValueExpr filterExpr) throws RepositoryException, MalformedQueryException, QueryEvaluationException { /* * Implementation note: * * a hook is introduced for NativeStore instances such that an extended * connection is used. The extended connection provides a method to * evaluate prepared queries without prior (obsolete) optimization. */ SailConnection sailConn = ((SailRepositoryConnection)conn).getSailConnection(); CloseableIteration<BindingSet, QueryEvaluationException> res; if (sailConn instanceof NativeStoreConnectionExt) { NativeStoreConnectionExt _conn = (NativeStoreConnectionExt)sailConn; res = (CloseableIteration<BindingSet, QueryEvaluationException>) _conn.evaluatePrecompiled(preparedQuery); } else { try { log.warn("Precompiled query optimization for native store could not be applied: use extended NativeStore initialization using NativeStoreConnectionExt"); res = (CloseableIteration<BindingSet, QueryEvaluationException>) sailConn.evaluate(preparedQuery, null, EmptyBindingSet.getInstance(), true); } catch (SailException e) { throw new QueryEvaluationException(e); } } if (bindings.size()>0) { res = new InsertBindingsIteration(res, bindings); } return res; }
Example 14
Source File: SPARQLQueryBuilder.java From inception with Apache License 2.0 | 5 votes |
/** * Method process the Tuple Query Results * * @param tupleQuery * Tuple Query Variable * @param aAll * True if entities with implicit namespaces (e.g. defined by RDF) * @return list of all the {@link KBHandle} */ private List<KBHandle> evaluateListQuery(TupleQuery tupleQuery, boolean aAll) throws QueryEvaluationException { try (TupleQueryResult result = tupleQuery.evaluate()) { List<KBHandle> handles = new ArrayList<>(); while (result.hasNext()) { BindingSet bindings = result.next(); if (bindings.size() == 0) { continue; } // LOG.trace("[{}] Bindings: {}", toHexString(hashCode()), bindings); String id = bindings.getBinding(VAR_SUBJECT_NAME).getValue().stringValue(); if (!id.contains(":") || (!aAll && hasImplicitNamespace(kb, id))) { continue; } KBHandle handle = new KBHandle(id); handle.setKB(kb); extractLabel(handle, bindings); extractDescription(handle, bindings); extractRange(handle, bindings); extractDomain(handle, bindings); handles.add(handle); } if (serverSideReduce) { return handles; } else { return reduceRedundantResults(handles); } } }
Example 15
Source File: QueryBindingSet.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
public QueryBindingSet(BindingSet bindingSet) { this(bindingSet.size()); addAll(bindingSet); }
Example 16
Source File: StrictEvaluationStrategy.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(BindingSetAssignment bsa, BindingSet bindings) throws QueryEvaluationException { final Iterator<BindingSet> iter = bsa.getBindingSets().iterator(); if (bindings.size() == 0) { // empty binding set return new CloseableIteratorIteration<>(iter); } CloseableIteration<BindingSet, QueryEvaluationException> result; final QueryBindingSet b = new QueryBindingSet(bindings); result = new LookAheadIteration<BindingSet, QueryEvaluationException>() { @Override protected BindingSet getNextElement() throws QueryEvaluationException { QueryBindingSet result = null; while (result == null && iter.hasNext()) { final BindingSet assignedBindings = iter.next(); for (String name : assignedBindings.getBindingNames()) { final Value assignedValue = assignedBindings.getValue(name); if (assignedValue != null) { // can be null if set to // UNDEF // check that the binding assignment does not // overwrite // existing bindings. Value bValue = b.getValue(name); if (bValue == null || assignedValue.equals(bValue)) { if (result == null) { result = new QueryBindingSet(b); } if (bValue == null) { // we are not overwriting an existing // binding. result.addBinding(name, assignedValue); } } else { // if values are not equal there is no // compatible // merge and we should return no next element. result = null; break; } } } } return result; } }; return result; }
Example 17
Source File: BindingAssigner.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Override public void optimize(TupleExpr tupleExpr, Dataset dataset, BindingSet bindings) { if (bindings.size() > 0) { tupleExpr.visit(new VarVisitor(bindings)); } }
Example 18
Source File: EventQueryNode.java From rya with Apache License 2.0 | 4 votes |
@Override public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(final BindingSet bindings) throws QueryEvaluationException { final List<BindingSet> list = new ArrayList<>(); try { final Collection<Event> searchEvents; final String subj; //if the provided binding set has the subject already, set it to the constant subject. if(!subjectConstant.isPresent() && bindings.hasBinding(subjectVar.get())) { subjectConstant = Optional.of(bindings.getValue(subjectVar.get()).stringValue()); } else if(bindings.size() != 0) { list.add(bindings); } // If the subject needs to be filled in, check if the subject variable is in the binding set. if(subjectConstant.isPresent()) { // if it is, fetch that value and then fetch the entity for the subject. subj = subjectConstant.get(); searchEvents = eventStore.search(Optional.of(new RyaIRI(subj)), Optional.of(geoFilters), Optional.of(temporalFilters)); } else { searchEvents = eventStore.search(Optional.empty(), Optional.of(geoFilters), Optional.of(temporalFilters)); } for(final Event event : searchEvents) { final MapBindingSet resultSet = new MapBindingSet(); if(event.getGeometry().isPresent()) { final Geometry geo = event.getGeometry().get(); final Value geoValue = VF.createLiteral(geo.toText()); final Var geoObj = geoPattern.getObjectVar(); resultSet.addBinding(geoObj.getName(), geoValue); } final Value temporalValue; if(event.isInstant() && event.getInstant().isPresent()) { final Optional<TemporalInstant> opt = event.getInstant(); DateTime dt = opt.get().getAsDateTime(); dt = dt.toDateTime(DateTimeZone.UTC); final String str = dt.toString(TemporalInstantRfc3339.FORMATTER); temporalValue = VF.createLiteral(str); } else if(event.getInterval().isPresent()) { temporalValue = VF.createLiteral(event.getInterval().get().getAsPair()); } else { temporalValue = null; } if(temporalValue != null) { final Var temporalObj = temporalPattern.getObjectVar(); resultSet.addBinding(temporalObj.getName(), temporalValue); } list.add(resultSet); } } catch (final ObjectStorageException e) { throw new QueryEvaluationException("Failed to evaluate the binding set", e); } return new CollectionIteration<>(list); }
Example 19
Source File: SPARQLQueryBindingSet.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
public SPARQLQueryBindingSet(BindingSet bindingSet) { this(bindingSet.size()); addAll(bindingSet); }
Example 20
Source File: StarQuery.java From rya with Apache License 2.0 | 4 votes |
public static Set<String> getCommonVars(final StarQuery query, final BindingSet bs) { final Set<String> starQueryVarNames = Sets.newHashSet(); if(bs == null || bs.size() == 0) { return Sets.newHashSet(); } final Set<String> bindingNames = bs.getBindingNames(); starQueryVarNames.addAll(query.getUnCommonVars()); if(!query.commonVarConstant()) { starQueryVarNames.add(query.getCommonVarName()); } return Sets.intersection(bindingNames, starQueryVarNames); }