org.eclipse.rdf4j.model.impl.LinkedHashModel Java Examples

The following examples show how to use org.eclipse.rdf4j.model.impl.LinkedHashModel. 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: RDFWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testWriteTwoStatementsObjectBNodeSinglePredicateSingleContextBNodeWithNamespace() throws Exception {
	Model input = new LinkedHashModel();
	input.setNamespace("ex", exNs);
	input.add(vf.createStatement(uri1, uri1, bnodeSingleUseObject, bnode));
	input.add(vf.createStatement(uri1, uri2, bnodeSingleUseObject, bnode));
	ByteArrayOutputStream outputWriter = new ByteArrayOutputStream();
	write(input, outputWriter);
	ByteArrayInputStream inputReader = new ByteArrayInputStream(outputWriter.toByteArray());
	Model parsedOutput = parse(inputReader, "");
	assertEquals(2, parsedOutput.size());
	assertEquals(1, parsedOutput.filter(uri1, uri1, null).size());
	assertEquals(1, parsedOutput.filter(uri1, uri2, null).size());
	assertEquals(1, parsedOutput.contexts().size());
	if (rdfWriterFactory.getRDFFormat().supportsContexts()) {
		assertTrue(parsedOutput.contexts().iterator().next() instanceof BNode);
	}
	assertEquals(1, parsedOutput.objects().size());
	assertTrue(parsedOutput.objects().iterator().next() instanceof BNode);
}
 
Example #2
Source File: RDFWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testWriteCommentURIContextURIBeforeNamespace() throws Exception {
	ByteArrayOutputStream outputWriter = new ByteArrayOutputStream();
	RDFWriter rdfWriter = rdfWriterFactory.getWriter(outputWriter);
	setupWriterConfig(rdfWriter.getWriterConfig());
	rdfWriter.startRDF();
	rdfWriter.handleStatement(vf.createStatement(uri1, uri1, uri1, uri1));
	rdfWriter.handleComment("This comment should not screw up parsing");
	rdfWriter.handleNamespace("ex1", exNs);
	rdfWriter.handleStatement(vf.createStatement(uri1, uri1, uri2, uri1));
	rdfWriter.endRDF();
	ByteArrayInputStream inputReader = new ByteArrayInputStream(outputWriter.toByteArray());
	RDFParser rdfParser = rdfParserFactory.getParser();
	setupParserConfig(rdfParser.getParserConfig());
	Model parsedOutput = new LinkedHashModel();
	rdfParser.setRDFHandler(new StatementCollector(parsedOutput));
	rdfParser.parse(inputReader, "");
	assertEquals(2, parsedOutput.size());
	if (rdfWriterFactory.getRDFFormat().supportsContexts()) {
		assertTrue(parsedOutput.contains(uri1, uri1, uri1, uri1));
		assertTrue(parsedOutput.contains(uri1, uri1, uri2, uri1));
	} else {
		assertTrue(parsedOutput.contains(uri1, uri1, uri1));
		assertTrue(parsedOutput.contains(uri1, uri1, uri2));
	}
}
 
Example #3
Source File: HTTPRepositoryConnection.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void add(Statement st, Resource... contexts) throws RepositoryException {
	if (!isActive()) {
		// operation is not part of a transaction - just send directly
		OpenRDFUtil.verifyContextNotNull(contexts);

		final Model m = new LinkedHashModel();

		if (contexts.length == 0) {
			// if no context is specified in the method call, statement's own
			// context (if any) is used.
			m.add(st.getSubject(), st.getPredicate(), st.getObject(), st.getContext());
		} else {
			m.add(st.getSubject(), st.getPredicate(), st.getObject(), contexts);
		}
		addModel(m);
	} else {
		super.add(st, contexts);
	}
}
 
Example #4
Source File: RDFWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testWriteCommentURIContextWithNamespaceBeforeNamespace() throws Exception {
	ByteArrayOutputStream outputWriter = new ByteArrayOutputStream();
	RDFWriter rdfWriter = rdfWriterFactory.getWriter(outputWriter);
	setupWriterConfig(rdfWriter.getWriterConfig());
	rdfWriter.startRDF();
	rdfWriter.handleNamespace("ex", exNs);
	rdfWriter.handleStatement(vf.createStatement(uri1, uri1, uri1, uri1));
	rdfWriter.handleComment("This comment should not screw up parsing");
	rdfWriter.handleNamespace("ex1", exNs);
	rdfWriter.endRDF();
	ByteArrayInputStream inputReader = new ByteArrayInputStream(outputWriter.toByteArray());
	RDFParser rdfParser = rdfParserFactory.getParser();
	setupParserConfig(rdfParser.getParserConfig());
	Model parsedOutput = new LinkedHashModel();
	rdfParser.setRDFHandler(new StatementCollector(parsedOutput));
	rdfParser.parse(inputReader, "");
	assertEquals(1, parsedOutput.size());
	if (rdfWriterFactory.getRDFFormat().supportsContexts()) {
		assertTrue(parsedOutput.contains(uri1, uri1, uri1, uri1));
	} else {
		assertTrue(parsedOutput.contains(uri1, uri1, uri1));
	}
}
 
Example #5
Source File: RepositoryConnectionTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testGraphSerialization() throws Exception {
	testCon.add(bob, name, nameBob);
	testCon.add(alice, name, nameAlice);

	try (RepositoryResult<Statement> statements = testCon.getStatements(null, null, null, true);) {
		Model graph = Iterations.addAll(statements, new LinkedHashModel());

		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		ObjectOutputStream out = new ObjectOutputStream(baos);
		out.writeObject(graph);
		out.close();

		ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
		ObjectInputStream in = new ObjectInputStream(bais);
		Model deserializedGraph = (Model) in.readObject();
		in.close();

		assertThat(deserializedGraph.isEmpty()).isFalse();
		assertThat(deserializedGraph).hasSameSizeAs(graph);
		for (Statement st : deserializedGraph) {
			assertThat(graph).contains(st);
			assertThat(testCon.hasStatement(st, true)).isTrue();
		}
	}
}
 
Example #6
Source File: RDFWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testWriteCommentBNodeContextBNodeBeforeNamespace() throws Exception {
	ByteArrayOutputStream outputWriter = new ByteArrayOutputStream();
	RDFWriter rdfWriter = rdfWriterFactory.getWriter(outputWriter);
	setupWriterConfig(rdfWriter.getWriterConfig());
	rdfWriter.startRDF();
	rdfWriter.handleStatement(vf.createStatement(uri1, uri1, uri1, bnode));
	rdfWriter.handleComment("This comment should not screw up parsing");
	rdfWriter.handleNamespace("ex1", exNs);
	rdfWriter.handleStatement(vf.createStatement(uri1, uri1, uri2, bnode));
	rdfWriter.endRDF();
	ByteArrayInputStream inputReader = new ByteArrayInputStream(outputWriter.toByteArray());
	RDFParser rdfParser = rdfParserFactory.getParser();
	setupParserConfig(rdfParser.getParserConfig());
	Model parsedOutput = new LinkedHashModel();
	rdfParser.setRDFHandler(new StatementCollector(parsedOutput));
	rdfParser.parse(inputReader, "");
	assertEquals(2, parsedOutput.size());
	assertTrue(parsedOutput.contains(uri1, uri1, uri1));
	assertTrue(parsedOutput.contains(uri1, uri1, uri2));
	assertEquals(1, parsedOutput.contexts().size());
}
 
Example #7
Source File: RDFWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testBogusIRICharacters() throws Exception {
	Model model = new LinkedHashModel();
	String illegal = " <>^|\t\n\r\"`";
	for (int i = 0; i < illegal.length(); i++) {
		model.add(vf.createIRI("urn:test:char" + illegal.charAt(i)), RDF.TYPE, RDFS.RESOURCE);
	}
	ByteArrayOutputStream outputWriter = new ByteArrayOutputStream();
	RDFWriter rdfWriter = rdfWriterFactory.getWriter(outputWriter);
	setupWriterConfig(rdfWriter.getWriterConfig());
	rdfWriter.startRDF();
	model.forEach(st -> rdfWriter.handleStatement(st));
	rdfWriter.endRDF();
	ByteArrayInputStream inputReader = new ByteArrayInputStream(outputWriter.toByteArray());
	RDFParser rdfParser = rdfParserFactory.getParser();
	setupParserConfig(rdfParser.getParserConfig().set(BasicParserSettings.VERIFY_URI_SYNTAX, false));
	Model parsedOutput = new LinkedHashModel();
	rdfParser.setRDFHandler(new StatementCollector(parsedOutput));
	rdfParser.parse(inputReader, "");
	Assert.assertEquals(model.size(), parsedOutput.size());
	ByteArrayInputStream inputReader2 = new ByteArrayInputStream(outputWriter.toByteArray());
	rdfParser.parse(inputReader2, "");
	Assert.assertEquals(model.size(), parsedOutput.size());
}
 
Example #8
Source File: RDFWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testWriteCommentBNodeContextBNodeWithNamespace() throws Exception {
	ByteArrayOutputStream outputWriter = new ByteArrayOutputStream();
	RDFWriter rdfWriter = rdfWriterFactory.getWriter(outputWriter);
	setupWriterConfig(rdfWriter.getWriterConfig());
	rdfWriter.startRDF();
	rdfWriter.handleNamespace("ex", exNs);
	rdfWriter.handleStatement(vf.createStatement(uri1, uri1, uri1, bnode));
	rdfWriter.handleComment("This comment should not screw up parsing");
	rdfWriter.handleStatement(vf.createStatement(uri1, uri1, uri2, bnode));
	rdfWriter.endRDF();
	ByteArrayInputStream inputReader = new ByteArrayInputStream(outputWriter.toByteArray());
	RDFParser rdfParser = rdfParserFactory.getParser();
	setupParserConfig(rdfParser.getParserConfig());
	Model parsedOutput = new LinkedHashModel();
	rdfParser.setRDFHandler(new StatementCollector(parsedOutput));
	rdfParser.parse(inputReader, "");
	assertEquals(2, parsedOutput.size());
	assertTrue(parsedOutput.contains(uri1, uri1, uri1));
	assertTrue(parsedOutput.contains(uri1, uri1, uri2));
	assertEquals(1, parsedOutput.contexts().size());
}
 
Example #9
Source File: CustomTurtleParserTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testLiteralWithNewlines() throws Exception {
	String namespace = "http://www.foo.com/bar#";
	String okLiteralString = "Literal \n without \n new line at the beginning. \n ";
	String errLiteralString = "\n Literal \n with \n new line at the beginning. \n ";

	IRI mySubject = vf.createIRI(namespace, "Subject");
	IRI myPredicate = vf.createIRI(namespace, "Predicate");
	Literal myOkObject = vf.createLiteral(okLiteralString);
	Literal myErrObject = vf.createLiteral(errLiteralString);

	StringWriter out = new StringWriter();
	Model model = new LinkedHashModel();
	model.add(mySubject, myPredicate, myOkObject);
	model.add(mySubject, myPredicate, myErrObject);
	Rio.write(model, out, RDFFormat.TURTLE);

	String str = out.toString();

	System.err.println(str);

	assertTrue("okLiteralString not found", str.contains(okLiteralString));
	assertTrue("errLiteralString not found", str.contains(errLiteralString));
}
 
Example #10
Source File: PropertyShape.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
String describe(SailRepositoryConnection connection, Resource resource) {
	GraphQuery graphQuery = connection.prepareGraphQuery("describe ?a where {BIND(?resource as ?a)}");
	graphQuery.setBinding("resource", resource);

	try (Stream<Statement> stream = graphQuery.evaluate().stream()) {

		LinkedHashModel statements = stream.collect(Collectors.toCollection(LinkedHashModel::new));
		statements.setNamespace(SHACL.PREFIX, SHACL.NAMESPACE);

		WriterConfig config = new WriterConfig();
		config.set(BasicWriterSettings.PRETTY_PRINT, true);
		config.set(BasicWriterSettings.INLINE_BLANK_NODES, true);

		StringWriter stringWriter = new StringWriter();

		Rio.write(statements, stringWriter, RDFFormat.TURTLE, config);

		return stringWriter.toString();
	}
}
 
Example #11
Source File: AbstractCommandTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/***
 * Add a new repository to the manager.
 *
 * @param configStream input stream of the repository configuration
 * @return ID of the repository as string
 * @throws IOException
 * @throws RDF4JException
 */
protected String addRepository(InputStream configStream) throws IOException, RDF4JException {
	RDFParser rdfParser = Rio.createParser(RDFFormat.TURTLE, SimpleValueFactory.getInstance());

	Model graph = new LinkedHashModel();
	rdfParser.setRDFHandler(new StatementCollector(graph));
	rdfParser.parse(
			new StringReader(IOUtil.readString(new InputStreamReader(configStream, StandardCharsets.UTF_8))),
			RepositoryConfigSchema.NAMESPACE);
	configStream.close();

	Resource repositoryNode = Models.subject(graph.filter(null, RDF.TYPE, RepositoryConfigSchema.REPOSITORY))
			.orElseThrow(() -> new RepositoryConfigException("could not find subject resource"));

	RepositoryConfig repoConfig = RepositoryConfig.create(graph, repositoryNode);
	repoConfig.validate();
	manager.addRepositoryConfig(repoConfig);

	String repId = Models.objectLiteral(graph.filter(repositoryNode, RepositoryConfigSchema.REPOSITORYID, null))
			.orElseThrow(() -> new RepositoryConfigException("missing repository id"))
			.stringValue();

	return repId;
}
 
Example #12
Source File: RDFWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testWriteCommentBNodeContext() throws Exception {
	ByteArrayOutputStream outputWriter = new ByteArrayOutputStream();
	RDFWriter rdfWriter = rdfWriterFactory.getWriter(outputWriter);
	setupWriterConfig(rdfWriter.getWriterConfig());
	rdfWriter.startRDF();
	rdfWriter.handleStatement(vf.createStatement(uri1, uri1, uri1, bnode));
	rdfWriter.handleComment("This comment should not screw up parsing");
	rdfWriter.endRDF();
	ByteArrayInputStream inputReader = new ByteArrayInputStream(outputWriter.toByteArray());
	RDFParser rdfParser = rdfParserFactory.getParser();
	setupParserConfig(rdfParser.getParserConfig());
	Model parsedOutput = new LinkedHashModel();
	rdfParser.setRDFHandler(new StatementCollector(parsedOutput));
	rdfParser.parse(inputReader, "");
	assertEquals(1, parsedOutput.size());
	assertTrue(parsedOutput.contains(uri1, uri1, uri1));
}
 
Example #13
Source File: RDFWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testWriteCommentURIContextURI() throws Exception {
	ByteArrayOutputStream outputWriter = new ByteArrayOutputStream();
	RDFWriter rdfWriter = rdfWriterFactory.getWriter(outputWriter);
	setupWriterConfig(rdfWriter.getWriterConfig());
	rdfWriter.startRDF();
	rdfWriter.handleStatement(vf.createStatement(uri1, uri1, uri1, uri1));
	rdfWriter.handleComment("This comment should not screw up parsing");
	rdfWriter.handleStatement(vf.createStatement(uri1, uri1, uri2, uri1));
	rdfWriter.endRDF();
	logger.debug(outputWriter.toString());
	ByteArrayInputStream inputReader = new ByteArrayInputStream(outputWriter.toByteArray());
	RDFParser rdfParser = rdfParserFactory.getParser();
	setupParserConfig(rdfParser.getParserConfig());
	Model parsedOutput = new LinkedHashModel();
	rdfParser.setRDFHandler(new StatementCollector(parsedOutput));
	rdfParser.parse(inputReader, "");
	assertEquals(2, parsedOutput.size());
	if (rdfWriterFactory.getRDFFormat().supportsContexts()) {
		assertTrue(parsedOutput.contains(uri1, uri1, uri1, uri1));
		assertTrue(parsedOutput.contains(uri1, uri1, uri2, uri1));
	} else {
		assertTrue(parsedOutput.contains(uri1, uri1, uri1));
		assertTrue(parsedOutput.contains(uri1, uri1, uri2));
	}
}
 
Example #14
Source File: AbstractNTriplesParserUnitTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testBlankNodeIdentifiersWithDotAsFirstCahracter() throws Exception {
	// The character . may appear anywhere except the first or last character.
	RDFParser ntriplesParser = new NTriplesParser();
	Model model = new LinkedHashModel();
	ntriplesParser.setRDFHandler(new StatementCollector(model));
	try {
		ntriplesParser.parse(new StringReader("_:123 <urn:test:predicate> _:.456 ."), NTRIPLES_TEST_URL);
		fail("Should have failed to parse invalid N-Triples bnode with '.' at the begining of the bnode label");
	} catch (Exception e) {
	}

	assertEquals(0, model.size());
	assertEquals(0, model.subjects().size());
	assertEquals(0, model.predicates().size());
	assertEquals(0, model.objects().size());
}
 
Example #15
Source File: StatementsTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testRDFStarReification() {
	Model rdfStarModel = RDFStarTestHelper.createRDFStarModel();

	Model reifiedModel = RDFStarTestHelper.createRDFReificationModel();

	Model convertedModel1 = new LinkedHashModel();
	rdfStarModel.forEach((s) -> Statements.convertRDFStarToReification(s, convertedModel1::add));
	assertTrue("RDF* conversion to reification with implicit VF",
			Models.isomorphic(reifiedModel, convertedModel1));

	Model convertedModel2 = new LinkedHashModel();
	rdfStarModel.forEach((s) -> Statements.convertRDFStarToReification(vf, s, convertedModel2::add));
	assertTrue("RDF* conversion to reification with explicit VF",
			Models.isomorphic(reifiedModel, convertedModel2));

	Model convertedModel3 = new LinkedHashModel();
	rdfStarModel.forEach((s) -> Statements.convertRDFStarToReification(vf, (t) -> vf.createBNode(t.stringValue()),
			s, convertedModel3::add));
	assertTrue("RDF* conversion to reification with explicit VF and custom BNode mapping",
			Models.isomorphic(reifiedModel, convertedModel3));
}
 
Example #16
Source File: RDFWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testWriteCommentBNodeContextWithNamespaceBeforeNamespace() throws Exception {
	ByteArrayOutputStream outputWriter = new ByteArrayOutputStream();
	RDFWriter rdfWriter = rdfWriterFactory.getWriter(outputWriter);
	setupWriterConfig(rdfWriter.getWriterConfig());
	rdfWriter.startRDF();
	rdfWriter.handleNamespace("ex", exNs);
	rdfWriter.handleStatement(vf.createStatement(uri1, uri1, uri1, bnode));
	rdfWriter.handleComment("This comment should not screw up parsing");
	rdfWriter.handleNamespace("ex1", exNs);
	rdfWriter.endRDF();
	ByteArrayInputStream inputReader = new ByteArrayInputStream(outputWriter.toByteArray());
	RDFParser rdfParser = rdfParserFactory.getParser();
	setupParserConfig(rdfParser.getParserConfig());
	Model parsedOutput = new LinkedHashModel();
	rdfParser.setRDFHandler(new StatementCollector(parsedOutput));
	rdfParser.parse(inputReader, "");
	assertEquals(1, parsedOutput.size());
	assertTrue(parsedOutput.contains(uri1, uri1, uri1));
}
 
Example #17
Source File: StatementCollectorTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Test method for {@link org.eclipse.rdf4j.rio.helpers.StatementCollector#StatementCollector(java.util.Collection)}
 * .
 */
@Test
public final void testStatementCollectorCollectionModel() throws Exception {
	Model testList = new LinkedHashModel();
	StatementCollector collector = new StatementCollector(testList);

	// Actual variable is exactly the same, although it could be theoretically
	// wrapped and still be consistent
	assertTrue(testList == collector.getStatements());
	assertNotNull(collector.getNamespaces());

	assertTrue(testList.getNamespaces().isEmpty());
	assertTrue(collector.getNamespaces().isEmpty());

	collector.handleNamespace("ns1", "http://example.org/ns1#");

	assertFalse(testList.getNamespaces().isEmpty());
	assertFalse(collector.getNamespaces().isEmpty());
	assertTrue(collector.getNamespaces().containsKey("ns1"));
	assertTrue(collector.getNamespaces().containsValue("http://example.org/ns1#"));
	assertTrue(testList.getNamespaces().iterator().next().getPrefix().equals("ns1"));
	assertTrue(testList.getNamespaces().iterator().next().getName().equals("http://example.org/ns1#"));
}
 
Example #18
Source File: RDFWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testWriteTwoStatementsSubjectBNodeSinglePredicateSingleContextBNodeWithNamespace() throws Exception {
	Model input = new LinkedHashModel();
	input.setNamespace("ex", exNs);
	input.add(vf.createStatement(bnodeSingleUseSubject, uri1, uri1, bnode));
	input.add(vf.createStatement(bnodeSingleUseSubject, uri1, uri2, bnode));
	ByteArrayOutputStream outputWriter = new ByteArrayOutputStream();
	write(input, outputWriter);
	ByteArrayInputStream inputReader = new ByteArrayInputStream(outputWriter.toByteArray());
	Model parsedOutput = parse(inputReader, "");
	assertEquals(2, parsedOutput.size());
	assertEquals(1, parsedOutput.filter(null, uri1, uri1).size());
	assertEquals(1, parsedOutput.filter(null, uri1, uri2).size());
	assertEquals(1, parsedOutput.contexts().size());
	if (rdfWriterFactory.getRDFFormat().supportsContexts()) {
		assertTrue(parsedOutput.contexts().iterator().next() instanceof BNode);
	}
	assertEquals(1, parsedOutput.subjects().size());
	assertTrue(parsedOutput.subjects().iterator().next() instanceof BNode);
}
 
Example #19
Source File: RDFWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testWriteTwoStatementsSubjectBNodeSinglePredicateSingleContextIRIWithNamespace() throws Exception {
	Model input = new LinkedHashModel();
	input.setNamespace("ex", exNs);
	input.add(vf.createStatement(bnodeSingleUseSubject, uri1, uri1, uri1));
	input.add(vf.createStatement(bnodeSingleUseSubject, uri1, uri2, uri1));
	ByteArrayOutputStream outputWriter = new ByteArrayOutputStream();
	write(input, outputWriter);
	ByteArrayInputStream inputReader = new ByteArrayInputStream(outputWriter.toByteArray());
	Model parsedOutput = parse(inputReader, "");
	assertEquals(2, parsedOutput.size());
	if (rdfWriterFactory.getRDFFormat().supportsContexts()) {
		assertEquals(1, parsedOutput.filter(null, uri1, uri1, uri1).size());
		assertEquals(1, parsedOutput.filter(null, uri1, uri2, uri1).size());
	} else {
		assertEquals(1, parsedOutput.filter(null, uri1, uri1).size());
		assertEquals(1, parsedOutput.filter(null, uri1, uri2).size());
	}
	assertEquals(1, parsedOutput.subjects().size());
	assertTrue(parsedOutput.subjects().iterator().next() instanceof BNode);
}
 
Example #20
Source File: RDFXMLParserCustomTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Test with the default ParserConfig settings. Ie, setParserConfig is not called.
 *
 * @throws Exception
 */
@Test
public void testEntityExpansionDefaultSettings() throws Exception {
	final Model aGraph = new LinkedHashModel();
	ParseErrorCollector errorCollector = new ParseErrorCollector();
	RDFParser aParser = Rio.createParser(RDFFormat.RDFXML)
			.setRDFHandler(new StatementCollector(aGraph))
			.setParseErrorListener(errorCollector);

	try {
		// this should trigger a SAX parse exception that will blow up at the
		// 64k entity limit rather than OOMing
		aParser.parse(
				this.getClass().getResourceAsStream("/testcases/rdfxml/openrdf/bad-entity-expansion-limit.rdf"),
				"http://example.org");
		fail("Parser did not throw an exception");
	} catch (RDFParseException e) {
		// assertTrue(e.getMessage().contains(
		// "The parser has encountered more than \"64,000\" entity expansions in this document; this is the limit
		// imposed by the "));
	}
	assertEquals(0, errorCollector.getWarnings().size());
	assertEquals(0, errorCollector.getErrors().size());
	assertEquals(1, errorCollector.getFatalErrors().size());
}
 
Example #21
Source File: ArrangedWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testWriteSingleStatementWithSubjectNamespace() {
	String otherNs = "http://example.net/";
	IRI uri0 = vf.createIRI(otherNs, "uri0");

	Model input = new LinkedHashModel();
	input.add(vf.createStatement(uri0, uri1, uri2));

	input.setNamespace("org", exNs);
	input.setNamespace("net", otherNs);

	ByteArrayOutputStream outputWriter = new ByteArrayOutputStream();
	write(input, outputWriter);

	String sep = System.lineSeparator();
	String expectedResult = "@prefix net: <http://example.net/> ." + sep +
			"@prefix org: <http://example.org/> ." + sep + sep +
			"net:uri0 org:uri1 org:uri2 ." + sep;

	assertEquals(expectedResult, outputWriter.toString());
}
 
Example #22
Source File: AbstractParserHandlingTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Tests whether an unknown language with the message and with a failure.
 */
@Test
public final void testUnknownLanguageWithMessageWithFailCase1() throws Exception {
	Model expectedModel = getTestModel(UNKNOWN_LANGUAGE_VALUE, UNKNOWN_LANGUAGE_TAG);
	InputStream input = getUnknownLanguageStream(expectedModel);

	testParser.getParserConfig().set(BasicParserSettings.FAIL_ON_UNKNOWN_LANGUAGES, true);

	try {
		testParser.parse(input, BASE_URI);
		fail("Did not receive expected exception");
	} catch (RDFParseException e) {
		// expected
	}

	assertErrorListener(0, 1, 0);
	assertModel(new LinkedHashModel());
}
 
Example #23
Source File: GraphReadingUtility.java    From mobi with GNU Affero General Public License v3.0 6 votes vote down vote up
public static Model readOntologies(final Collection<Pair<File, String>> pairs) throws IOException {
    final Model overall = new LinkedHashModel();
    final List<String> issues = new ArrayList<>();
    pairs.parallelStream().forEach(pair -> {
        try {
            readOntology(pair.getLeft(), pair.getRight());
        } catch (Exception e) {
            LOG.error("Issue reading ontology '" + pair.getLeft() + "'" + e.getMessage(), e);
            issues.add("Issue reading ontology '" + pair.getLeft() + "'" + e.getMessage()
                    + "\n\tEnsure the file format matches type file suffix.");
        }
    });
    if (!pairs.isEmpty()) {
        throw new IOException(StringUtils.join(issues, "\n"));
    }
    return overall;
}
 
Example #24
Source File: AbstractNTriplesParserUnitTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testUriWithEscapeCharactersShouldFailToParse() throws Exception {
	RDFParser ntriplesParser = createRDFParser();
	Model model = new LinkedHashModel();
	ntriplesParser.setRDFHandler(new StatementCollector(model));

	String nt = "<http://example/\\n> <http://example/p> <http://example/o> .";

	try {
		ntriplesParser.parse(new StringReader(nt), NTRIPLES_TEST_URL);
		fail("Should have failed to parse invalid N-Triples uri with space");
	} catch (RDFParseException ignored) {
	}

	assertEquals(0, model.size());
	assertEquals(0, model.subjects().size());
	assertEquals(0, model.predicates().size());
	assertEquals(0, model.objects().size());
}
 
Example #25
Source File: RDFWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testWriteCommentBNodeContextBNodeWithNamespaceBeforeNamespace() throws Exception {
	ByteArrayOutputStream outputWriter = new ByteArrayOutputStream();
	RDFWriter rdfWriter = rdfWriterFactory.getWriter(outputWriter);
	setupWriterConfig(rdfWriter.getWriterConfig());
	rdfWriter.startRDF();
	rdfWriter.handleNamespace("ex", exNs);
	rdfWriter.handleStatement(vf.createStatement(uri1, uri1, uri1, bnode));
	rdfWriter.handleComment("This comment should not screw up parsing");
	rdfWriter.handleNamespace("ex1", exNs);
	rdfWriter.handleStatement(vf.createStatement(uri1, uri1, uri2, bnode));
	rdfWriter.endRDF();
	ByteArrayInputStream inputReader = new ByteArrayInputStream(outputWriter.toByteArray());
	RDFParser rdfParser = rdfParserFactory.getParser();
	setupParserConfig(rdfParser.getParserConfig());
	Model parsedOutput = new LinkedHashModel();
	rdfParser.setRDFHandler(new StatementCollector(parsedOutput));
	rdfParser.parse(inputReader, "");
	assertEquals(2, parsedOutput.size());
	assertTrue(parsedOutput.contains(uri1, uri1, uri1));
	assertTrue(parsedOutput.contains(uri1, uri1, uri2));
	assertEquals(1, parsedOutput.contexts().size());
}
 
Example #26
Source File: RDFWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void testSES2030BNodeCollisionsInternal(boolean preserveBNodeIDs) throws Exception {
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		RDFWriter rdfWriter = rdfWriterFactory.getWriter(output);
		setupWriterConfig(rdfWriter.getWriterConfig());
		rdfWriter.startRDF();
		int count = 18;
		for (int i = 0; i < count; i++) {
			BNode bNode2 = vf.createBNode("a" + Integer.toHexString(i).toUpperCase());
			// System.out.println(bNode2.getID());
			rdfWriter.handleStatement(vf.createStatement(uri1, uri2, bNode2));
		}
		rdfWriter.endRDF();
		RDFParser rdfParser = rdfParserFactory.getParser();
		setupParserConfig(rdfParser.getParserConfig());
		if (preserveBNodeIDs) {
			rdfParser.getParserConfig().set(BasicParserSettings.PRESERVE_BNODE_IDS, true);
		}
		Model parsedModel = new LinkedHashModel();
		rdfParser.setRDFHandler(new StatementCollector(parsedModel));
		rdfParser.parse(new ByteArrayInputStream(output.toByteArray()), "");
//		if (count != parsedModel.size()) {
//			Rio.write(parsedModel, System.out, RDFFormat.NQUADS);
//		}
		assertEquals(count, parsedModel.size());
	}
 
Example #27
Source File: AbstractNTriplesParserUnitTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testUriWithSpaceShouldFailToParse() throws Exception {
	RDFParser ntriplesParser = createRDFParser();
	Model model = new LinkedHashModel();
	ntriplesParser.setRDFHandler(new StatementCollector(model));

	String nt = "<http://example/ space> <http://example/p> <http://example/o> .";

	try {
		ntriplesParser.parse(new StringReader(nt), NTRIPLES_TEST_URL);
		fail("Should have failed to parse invalid N-Triples uri with space");
	} catch (RDFParseException ignored) {
	}

	assertEquals(0, model.size());
	assertEquals(0, model.subjects().size());
	assertEquals(0, model.predicates().size());
	assertEquals(0, model.objects().size());
}
 
Example #28
Source File: RDFWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testWriteTwoStatementsSubjectBNodeSinglePredicateSingleContextBNode() throws Exception {
	Model input = new LinkedHashModel();
	input.add(vf.createStatement(bnodeSingleUseSubject, uri1, uri1, bnode));
	input.add(vf.createStatement(bnodeSingleUseSubject, uri1, uri2, bnode));
	ByteArrayOutputStream outputWriter = new ByteArrayOutputStream();
	write(input, outputWriter);
	ByteArrayInputStream inputReader = new ByteArrayInputStream(outputWriter.toByteArray());
	Model parsedOutput = parse(inputReader, "");
	assertEquals(2, parsedOutput.size());
	assertEquals(1, parsedOutput.filter(null, uri1, uri1).size());
	assertEquals(1, parsedOutput.filter(null, uri1, uri2).size());
	assertEquals(1, parsedOutput.contexts().size());
	if (rdfWriterFactory.getRDFFormat().supportsContexts()) {
		assertTrue(parsedOutput.contexts().iterator().next() instanceof BNode);
	}
	assertEquals(1, parsedOutput.subjects().size());
	assertTrue(parsedOutput.subjects().iterator().next() instanceof BNode);
}
 
Example #29
Source File: RDFWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testSuccessBNodeParsesAreDistinct() throws Exception {
	ByteArrayOutputStream outputWriter = new ByteArrayOutputStream();
	RDFWriter rdfWriter = rdfWriterFactory.getWriter(outputWriter);
	setupWriterConfig(rdfWriter.getWriterConfig());
	rdfWriter.startRDF();
	rdfWriter.handleStatement(vf.createStatement(uri1, uri1, bnode));
	rdfWriter.endRDF();
	ByteArrayInputStream inputReader = new ByteArrayInputStream(outputWriter.toByteArray());
	RDFParser rdfParser = rdfParserFactory.getParser();
	setupParserConfig(rdfParser.getParserConfig());
	Model parsedOutput = new LinkedHashModel();
	rdfParser.setRDFHandler(new StatementCollector(parsedOutput));
	rdfParser.parse(inputReader, "");
	assertEquals(1, parsedOutput.size());
	ByteArrayInputStream inputReader2 = new ByteArrayInputStream(outputWriter.toByteArray());
	rdfParser.parse(inputReader2, "");
	assertEquals(2, parsedOutput.size());
}
 
Example #30
Source File: RDFWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testWriteSingleStatementSubjectBNodeSingleContextIRI() throws Exception {
	Model input = new LinkedHashModel();
	input.add(vf.createStatement(bnodeSingleUseSubject, uri1, uri1, uri1));
	ByteArrayOutputStream outputWriter = new ByteArrayOutputStream();
	write(input, outputWriter);
	ByteArrayInputStream inputReader = new ByteArrayInputStream(outputWriter.toByteArray());
	Model parsedOutput = parse(inputReader, "");
	assertEquals(1, parsedOutput.size());
	if (rdfWriterFactory.getRDFFormat().supportsContexts()) {
		assertEquals(1, parsedOutput.filter(null, uri1, uri1, uri1).size());
	} else {
		assertEquals(1, parsedOutput.filter(null, uri1, uri1).size());
	}
	assertEquals(1, parsedOutput.subjects().size());
	assertTrue(parsedOutput.subjects().iterator().next() instanceof BNode);
}