Java Code Examples for org.eclipse.rdf4j.rio.helpers.StatementCollector#getStatements()
The following examples show how to use
org.eclipse.rdf4j.rio.helpers.StatementCollector#getStatements() .
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: SpinRendererTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
private static String toRDF(StatementCollector stmts) throws RDFHandlerException { WriterConfig config = new WriterConfig(); config.set(BasicWriterSettings.PRETTY_PRINT, false); StringBuilderWriter writer = new StringBuilderWriter(); final RDFWriter rdfWriter = Rio.createWriter(RDFFormat.TURTLE, writer); rdfWriter.setWriterConfig(config); rdfWriter.startRDF(); for (Map.Entry<String, String> entry : stmts.getNamespaces().entrySet()) { rdfWriter.handleNamespace(entry.getKey(), entry.getValue()); } for (final Statement st : stmts.getStatements()) { rdfWriter.handleStatement(st); } rdfWriter.endRDF(); writer.close(); return writer.toString(); }
Example 2
Source File: AbstractNQuadsWriterTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Test public void testReadWrite() throws RDFHandlerException, IOException, RDFParseException { StatementCollector statementCollector = new StatementCollector(); parser.setRDFHandler(statementCollector); parser.parse(this.getClass().getResourceAsStream("/testcases/nquads/test2.nq"), "http://test.base.uri"); Assert.assertEquals(400, statementCollector.getStatements().size()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); writer = rdfWriterFactory.getWriter(baos); writer.startRDF(); for (Statement nextStatement : statementCollector.getStatements()) { writer.handleStatement(nextStatement); } writer.endRDF(); Assert.assertEquals("Unexpected number of lines.", 400, baos.toString().split("\n").length); }
Example 3
Source File: SpinParserTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Test public void testSpinParser() throws IOException, RDF4JException { StatementCollector expected = new StatementCollector(); RDFParser parser = Rio.createParser(RDFFormat.TURTLE); parser.setRDFHandler(expected); try (InputStream rdfStream = testURL.openStream()) { parser.parse(rdfStream, testURL.toString()); } // get query resource from sp:text Resource queryResource = null; for (Statement stmt : expected.getStatements()) { if (SP.TEXT_PROPERTY.equals(stmt.getPredicate())) { queryResource = stmt.getSubject(); break; } } assertNotNull(queryResource); TripleSource store = new ModelTripleSource(new TreeModel(expected.getStatements())); ParsedOperation textParsedOp = textParser.parse(queryResource, store); ParsedOperation rdfParsedOp = rdfParser.parse(queryResource, store); if (textParsedOp instanceof ParsedQuery) { assertEquals(((ParsedQuery) textParsedOp).getTupleExpr(), ((ParsedQuery) rdfParsedOp).getTupleExpr()); } else { assertEquals(((ParsedUpdate) textParsedOp).getUpdateExprs(), ((ParsedUpdate) rdfParsedOp).getUpdateExprs()); } }
Example 4
Source File: SpinRendererTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Test public void testSpinRenderer() throws IOException, RDF4JException { StatementCollector expected = new StatementCollector(); RDFParser parser = Rio.createParser(RDFFormat.TURTLE); parser.setRDFHandler(expected); try (InputStream rdfStream = testURL.openStream()) { parser.parse(rdfStream, testURL.toString()); } // get query from sp:text String query = null; for (Statement stmt : expected.getStatements()) { if (SP.TEXT_PROPERTY.equals(stmt.getPredicate())) { query = stmt.getObject().stringValue(); break; } } assertNotNull(query); ParsedOperation parsedOp = QueryParserUtil.parseOperation(QueryLanguage.SPARQL, query, testURL.toString()); StatementCollector actual = new StatementCollector(); renderer.render(parsedOp, actual); Object operation = (parsedOp instanceof ParsedQuery) ? ((ParsedQuery) parsedOp).getTupleExpr() : ((ParsedUpdate) parsedOp).getUpdateExprs(); assertTrue("Operation was\n" + operation + "\nExpected\n" + toRDF(expected) + "\nbut was\n" + toRDF(actual), Models.isomorphic(actual.getStatements(), expected.getStatements())); }
Example 5
Source File: SpinSailWithoutRDFSInferencerTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
private void assertStatements(String ttl) throws RDFParseException, RDFHandlerException, IOException, RepositoryException { StatementCollector expected = new StatementCollector(); RDFParser parser = Rio.createParser(RDFFormat.TURTLE); parser.setRDFHandler(expected); URL url = getClass().getResource(BASE_DIR + ttl); try (InputStream rdfStream = url.openStream()) { parser.parse(rdfStream, url.toString()); } for (Statement stmt : expected.getStatements()) { assertTrue("Expected statement: " + stmt, conn.hasStatement(stmt, true)); } }
Example 6
Source File: SpinSailTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
private void assertStatements(String ttl) throws RDFParseException, RDFHandlerException, IOException, RepositoryException { StatementCollector expected = new StatementCollector(); RDFParser parser = Rio.createParser(RDFFormat.TURTLE); parser.setRDFHandler(expected); URL url = getClass().getResource(BASE_DIR + ttl); try (InputStream rdfStream = url.openStream()) { parser.parse(rdfStream, url.toString()); } for (Statement stmt : expected.getStatements()) { assertTrue("Expected statement: " + stmt, conn.hasStatement(stmt, true)); } }
Example 7
Source File: RDFWriterTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Test public void testPrefixRedefinition() throws RDFHandlerException, RDFParseException, IOException { String ns1 = "a:"; String ns2 = "b:"; String ns3 = "c:"; IRI uri1 = vf.createIRI(ns1, "r1"); IRI uri2 = vf.createIRI(ns2, "r2"); IRI uri3 = vf.createIRI(ns3, "r3"); Statement st = vf.createStatement(uri1, uri2, uri3); ByteArrayOutputStream out = new ByteArrayOutputStream(); RDFWriter rdfWriter = rdfWriterFactory.getWriter(out); setupWriterConfig(rdfWriter.getWriterConfig()); rdfWriter.startRDF(); rdfWriter.handleNamespace("", ns1); rdfWriter.handleNamespace("", ns2); rdfWriter.handleNamespace("", ns3); rdfWriter.handleStatement(st); rdfWriter.endRDF(); ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); RDFParser rdfParser = rdfParserFactory.getParser(); setupParserConfig(rdfParser.getParserConfig()); rdfParser.setValueFactory(vf); StatementCollector stCollector = new StatementCollector(); rdfParser.setRDFHandler(stCollector); rdfParser.parse(in, "foo:bar"); Collection<Statement> statements = stCollector.getStatements(); assertEquals("Unexpected number of statements", 1, statements.size()); Statement parsedSt = statements.iterator().next(); assertEquals("Written and parsed statements are not equal", st, parsedSt); }
Example 8
Source File: RDFWriterTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Test public void testIllegalPrefix() throws RDFHandlerException, RDFParseException, IOException { String ns1 = "a:"; String ns2 = "b:"; String ns3 = "c:"; IRI uri1 = vf.createIRI(ns1, "r1"); IRI uri2 = vf.createIRI(ns2, "r2"); IRI uri3 = vf.createIRI(ns3, "r3"); Statement st = vf.createStatement(uri1, uri2, uri3); ByteArrayOutputStream out = new ByteArrayOutputStream(); RDFWriter rdfWriter = rdfWriterFactory.getWriter(out); setupWriterConfig(rdfWriter.getWriterConfig()); rdfWriter.startRDF(); rdfWriter.handleNamespace("1", ns1); rdfWriter.handleNamespace("_", ns2); rdfWriter.handleNamespace("a%", ns3); rdfWriter.handleStatement(st); rdfWriter.endRDF(); ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); RDFParser rdfParser = rdfParserFactory.getParser(); setupParserConfig(rdfParser.getParserConfig()); rdfParser.setValueFactory(vf); StatementCollector stCollector = new StatementCollector(); rdfParser.setRDFHandler(stCollector); rdfParser.parse(in, "foo:bar"); Collection<Statement> statements = stCollector.getStatements(); assertEquals("Unexpected number of statements", 1, statements.size()); Statement parsedSt = statements.iterator().next(); assertEquals("Written and parsed statements are not equal", st, parsedSt); }
Example 9
Source File: GraphReadingUtility.java From mobi with GNU Affero General Public License v3.0 | 5 votes |
public static Model readOntology(final RDFFormat format, final InputStream is, final String baseURI) throws RDFParseException, RDFHandlerException, UnsupportedRDFormatException, IOException { final StatementCollector collector = new StatementCollector(); final RDFParser parser = Rio.createParser(format); parser.setRDFHandler(collector); parser.parse(is, baseURI); return new LinkedHashModel(collector.getStatements()); }
Example 10
Source File: TurtleParserTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 2 votes |
@Test public void rdfXmlLoadedFromInsideAJarResolvesRelativeUris() throws IOException { URL zipfileUrl = TurtleParserTest.class.getResource("sample-with-turtle-data.zip"); assertNotNull("The sample-with-turtle-data.zip file must be present for this test", zipfileUrl); String url = "jar:" + zipfileUrl + "!/index.ttl"; RDFParser parser = new TurtleParser(); StatementCollector sc = new StatementCollector(); parser.setRDFHandler(sc); try (InputStream in = new URL(url).openStream()) { parser.parse(in, url); } Collection<Statement> stmts = sc.getStatements(); assertThat(stmts).hasSize(2); Iterator<Statement> iter = stmts.iterator(); Statement stmt1 = iter.next(), stmt2 = iter.next(); assertEquals(vf.createIRI("http://www.example.com/#"), stmt1.getSubject()); assertEquals(vf.createIRI("http://www.example.com/ns/#document-about"), stmt1.getPredicate()); Resource res = (Resource) stmt1.getObject(); String resourceUrl = res.stringValue(); assertThat(resourceUrl).startsWith("jar:" + zipfileUrl + "!"); URL javaUrl = new URL(resourceUrl); assertEquals("jar", javaUrl.getProtocol()); try (InputStream uc = javaUrl.openStream()) { assertEquals("The resource stream should be empty", -1, uc.read()); } assertEquals(res, stmt2.getSubject()); assertEquals(DC.TITLE, stmt2.getPredicate()); assertEquals(vf.createLiteral("Empty File"), stmt2.getObject()); }