Java Code Examples for org.eclipse.rdf4j.model.util.Models#isomorphic()

The following examples show how to use org.eclipse.rdf4j.model.util.Models#isomorphic() . 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: DynamicModel.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public boolean equals(Object o) {
	if (this == o) {
		return true;
	}

	if (o instanceof Model) {
		Model model = (Model) o;
		return Models.isomorphic(this, model);
	} else if (o instanceof Set) {
		if (this.size() != ((Set<?>) o).size()) {
			return false;
		}
		try {
			return Models.isomorphic(this, (Iterable<? extends Statement>) o);
		} catch (ClassCastException e) {
			return false;
		}
	}

	return false;

}
 
Example 2
Source File: AbstractModel.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public boolean equals(Object o) {
	if (this == o) {
		return true;
	}
	if (o instanceof Model) {
		Model model = (Model) o;
		return Models.isomorphic(this, model);
	} else if (o instanceof Set) {
		if (this.size() != ((Set<?>) o).size()) {
			return false;
		}
		try {
			return Models.isomorphic(this, (Iterable<? extends Statement>) o);
		} catch (ClassCastException e) {
			return false;
		}
	}

	return false;
}
 
Example 3
Source File: SimpleOntology.java    From mobi with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }

    if (obj instanceof SimpleOntology) {
        SimpleOntology simpleOntology = (SimpleOntology) obj;
        OntologyId ontologyId = simpleOntology.getOntologyId();
        if (this.ontologyId.equals(ontologyId)) {
            org.eclipse.rdf4j.model.Model thisSesameModel = this.asSesameModel();
            org.eclipse.rdf4j.model.Model otherSesameModel = simpleOntology.asSesameModel();
            return Models.isomorphic(thisSesameModel, otherSesameModel);
        }
    }

    return false;
}
 
Example 4
Source File: SimpleNamedGraph.java    From mobi with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Compares two NamedGraphs, and returns true if they consist of isomorphic graphs and the isomorphic graph
 * identifiers map 1:1 to each other. RDF graphs are isomorphic graphs if statements from one graphs can be mapped
 * 1:1 on to statements in the other graphs. In this mapping, blank nodes are not considered mapped when having an
 * identical internal id, but are mapped from one graph to the other by looking at the statements in which the blank
 * nodes occur.
 *
 * Note: Depending on the size of the models, this can be an expensive operation.
 *
 * @return true if they are isomorphic graphs and the isomorphic graph identifiers map 1:1 to each other
 */
@Override
public boolean equals(Object o) {
    if (this == o) {
        return true;
    }
    if (o instanceof SimpleNamedGraph) {
        SimpleNamedGraph graph = (SimpleNamedGraph) o;

        if (!getGraphID().equals(graph.getGraphID()))
            return false;

        SesameModelWrapper model1 = new SesameModelWrapper(new org.eclipse.rdf4j.model.impl.LinkedHashModel());
        model1.addAll(this);

        SesameModelWrapper model2 = new SesameModelWrapper(new org.eclipse.rdf4j.model.impl.LinkedHashModel());
        model2.addAll(graph);

        return Models.isomorphic(model1.getSesameModel(), model2.getSesameModel());
    }
    return false;
}
 
Example 5
Source File: RDF4JStore.java    From rmlmapper-java with MIT License 5 votes vote down vote up
@Override
public boolean equals(Object o) {
    if (o instanceof RDF4JStore) {
        RDF4JStore otherStore = (RDF4JStore) o;

        return Models.isomorphic(model, otherStore.getModel());
    } else {
        return false;
    }
}
 
Example 6
Source File: RDF4JStore.java    From rmlmapper-java with MIT License 5 votes vote down vote up
@Override
public boolean isIsomorphic(QuadStore store) {
    if (store instanceof RDF4JStore) {
        RDF4JStore rdf4JStore = (RDF4JStore) store;
        return Models.isomorphic(model, rdf4JStore.getModel());
    } else {
        throw new UnsupportedOperationException();
    }
}
 
Example 7
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 8
Source File: CustomTurtleParserTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testSES2019ParseLongLiterals() throws Exception {
	parser.parse(this.getClass().getResourceAsStream("/testcases/turtle/turtle-long-literals-test.ttl"), "");

	assertTrue(errors.getWarnings().isEmpty());
	assertTrue(errors.getErrors().isEmpty());
	assertTrue(errors.getFatalErrors().isEmpty());

	assertFalse(statementCollector.getStatements().isEmpty());
	assertEquals(5, statementCollector.getStatements().size());

	Models.isomorphic(statementCollector.getStatements(),
			Rio.parse(this.getClass().getResourceAsStream("/testcases/turtle/turtle-long-literals-test.nt"), "",
					RDFFormat.NTRIPLES));
}
 
Example 9
Source File: N3ParserTestCase.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected void runTest() throws Exception {
	// Parse input data
	RDFParser turtleParser = createRDFParser();
	turtleParser.setDatatypeHandling(RDFParser.DatatypeHandling.IGNORE);

	Set<Statement> inputCollection = new LinkedHashSet<>();
	StatementCollector inputCollector = new StatementCollector(inputCollection);
	turtleParser.setRDFHandler(inputCollector);

	InputStream in = inputURL.openStream();
	turtleParser.parse(in, base(inputURL.toExternalForm()));
	in.close();

	// Parse expected output data
	NTriplesParser ntriplesParser = new NTriplesParser();
	ntriplesParser.setDatatypeHandling(RDFParser.DatatypeHandling.IGNORE);

	Set<Statement> outputCollection = new LinkedHashSet<>();
	StatementCollector outputCollector = new StatementCollector(outputCollection);
	ntriplesParser.setRDFHandler(outputCollector);

	in = outputURL.openStream();
	ntriplesParser.parse(in, base(outputURL.toExternalForm()));
	in.close();

	// Check equality of the two models
	if (!Models.isomorphic(inputCollection, outputCollection)) {
		System.err.println("===models not equal===");
		// System.err.println("Expected: " + outputCollection);
		// System.err.println("Actual : " + inputCollection);
		// System.err.println("======================");

		List<Statement> missing = new LinkedList<>(outputCollection);
		missing.removeAll(inputCollection);

		List<Statement> unexpected = new LinkedList<>(inputCollection);
		unexpected.removeAll(outputCollection);

		if (!missing.isEmpty()) {
			System.err.println("Missing   : " + missing);
		}
		if (!unexpected.isEmpty()) {
			System.err.println("Unexpected: " + unexpected);
		}

		fail("models not equal");
	}
}
 
Example 10
Source File: RDFJSONParserTestCase.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected void runTest() throws Exception {
	// Parse input data
	RDFParser rdfjsonParser = createRDFParser();
	rdfjsonParser.setDatatypeHandling(RDFParser.DatatypeHandling.IGNORE);

	Set<Statement> inputCollection = new LinkedHashSet<>();
	StatementCollector inputCollector = new StatementCollector(inputCollection);
	rdfjsonParser.setRDFHandler(inputCollector);

	InputStream in = this.getClass().getResourceAsStream(inputURL);
	rdfjsonParser.parse(in, baseURL);
	in.close();

	// Parse expected output data
	NTriplesParser ntriplesParser = new NTriplesParser();
	ntriplesParser.setDatatypeHandling(RDFParser.DatatypeHandling.IGNORE);

	Set<Statement> outputCollection = new LinkedHashSet<>();
	StatementCollector outputCollector = new StatementCollector(outputCollection);
	ntriplesParser.setRDFHandler(outputCollector);

	if (outputURL != null) {
		// System.out.println(this.outputURL);
		//
		// NTriplesWriter nTriplesWriter = new NTriplesWriter(System.out);
		// nTriplesWriter.startRDF();
		// for(Statement nextStatment : inputCollection) {
		// nTriplesWriter.handleStatement(nextStatment);
		// }
		// nTriplesWriter.endRDF();

		in = this.getClass().getResourceAsStream(outputURL);
		ntriplesParser.parse(in, baseURL);
		in.close();

		// Check equality of the two models
		if (!Models.isomorphic(inputCollection, outputCollection)) {
			System.err.println("===models not equal===");
			System.err.println("Expected: " + outputCollection);
			System.err.println("Actual  : " + inputCollection);
			System.err.println("======================");

			fail("models not equal");
		}
	}
}
 
Example 11
Source File: PositiveParserTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected void runTest() throws Exception {
	// Parse input data
	// targetParser.setDatatypeHandling(RDFParser.DatatypeHandling.IGNORE);

	Set<Statement> inputCollection = new LinkedHashSet<>();
	StatementCollector inputCollector = new StatementCollector(inputCollection);
	targetParser.setRDFHandler(inputCollector);

	InputStream in = this.getClass().getResourceAsStream(inputURL);
	assertNotNull("Test resource was not found: inputURL=" + inputURL, in);

	logger.debug("test: " + inputURL);

	ParseErrorCollector el = new ParseErrorCollector();
	targetParser.setParseErrorListener(el);

	try {
		targetParser.parse(in, baseURL);
	} finally {
		in.close();

		if (!el.getFatalErrors().isEmpty()) {
			logger.error("[Turtle] Input file had fatal parsing errors: \n" + el.getFatalErrors());
		}

		if (!el.getErrors().isEmpty()) {
			logger.error("[Turtle] Input file had parsing errors: \n" + el.getErrors());
		}

		if (!el.getWarnings().isEmpty()) {
			logger.warn("[Turtle] Input file had parsing warnings: \n" + el.getWarnings());
		}
	}

	if (outputURL != null) {
		// Parse expected output data
		ntriplesParser.setDatatypeHandling(RDFParser.DatatypeHandling.IGNORE);

		Set<Statement> outputCollection = new LinkedHashSet<>();
		StatementCollector outputCollector = new StatementCollector(outputCollection);
		ntriplesParser.setRDFHandler(outputCollector);

		in = this.getClass().getResourceAsStream(outputURL);
		try {
			ntriplesParser.parse(in, baseURL);
		} finally {
			in.close();
		}

		// Check equality of the two models
		if (!Models.isomorphic(inputCollection, outputCollection)) {
			logger.error("===models not equal===\n"
					+ "Expected: " + outputCollection + "\n"
					+ "Actual  : " + inputCollection + "\n"
					+ "======================");

			fail("models not equal");
		}
	}
}
 
Example 12
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 13
Source File: SesameModelWrapper.java    From mobi with GNU Affero General Public License v3.0 3 votes vote down vote up
/**
 * Compares two RDF models, and returns true if they consist of isomorphic graphs and the isomorphic graph
 * identifiers map 1:1 to each other. RDF graphs are isomorphic graphs if statements from one graphs can be mapped
 * 1:1 on to statements in the other graphs. In this mapping, blank nodes are not considered mapped when having an
 * identical internal id, but are mapped from one graph to the other by looking at the statements in which the blank
 * nodes occur.
 *
 * A Model can consist of more than one graph (denoted by context identifiers). Two models are considered isomorphic
 * if for each of the graphs in one model, an isomorphic graph exists in the other model, and the context
 * identifiers of these graphs are either identical or (in the case of blank nodes) map 1:1 on each other.
 *
 * Note: Depending on the size of the models, this can be an expensive operation.
 *
 * @return true if they consist of isomorphic graphs and the isomorphic graph identifiers map 1:1 to each other
 */
@Override
public boolean equals(Object o) {
    if (this == o) {
        return true;
    }
    if (o instanceof SesameModelWrapper) {
        SesameModelWrapper model = (SesameModelWrapper) o;
        return Models.isomorphic(this.sesameModel, model.getSesameModel());
    }
    return false;
}