org.eclipse.rdf4j.common.iteration.Iteration Java Examples

The following examples show how to use org.eclipse.rdf4j.common.iteration.Iteration. 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: IterationBenchmarks.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private Iteration<String, Exception> getIterator(List<String> list) throws Exception {
	return new Iteration<String, Exception>() {

		Iterator<String> iterator = list.iterator();

		@Override
		public boolean hasNext() throws Exception {
			return iterator.hasNext();
		}

		@Override
		public String next() throws Exception {
			return iterator.next();
		}

		@Override
		public void remove() throws Exception {

		}
	};
}
 
Example #2
Source File: LimitedSizeEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(final Difference difference,
		final BindingSet bindings) throws QueryEvaluationException {
	Iteration<BindingSet, QueryEvaluationException> leftArg, rightArg;

	leftArg = new DelayedIteration<BindingSet, QueryEvaluationException>() {

		@Override
		protected Iteration<BindingSet, QueryEvaluationException> createIteration()
				throws QueryEvaluationException {
			return evaluate(difference.getLeftArg(), bindings);
		}
	};

	rightArg = new DelayedIteration<BindingSet, QueryEvaluationException>() {

		@Override
		protected Iteration<BindingSet, QueryEvaluationException> createIteration()
				throws QueryEvaluationException {
			return evaluate(difference.getRightArg(), bindings);
		}
	};

	return new LimitedSizeSPARQLMinusIteration(leftArg, rightArg, used, maxSize);
}
 
Example #3
Source File: LimitedSizeEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(final Intersection intersection,
		final BindingSet bindings) throws QueryEvaluationException {
	Iteration<BindingSet, QueryEvaluationException> leftArg, rightArg;

	leftArg = new DelayedIteration<BindingSet, QueryEvaluationException>() {

		@Override
		protected Iteration<BindingSet, QueryEvaluationException> createIteration()
				throws QueryEvaluationException {
			return evaluate(intersection.getLeftArg(), bindings);
		}
	};

	rightArg = new DelayedIteration<BindingSet, QueryEvaluationException>() {

		@Override
		protected Iteration<BindingSet, QueryEvaluationException> createIteration()
				throws QueryEvaluationException {
			return evaluate(intersection.getRightArg(), bindings);
		}
	};

	return new LimitedSizeIntersectIteration(leftArg, rightArg, used, maxSize);
}
 
Example #4
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@SuppressWarnings("unchecked")
public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(final Union union,
		final BindingSet bindings) throws QueryEvaluationException {
	Iteration<BindingSet, QueryEvaluationException> leftArg, rightArg;

	leftArg = new DelayedIteration<BindingSet, QueryEvaluationException>() {

		@Override
		protected Iteration<BindingSet, QueryEvaluationException> createIteration()
				throws QueryEvaluationException {
			return evaluate(union.getLeftArg(), bindings);
		}
	};

	rightArg = new DelayedIteration<BindingSet, QueryEvaluationException>() {

		@Override
		protected Iteration<BindingSet, QueryEvaluationException> createIteration()
				throws QueryEvaluationException {
			return evaluate(union.getRightArg(), bindings);
		}
	};

	return new UnionIteration<>(leftArg, rightArg);
}
 
Example #5
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(final Intersection intersection,
		final BindingSet bindings) throws QueryEvaluationException {
	Iteration<BindingSet, QueryEvaluationException> leftArg, rightArg;

	leftArg = new DelayedIteration<BindingSet, QueryEvaluationException>() {

		@Override
		protected Iteration<BindingSet, QueryEvaluationException> createIteration()
				throws QueryEvaluationException {
			return evaluate(intersection.getLeftArg(), bindings);
		}
	};

	rightArg = new DelayedIteration<BindingSet, QueryEvaluationException>() {

		@Override
		protected Iteration<BindingSet, QueryEvaluationException> createIteration()
				throws QueryEvaluationException {
			return evaluate(intersection.getRightArg(), bindings);
		}
	};

	return new IntersectIteration<>(leftArg, rightArg);
}
 
Example #6
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(final Difference difference,
		final BindingSet bindings) throws QueryEvaluationException {
	Iteration<BindingSet, QueryEvaluationException> leftArg, rightArg;

	leftArg = new DelayedIteration<BindingSet, QueryEvaluationException>() {

		@Override
		protected Iteration<BindingSet, QueryEvaluationException> createIteration()
				throws QueryEvaluationException {
			return evaluate(difference.getLeftArg(), bindings);
		}
	};

	rightArg = new DelayedIteration<BindingSet, QueryEvaluationException>() {

		@Override
		protected Iteration<BindingSet, QueryEvaluationException> createIteration()
				throws QueryEvaluationException {
			return evaluate(difference.getRightArg(), bindings);
		}
	};

	return new SPARQLMinusIteration<>(leftArg, rightArg);
}
 
Example #7
Source File: SPARQLConnection.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Converts a {@link TupleQueryResult} resulting from the {@link #EVERYTHING_WITH_GRAPH} to a statement by using the
 * respective values from the {@link BindingSet} or (if provided) the ones from the arguments.
 *
 * @param iter the {@link TupleQueryResult}
 * @param subj the subject {@link Resource} used as input or <code>null</code> if wildcard was used
 * @param pred the predicate {@link IRI} used as input or <code>null</code> if wildcard was used
 * @param obj  the object {@link Value} used as input or <code>null</code> if wildcard was used
 * @return the converted iteration
 */
protected Iteration<Statement, QueryEvaluationException> toStatementIteration(TupleQueryResult iter,
		final Resource subj, final IRI pred, final Value obj) {

	return new ConvertingIteration<BindingSet, Statement, QueryEvaluationException>(iter) {

		@Override
		protected Statement convert(BindingSet b) throws QueryEvaluationException {

			Resource s = subj == null ? (Resource) b.getValue("s") : subj;
			IRI p = pred == null ? (IRI) b.getValue("p") : pred;
			Value o = obj == null ? b.getValue("o") : obj;
			Resource ctx = (Resource) b.getValue("ctx");

			return SimpleValueFactory.getInstance().createStatement(s, p, o, ctx);
		}

	};
}
 
Example #8
Source File: SpinParser.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private Projection visitResultVariables(Resource resultVars, Map<String, ProjectionElem> previousProjElems)
		throws RDF4JException {
	ProjectionElemList projElemList = new ProjectionElemList();
	Iteration<Resource, QueryEvaluationException> iter = TripleSources.listResources(resultVars,
			store);
	while (iter.hasNext()) {
		Resource r = iter.next();
		ProjectionElem projElem = visitResultVariable(r, previousProjElems);
		projElemList.addElement(projElem);
	}

	Projection proj = new Projection();
	proj.setProjectionElemList(projElemList);

	tupleRoot = proj;
	return proj;
}
 
Example #9
Source File: SpinParser.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private Projection visitResultNodes(Resource resultNodes) throws RDF4JException {
	ProjectionElemList projElemList = new ProjectionElemList();
	Iteration<Resource, QueryEvaluationException> iter = TripleSources.listResources(resultNodes,
			store);
	while (iter.hasNext()) {
		Resource r = iter.next();
		ProjectionElem projElem = visitResultNode(r);
		projElemList.addElement(projElem);
	}

	Projection proj = new Projection();
	proj.setProjectionElemList(projElemList);

	tupleRoot = new DescribeOperator(proj);
	return proj;
}
 
Example #10
Source File: HasAllObjects.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Value evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException {
	QueryPreparer qp = getCurrentQueryPreparer();
	if (args.length != 3) {
		throw new ValueExprEvaluationException(
				String.format("%s requires 3 argument, got %d", getURI(), args.length));
	}
	Resource subj = (Resource) args[0];
	IRI pred = (IRI) args[1];
	Resource list = (Resource) args[2];
	try {
		Iteration<Value, QueryEvaluationException> iter = TripleSources.list(list, qp.getTripleSource());
		while (iter.hasNext()) {
			Value obj = iter.next();
			if (TripleSources.single(subj, pred, obj, qp.getTripleSource()) == null) {
				return BooleanLiteral.FALSE;
			}
		}
	} catch (QueryEvaluationException e) {
		throw new ValueExprEvaluationException(e);
	}
	return BooleanLiteral.TRUE;
}
 
Example #11
Source File: ContextAwareConnection.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public <E extends Exception> void add(Iteration<? extends Statement, E> statementIter, Resource... contexts)
		throws RepositoryException, E {
	final IRI insertContext = getInsertContext();
	if (isNilContext(contexts)) {
		super.add(new ConvertingIteration<Statement, Statement, E>(statementIter) {

			@Override
			protected Statement convert(Statement st) {
				if (st.getContext() == null) {
					return getValueFactory().createStatement(st.getSubject(), st.getPredicate(), st.getObject(),
							insertContext);
				}
				return st;
			}
		});
	} else {
		super.add(statementIter, contexts);
	}
}
 
Example #12
Source File: ContextAwareConnection.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Removes the supplied statements from a specific context in this repository, ignoring any context information
 * carried by the statements themselves.
 *
 * @param statementIter The statements to remove. In case the iterator is a {@link CloseableIteration}, it will be
 *                      closed before this method returns.
 * @throws RepositoryException If the statements could not be removed from the repository, for example because the
 *                             repository is not writable.
 * @see #getRemoveContexts()
 */
@Override
public <E extends Exception> void remove(Iteration<? extends Statement, E> statementIter, Resource... contexts)
		throws RepositoryException, E {
	final IRI[] removeContexts = getRemoveContexts();
	if (isAllContext(contexts) && removeContexts.length == 1) {
		super.remove(new ConvertingIteration<Statement, Statement, E>(statementIter) {

			@Override
			protected Statement convert(Statement st) {
				if (st.getContext() == null) {
					return getValueFactory().createStatement(st.getSubject(), st.getPredicate(), st.getObject(),
							removeContexts[0]);
				}
				return st;
			}
		});
	} else {
		super.remove(statementIter, contexts);
	}
}
 
Example #13
Source File: RDFStoreTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private <T> T first(Iteration<T, ?> iter) throws Exception {
	try {
		if (iter.hasNext()) {
			return iter.next();
		}
	} finally {
		Iterations.closeCloseable(iter);
	}

	return null;
}
 
Example #14
Source File: LimitedSizeIntersectIteration.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Set<BindingSet> addSecondSet(Iteration<? extends BindingSet, ? extends QueryEvaluationException> arg2,
		Set<BindingSet> set) throws QueryEvaluationException {

	LimitedSizeIteratorUtil.addAll(arg2, set, used, maxSize);
	return set;
}
 
Example #15
Source File: LimitedSizeDistinctIteration.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @param iter
 */
public LimitedSizeDistinctIteration(Iteration<? extends BindingSet, ? extends QueryEvaluationException> iter,
		AtomicLong used, long maxSize) {
	super(iter);
	this.used = used;
	this.maxSize = maxSize;
}
 
Example #16
Source File: LimitedSizeIteratorUtil.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @param arg2       the iteration with elements to add to the includeSet
 * @param includeSet the set that should have all unique elements of arg2
 * @param used       the collection size counter of all collections used in answering a query
 * @param maxSize    the point at which we throw a new query exception
 * @return the includeSet
 * @throws QueryEvaluationException trigerred when maxSize is smaller than the used value
 */
public static Set<BindingSet> addAll(Iteration<? extends BindingSet, ? extends QueryEvaluationException> arg2,
		Set<BindingSet> includeSet, AtomicLong used, long maxSize) throws QueryEvaluationException {
	while (arg2.hasNext()) {
		if (includeSet.add(arg2.next()) && used.incrementAndGet() > maxSize) {
			throw new QueryEvaluationException("Size limited reached inside intersect operator");
		}
	}
	return includeSet;
}
 
Example #17
Source File: LimitedSizeIntersectIteration.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public LimitedSizeIntersectIteration(Iteration<? extends BindingSet, ? extends QueryEvaluationException> arg1,
		Iteration<? extends BindingSet, ? extends QueryEvaluationException> arg2, boolean distinct, AtomicLong used,
		long maxSize) {
	super(arg1, arg2, distinct);
	this.used = used;
	this.maxSize = maxSize;
}
 
Example #18
Source File: TripleSources.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static Iteration<Value, QueryEvaluationException> list(final Resource subj,
		final TripleSource store) throws QueryEvaluationException {
	if (subj == null) {
		throw new NullPointerException("RDF list subject cannot be null");
	}
	return new Iteration<Value, QueryEvaluationException>() {

		Resource list = subj;

		@Override
		public boolean hasNext() throws QueryEvaluationException {
			return !RDF.NIL.equals(list);
		}

		@Override
		public Value next() throws QueryEvaluationException {
			Value v = singleValue(list, RDF.FIRST, store);
			if (v == null) {
				throw new QueryEvaluationException("List missing rdf:first: " + list);
			}
			Resource nextList = (Resource) singleValue(list, RDF.REST, store);
			if (nextList == null) {
				throw new QueryEvaluationException("List missing rdf:rest: " + list);
			}
			list = nextList;
			return v;
		}

		@Override
		public void remove() throws QueryEvaluationException {
			throw new UnsupportedOperationException();
		}
	};
}
 
Example #19
Source File: DescribeIteration.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public DescribeIteration(Iteration<BindingSet, QueryEvaluationException> sourceIter, EvaluationStrategy strategy,
		Set<String> describeExprNames, BindingSet parentBindings) {
	this.strategy = strategy;
	this.sourceIter = sourceIter;
	this.describeExprNames = new ArrayList<>(describeExprNames);
	this.parentBindings = parentBindings;
}
 
Example #20
Source File: SPARQLMinusIteration.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Creates a new MinusIteration that returns the results of the left argument minus the results of the right
 * argument.
 *
 * @param leftArg  An Iteration containing the main set of elements.
 * @param rightArg An Iteration containing the set of elements that should be filtered from the main set.
 * @param distinct Flag indicating whether duplicate elements should be filtered from the result.
 */
public SPARQLMinusIteration(Iteration<BindingSet, X> leftArg, Iteration<BindingSet, X> rightArg, boolean distinct) {
	super(leftArg);

	assert rightArg != null;

	this.rightArg = rightArg;
	this.distinct = distinct;
	this.initialized = false;
}
 
Example #21
Source File: RepositoryConnectionWrapper.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public <E extends Exception> void add(Iteration<? extends Statement, E> statementIter, Resource... contexts)
		throws RepositoryException, E {
	if (isDelegatingAdd()) {
		getDelegate().add(statementIter, contexts);
	} else {
		super.add(statementIter, contexts);
	}
}
 
Example #22
Source File: RepositoryConnectionWrapper.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public <E extends Exception> void remove(Iteration<? extends Statement, E> statementIter, Resource... contexts)
		throws RepositoryException, E {
	if (isDelegatingRemove()) {
		getDelegate().remove(statementIter, contexts);
	} else {
		super.remove(statementIter, contexts);
	}
}
 
Example #23
Source File: SpinParser.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private Order visitOrderBy(Resource orderby) throws RDF4JException {
	Order order = new Order();
	Iteration<Resource, QueryEvaluationException> iter = TripleSources.listResources(orderby, store);
	while (iter.hasNext()) {
		Resource r = iter.next();
		OrderElem orderElem = visitOrderByCondition(r);
		order.addElement(orderElem);
	}
	return order;
}
 
Example #24
Source File: SpinParser.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void visitInsert(Resource insert) throws RDF4JException {
	Iteration<Resource, QueryEvaluationException> groupIter = TripleSources.listResources(insert,
			store);
	while (groupIter.hasNext()) {
		Resource r = groupIter.next();
		Value type = TripleSources.singleValue(r, RDF.TYPE, store);
		visitPattern(r, (type != null) ? Collections.singleton((IRI) type) : Collections.<IRI>emptySet(), null);
	}
}
 
Example #25
Source File: SpinParser.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private TupleExpr visitHaving(Resource having) throws RDF4JException {
	UnaryTupleOperator op = (UnaryTupleOperator) group.getParentNode();
	op.setArg(new Extension(group));
	Iteration<Resource, QueryEvaluationException> iter = TripleSources.listResources(having, store);
	while (iter.hasNext()) {
		Resource r = iter.next();
		ValueExpr havingExpr = visitExpression(r);
		Filter filter = new Filter(op.getArg(), havingExpr);
		op.setArg(filter);
		op = filter;
	}
	return op;
}
 
Example #26
Source File: FilteringIteration.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public FilteringIteration(Iteration<E, X> wrappedIteration, Resource subject, IRI predicate, Value object,
		boolean inferred, Resource... context) {
	this.wrappedIteration = wrappedIteration;
	this.subject = subject;
	this.predicate = predicate;
	this.object = object;
	this.inferred = inferred;
	this.context = context;
}
 
Example #27
Source File: LockingIteration.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Creates a new LockingIteration.
 *
 * @param lock The lock to release when the itererator is closed, must not be <tt>null</tt>.
 * @param iter The underlying Iteration, must not be <tt>null</tt>.
 */
public LockingIteration(Lock lock, Iteration<? extends E, X> iter) {
	super(iter);

	assert lock != null;
	this.lock = lock;
}
 
Example #28
Source File: RDFStoreTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private int countElements(Iteration<?, ?> iter) throws Exception {
	int count = 0;

	try {
		while (iter.hasNext()) {
			iter.next();
			count++;
		}
	} finally {
		Iterations.closeCloseable(iter);
	}

	return count;
}
 
Example #29
Source File: SpinParser.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void visitDelete(Resource delete) throws RDF4JException {
	Iteration<Resource, QueryEvaluationException> groupIter = TripleSources.listResources(delete,
			store);
	while (groupIter.hasNext()) {
		Resource r = groupIter.next();
		Value type = TripleSources.singleValue(r, RDF.TYPE, store);
		visitPattern(r, (type != null) ? Collections.singleton((IRI) type) : Collections.<IRI>emptySet(), null);
	}
}
 
Example #30
Source File: AbstractParserQuery.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public QueryInterruptIteration(Iteration<? extends BindingSet, ? extends QueryEvaluationException> iter,
		long timeLimit) {
	super(iter, timeLimit);
}