All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.bbn.parliament.util.QuerySolutionStream Maven / Gradle / Ivy

// Copyright (c) 2019-2023 RTX BBN Technologies
// All rights reserved.

package com.bbn.parliament.util;

import java.util.Comparator;
import java.util.Iterator;
import java.util.Optional;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.function.ToDoubleFunction;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
import java.util.stream.Collector;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import org.apache.jena.query.Dataset;
import org.apache.jena.query.ParameterizedSparqlString;
import org.apache.jena.query.Query;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryExecutionFactory;
import org.apache.jena.query.QueryFactory;
import org.apache.jena.query.QuerySolution;
import org.apache.jena.query.ResultSet;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.sparql.exec.http.QueryExecutionHTTP;
import org.apache.jena.sparql.exec.http.QuerySendMode;

/**
 * An implementation of Stream<QuerySolution> that converts a Jena
 * QueryExecution into a stream of QuerySolution objects. Note that this class
 * should always be instantiated via a try-with-resources statement to ensure
 * proper closure of the underlying QueryExecution.
 *
 * @author iemmons
 */
public class QuerySolutionStream implements Stream {
	private transient final QueryExecution qe;
	private transient final Stream underlyingStream;

	/**
	 * Creates an AutoCloseable stream of QuerySolution objects by calling
	 * {@code QueryExecutionFactory.sparqlService(service, queryStr)}
	 *
	 * @param queryStr The query to execute
	 * @param service The service against which to execute the query
	 */
	public QuerySolutionStream(String queryStr, String service) {
		this(QueryFactory.create(queryStr), service);
	}

	/**
	 * Creates an AutoCloseable stream of QuerySolution objects by calling
	 * {@code QueryExecutionFactory.sparqlService(service, query)}
	 *
	 * @param query The query to execute
	 * @param service The service against which to execute the query
	 */
	@SuppressWarnings("resource")
	public QuerySolutionStream(Query query, String service) {
		this(QueryExecutionHTTP.newBuilder()
			.endpoint(service)
			.sendMode(QuerySendMode.asPostForm)
			.query(query)
			.build());
	}

	/**
	 * Creates an AutoCloseable stream of QuerySolution objects by calling
	 * {@code QueryExecutionFactory.sparqlService(service, query)}
	 *
	 * @param pss The ParameterizedSparqlString containing the query to execute
	 * @param service The service against which to execute the query
	 */
	public QuerySolutionStream(ParameterizedSparqlString pss, String service) {
		this(QueryFactory.create(pss.asQuery()), service);
	}

	/**
	 * Creates an AutoCloseable stream of QuerySolution objects by calling
	 * {@code QueryExecutionFactory.create(queryStr, dataset)}
	 *
	 * @param queryStr The query to execute
	 * @param dataset The dataset against which to execute the query
	 */
	@SuppressWarnings("resource")
	public QuerySolutionStream(String queryStr, Dataset dataset) {
		this(QueryExecutionFactory.create(queryStr, dataset));
	}

	/**
	 * Creates an AutoCloseable stream of QuerySolution objects by calling
	 * {@code QueryExecutionFactory.create(query, dataset)}
	 *
	 * @param query The query to execute
	 * @param dataset The dataset against which to execute the query
	 */
	@SuppressWarnings("resource")
	public QuerySolutionStream(Query query, Dataset dataset) {
		this(QueryExecutionFactory.create(query, dataset));
	}

	/**
	 * Creates an AutoCloseable stream of QuerySolution objects by calling
	 * {@code QueryExecutionFactory.create(query, dataset)}
	 *
	 * @param pss The ParameterizedSparqlString containing the query to execute
	 * @param dataset The dataset against which to execute the query
	 */
	@SuppressWarnings("resource")
	public QuerySolutionStream(ParameterizedSparqlString pss, Dataset dataset) {
		this(QueryExecutionFactory.create(pss.asQuery(), dataset));
	}

	/**
	 * Creates an AutoCloseable stream of QuerySolution objects by calling
	 * {@code QueryExecutionFactory.create(queryStr, model)}
	 *
	 * @param queryStr The query to execute
	 * @param model The model against which to execute the query
	 */
	@SuppressWarnings("resource")
	public QuerySolutionStream(String queryStr, Model model) {
		this(QueryExecutionFactory.create(queryStr, model));
	}

	/**
	 * Creates an AutoCloseable stream of QuerySolution objects by calling
	 * {@code QueryExecutionFactory.create(query, model)}
	 *
	 * @param query The query to execute
	 * @param model The model against which to execute the query
	 */
	@SuppressWarnings("resource")
	public QuerySolutionStream(Query query, Model model) {
		this(QueryExecutionFactory.create(query, model));
	}

	/**
	 * Creates an AutoCloseable stream of QuerySolution objects by calling
	 * {@code QueryExecutionFactory.create(query, model)}
	 *
	 * @param pss The ParameterizedSparqlString containing the query to execute
	 * @param model The model against which to execute the query
	 */
	@SuppressWarnings("resource")
	public QuerySolutionStream(ParameterizedSparqlString pss, Model model) {
		this(QueryExecutionFactory.create(pss.asQuery(), model));
	}

	/**
	 * Creates an AutoCloseable stream of QuerySolution objects that wraps its
	 * argument
	 *
	 * @param queryExecution The QueryExecution object from which the stream of
	 *                       QuerySolution objects is obtained
	 */
	public QuerySolutionStream(QueryExecution queryExecution) {
		qe = queryExecution;
		ResultSet rs = qe.execSelect();
		Spliterator spl = Spliterators.spliteratorUnknownSize(rs, Spliterator.IMMUTABLE);
		underlyingStream = StreamSupport.stream(spl, false);
	}

	@Override
	public Iterator iterator() {
		return underlyingStream.iterator();
	}

	@Override
	public Spliterator spliterator() {
		return underlyingStream.spliterator();
	}

	@Override
	public boolean isParallel() {
		return underlyingStream.isParallel();
	}

	@Override
	public Stream sequential() {
		return underlyingStream.sequential();
	}

	@Override
	public Stream parallel() {
		return underlyingStream.parallel();
	}

	@Override
	public Stream unordered() {
		return underlyingStream.unordered();
	}

	@Override
	public Stream onClose(Runnable closeHandler) {
		return underlyingStream.onClose(closeHandler);
	}

	@Override
	public void close() {
		qe.close();
	}

	@Override
	public Stream filter(Predicate predicate) {
		return underlyingStream.filter(predicate);
	}

	@Override
	public  Stream map(Function mapper) {
		return underlyingStream.map(mapper);
	}

	@Override
	public IntStream mapToInt(ToIntFunction mapper) {
		return underlyingStream.mapToInt(mapper);
	}

	@Override
	public LongStream mapToLong(ToLongFunction mapper) {
		return underlyingStream.mapToLong(mapper);
	}

	@Override
	public DoubleStream mapToDouble(ToDoubleFunction mapper) {
		return underlyingStream.mapToDouble(mapper);
	}

	@Override
	public  Stream flatMap(Function> mapper) {
		return underlyingStream.flatMap(mapper);
	}

	@Override
	public IntStream flatMapToInt(Function mapper) {
		return underlyingStream.flatMapToInt(mapper);
	}

	@Override
	public LongStream flatMapToLong(Function mapper) {
		return underlyingStream.flatMapToLong(mapper);
	}

	@Override
	public DoubleStream flatMapToDouble(Function mapper) {
		return underlyingStream.flatMapToDouble(mapper);
	}

	@Override
	public Stream distinct() {
		return underlyingStream.distinct();
	}

	@Override
	public Stream sorted() {
		return underlyingStream.sorted();
	}

	@Override
	public Stream sorted(Comparator comparator) {
		return underlyingStream.sorted(comparator);
	}

	@Override
	public Stream peek(Consumer action) {
		return underlyingStream.peek(action);
	}

	@Override
	public Stream limit(long maxSize) {
		return underlyingStream.limit(maxSize);
	}

	@Override
	public Stream skip(long n) {
		return underlyingStream.skip(n);
	}

	@Override
	public void forEach(Consumer action) {
		underlyingStream.forEach(action);
	}

	@Override
	public void forEachOrdered(Consumer action) {
		underlyingStream.forEachOrdered(action);
	}

	@Override
	public Object[] toArray() {
		return underlyingStream.toArray();
	}

	@Override
	public  A[] toArray(IntFunction generator) {
		return underlyingStream.toArray(generator);
	}

	@Override
	public QuerySolution reduce(QuerySolution identity, BinaryOperator accumulator) {
		return underlyingStream.reduce(identity, accumulator);
	}

	@Override
	public Optional reduce(BinaryOperator accumulator) {
		return underlyingStream.reduce(accumulator);
	}

	@Override
	public  U reduce(U identity, BiFunction accumulator, BinaryOperator combiner) {
		return underlyingStream.reduce(identity, accumulator, combiner);
	}

	@Override
	public  R collect(Supplier supplier, BiConsumer accumulator,
		BiConsumer combiner) {
		return underlyingStream.collect(supplier, accumulator, combiner);
	}

	@Override
	public  R collect(Collector collector) {
		return underlyingStream.collect(collector);
	}

	@Override
	public Optional min(Comparator comparator) {
		return underlyingStream.min(comparator);
	}

	@Override
	public Optional max(Comparator comparator) {
		return underlyingStream.max(comparator);
	}

	@Override
	public long count() {
		return underlyingStream.count();
	}

	@Override
	public boolean anyMatch(Predicate predicate) {
		return underlyingStream.anyMatch(predicate);
	}

	@Override
	public boolean allMatch(Predicate predicate) {
		return underlyingStream.allMatch(predicate);
	}

	@Override
	public boolean noneMatch(Predicate predicate) {
		return underlyingStream.noneMatch(predicate);
	}

	@Override
	public Optional findFirst() {
		return underlyingStream.findFirst();
	}

	@Override
	public Optional findAny() {
		return underlyingStream.findAny();
	}
}