Java Code Examples for org.eclipse.rdf4j.common.iteration.Iterations#asList()

The following examples show how to use org.eclipse.rdf4j.common.iteration.Iterations#asList() . 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: HashJoinTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testMultipleBindings() throws Exception {

	List<BindingSet> leftBlock = new ArrayList<>();
	leftBlock.add(bindingSet(binding("x", irid("p1")), binding("y", l("P1"))));
	leftBlock.add(bindingSet(binding("x", irid("p2")), binding("y", l("P2"))));

	List<BindingSet> rightBlock = new ArrayList<>();
	rightBlock.add(bindingSet(binding("x", irid("p2")), binding("z", l("something"))));

	CloseableIteration<BindingSet, QueryEvaluationException> joinResultIter = HashJoin.join(leftBlock, rightBlock,
			Sets.newHashSet("x"),
			Collections.emptyList());
	List<BindingSet> joinResult = Iterations.asList(joinResultIter);

	Assertions.assertEquals(1, joinResult.size());
	Assertions.assertEquals(
			bindingSet(binding("x", irid("p2")), binding("y", l("P2")), binding("z", l("something"))),
			joinResult.get(0));
}
 
Example 2
Source File: ElasticsearchStoreTransactionsTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testGetData() {
	try (NotifyingSailConnection connection = elasticsearchStore.getConnection()) {
		connection.begin(IsolationLevels.NONE);
		connection.addStatement(RDF.TYPE, RDF.TYPE, RDFS.RESOURCE);
		connection.flush();
		List<? extends Statement> statements = Iterations.asList(connection.getStatements(null, null, null, true));

		connection.commit();

		System.out.println(Arrays.toString(statements.toArray()));

		assertEquals(1, statements.size());
		assertEquals(SimpleValueFactory.getInstance().createStatement(RDF.TYPE, RDF.TYPE, RDFS.RESOURCE),
				statements.get(0));
	}

}
 
Example 3
Source File: MemTripleSourceTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Test method for
 * {@link org.eclipse.rdf4j.sail.memory.MemTripleSource#getStatements(org.eclipse.rdf4j.model.Resource, org.eclipse.rdf4j.model.IRI, org.eclipse.rdf4j.model.Value, org.eclipse.rdf4j.model.Resource[])}
 * .
 */
@Test
public final void testGetStatementsNoContextsPredicateOwlClassNoContexts() throws Exception {
	loadTestData("/alp-testdata.ttl");
	TripleSource source = getTripleSourceCommitted();

	try (CloseableIteration<? extends Statement, QueryEvaluationException> statements = source.getStatements(null,
			RDF.TYPE, OWL.CLASS)) {
		List<Statement> list = Iterations.asList(statements);

		assertEquals(4, list.size());
	}
}
 
Example 4
Source File: MemTripleSourceTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Test method for
 * {@link org.eclipse.rdf4j.sail.memory.MemTripleSource#getStatements(org.eclipse.rdf4j.model.Resource, org.eclipse.rdf4j.model.IRI, org.eclipse.rdf4j.model.Value, org.eclipse.rdf4j.model.Resource[])}
 * .
 */
@Test
public final void testGetStatementsOneContextOnePredicate() throws Exception {
	loadTestData("/alp-testdata.ttl", this.alice);
	TripleSource source = getTripleSourceCommitted();

	try (CloseableIteration<? extends Statement, QueryEvaluationException> statements = source.getStatements(null,
			RDFS.SUBCLASSOF, null)) {
		List<Statement> list = Iterations.asList(statements);

		assertEquals(4, list.size());
	}
}
 
Example 5
Source File: MemTripleSourceTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Test method for
 * {@link org.eclipse.rdf4j.sail.memory.MemTripleSource#getStatements(org.eclipse.rdf4j.model.Resource, org.eclipse.rdf4j.model.IRI, org.eclipse.rdf4j.model.Value, org.eclipse.rdf4j.model.Resource[])}
 * .
 */
@Test
public final void testGetStatementsTwoContextsPredicateExClassTwoContexts() throws Exception {
	loadTestData("/alp-testdata.ttl", this.alice, this.bob);
	TripleSource source = getTripleSourceCommitted();

	try (CloseableIteration<? extends Statement, QueryEvaluationException> statements = source.getStatements(null,
			RDFS.SUBCLASSOF, f.createIRI(EX_NS, "A"), this.alice, this.bob)) {
		List<Statement> list = Iterations.asList(statements);

		assertEquals(6, list.size());
	}
}
 
Example 6
Source File: MemTripleSourceTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Test method for
 * {@link org.eclipse.rdf4j.sail.memory.MemTripleSource#getStatements(org.eclipse.rdf4j.model.Resource, org.eclipse.rdf4j.model.IRI, org.eclipse.rdf4j.model.Value, org.eclipse.rdf4j.model.Resource[])}
 * .
 */
@Test
public final void testGetStatementsNoContextsPredicateOwlClassTwoContexts() throws Exception {
	loadTestData("/alp-testdata.ttl");
	TripleSource source = getTripleSourceCommitted();

	try (CloseableIteration<? extends Statement, QueryEvaluationException> statements = source.getStatements(null,
			RDF.TYPE, OWL.CLASS, this.alice, this.bob)) {
		List<Statement> list = Iterations.asList(statements);

		assertEquals(0, list.size());
	}
}
 
Example 7
Source File: MemTripleSourceTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Test method for
 * {@link org.eclipse.rdf4j.sail.memory.MemTripleSource#getStatements(org.eclipse.rdf4j.model.Resource, org.eclipse.rdf4j.model.IRI, org.eclipse.rdf4j.model.Value, org.eclipse.rdf4j.model.Resource[])}
 * .
 */
@Test
public final void testGetStatementsTwoContextsExClassPredicateTwoContexts() throws Exception {
	loadTestData("/alp-testdata.ttl", this.alice, this.bob);
	TripleSource source = getTripleSourceCommitted();

	try (CloseableIteration<? extends Statement, QueryEvaluationException> statements = source
			.getStatements(f.createIRI(EX_NS, "C"), RDFS.SUBCLASSOF, null, this.alice, this.bob)) {
		List<Statement> list = Iterations.asList(statements);

		assertEquals(2, list.size());
	}
}
 
Example 8
Source File: MemTripleSourceTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Test method for
 * {@link org.eclipse.rdf4j.sail.memory.MemTripleSource#getStatements(org.eclipse.rdf4j.model.Resource, org.eclipse.rdf4j.model.IRI, org.eclipse.rdf4j.model.Value, org.eclipse.rdf4j.model.Resource[])}
 * .
 */
@Test
public final void testGetStatementsThreeContextsThreeContexts() throws Exception {
	loadTestData("/alp-testdata.ttl", this.alice, this.bob, this.mary);
	TripleSource source = getTripleSourceCommitted();

	try (CloseableIteration<? extends Statement, QueryEvaluationException> statements = source.getStatements(null,
			null, null, this.alice, this.bob, this.mary)) {
		List<Statement> list = Iterations.asList(statements);

		assertEquals(24, list.size());
	}
}
 
Example 9
Source File: ContextsServlet.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected void service(TupleResultBuilder builder, RepositoryConnection con)
		throws RepositoryException, QueryResultHandlerException {
	for (Resource ctx : Iterations.asList(con.getContextIDs())) {
		builder.result(ctx);
	}
}
 
Example 10
Source File: ReadCacheBenchmark.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Benchmark
public List<BindingSet> complexQueryWithoutCache() {

	try (SailRepositoryConnection connection = repoWithoutCache.getConnection()) {
		return Iterations.asList(connection
				.prepareTupleQuery(query5)
				.evaluate());
	}
}
 
Example 11
Source File: MemTripleSourceTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Test method for
 * {@link org.eclipse.rdf4j.sail.memory.MemTripleSource#getStatements(org.eclipse.rdf4j.model.Resource, org.eclipse.rdf4j.model.IRI, org.eclipse.rdf4j.model.Value, org.eclipse.rdf4j.model.Resource[])}
 * .
 */
@Test
public final void testGetStatementsOneContextPredicateOwlClassTwoContexts() throws Exception {
	loadTestData("/alp-testdata.ttl", this.alice);
	TripleSource source = getTripleSourceCommitted();

	try (CloseableIteration<? extends Statement, QueryEvaluationException> statements = source.getStatements(null,
			RDF.TYPE, OWL.CLASS, this.alice, this.bob)) {
		List<Statement> list = Iterations.asList(statements);

		assertEquals(4, list.size());
	}
}
 
Example 12
Source File: QueryBenchmark.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Benchmark
public List<BindingSet> groupByQuery() {

	try (SailRepositoryConnection connection = repository.getConnection()) {
		return Iterations.asList(connection
				.prepareTupleQuery(query1)
				.evaluate());
	}
}
 
Example 13
Source File: MemTripleSourceTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Test method for
 * {@link org.eclipse.rdf4j.sail.memory.MemTripleSource#getStatements(org.eclipse.rdf4j.model.Resource, org.eclipse.rdf4j.model.IRI, org.eclipse.rdf4j.model.Value, org.eclipse.rdf4j.model.Resource[])}
 * .
 */
@Test
public final void testGetStatementsOneContextPredicateExClassOneContext() throws Exception {
	loadTestData("/alp-testdata.ttl", this.alice);
	TripleSource source = getTripleSourceCommitted();

	try (CloseableIteration<? extends Statement, QueryEvaluationException> statements = source.getStatements(null,
			RDFS.SUBCLASSOF, f.createIRI(EX_NS, "A"), this.alice)) {
		List<Statement> list = Iterations.asList(statements);

		assertEquals(3, list.size());
	}
}
 
Example 14
Source File: MemTripleSourceTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Test method for
 * {@link org.eclipse.rdf4j.sail.memory.MemTripleSource#getStatements(org.eclipse.rdf4j.model.Resource, org.eclipse.rdf4j.model.IRI, org.eclipse.rdf4j.model.Value, org.eclipse.rdf4j.model.Resource[])}
 * .
 */
@Test
public final void testGetStatementsOneContextAllNull() throws Exception {
	loadTestData("/alp-testdata.ttl", this.alice);
	TripleSource source = getTripleSourceCommitted();

	try (CloseableIteration<? extends Statement, QueryEvaluationException> statements = source.getStatements(null,
			null, null)) {
		List<Statement> list = Iterations.asList(statements);

		assertEquals(8, list.size());
	}
}
 
Example 15
Source File: ElasticsearchStoreTransactionsTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void testGetDataSailRepositoryStringObject() {
	SailRepository elasticsearchStore = new SailRepository(this.elasticsearchStore);
	try (SailRepositoryConnection connection = elasticsearchStore.getConnection()) {

		Literal label = vf.createLiteral("label1");
		connection.add(RDF.TYPE, RDFS.LABEL, label);

		List<? extends Statement> statements = Iterations
				.asList(connection.getStatements(null, null, label, true));

		System.out.println(Arrays.toString(statements.toArray()));

		assertEquals(1, statements.size());

		assertEquals(label, statements.get(0).getObject());

	}

}
 
Example 16
Source File: FedXInRDF4JWorkbenchTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void testFederationFilter() throws Exception {

	assumeSparqlEndpoint();

	// load some data into endpoint1 and endpoint2
	loadDataSet(server.getRepository(1), "/tests/data/data1.ttl");
	loadDataSet(server.getRepository(2), "/tests/data/data2.ttl");

	final String repositoryId = "my-federation";
	final SPARQLEmbeddedServer rdf4jServer = (SPARQLEmbeddedServer) server;
	final File dataDir = rdf4jServer.getDataDir();
	String repoPath = "server/repositories";
	if (PlatformFactory.getPlatform().dataDirPreserveCase()) {
		repoPath = "Server/repositories";
	}
	final File repositoriesDir = new File(dataDir, repoPath);

	// preparation: add configuration files to the repository
	File fedXDataDir = new File(repositoriesDir, repositoryId);
	fedXDataDir.mkdirs();

	FileUtils.copyFile(toFile("/tests/rdf4jserver/config.ttl"), new File(fedXDataDir, "config.ttl"));

	String fedXSparqlUrl = rdf4jServer.getRepositoryUrl(repositoryId);
	SPARQLRepository repo = new SPARQLRepository(fedXSparqlUrl);
	repo.init();

	String query = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" +
			"PREFIX ns1: <http://namespace1.org/> "
			+ "SELECT * WHERE { "
			+ "?person a foaf:Person ."
			+ "?person foaf:name ?name .\n"
			+ "FILTER (?name IN ('Person1', 'Person2')) "
			+ "}";

	try (RepositoryConnection conn = repo.getConnection()) {
		try (TupleQueryResult tqr = conn.prepareTupleQuery(query).evaluate()) {

			List<BindingSet> res = Iterations.asList(tqr);
			assertContainsAll(res, "person",
					Sets.newHashSet(iri("http://namespace1.org/", "Person_1"),
							iri("http://namespace1.org/", "Person_2")));
		}
	}

	repo.shutDown();

	// temporary workaround: shutdown the federation repository explicitly here to
	// avoid a long running test. This is because the federation keeps an open
	// connection to other endpoints hosted in the same server, and the shutdown
	// sequence is arbitrary.
	Repository fedx = rdf4jServer.getRepositoryResolver().getRepository(repositoryId);
	fedx.shutDown();
}
 
Example 17
Source File: QueryBenchmark.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Setup(Level.Trial)
public void beforeClass() throws IOException {

	file = Files.newTemporaryFolder();

	repository = new SailRepository(new NativeStore(file, "spoc,ospc,psoc"));

	try (SailRepositoryConnection connection = repository.getConnection()) {
		connection.begin(IsolationLevels.NONE);
		connection.add(getResourceAsStream("benchmarkFiles/datagovbe-valid.ttl"), "", RDFFormat.TURTLE);
		connection.commit();
	}

	try (SailRepositoryConnection connection = repository.getConnection()) {

		statementList = Iterations.asList(connection.getStatements(null, RDF.TYPE, null, false));
	}

	System.gc();

}
 
Example 18
Source File: FedXWithLocalRepositoryManagerTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void testWithLocalRepositoryManager_CustomFederatedServiceResolver() throws Exception {

	addMemoryStore("repo1");
	addMemoryStore("repo2");
	addMemoryStore("serviceRepo");

	// register custom federated service resolver
	AbstractFederatedServiceResolver serviceResolver = new SPARQLServiceResolver() {
		@Override
		protected FederatedService createService(String serviceUrl) throws QueryEvaluationException {
			if (serviceUrl.equals("http://serviceRepo")) {
				Repository serviceRepo = repoManager.getRepository("serviceRepo");
				return new RepositoryFederatedService(serviceRepo, false);
			}
			throw new IllegalArgumentException("Service url cannot be resolved: " + serviceUrl);
		}
	};
	repoManager.externalResolver = serviceResolver;

	ValueFactory vf = SimpleValueFactory.getInstance();
	addData("repo1", Lists.newArrayList(
			vf.createStatement(vf.createIRI("http://ex.org/p1"), RDF.TYPE, FOAF.PERSON)));
	addData("repo2", Lists.newArrayList(
			vf.createStatement(vf.createIRI("http://ex.org/p2"), RDF.TYPE, FOAF.PERSON)));

	addData("serviceRepo", Lists.newArrayList(
			vf.createStatement(vf.createIRI("http://ex.org/p1"), FOAF.NAME, vf.createLiteral("Person 1")),
			vf.createStatement(vf.createIRI("http://ex.org/p2"), FOAF.NAME, vf.createLiteral("Person 2"))));

	FedXRepositoryConfig fedXRepoConfig = FedXRepositoryConfigBuilder.create()
			.withResolvableEndpoint(Arrays.asList("repo1", "repo2"))
			.build();
	repoManager.addRepositoryConfig(new RepositoryConfig("federation", fedXRepoConfig));

	Repository repo = repoManager.getRepository("federation");

	try (RepositoryConnection conn = repo.getConnection()) {

		TupleQuery tq = conn.prepareTupleQuery(
				"SELECT * WHERE { ?person a ?PERSON . { SERVICE <http://serviceRepo> { ?person ?NAME ?name} } }");
		tq.setBinding("PERSON", FOAF.PERSON);
		tq.setBinding("NAME", FOAF.NAME);

		List<BindingSet> bindings = Iterations.asList(tq.evaluate());
		Assertions.assertEquals(2, bindings.size()); // two persons

		BindingSet b1 = bindings.get(0);
		BindingSet b2 = bindings.get(1);

		if (b1.getValue("person").equals(vf.createIRI("http://ex.org/p1"))) {
			Assertions.assertEquals(vf.createLiteral("Person 1"), b1.getValue("name"));
			Assertions.assertEquals(vf.createLiteral("Person 2"), b2.getValue("name"));
		} else {
			Assertions.assertEquals(vf.createLiteral("Person 2"), b1.getValue("name"));
			Assertions.assertEquals(vf.createLiteral("Person 1"), b2.getValue("name"));
		}

	}

}
 
Example 19
Source File: ElasticsearchStoreTransactionsTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void testGetDataSailRepositoryLang() {
	SailRepository elasticsearchStore = new SailRepository(this.elasticsearchStore);
	try (SailRepositoryConnection connection = elasticsearchStore.getConnection()) {

		Literal label = vf.createLiteral("norsk bokmål", "nb");
		connection.add(RDF.TYPE, RDFS.LABEL, label);

		List<? extends Statement> statements = Iterations
				.asList(connection.getStatements(null, null, label, true));

		System.out.println(Arrays.toString(statements.toArray()));

		assertEquals(1, statements.size());

		assertEquals(label, statements.get(0).getObject());

	}

}
 
Example 20
Source File: ElasticsearchStoreTransactionsTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void testGetDataSailRepositoryBNodeObject() {
	SailRepository elasticsearchStore = new SailRepository(this.elasticsearchStore);
	try (SailRepositoryConnection connection = elasticsearchStore.getConnection()) {

		BNode bNode = vf.createBNode();
		connection.add(bNode, RDF.TYPE, bNode);

		List<? extends Statement> statements = Iterations
				.asList(connection.getStatements(bNode, RDF.TYPE, bNode, true));

		System.out.println(Arrays.toString(statements.toArray()));

		assertEquals(1, statements.size());

		assertEquals(bNode, statements.get(0).getSubject());
		assertEquals(bNode, statements.get(0).getObject());

	}

}