info.aduna.iteration.CloseableIteration Java Examples

The following examples show how to use info.aduna.iteration.CloseableIteration. 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: RepositoryConnectionTest.java    From database with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testRemoveStatementIteration()
	throws Exception
{
	testCon.begin();
	testCon.add(alice, name, nameAlice);
	testCon.add(bob, name, nameBob);
	testCon.commit();

	assertThat(testCon.hasStatement(bob, name, nameBob, false), is(equalTo(true)));
	assertThat(testCon.hasStatement(alice, name, nameAlice, false), is(equalTo(true)));

	CloseableIteration<? extends Statement, RepositoryException> iter = testCon.getStatements(null, null,
			null, false);

	try {
		testCon.remove(iter);
	}
	finally {
		iter.close();
	}

	assertThat(testCon.hasStatement(bob, name, nameBob, false), is(equalTo(false)));
	assertThat(testCon.hasStatement(alice, name, nameAlice, false), is(equalTo(false)));
}
 
Example #2
Source File: RepositoryModel.java    From semweb4j with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public ClosableIterator<org.ontoware.rdf2go.model.Statement> findStatements(
        ResourceOrVariable subject, UriOrVariable predicate, NodeOrVariable object)
        throws ModelRuntimeException {
	assertModel();
	// convert parameters to OpenRDF data types
	org.openrdf.model.Resource openRdfSubject = (org.openrdf.model.Resource)ConversionUtil
	        .toOpenRDF(subject, this.valueFactory);
	org.openrdf.model.URI openRdfPredicate = (org.openrdf.model.URI)ConversionUtil.toOpenRDF(
	        predicate, this.valueFactory);
	Value openRdfObject = ConversionUtil.toOpenRDF(object, this.valueFactory);
	
	try {
		// find the matching statements
		CloseableIteration<? extends org.openrdf.model.Statement,? extends OpenRDFException> statements = this.connection
		        .getStatements(openRdfSubject, openRdfPredicate, openRdfObject, true,
		                this.openRdfContext);
		// wrap them in a StatementIterable
		return new StatementIterator(statements, this);
	} catch(RepositoryException e) {
		throw new ModelRuntimeException(e);
	}
}
 
Example #3
Source File: RDFSContainer.java    From anno4j with Apache License 2.0 6 votes vote down vote up
private int findSize() throws RepositoryException {
	CloseableIteration<? extends Statement, RepositoryException> iter;
	HashSet<URI> set = new HashSet<URI>();
	ObjectConnection conn = getObjectConnection();
	iter = conn.getStatements(getResource(), null, null);
	try {
		while (iter.hasNext()) {
			set.add(iter.next().getPredicate());
		}
	} finally {
		iter.close();
	}
	int index = 0;
	while (set.contains(getMemberPredicate(index)))
		index++;
	return index;
}
 
Example #4
Source File: RepositoryConnectionTest.java    From database with GNU General Public License v2.0 6 votes vote down vote up
private int getTotalStatementCount(RepositoryConnection connection)
	throws RepositoryException
{
	CloseableIteration<? extends Statement, RepositoryException> iter = connection.getStatements(null,
			null, null, true);

	try {
		int size = 0;

		while (iter.hasNext()) {
			iter.next();
			++size;
		}

		return size;
	}
	finally {
		iter.close();
	}
}
 
Example #5
Source File: RDFList.java    From anno4j with Apache License 2.0 6 votes vote down vote up
Value getFirst(Resource list) {
	if (list == null)
		return null;
	try {
		CloseableIteration<Value, RepositoryException> stmts;
		stmts = getValues(list, RDF.FIRST, null);
		try {
			if (stmts.hasNext())
				return stmts.next();
			return null;
		} finally {
			stmts.close();
		}
	} catch (RepositoryException e) {
		throw new ObjectStoreException(e);
	}
}
 
Example #6
Source File: RDFList.java    From anno4j with Apache License 2.0 6 votes vote down vote up
Resource getRest(Resource list) {
	if (list == null)
		return null;
	try {
		CloseableIteration<Value, RepositoryException> stmts;
		stmts = getValues(list, RDF.REST, null);
		try {
			if (stmts.hasNext())
				return (Resource) stmts.next();
			return null;
		} finally {
			stmts.close();
		}
	} catch (RepositoryException e) {
		throw new ObjectStoreException(e);
	}
}
 
Example #7
Source File: BigdataStoreTest.java    From database with GNU General Public License v2.0 6 votes vote down vote up
private CloseableIteration<? extends BindingSet, QueryEvaluationException> 
		evaluate(final String query, final SailConnection con, final BindingSet bs) 
			throws Exception {
	
	// new pattern
	((BigdataSailConnection) con).flush();
	final AbstractTripleStore db = ((BigdataSailConnection) con).getTripleStore();
	final ASTContainer astContainer = new Bigdata2ASTSPARQLParser().parseQuery2(query, null);
	final QueryRoot originalQuery = astContainer.getOriginalAST();
	originalQuery.setIncludeInferred(false);
	final TupleQueryResult queryResult = ASTEvalHelper.evaluateTupleQuery(
	        db, astContainer, new QueryBindingSet(
	        		bs), null /* dataset */);
	
	return queryResult;
	
}
 
Example #8
Source File: CumulusRDFSailConnection.java    From cumulusrdf with Apache License 2.0 6 votes vote down vote up
@Override
protected CloseableIteration<? extends Statement, SailException> getStatementsInternal(
		final Resource subj,
		final URI pred,
		final Value obj,
		final boolean includeInferred,
		final Resource... contexts) throws SailException {

	if (_quad && (contexts == null || contexts.length == 0)) {
		throw new IllegalArgumentException("A quadstore always needs a context.");
	}

	if (contexts != null && contexts.length > 0) {
		@SuppressWarnings("unchecked")
		CloseableIteration<Statement, SailException>[] iterations = new CloseableIteration[contexts.length];

		for (int i = 0; i < contexts.length; i++) {
			iterations[i] = newStatementIterator(subj, pred, obj, contexts[i]);
		}

		return new CloseableMultiIterator<Statement, SailException>(iterations);
	} else {
		return newStatementIterator(subj, pred, obj, contexts);
	}
}
 
Example #9
Source File: CachedPropertySet.java    From anno4j with Apache License 2.0 6 votes vote down vote up
@Override
protected synchronized CloseableIteration<?, ?> getObjects() throws RepositoryException,
		QueryEvaluationException {
	if (creator == null || factory == null) {
		return super.getObjects();
	} else if (binding == null) {
		ObjectQuery query = factory.createQuery(creator);
		if (query == null)
			return super.getObjects();
		try {
			query.setBinding("self", getResource());
			return query.evaluate(creator.getPropertyType());
		} finally {
			factory.returnQuery(creator, query);
		}
	} else {
		CloseableIteratorIteration<BindingSet, QueryEvaluationException> result;
		result = new CloseableIteratorIteration<BindingSet, QueryEvaluationException>(
				bindings.iterator());
		return new ObjectCursor(getObjectConnection(), result, binding);
	}
}
 
Example #10
Source File: RDFStoreTest.java    From database with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testGetNamespaces()
	throws Exception
{
	con.begin();
	con.setNamespace("rdf", RDF.NAMESPACE);
	con.commit();

	CloseableIteration<? extends Namespace, SailException> namespaces = con.getNamespaces();
	try {
		assertTrue(namespaces.hasNext());
		Namespace rdf = namespaces.next();
		assertEquals("rdf", rdf.getPrefix());
		assertEquals(RDF.NAMESPACE, rdf.getName());
		assertTrue(!namespaces.hasNext());
	}
	finally {
		namespaces.close();
	}
}
 
Example #11
Source File: CumulusRDFSailConnection.java    From cumulusrdf with Apache License 2.0 6 votes vote down vote up
@Override
public CloseableIteration<? extends Statement, QueryEvaluationException> getStatements(
		final Resource subj,
		final URI pred,
		final Value obj,
		final Resource... contexts) throws QueryEvaluationException {
	try {
		if (contexts != null && contexts.length > 0) {
			@SuppressWarnings("unchecked")
			final CloseableIteration<Statement, QueryEvaluationException>[] iterations = new CloseableIteration[contexts.length];
			
			for (int i = 0; i < contexts.length; i++) {
				iterations[i] = newStatementIterator(subj, pred, obj, contexts[i]);
			}

			return new CloseableMultiIterator<Statement, QueryEvaluationException>(iterations);
		} else {
			return newStatementIterator(subj, pred, obj, contexts);
		}
	} catch (final SailException exception) {
		LOGGER.error(MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE, exception);
		throw new QueryEvaluationException(exception);
	}
}
 
Example #12
Source File: CumulusRDFSailConnection.java    From cumulusrdf with Apache License 2.0 6 votes vote down vote up
public CloseableIteration<? extends Statement, QueryEvaluationException> getRangeStatements(
		final Resource subj, 
		final URI pred, 
		final Literal lowerBound,
		final boolean lower_equals, 
		final Literal upperBound, 
		final boolean upper_equals, 
		final Literal equals, 
		final boolean reverse) throws QueryEvaluationException {
	try {
		return createRangeStatementIterator(subj, pred, lowerBound, lower_equals, upperBound, upper_equals, equals, reverse);
	} catch (SailException e) {
		e.printStackTrace();
		throw new QueryEvaluationException(e);
	}
}
 
Example #13
Source File: RDFStoreTest.java    From database with GNU General Public License v2.0 6 votes vote down vote up
protected int verifyQueryResult(
		CloseableIteration<? extends BindingSet, QueryEvaluationException> resultIter,
		int expectedBindingCount)
	throws QueryEvaluationException
{
	int resultCount = 0;

	while (resultIter.hasNext()) {
		BindingSet resultBindings = resultIter.next();
		resultCount++;

		assertEquals("Wrong number of binding names for binding set", expectedBindingCount,
				resultBindings.getBindingNames().size());

		int bindingCount = 0;
		Iterator<Binding> bindingIter = resultBindings.iterator();
		while (bindingIter.hasNext()) {
			bindingIter.next();
			bindingCount++;
		}

		assertEquals("Wrong number of bindings in binding set", expectedBindingCount, bindingCount);
	}

	return resultCount;
}
 
Example #14
Source File: ForwardChainingRDFSInferencerConnection.java    From semweb4j with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
	 * aaa xxx bbb 
	 * xxx nrl:inverseProperty yyy
	 * -->
	 * bbb yyy aaa
	 * @return
	 * @throws SailException
	 */
	private int applyRuleN1b()
	throws SailException
{
	int nofInferred = 0;
	
	Iterator<Statement> ntIter = this.newThisIteration.match(null, null, null);

	while (ntIter.hasNext()) {
		Statement nt = ntIter.next();

		Resource xxx = nt.getPredicate();

		CloseableIteration<? extends Statement, SailException> t1Iter;
		t1Iter = getWrappedConnection().getStatements(xxx,NRL_InverseProperty, null, true);

		while (t1Iter.hasNext()) {
			Statement t1 = t1Iter.next();

			Value yyy = t1.getObject();
			if (yyy instanceof URI) {
				Resource aaa = 	nt.getSubject();
				Value bbb = nt.getObject();
				boolean added = addInferredStatement((Resource)bbb, (URI) yyy, aaa);
				if (added) {
					nofInferred++;
				}
			}
		}
		t1Iter.close();
	}

	return nofInferred;
}
 
Example #15
Source File: RepositoryModel.java    From semweb4j with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public ClosableIterator<Statement> iterator() {
	assertModel();
	try {
		CloseableIteration<? extends org.openrdf.model.Statement,RepositoryException> statements = this.connection
		        .getStatements(null, null, null, true, this.openRdfContext);
		return new StatementIterator(statements, this);
	} catch(RepositoryException e) {
		throw new ModelRuntimeException(e);
	}
}
 
Example #16
Source File: ForwardChainingRDFSInferencerConnection.java    From semweb4j with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * rrr rdfs:subPropertyOf  ppp 
 * rrr nrl:inverseProperty sss 
 * ppp nrl:inverseProperty qqq 
 * -->
 * sss rdfs:subPropertyOf  qqq
 * @return
 * @throws SailException
 */
private int applyRuleN2b()
throws SailException
{
	int nofInferred = 0;
	Iterator<Statement> it1 = this.newThisIteration.match(null, RDFS.SUBPROPERTYOF, null);
	while (it1.hasNext()) {
		Statement stmt1 = it1.next();
		Resource rrr = stmt1.getSubject();
		Value ppp = stmt1.getObject();
		if(ppp instanceof Resource) {
			CloseableIteration<? extends Statement, SailException> it2;
			it2 = getWrappedConnection().getStatements(rrr,NRL_InverseProperty, null, true);
			while (it2.hasNext()) {
				Statement stmt2 = it2.next();
				Value sss = stmt2.getObject();
				if(sss instanceof Resource) {
					CloseableIteration<? extends Statement, SailException> it3;
					it3 = getWrappedConnection().getStatements( (Resource) ppp,NRL_InverseProperty, null, true);
					while (it3.hasNext()) {
						Statement stmt3 = it3.next();
						Value qqq = stmt3.getObject();
						if( qqq instanceof Resource) {
							boolean added = addInferredStatement((Resource)sss, RDFS.SUBPROPERTYOF, qqq);
							if (added) {
								nofInferred++;
							}
						}
					}
					it3.close();
				}
			}
			it2.close();
		}
	}
	return nofInferred;
}
 
Example #17
Source File: ObjectConnection.java    From anno4j with Apache License 2.0 5 votes vote down vote up
/**
 * Loads the list of resources assumed to implement the given concept. The
 * concept must be a named concept and cannot be mapped to rdfs:Resource.
 */
public synchronized <T> Result<T> getObjects(final Class<T> concept,
		Resource... resources) throws RepositoryException,
		QueryEvaluationException {
	try {
		int size = resources.length;
		ObjectQuery query = getObjectQuery(concept, size);
		if (size == 1) {
			query.setBinding(ObjectFactory.VAR_PREFIX, resources[0]);
		} else if (size > 1) {
			for (int i = 0; i < size; i++) {
				query.setBinding(ObjectFactory.VAR_PREFIX + i, resources[i]);
			}
		}
		final List<Resource> list = new ArrayList<Resource>(size);
		list.addAll(Arrays.asList(resources));
		CloseableIteration<T, QueryEvaluationException> iter;
		final Result<T> result = query.evaluate(concept);
		iter = new LookAheadIteration<T, QueryEvaluationException>() {
			@Override
			protected T getNextElement() throws QueryEvaluationException {
				T next = result.next();
				if (next != null) {
					list.remove(((RDFObject) next).getResource());
					return next;
				}
				if (!list.isEmpty())
					return (T) cache(of.createObject(list.remove(0)));
				return null;
			}
		};
		return new ResultImpl<T>(iter);
	} catch (MalformedQueryException e) {
		throw new AssertionError(e);
	}
}
 
Example #18
Source File: ForwardChainingRDFSInferencerConnection.java    From semweb4j with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
	 * xxx nrl:inverseProperty yyy
	 * aaa xxx bbb 
	 * -->
	 * bbb yyy aaa
	 * @return
	 * @throws SailException
	 */
	private int applyRuleN1a()
	throws SailException
{
	int nofInferred = 0;
	
	Iterator<Statement> ntIter = this.newThisIteration.match(null, NRL_InverseProperty, null);

	while (ntIter.hasNext()) {
		Statement nt = ntIter.next();

		Resource xxx = nt.getSubject();
		Value yyy = nt.getObject();

		if (xxx instanceof URI && yyy instanceof URI) {
			// apply to triples using the property
			CloseableIteration<? extends Statement, SailException> t1Iter;
			t1Iter = getWrappedConnection().getStatements(null, (URI)xxx, null, true);

			while (t1Iter.hasNext()) {
				Statement t1 = t1Iter.next();

				Value aaa = t1.getSubject();
				Value bbb = t1.getObject();
				if (bbb instanceof Resource) {
					boolean added = addInferredStatement((Resource)bbb, (URI) yyy, aaa);
					if (added) {
						nofInferred++;
					}
				}
			}
			t1Iter.close();
		}
	}

	return nofInferred;
}
 
Example #19
Source File: ForwardChainingRDFSInferencerConnection.java    From semweb4j with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private int applyRuleRdfs11_2()
	throws SailException
{
	int nofInferred = 0;

	Iterator<Statement> ntIter = this.newThisIteration.match(null, RDFS.SUBCLASSOF, null);

	while (ntIter.hasNext()) {
		Statement nt = ntIter.next();

		Resource yyy = nt.getSubject();
		Value zzz = nt.getObject();

		if (zzz instanceof Resource) {
			CloseableIteration<? extends Statement, SailException> t1Iter;
			t1Iter = getWrappedConnection().getStatements(null, RDFS.SUBCLASSOF, yyy, true);

			while (t1Iter.hasNext()) {
				Statement t1 = t1Iter.next();

				Resource xxx = t1.getSubject();

				boolean added = addInferredStatement(xxx, RDFS.SUBCLASSOF, zzz);
				if (added) {
					nofInferred++;
				}
			}
			t1Iter.close();
		}
	}

	return nofInferred;
}
 
Example #20
Source File: ForwardChainingRDFSInferencerConnection.java    From semweb4j with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private int applyRuleRdfs11_1()
	throws SailException
{
	int nofInferred = 0;

	Iterator<Statement> ntIter = this.newThisIteration.match(null, RDFS.SUBCLASSOF, null);

	while (ntIter.hasNext()) {
		Statement nt = ntIter.next();

		Resource xxx = nt.getSubject();
		Value yyy = nt.getObject();

		if (yyy instanceof Resource) {
			CloseableIteration<? extends Statement, SailException> t1Iter;
			t1Iter = getWrappedConnection().getStatements((Resource)yyy, RDFS.SUBCLASSOF, null, true);

			while (t1Iter.hasNext()) {
				Statement t1 = t1Iter.next();

				Value zzz = t1.getObject();

				if (zzz instanceof Resource) {
					boolean added = addInferredStatement(xxx, RDFS.SUBCLASSOF, zzz);
					if (added) {
						nofInferred++;
					}
				}
			}
			t1Iter.close();
		}
	}

	return nofInferred;
}
 
Example #21
Source File: ForwardChainingRDFSInferencerConnection.java    From semweb4j with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private int applyRuleRdfs9_2()
	throws SailException
{
	int nofInferred = 0;

	Iterator<Statement> ntIter = this.newThisIteration.match(null, RDF.TYPE, null);

	while (ntIter.hasNext()) {
		Statement nt = ntIter.next();

		Resource aaa = nt.getSubject();
		Value xxx = nt.getObject();

		if (xxx instanceof Resource) {
			CloseableIteration<? extends Statement, SailException> t1Iter;
			t1Iter = getWrappedConnection().getStatements((Resource)xxx, RDFS.SUBCLASSOF, null, true);

			while (t1Iter.hasNext()) {
				Statement t1 = t1Iter.next();

				Value yyy = t1.getObject();

				if (yyy instanceof Resource) {
					boolean added = addInferredStatement(aaa, RDF.TYPE, yyy);
					if (added) {
						nofInferred++;
					}
				}
			}
			t1Iter.close();
		}
	}

	return nofInferred;
}
 
Example #22
Source File: ForwardChainingRDFSInferencerConnection.java    From semweb4j with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private int applyRuleRdfs3_2()
	throws SailException
{
	int nofInferred = 0;

	Iterator<Statement> ntIter = this.newThisIteration.match(null, RDFS.RANGE, null);

	while (ntIter.hasNext()) {
		Statement nt = ntIter.next();

		Resource aaa = nt.getSubject();
		Value zzz = nt.getObject();

		if (aaa instanceof URI && zzz instanceof Resource) {
			CloseableIteration<? extends Statement, SailException> t1Iter;
			t1Iter = getWrappedConnection().getStatements(null, (URI)aaa, null, true);

			while (t1Iter.hasNext()) {
				Statement t1 = t1Iter.next();

				Value uuu = t1.getObject();
				if (uuu instanceof Resource) {
					boolean added = addInferredStatement((Resource)uuu, RDF.TYPE, zzz);
					if (added) {
						nofInferred++;
					}
				}
			}
			t1Iter.close();
		}
	}

	return nofInferred;

}
 
Example #23
Source File: ForwardChainingRDFSInferencerConnection.java    From semweb4j with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private int applyRuleRdfs9_1()
	throws SailException
{
	int nofInferred = 0;

	Iterator<Statement> ntIter = this.newThisIteration.match(null, RDFS.SUBCLASSOF, null);

	while (ntIter.hasNext()) {
		Statement nt = ntIter.next();

		Resource xxx = nt.getSubject();
		Value yyy = nt.getObject();

		if (yyy instanceof Resource) {
			CloseableIteration<? extends Statement, SailException> t1Iter;
			t1Iter = getWrappedConnection().getStatements(null, RDF.TYPE, xxx, true);

			while (t1Iter.hasNext()) {
				Statement t1 = t1Iter.next();

				Resource aaa = t1.getSubject();

				boolean added = addInferredStatement(aaa, RDF.TYPE, yyy);
				if (added) {
					nofInferred++;
				}
			}
			t1Iter.close();
		}
	}

	return nofInferred;
}
 
Example #24
Source File: ForwardChainingRDFSInferencerConnection.java    From semweb4j with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private int applyRuleRdfs2_1()
	throws SailException
{
	int nofInferred = 0;

	Iterator<Statement> ntIter = this.newThisIteration.match(null, null, null);

	while (ntIter.hasNext()) {
		Statement nt = ntIter.next();

		Resource xxx = nt.getSubject();
		URI aaa = nt.getPredicate();

		CloseableIteration<? extends Statement, SailException> t1Iter;
		t1Iter = getWrappedConnection().getStatements(aaa, RDFS.DOMAIN, null, true);

		while (t1Iter.hasNext()) {
			Statement t1 = t1Iter.next();

			Value zzz = t1.getObject();
			if (zzz instanceof Resource) {
				boolean added = addInferredStatement(xxx, RDF.TYPE, zzz);
				if (added) {
					nofInferred++;
				}
			}
		}
		t1Iter.close();
	}

	return nofInferred;
}
 
Example #25
Source File: RangeEvaluationStrategy.java    From cumulusrdf with Apache License 2.0 5 votes vote down vote up
public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(StatementPattern sp, final BindingSet bindings)
		throws QueryEvaluationException {
	if (sp instanceof RangeStatementPattern) {
		return evaluate((RangeStatementPattern) sp, bindings);
	} else {
		return super.evaluate(sp, bindings);
	}
}
 
Example #26
Source File: CumulusRDFSailConnection.java    From cumulusrdf with Apache License 2.0 5 votes vote down vote up
@Override
protected CloseableIteration<? extends BindingSet, QueryEvaluationException> evaluateInternal(TupleExpr tupleExpr, Dataset dataset,
		BindingSet bindings, boolean includeInferred) throws SailException {
	// Lock stLock = _sail.getStatementsReadLock();
	// Clone the tuple expression to allow for more aggressive optimizations
	tupleExpr = tupleExpr.clone();

	if (!(tupleExpr instanceof QueryRoot)) {
		// Add a dummy root node to the tuple expressions to allow the
		// optimizers to modify the actual root node
		tupleExpr = new QueryRoot(tupleExpr);
	}

	TripleSource tripleSource = new CumulusRDFTripleSource();
	EvaluationStrategy strategy = new RangeEvaluationStrategy(tripleSource, dataset);

	new BindingAssigner().optimize(tupleExpr, dataset, bindings);
	new ConstantOptimizer(strategy).optimize(tupleExpr, dataset, bindings);
	new CompareOptimizer().optimize(tupleExpr, dataset, bindings);
	new ConjunctiveConstraintSplitter().optimize(tupleExpr, dataset, bindings);
	new DisjunctiveConstraintOptimizer().optimize(tupleExpr, dataset, bindings);
	new SameTermFilterOptimizer().optimize(tupleExpr, dataset, bindings);
	new QueryModelNormalizer().optimize(tupleExpr, dataset, bindings);

	new CumulusQueryOptimizer(_crdf.isRangeIndexesSupportEnabled()).optimize(tupleExpr, dataset, bindings);
	new QueryJoinOptimizer(_select_est).optimize(tupleExpr, dataset, bindings); //		

	new FilterOptimizer().optimize(tupleExpr, dataset, bindings);
	new IterativeEvaluationOptimizer().optimize(tupleExpr, dataset, bindings);
	new OrderLimitOptimizer().optimize(tupleExpr, dataset, bindings);

	try {
		return strategy.evaluate(tupleExpr, EmptyBindingSet.getInstance());
	} catch (QueryEvaluationException e) {
		e.printStackTrace();
		throw new SailException(e);
	}
}
 
Example #27
Source File: ContextsHandler.java    From cumulusrdf with Apache License 2.0 5 votes vote down vote up
@Override
public ModelAndView serve(final Repository repository, final HttpServletRequest request, final HttpServletResponse response)
		throws Exception {
	Map<String, Object> model = new HashMap<String, Object>();
	TupleQueryResultWriterFactory factory = ProtocolUtil.getAcceptableService(request, response,
			TupleQueryResultWriterRegistry.getInstance());

	if (METHOD_GET.equals(request.getMethod())) {
		List<String> columnNames = Arrays.asList("contextID");
		List<BindingSet> contexts = new ArrayList<BindingSet>();
		RepositoryConnection repositoryCon = repository.getConnection();
		synchronized (repositoryCon) {
			try {
				CloseableIteration<? extends Resource, RepositoryException> contextIter = repositoryCon.getContextIDs();

				try {
					while (contextIter.hasNext()) {
						BindingSet bindingSet = new ListBindingSet(columnNames, contextIter.next());
						contexts.add(bindingSet);
					}
				} finally {
					contextIter.close();
				}
			} catch (RepositoryException e) {
				throw new ServerHTTPException("Repository error: " + e.getMessage(), e);
			}
		}
		model.put(QueryResultView.QUERY_RESULT_KEY, new TupleQueryResultImpl(columnNames, contexts));
	}

	model.put(QueryResultView.FILENAME_HINT_KEY, "contexts");
	model.put(QueryResultView.FACTORY_KEY, factory);
	model.put(QueryResultView.HEADERS_ONLY, METHOD_HEAD.equals(request.getMethod()));
	return new ModelAndView(TupleQueryResultView.getInstance(), model);
}
 
Example #28
Source File: AST2BOpUpdate.java    From database with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Efficiently resolve openrdf {@link BindingSet} into a chunked bigdata
 * {@link IBindingSet}[] iterator. The closeable semantics of the iteration
 * pattern are preserved.
 * 
 * @param r
 *            The {@link LexiconRelation}.
 * @param chunkSize
 *            When converting the openrdf binding set iteration pattern into
 *            a chunked iterator pattern, this will be the target chunk
 *            size.
 * @param result
 *            The openrdf solutions.
 * @return An iterator visiting chunked bigdata solutions.
 * 
 *         TODO We should not have to do this. We should stay within native
 *         bigdata IBindingSet[]s and the native bigdata iterators
 */
private static ICloseableIterator<IBindingSet[]> asBigdataIterator(
		final LexiconRelation r,
		final int chunkSize,
		final CloseableIteration<BindingSet, QueryEvaluationException> result) {
	
	// Wrap with streaming iterator pattern.
	final Striterator sitr = new Striterator(
			// Chunk up the openrdf solutions.
			new Chunkerator<BindingSet>(
					// Convert the Sesame iteration into a Bigdata iterator.
					new Sesame2BigdataIterator<BindingSet, QueryEvaluationException>(
							result), chunkSize));

	// Add filter to batch resolve BindingSet[] => IBindingSet[].
	sitr.addFilter(new Resolver() {
		
		private static final long serialVersionUID = 1L;

		@Override
		protected Object resolve(Object obj) {

			// Visiting openrdf BindingSet[] chunks.
			final BindingSet[] in = (BindingSet[]) obj;
			
			// Batch resolve to IBindingSet[].
			final IBindingSet[] out = BigdataOpenRDFBindingSetsResolverator
					.resolveChunk(r, in);
			
			// Return Bigdata IBindingSet[].
			return out;
		}
	});
	
	return sitr;

}
 
Example #29
Source File: BigdataSail.java    From database with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Bigdata now uses an internal query model which differs significantly
 * from the Sesame query model. Support is no longer provided for
 * {@link TupleExpr} evaluation. SPARQL queries must be prepared and
 * evaluated using a {@link BigdataSailRepositoryConnection}.
 * 
 * @throws SailException
 *             <em>always</em>.
 */
public CloseableIteration<? extends BindingSet, QueryEvaluationException> evaluate(
        final TupleExpr tupleExpr, //
        final Dataset dataset,//
        final BindingSet bindings,//
        final boolean includeInferred//
) throws SailException {

    throw new SailException(ERR_OPENRDF_QUERY_MODEL);

}
 
Example #30
Source File: BigdataSail.java    From database with GNU General Public License v2.0 5 votes vote down vote up
public CloseableIteration<? extends Statement, SailException> getStatements(
        final Resource s, final URI p, final Value o,
        final Resource context)
        throws SailException {
    return getStatements(s,p,o,true/*includeInferred*/,context==null?
            new Resource[]{}:new Resource[]{context});
}