Java Code Examples for org.apache.jena.riot.system.StreamRDF#start()

The following examples show how to use org.apache.jena.riot.system.StreamRDF#start() . 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: StreamingRDFWriter.java    From tarql with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void writeTurtle(String baseIRI, PrefixMapping prefixes, boolean writeBase) {
	// Auto-register RDF prefix so that rdf:type is displayed well
	// All other prefixes come from the query and should be as author intended
	prefixes = ensureRDFPrefix(prefixes);

	if (writeBase) {
		// Jena's streaming Turtle writers don't output base even if it is provided,
		// so we write it directly.
		IndentedWriter w = new IndentedWriter(out);
		RiotLib.writeBase(w, baseIRI);
		w.flush();
	}
	
	StreamRDF writer = new WriterStreamRDFBlocks(out);
	if (dedupWindowSize > 0) {
		writer = new StreamRDFDedup(writer, dedupWindowSize);
	}
	writer.start();
	writer.base(baseIRI);
	for (Entry<String, String> e : prefixes.getNsPrefixMap().entrySet()) {
		writer.prefix(e.getKey(), e.getValue());
	}
	StreamOps.sendTriplesToStream(triples, writer);
	writer.finish();
}
 
Example 2
Source File: rdf2patch.java    From rdf-delta with Apache License 2.0 5 votes vote down vote up
@Override
protected void exec() {
    StreamRDF dest  = RDFPatchOps.write(System.out);
    dest.start();
    if ( getPositional().isEmpty() )
        execOne(System.in);
    getPositional().forEach(fn->RDFDataMgr.parse(dest, fn));
    dest.finish();
}
 
Example 3
Source File: StreamingRDFWriter.java    From tarql with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void writeNTriples() {
	StreamRDF writer = new WriterStreamRDFPlain(new IndentedWriter(out));
	if (dedupWindowSize > 0) {
		writer = new StreamRDFDedup(writer, dedupWindowSize);
	}
	writer.start();
	StreamOps.sendTriplesToStream(triples, writer);
	writer.finish();
}
 
Example 4
Source File: StreamRDFDedupTest.java    From tarql with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Test public void shouldPassThroughTriples() {
	StreamRDF dedup = new StreamRDFDedup(new MockStreamRDF());
	dedup.start();
	dedup.triple(triple("<a> <a> <a>"));
	dedup.triple(triple("<b> <b> <b>"));
	dedup.triple(triple("<c> <c> <c>"));
	dedup.finish();
	assertEquals(triples("<a> <a> <a>", "<b> <b> <b>", "<c> <c> <c>"), received);
}
 
Example 5
Source File: StreamRDFDedupTest.java    From tarql with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Test public void shouldRemoveDuplicateInWindow() {
	StreamRDF dedup = new StreamRDFDedup(new MockStreamRDF());
	dedup.start();
	dedup.triple(triple("<a> <a> <a>"));
	dedup.triple(triple("<a> <a> <a>"));
	dedup.finish();
	assertEquals(triples("<a> <a> <a>"), received);
}
 
Example 6
Source File: StreamRDFDedupTest.java    From tarql with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Test public void shouldNotRemoveDuplicateOutsideWindow() {
	StreamRDF dedup = new StreamRDFDedup(new MockStreamRDF(), 2);
	dedup.start();
	dedup.triple(triple("<a> <a> <a>"));
	dedup.triple(triple("<b> <b> <b>"));
	dedup.triple(triple("<a> <a> <a>"));
	dedup.triple(triple("<c> <c> <c>"));
	dedup.triple(triple("<a> <a> <a>"));
	dedup.finish();
	assertEquals(triples("<a> <a> <a>", "<b> <b> <b>", "<c> <c> <c>", "<a> <a> <a>"), received);
}
 
Example 7
Source File: JenaIOService.java    From trellis with Apache License 2.0 4 votes vote down vote up
@Override
public void write(final Stream<Triple> triples, final OutputStream output, final RDFSyntax syntax,
        final String baseUrl, final IRI... profiles) {
    requireNonNull(triples, "The triples stream may not be null!");
    requireNonNull(output, "The output stream may not be null!");
    requireNonNull(syntax, "The RDF syntax value may not be null!");

    try {
        if (RDFA.equals(syntax)) {
            writeHTML(triples, output, baseUrl);
        } else {
            final Lang lang = JenaCommonsRDF.toJena(syntax).orElseThrow(() ->
                    new TrellisRuntimeException("Invalid content type: " + syntax.mediaType()));

            final RDFFormat format = defaultSerialization(lang);

            if (format != null) {
                LOGGER.debug("Writing stream-based RDF: {}", format);
                final StreamRDF stream = getWriterStream(output, format, null);
                stream.start();
                nsService.getNamespaces().forEach(stream::prefix);
                if (shouldUseRelativeIRIs(relativeIRIs, profiles)) {
                    stream.base(baseUrl);
                }
                triples.map(JenaCommonsRDF::toJena).forEachOrdered(stream::triple);
                stream.finish();
            } else {
                LOGGER.debug("Writing buffered RDF: {}", lang);
                final org.apache.jena.graph.Graph graph = createDefaultGraph();
                graph.getPrefixMapping().setNsPrefixes(nsService.getNamespaces());
                triples.map(JenaCommonsRDF::toJena).forEachOrdered(graph::add);
                if (JSONLD.equals(lang)) {
                    writeJsonLd(output, DatasetGraphFactory.create(graph), profiles);
                } else {
                    RDFDataMgr.write(output, graph, lang);
                }
            }
        }
    } catch (final AtlasException ex) {
        throw new TrellisRuntimeException(ex);
    }
}