info.aduna.iteration.Iterations Java Examples

The following examples show how to use info.aduna.iteration.Iterations. 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: RepositoryModel.java    From semweb4j with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public Map<String,String> getNamespaces() {
	assertModel();
	Map<String,String> nsMap = new HashMap<String,String>();
	try {
		RepositoryResult<Namespace> openrdfMap = this.connection.getNamespaces();
		openrdfMap.enableDuplicateFilter();
		List<Namespace> openrdfList =  Iterations.asList(openrdfMap);
		for(Namespace openrdfNamespace : openrdfList) {
			nsMap.put(openrdfNamespace.getPrefix(), openrdfNamespace.getName());
		}
		return nsMap;
	} catch(RepositoryException e) {
		throw new ModelRuntimeException(e);
	}
}
 
Example #2
Source File: RDFStoreTest.java    From database with GNU General Public License v2.0 6 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 #3
Source File: ForwardChainingRDFSInferencerConnection.java    From semweb4j with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void flushUpdates()
	throws SailException
{
	super.flushUpdates();

	if (this.statementsRemoved) {
		this.logger.debug("statements removed, starting inferencing from scratch");
		clearInferred();
		addAxiomStatements();

		this.newStatements = new GraphImpl();
		Iterations.addAll(getWrappedConnection().getStatements(null, null, null, true), this.newStatements);

		this.statementsRemoved = false;
	}

	doInferencing();
}
 
Example #4
Source File: RepositoryConnectionTest.java    From database with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testRemoveStatementCollection()
	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)));

	Collection<Statement> c = Iterations.addAll(testCon.getStatements(null, null, null, false),
			new ArrayList<Statement>());

	testCon.remove(c);

	assertThat(testCon.hasStatement(bob, name, nameBob, false), is(equalTo(false)));
	assertThat(testCon.hasStatement(alice, name, nameAlice, false), is(equalTo(false)));
}
 
Example #5
Source File: RepositoryConnectionTest.java    From database with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testGetContextIDs()
	throws Exception
{
	assertThat(Iterations.asList(testCon.getContextIDs()).size(), is(equalTo(0)));

	// load data
	testCon.add(bob, name, nameBob, context1);
	assertThat(Iterations.asList(testCon.getContextIDs()), is(equalTo(Arrays.asList((Resource)context1))));

	testCon.remove(bob, name, nameBob, context1);
	assertThat(Iterations.asList(testCon.getContextIDs()).size(), is(equalTo(0)));

	testCon.add(bob, name, nameBob, context2);
	assertThat(Iterations.asList(testCon.getContextIDs()), is(equalTo(Arrays.asList((Resource)context2))));
}
 
Example #6
Source File: RepositoryConnectionTest.java    From database with GNU General Public License v2.0 5 votes vote down vote up
@Test
	public void testDefaultInsertContext()
		throws Exception
	{
		ContextAwareConnection con = new ContextAwareConnection(testCon);
		URI defaultGraph = vf.createURI("urn:test:default");
		con.setInsertContext(defaultGraph);
		con.add(vf.createURI(URN_TEST_S1), vf.createURI(URN_TEST_P1), vf.createURI(URN_TEST_O1));
		con.prepareUpdate("INSERT DATA { graph <urn:test:default> { <urn:test:s2> <urn:test:p2> \"l2\" }}").execute();
		assertThat(Iterations.asList(con.getStatements(null, null, null)).size(), is(equalTo(2)));
		assertThat(Iterations.asList(con.getStatements(null, null, null, defaultGraph)).size(), is(equalTo(2)));
		assertThat(size(defaultGraph), is(equalTo(2)));
		con.add(vf.createURI("urn:test:s3"), vf.createURI("urn:test:p3"), vf.createURI("urn:test:o3"),
				(Resource)null);
		con.add(vf.createURI("urn:test:s4"), vf.createURI("urn:test:p4"), vf.createURI("urn:test:o4"),
				vf.createURI(URN_TEST_OTHER));
		assertThat(Iterations.asList(con.getStatements(null, null, null)).size(), is(equalTo(4)));
		assertThat(Iterations.asList(con.getStatements(null, null, null, defaultGraph)).size(), is(equalTo(3)));
		assertThat(Iterations.asList(testCon.getStatements(null, null, null, true)).size(), is(equalTo(4)));
		assertThat(size(defaultGraph), is(equalTo(3)));
		assertThat(size(vf.createURI(URN_TEST_OTHER)), is(equalTo(1)));
		// not working.
//		con.prepareUpdate(SPARQL_DEL_ALL).execute();
		con.clear();
		assertThat(Iterations.asList(con.getStatements(null, null, null)).size(), is(equalTo(0)));
		assertThat(Iterations.asList(testCon.getStatements(null, null, null, true)).size(), is(equalTo(0)));
		assertThat(size(defaultGraph), is(equalTo(0)));
		assertThat(size(vf.createURI(URN_TEST_OTHER)), is(equalTo(0)));
	}
 
Example #7
Source File: WebTestUtils.java    From cumulusrdf with Apache License 2.0 5 votes vote down vote up
/**
 * @see org.openrdf.query.QueryResultUtil
 */
public static boolean matchTupleQueryResults(TupleQueryResult res1, TupleQueryResult res2) throws QueryEvaluationException {

	List<BindingSet> queryResult1 = Iterations.asList(res1);
	List<BindingSet> queryResult2 = Iterations.asList(res2);

	if (queryResult1.size() != queryResult2.size()) {
		return false;
	}

	for (BindingSet bs1 : queryResult1) {

		boolean hit = false;

		for (BindingSet bs2 : queryResult2) {

			if (bindingSetsMatch(bs1, bs2)) {
				hit = true;
				break;
			}
		}

		if (!hit) {
			return false;
		}
	}

	return true;
}
 
Example #8
Source File: IntegrationTestSupertypeLayer.java    From cumulusrdf with Apache License 2.0 5 votes vote down vote up
/**
 * Executes a given CONSTRUCT query against a given dataset.
 * 
 * @param data the mistery guest containing test data (query and dataset)
 * @throws Exception never, otherwise the test fails.
 */
protected void constructTest(final MisteryGuest data) throws Exception {
	load(data);
	
	final String queryString = queryString(data.query);
	final GraphQuery localQuery = localConnection.prepareGraphQuery(QueryLanguage.SPARQL, queryString);
	final GraphQuery cumulusQuery = cumulusConnection.prepareGraphQuery(QueryLanguage.SPARQL, queryString);
	
	GraphQueryResult localResult = localQuery.evaluate();
	GraphQueryResult cumulusResult = cumulusQuery.evaluate();
	try {
		assertTrue(
				ModelUtil.equals(
						statements(Iterations.asSet(localResult)), 
						statements(Iterations.asSet(cumulusResult))));			
	} catch (final AssertionError exception) {
		final GraphQueryResult debugLocalResult = localQuery.evaluate();
		final GraphQueryResult debugCumulusResult = cumulusQuery.evaluate();
			
		System.err.println("***** LOCAL ******");
		QueryResultIO.write(debugLocalResult, RDFFormat.NTRIPLES, System.err);

		System.err.println("***** CRDF ******");
		QueryResultIO.write(debugCumulusResult, RDFFormat.NTRIPLES, System.err);
		
		debugCumulusResult.close();
		debugLocalResult.close();
		throw exception;
	}
	
	cumulusResult.close();
	localResult.close();
}
 
Example #9
Source File: MutableTupleQueryResult.java    From database with GNU General Public License v2.0 5 votes vote down vote up
public <E extends Exception> MutableTupleQueryResult(Collection<String> bindingNames,
		Iteration<? extends BindingSet, E> bindingSetIter)
	throws E
{
	this.bindingNames.addAll(bindingNames);
	Iterations.addAll(bindingSetIter, this.bindingSets);
}
 
Example #10
Source File: BigdataStoreTest.java    From database with GNU General Public License v2.0 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 #11
Source File: RepositoryConnectionTest.java    From database with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testExclusiveNullContext()
	throws Exception
{
	ContextAwareConnection con = new ContextAwareConnection(testCon);
	URI defaultGraph = BD.NULL_GRAPH; // null context
	con.setReadContexts(defaultGraph);
	con.setInsertContext(defaultGraph);
	con.setRemoveContexts(defaultGraph);
	con.add(vf.createURI(URN_TEST_S1), vf.createURI(URN_TEST_P1), vf.createURI(URN_TEST_O1));
	con.prepareUpdate("INSERT DATA { <urn:test:s2> <urn:test:p2> \"l2\" }").execute();
	assertThat(Iterations.asList(con.getStatements(null, null, null)).size(), is(equalTo(2)));
	assertThat(Iterations.asList(con.getStatements(null, null, null, defaultGraph)).size(), is(equalTo(2)));
	assertThat(size(defaultGraph), is(equalTo(2)));
	con.add(vf.createURI("urn:test:s3"), vf.createURI("urn:test:p3"), vf.createURI("urn:test:o3"),
			(Resource)null);
	con.add(vf.createURI("urn:test:s4"), vf.createURI("urn:test:p4"), vf.createURI("urn:test:o4"),

	vf.createURI(URN_TEST_OTHER));
	assertThat(Iterations.asList(con.getStatements(null, null, null)).size(), is(equalTo(3)));
	assertThat(Iterations.asList(testCon.getStatements(null, null, null, true)).size(), is(equalTo(4)));
	assertThat(size(defaultGraph), is(equalTo(3)));
	assertThat(size(vf.createURI(URN_TEST_OTHER)), is(equalTo(1)));
	con.prepareUpdate(SPARQL_DEL_ALL).execute();
	assertThat(Iterations.asList(con.getStatements(null, null, null)).size(), is(equalTo(0)));
	assertThat(Iterations.asList(testCon.getStatements(null, null, null, true)).size(), is(equalTo(1)));
	assertThat(size(defaultGraph), is(equalTo(0)));
	assertThat(size(vf.createURI(URN_TEST_OTHER)), is(equalTo(1)));
}
 
Example #12
Source File: TripleStoreBlazegraph.java    From powsybl-core with Mozilla Public License 2.0 5 votes vote down vote up
private static void copyNamespaces(RepositoryConnection source, RepositoryConnection target) {
    try {
        RepositoryResult<Namespace> ns = source.getNamespaces();
        for (Namespace namespace : Iterations.asList(ns)) {
            target.setNamespace(namespace.getPrefix(), namespace.getName());
        }
    } catch (RepositoryException e) {
        LOG.error("Copying Namespaces : {}", e.getMessage());
    }
}
 
Example #13
Source File: RepositoryConnectionTest.java    From database with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testDefaultContext()
	throws Exception
{
	ContextAwareConnection con = new ContextAwareConnection(testCon);
	URI defaultGraph = vf.createURI("urn:test:default");
	con.setReadContexts(defaultGraph);
	con.setInsertContext(defaultGraph);
	con.setRemoveContexts(defaultGraph);
	con.add(vf.createURI(URN_TEST_S1), vf.createURI(URN_TEST_P1), vf.createURI(URN_TEST_O1));
	con.prepareUpdate("INSERT DATA { graph <urn:test:default> { <urn:test:s2> <urn:test:p2> \"l2\" }}").execute();
	assertThat(Iterations.asList(con.getStatements(null, null, null)).size(), is(equalTo(2)));
	assertThat(Iterations.asList(con.getStatements(null, null, null, defaultGraph)).size(), is(equalTo(2)));
	assertThat(size(defaultGraph), is(equalTo(2)));
	con.add(vf.createURI("urn:test:s3"), vf.createURI("urn:test:p3"), vf.createURI("urn:test:o3"),
			(Resource)null);
	con.add(vf.createURI("urn:test:s4"), vf.createURI("urn:test:p4"), vf.createURI("urn:test:o4"),
			vf.createURI(URN_TEST_OTHER));
	assertThat(Iterations.asList(con.getStatements(null, null, null)).size(), is(equalTo(3)));
	assertThat(Iterations.asList(testCon.getStatements(null, null, null, true)).size(), is(equalTo(4)));
	assertThat(size(defaultGraph), is(equalTo(3)));
	assertThat(size(vf.createURI(URN_TEST_OTHER)), is(equalTo(1)));
	con.prepareUpdate(SPARQL_DEL_ALL).execute();
	assertThat(Iterations.asList(con.getStatements(null, null, null)).size(), is(equalTo(0)));
	assertThat(Iterations.asList(testCon.getStatements(null, null, null, true)).size(), is(equalTo(1)));
	assertThat(size(defaultGraph), is(equalTo(0)));
	assertThat(size(vf.createURI(URN_TEST_OTHER)), is(equalTo(1)));
}
 
Example #14
Source File: RepositoryConnectionTest.java    From database with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testGetNamespaces()
	throws Exception
{
	setupNamespaces();
	Map<String, String> map = Namespaces.asMap(Iterations.asSet(testCon.getNamespaces()));
	assertThat(map.size(), is(equalTo(3)));
	assertThat(map.keySet(), hasItems(EXAMPLE, RDFS_PREFIX, RDF_PREFIX));
	assertThat(map.get(EXAMPLE), is(equalTo(EXAMPLE_NS)));
	assertThat(map.get(RDFS_PREFIX), is(equalTo(RDFS_NS)));
	assertThat(map.get(RDF_PREFIX), is(equalTo("http://www.w3.org/1999/02/22-rdf-syntax-ns#")));
}
 
Example #15
Source File: RepositoryConnectionTest.java    From database with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testGetStatements()
	throws Exception
{
	testCon.add(bob, name, nameBob);

	assertTrue("Repository should contain statement", testCon.hasStatement(bob, name, nameBob, false));

	RepositoryResult<Statement> result = testCon.getStatements(null, name, null, false);

	try {
		assertNotNull("Iterator should not be null", result);
		assertTrue("Iterator should not be empty", result.hasNext());

		while (result.hasNext()) {
			Statement st = result.next();
			assertNull("Statement should not be in a context ", st.getContext());
			assertTrue("Statement predicate should be equal to name ", st.getPredicate().equals(name));
		}
	}
	finally {
		result.close();
	}

	List<Statement> list = Iterations.addAll(testCon.getStatements(null, name, null, false),
			new ArrayList<Statement>());

	assertNotNull("List should not be null", list);
	assertFalse("List should not be empty", list.isEmpty());
}
 
Example #16
Source File: RDFStoreTest.java    From database with GNU General Public License v2.0 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 #17
Source File: BigdataSPARQLUpdateConformanceTest.java    From database with GNU General Public License v2.0 4 votes vote down vote up
@Override
    protected void runTest()
        throws Exception
    {
        BigdataSailRepositoryConnection con = (BigdataSailRepositoryConnection) dataRep.getConnection();
        BigdataSailRepositoryConnection erCon = (BigdataSailRepositoryConnection) expectedResultRepo.getConnection();
        try {
            String updateString = readUpdateString();
            
            con.begin();
//            con.setReadContexts((URI)null);
            
            Update update = con.prepareUpdate(QueryLanguage.SPARQL, updateString, requestFileURL);
//            if (this.dataset != null) {
//                update.setDataset(this.dataset);
//            }
            update.execute();

            con.commit();
            
            // check default graph
            compareGraphs(Iterations.asList(con.getStatements(null, null, null, true, (Resource)null)),
                    Iterations.asList(erCon.getStatements(null, null, null, true, (Resource)null)),
                    (BigdataSailRepositoryConnection) con);

            for (String namedGraph : inputNamedGraphs.keySet()) {
                URI contextURI = con.getValueFactory().createURI(namedGraph.replaceAll("\"", ""));
                compareGraphs(Iterations.asList(con.getStatements(null, null, null, true, contextURI)),
                        Iterations.asList(erCon.getStatements(null, null, null, true, contextURI)),
                        (BigdataSailRepositoryConnection) con);
            }
        }
        catch(Exception e) {
            e.printStackTrace();
            if(con.isActive()) {
                con.rollback();
            }
            throw e;
        }
        finally {
            con.close();
            erCon.close();
        }
    }
 
Example #18
Source File: SPARQLUpdateConformanceTest.java    From database with GNU General Public License v2.0 4 votes vote down vote up
@Override
	protected void runTest()
		throws Exception
	{
		RepositoryConnection con = dataRep.getConnection();
		RepositoryConnection erCon = expectedResultRepo.getConnection();
		try {
			String updateString = readUpdateString();

			con.begin();
//			con.setReadContexts((URI)null);
			
			Update update = con.prepareUpdate(QueryLanguage.SPARQL, updateString, requestFileURL);
			if (this.dataset != null) {
				update.setDataset(this.dataset);
			}
			update.execute();

			con.commit();
			
			// check default graph
			logger.info("checking default graph");
			compareGraphs(Iterations.asList(con.getStatements(null, null, null, true, (Resource)null)),
					Iterations.asList(erCon.getStatements(null, null, null, true, (Resource)null)));

			for (String namedGraph : inputNamedGraphs.keySet()) {
				logger.info("checking named graph {}", namedGraph);
				URI contextURI = con.getValueFactory().createURI(namedGraph.replaceAll("\"", ""));
				compareGraphs(Iterations.asList(con.getStatements(null, null, null, true, contextURI)),
						Iterations.asList(erCon.getStatements(null, null, null, true, contextURI)));
			}
		}
		catch(Exception e) {
			if(con.isActive()) {
				con.rollback();
			}
			throw e;
		}
		finally {
			con.close();
			erCon.close();
		}
	}