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

The following examples show how to use org.eclipse.rdf4j.common.iteration.Iterations#asSet() . 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: SpinSailConnection.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private int executeConstructors(Resource subj, List<IRI> classHierarchy)
		throws RDF4JException {
	int nofInferred = 0;
	Set<Resource> constructed = Iterations.asSet(TripleSources.getObjectResources(
			subj, EXECUTED, tripleSource));

	for (IRI cls : classHierarchy) {
		List<Resource> constructors = getConstructorsForClass(cls);
		if (!constructors.isEmpty()) {
			logger.trace("executing constructors for resource {} of class {}", subj, cls);

			for (Resource constructor : constructors) {
				if (constructed.add(constructor)) {
					logger.trace("executing constructor {} for resource {}", constructor, subj);
					nofInferred += executeRule(subj, constructor);
					addInferredStatement(subj, EXECUTED, constructor);
				}
			}
		}
	}
	logger.trace("added {} new triples via constructors for resource {}", nofInferred, subj);
	return nofInferred;
}
 
Example 2
Source File: RepositoryUtil.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Compares two models defined by the default context of two repositories and returns the difference between the
 * first and the second model (that is, all statements that are present in rep1 but not in rep2). Blank node IDs are
 * not relevant for model equality, they are mapped from one model to the other by using the attached properties.
 * Note that the method pulls the entire default context of both repositories into main memory. Use with caution.
 * <p>
 * <b>NOTE: this algorithm is currently broken; it doesn't actually map blank nodes between the two models.</b>
 *
 * @return The collection of statements that is the difference between rep1 and rep2.
 */
public static Collection<? extends Statement> difference(Repository rep1, Repository rep2)
		throws RepositoryException {
	Collection<Statement> model1;
	Collection<Statement> model2;

	try (RepositoryConnection con1 = rep1.getConnection()) {
		model1 = Iterations.asSet(con1.getStatements(null, null, null, false));
	}

	try (RepositoryConnection con2 = rep2.getConnection()) {
		model2 = Iterations.asSet(con2.getStatements(null, null, null, false));
	}

	return difference(model1, model2);
}
 
Example 3
Source File: SailModel.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Set<Namespace> getNamespaces() {
	Set<Namespace> namespaces;
	try {
		try (CloseableIteration<? extends Namespace, SailException> iter = conn.getNamespaces()) {
			namespaces = Iterations.asSet(conn.getNamespaces());
		}
	} catch (SailException e) {
		throw new ModelException(e);
	}
	return namespaces;
}
 
Example 4
Source File: RepositoryUtil.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Compares the models in the default contexts of the two supplied repositories and returns true if they are equal.
 * Models are equal if they contain the same set of statements. bNodes IDs are not relevant for model equality, they
 * are mapped from one model to the other by using the attached properties. Note that the method pulls the entire
 * default context of both repositories into main memory. Use with caution.
 */
public static boolean equals(Repository rep1, Repository rep2) throws RepositoryException {
	// Fetch statements from rep1 and rep2
	Set<Statement> model1, model2;

	try (RepositoryConnection con1 = rep1.getConnection()) {
		model1 = Iterations.asSet(con1.getStatements(null, null, null, true));
	}

	try (RepositoryConnection con2 = rep2.getConnection()) {
		model2 = Iterations.asSet(con2.getStatements(null, null, null, true));
	}

	return Models.isomorphic(model1, model2);
}
 
Example 5
Source File: RepositoryUtil.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Compares the models of the default context of two repositories and returns true if rep1 is a subset of rep2. Note
 * that the method pulls the entire default context of both repositories into main memory. Use with caution.
 */
public static boolean isSubset(Repository rep1, Repository rep2) throws RepositoryException {
	Set<Statement> model1, model2;

	try (RepositoryConnection con1 = rep1.getConnection()) {
		model1 = Iterations.asSet(con1.getStatements(null, null, null, true));
	}

	try (RepositoryConnection con2 = rep2.getConnection()) {
		model2 = Iterations.asSet(con2.getStatements(null, null, null, true));
	}

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

		Resource context1 = vf.createIRI("http://example.com/context1");
		Resource context2 = vf.createBNode();
		Resource context3 = vf.createIRI("http://example.com/context3");

		connection.add(RDF.TYPE, RDF.TYPE, RDFS.RESOURCE, context1);
		connection.add(RDF.TYPE, RDF.TYPE, RDF.PROPERTY, context2);
		connection.add(RDF.TYPE, RDF.TYPE, RDF.PREDICATE, context3);
		connection.add(RDF.TYPE, RDF.TYPE, vf.createLiteral("no context"));

		printAllDocs();

		StopWatch stopWatch = StopWatch.createStarted();
		Set<Statement> context1Statements = Iterations
				.asSet(connection.getStatements(null, RDF.TYPE, null, context1));

		stopWatch.stop();
		logTime(stopWatch, "Query", TimeUnit.MILLISECONDS);

		stopWatch = StopWatch.createStarted();

		Set<Statement> context2Statements = Iterations
				.asSet(connection.getStatements(null, RDF.TYPE, null, context2));

		stopWatch.stop();
		logTime(stopWatch, "Query", TimeUnit.MILLISECONDS);

		stopWatch = StopWatch.createStarted();

		Set<Statement> context3Statements = Iterations
				.asSet(connection.getStatements(null, RDF.TYPE, null, context3));

		stopWatch.stop();
		logTime(stopWatch, "Query", TimeUnit.MILLISECONDS);

		stopWatch = StopWatch.createStarted();

		Set<Statement> contextNoneStatements = Iterations
				.asSet(connection.getStatements(null, RDF.TYPE, null, true, (Resource) null));

		stopWatch.stop();
		logTime(stopWatch, "Query", TimeUnit.MILLISECONDS);

		stopWatch = StopWatch.createStarted();

		Set<Statement> contextAllStatements = Iterations
				.asSet(connection.getStatements(null, RDF.TYPE, null));

		stopWatch.stop();
		logTime(stopWatch, "Query", TimeUnit.MILLISECONDS);

		stopWatch = StopWatch.createStarted();

		Set<Statement> contextContext1And2Statements = Iterations
				.asSet(connection.getStatements(null, RDF.TYPE, null, context1, context2));

		stopWatch.stop();
		logTime(stopWatch, "Query", TimeUnit.MILLISECONDS);

		Statement statementContext1 = vf.createStatement(RDF.TYPE, RDF.TYPE, RDFS.RESOURCE, context1);
		Statement statementContext2 = vf.createStatement(RDF.TYPE, RDF.TYPE, RDF.PROPERTY, context2);
		Statement statementContext3 = vf.createStatement(RDF.TYPE, RDF.TYPE, RDF.PREDICATE, context3);
		Statement statementContextNone = vf.createStatement(RDF.TYPE, RDF.TYPE, vf.createLiteral("no context"));

		assertEquals(asSet(statementContext1), context1Statements);
		assertEquals(asSet(statementContext2), context2Statements);
		assertEquals(asSet(statementContext3), context3Statements);
		assertEquals(asSet(statementContext1, statementContext2), contextContext1And2Statements);
		assertEquals(asSet(statementContextNone), contextNoneStatements);
		assertEquals(asSet(statementContext1, statementContext2, statementContext3, statementContextNone),
				contextAllStatements);

	}

}
 
Example 7
Source File: SPARQLMinusIteration.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
protected Set<BindingSet> makeSet(Iteration<BindingSet, X> rightArg2) throws X {
	return Iterations.asSet(rightArg);
}
 
Example 8
Source File: QueryResults.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Compares two graph query results and returns {@code true} if they are equal. Two graph query results are
 * considered equal if they are isomorphic graphs. Note that the method consumes both query results fully.
 *
 * @param result1 the first query result to compare
 * @param result2 the second query result to compare.
 * @return {@code true} if the supplied graph query results are isomorphic graphs, {@code false} otherwise.
 * @throws QueryEvaluationException
 * @see Models#isomorphic(Iterable, Iterable)
 */
public static boolean equals(GraphQueryResult result1, GraphQueryResult result2) throws QueryEvaluationException {
	Set<? extends Statement> graph1 = Iterations.asSet(result1);
	Set<? extends Statement> graph2 = Iterations.asSet(result2);

	return Models.isomorphic(graph1, graph2);
}
 
Example 9
Source File: IterationBenchmarks.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
@Benchmark
public Set<String> asSet() throws Exception {

	return Iterations.asSet(getIterator(strings));

}
 
Example 10
Source File: IterationBenchmarks.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
@Benchmark
public Set<String> asSetDuplicate() throws Exception {

	return Iterations.asSet(getIterator(duplicates));

}