Java Code Examples for org.eclipse.rdf4j.rio.Rio#createWriter()

The following examples show how to use org.eclipse.rdf4j.rio.Rio#createWriter() . 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: InferencingTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private File dumpStatements(String name, Collection<? extends Statement> statements) throws Exception {
	// Dump results to tmp file for debugging
	String tmpDir = System.getProperty("java.io.tmpdir");
	name = name.replace("/", "_");
	File tmpFile = new File(tmpDir, "junit-" + name + ".nt");
	tmpFile.createNewFile();

	try (OutputStream export = new BufferedOutputStream(new FileOutputStream(tmpFile))) {
		RDFWriter writer = Rio.createWriter(RDFFormat.NTRIPLES, export);

		writer.startRDF();
		for (Statement st : statements) {
			writer.handleStatement(st);
		}
		writer.endRDF();
	}

	return tmpFile;
}
 
Example 2
Source File: SpinRendererTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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 3
Source File: SimpleRecordServiceTest.java    From mobi with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void exportUsingBatchExporterTest() throws Exception {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    BatchExporter exporter =  new BatchExporter(transformer, new BufferedGroupingRDFHandler(Rio.createWriter(RDFFormat.JSONLD, os)));
    RecordOperationConfig config = new OperationConfig();

    config.set(RecordExportSettings.BATCH_EXPORTER, exporter);

    assertFalse(exporter.isActive());
    exporter.startRDF();
    assertTrue(exporter.isActive());
    recordService.export(testIRI, config, connection);
    exporter.endRDF();
    assertFalse(exporter.isActive());

    Model outputModel = Values.mobiModel(Rio.parse((IOUtils.toInputStream(os.toString())), "", RDFFormat.JSONLD));
    assertEquals(testRecord.getModel(), outputModel);

    verify(utilsService).optObject(eq(testIRI), any(OrmFactory.class), eq(connection));
}
 
Example 4
Source File: RestUtils.java    From mobi with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Converts a {@link Model} into a string containing grouped RDF in the specified RDFFormat.
 *
 * @param model       A {@link Model} of RDF to convert.
 * @param format      The RDFFormat the RDF should be serialized into.
 * @param transformer The SesameTransformer for model conversions.
 * @return A String of the serialized grouped RDF from the Model.
 */
public static String groupedModelToString(Model model, RDFFormat format, SesameTransformer transformer) {
    long start = System.currentTimeMillis();
    try {
        StringWriter sw = new StringWriter();
        RDFHandler rdfWriter = new BufferedGroupingRDFHandler(Rio.createWriter(format, sw));
        Rio.write(new StatementIterable(model, transformer), rdfWriter);
        return sw.toString();
    } finally {
        LOG.trace("groupedModelToString took {}ms", System.currentTimeMillis() - start);
    }
}
 
Example 5
Source File: KnowledgeBaseServiceImpl.java    From inception with Apache License 2.0 5 votes vote down vote up
@Override
public void exportData(KnowledgeBase kb, RDFFormat format, OutputStream os)
{
    if (kb.getType() != RepositoryType.LOCAL) {
        log.info("Not exporting non-local knowledge base: [{}]", kb.getName());
        return;
    }
    try (RepositoryConnection conn = getConnection(kb)) {
        RDFWriter rdfWriter = Rio.createWriter(format, os);
        conn.export(rdfWriter);
    }
}
 
Example 6
Source File: RepositoryModel.java    From semweb4j with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void writeTo(OutputStream stream, Syntax syntax) throws
		// interface allows it
		IOException, ModelRuntimeException {
	RDFWriter rdfWriter = Rio.createWriter(getRDFFormat(syntax), stream);
	writeTo(rdfWriter);
}
 
Example 7
Source File: RecordExportServiceImpl.java    From mobi with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void export(RecordExportConfig config) throws IOException {
    BatchExporter writer = new BatchExporter(transformer, new BufferedGroupingRDFHandler(
            Rio.createWriter(config.getFormat(), config.getOutput())));
    writer.setLogger(LOG);
    writer.setPrintToSystem(true);

    com.mobi.rdf.api.Resource localCatalog = configProvider.getLocalCatalogIRI();

    Set<com.mobi.rdf.api.Resource> records;
    if (config.getRecords() == null) {
        records = catalogManager.getRecordIds(localCatalog);
    } else {
        records = config.getRecords().stream()
                .map(recordString -> vf.createIRI(recordString))
                .collect(Collectors.toSet());
    }

    writer.startRDF();
    records.forEach(resource -> {
        // Write Record
        Record record = catalogManager.getRecord(localCatalog, resource, recordFactory)
                .orElseThrow(() -> new IllegalStateException("Could not retrieve record " + resource));
        record.getModel().forEach(writer::handleStatement);

        // Write Versioned Data
        IRI typeIRI = vf.createIRI(com.mobi.ontologies.rdfs.Resource.type_IRI);
        IRI versionedRDFRecordType = vf.createIRI(VersionedRDFRecord.TYPE);
        if (record.getProperties(typeIRI).contains(versionedRDFRecordType)) {
            exportVersionedRDFData(resource, writer);
        }
    });
    writer.endRDF();
}
 
Example 8
Source File: RDFExportServiceImpl.java    From mobi with GNU Affero General Public License v3.0 5 votes vote down vote up
private void export(Iterable<Statement> statements, OutputStream output, RDFFormat format) throws IOException {
    if (!quadFormats.contains(format)) {
        LOGGER.warn("RDF format does not support quads.");
    }
    RDFHandler rdfWriter = new BufferedGroupingRDFHandler(Rio.createWriter(format, output));
    Rio.write(new StatementIterable(statements, transformer), rdfWriter);
}
 
Example 9
Source File: SimpleOntology.java    From mobi with GNU Affero General Public License v3.0 5 votes vote down vote up
private OutputStream getOntologyOutputStream(boolean skolemize, boolean prettyPrint, RDFFormat format, OutputStream outputStream) {
    long startTime = getStartTime();
    try (DatasetConnection conn = getDatasetConnection()) {
        RepositoryResult<Statement> statements = conn.getStatements(null, null, null,
                conn.getSystemDefaultNamedGraph());

        RDFHandler rdfWriter;
        if (prettyPrint) {
            rdfWriter = new BufferedGroupingRDFHandler(Rio.createWriter(format, outputStream));
        } else {
            rdfWriter = Rio.createWriter(format, outputStream);
        }

        RemoveContextHandler removeContextSH = new RemoveContextHandler(vf);
        if (skolemize) {
            SkolemizeHandler skolemizeSH = new SkolemizeHandler(bNodeService);
            com.mobi.persistence.utils.rio.Rio.write(statements, rdfWriter, transformer, skolemizeSH, removeContextSH);
        } else {
            com.mobi.persistence.utils.rio.Rio.write(statements, rdfWriter, transformer, removeContextSH);
        }

        undoApplyDifferenceIfPresent(conn);
    } catch (RDFHandlerException e) {
        throw new MobiOntologyException("Error while writing Ontology.");
    }
    logTrace("getOntologyOutputStream(" + format.getName() + ", outputStream)", startTime);
    return outputStream;
}
 
Example 10
Source File: Rdf4jWriter.java    From timbuctoo with GNU General Public License v3.0 5 votes vote down vote up
public Rdf4jWriter(OutputStream outputStream, RDFFormat rdfFormat) {
  writer = new BufferedWriter(new OutputStreamWriter(outputStream, UTF_8));
  rdfWriter = Rio.createWriter(rdfFormat, writer);
  this.rdfFormat = rdfFormat;
  valueFactory = SimpleValueFactory.getInstance();
  rdfWriter.startRDF();
}
 
Example 11
Source File: RestUtils.java    From mobi with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Converts a {@link Model} into a skolemized string containing grouped RDF in the specified RDFFormat.
 *
 * @param model       A {@link Model} of RDF to convert.
 * @param format      The RDFFormat the RDF should be serialized into.
 * @param transformer The SesameTransformer for model conversions.
 * @param service     The BNodeService for skolemization.
 * @return A skolemized String of the serialized grouped RDF from the Model.
 */
public static String groupedModelToSkolemizedString(Model model, RDFFormat format, SesameTransformer transformer,
                                                    BNodeService service) {
    long start = System.currentTimeMillis();
    try {
        StringWriter sw = new StringWriter();
        RDFHandler rdfWriter = new BufferedGroupingRDFHandler(Rio.createWriter(format, sw));
        Rio.write(new SkolemizedStatementIterable(model, transformer, service), rdfWriter);
        return sw.toString();
    } finally {
        LOG.trace("groupedModelToSkolemizedString took {}ms", System.currentTimeMillis() - start);
    }
}
 
Example 12
Source File: VersionedRDFRecordServiceTest.java    From mobi with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void exportSpecificBranch() throws Exception {
    Branch doNotWriteBranch = branchFactory.createNew(VALUE_FACTORY.createIRI("http://mobi.com/test/branches#branch2"));
    doNotWriteBranch.setHead(headCommit);
    doNotWriteBranch.setProperty(VALUE_FACTORY.createLiteral("Test Record"), VALUE_FACTORY.createIRI(_Thing.title_IRI));
    testRecord.addBranch(doNotWriteBranch);

    Set<Resource> branchesToExport = new HashSet<>();
    branchesToExport.add(branchIRI);

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    BatchExporter exporter =  new BatchExporter(transformer, new BufferedGroupingRDFHandler(Rio.createWriter(RDFFormat.JSONLD, os)));
    RecordOperationConfig config = new OperationConfig();

    config.set(RecordExportSettings.BATCH_EXPORTER, exporter);
    config.set(VersionedRDFRecordExportSettings.BRANCHES_TO_EXPORT, branchesToExport);

    assertFalse(exporter.isActive());
    exporter.startRDF();
    assertTrue(exporter.isActive());
    recordService.export(testIRI, config, connection);
    exporter.endRDF();
    assertFalse(exporter.isActive());

    Model outputModel = Values.mobiModel(Rio.parse((IOUtils.toInputStream(os.toString())), "", RDFFormat.JSONLD));
    testRecord.removeBranch(doNotWriteBranch);
    assertTrue(outputModel.containsAll(testRecord.getModel()));
    assertTrue(outputModel.containsAll(branch.getModel()));
    assertFalse(outputModel.containsAll(doNotWriteBranch.getModel()));
    assertTrue(outputModel.containsAll(difference.getDeletions()));

    verify(utilsService).optObject(eq(testIRI), any(OrmFactory.class), eq(connection));
    verify(utilsService).getBranch(eq(testRecord), eq(branchIRI), any(OrmFactory.class), eq(connection));
    verify(utilsService).getHeadCommitIRI(eq(branch));
    verify(utilsService).getCommitChain(eq(commitIRI), eq(false), any(RepositoryConnection.class));
    verify(utilsService).getExpectedObject(eq(commitIRI), any(OrmFactory.class), eq(connection));
    verify(utilsService).getRevisionChanges(eq(commitIRI), eq(connection));
}
 
Example 13
Source File: VersionedRDFRecordServiceTest.java    From mobi with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void exportRecordOnlyTest() throws Exception {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    BatchExporter exporter =  new BatchExporter(transformer, new BufferedGroupingRDFHandler(Rio.createWriter(RDFFormat.JSONLD, os)));
    RecordOperationConfig config = new OperationConfig();

    config.set(RecordExportSettings.BATCH_EXPORTER, exporter);
    config.set(VersionedRDFRecordExportSettings.WRITE_VERSIONED_DATA, false);

    assertFalse(exporter.isActive());
    exporter.startRDF();
    assertTrue(exporter.isActive());
    recordService.export(testIRI, config, connection);
    exporter.endRDF();
    assertFalse(exporter.isActive());

    Model outputModel = Values.mobiModel(Rio.parse((IOUtils.toInputStream(os.toString())), "", RDFFormat.JSONLD));
    assertTrue(outputModel.containsAll(testRecord.getModel()));
    assertFalse(outputModel.containsAll(branch.getModel()));
    assertFalse(outputModel.containsAll(difference.getDeletions()));

    verify(utilsService).optObject(eq(testIRI), any(OrmFactory.class), eq(connection));
    verify(utilsService, never()).getBranch(eq(testRecord), eq(branchIRI), any(OrmFactory.class), eq(connection));
    verify(utilsService, never()).getHeadCommitIRI(eq(branch));
    verify(utilsService, never()).getCommitChain(eq(commitIRI), eq(false), any(RepositoryConnection.class));
    verify(utilsService, never()).getExpectedObject(eq(commitIRI), any(OrmFactory.class), eq(connection));
    verify(utilsService, never()).getRevisionChanges(eq(commitIRI), eq(connection));
}
 
Example 14
Source File: VersionedRDFRecordServiceTest.java    From mobi with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void exportUsingBatchExporterTest() throws Exception {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    BatchExporter exporter =  new BatchExporter(transformer, new BufferedGroupingRDFHandler(Rio.createWriter(RDFFormat.JSONLD, os)));
    RecordOperationConfig config = new OperationConfig();

    config.set(RecordExportSettings.BATCH_EXPORTER, exporter);

    assertFalse(exporter.isActive());
    exporter.startRDF();
    assertTrue(exporter.isActive());
    recordService.export(testIRI, config, connection);
    exporter.endRDF();
    assertFalse(exporter.isActive());

    Model outputModel = Values.mobiModel(Rio.parse((IOUtils.toInputStream(os.toString())), "", RDFFormat.JSONLD));
    assertTrue(outputModel.containsAll(testRecord.getModel()));
    assertTrue(outputModel.containsAll(branch.getModel()));
    assertTrue(outputModel.containsAll(difference.getAdditions()));
    assertTrue(outputModel.containsAll(difference.getDeletions()));

    verify(utilsService).optObject(eq(testIRI), any(OrmFactory.class), eq(connection));
    verify(utilsService).getBranch(eq(testRecord), eq(branchIRI), any(OrmFactory.class), eq(connection));
    verify(utilsService).getHeadCommitIRI(eq(branch));
    verify(utilsService).getCommitChain(eq(commitIRI), eq(false), any(RepositoryConnection.class));
    verify(utilsService).getExpectedObject(eq(commitIRI), any(OrmFactory.class), eq(connection));
    verify(utilsService).getRevisionChanges(eq(commitIRI), eq(connection));
}
 
Example 15
Source File: QueryResultIO.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Writes a graph query result document in a specific RDF format to an output stream.
 *
 * @param gqr    The query result to write.
 * @param format The file format of the document to write.
 * @param out    An OutputStream to write the document to.
 * @throws IOException                  If an I/O error occurred while writing the query result document to the
 *                                      stream.
 * @throws RDFHandlerException          If such an exception is thrown by the used RDF writer.
 * @throws QueryEvaluationException
 * @throws UnsupportedRDFormatException If an unsupported query result file format was specified.
 */
public static void writeGraph(GraphQueryResult gqr, RDFFormat format, OutputStream out)
		throws IOException, RDFHandlerException, UnsupportedRDFormatException, QueryEvaluationException {
	RDFWriter writer = Rio.createWriter(format, out);
	try {
		QueryResults.report(gqr, writer);
	} catch (RDFHandlerException e) {
		if (e.getCause() instanceof IOException) {
			throw (IOException) e.getCause();
		} else {
			throw e;
		}
	}
}
 
Example 16
Source File: QueryEvaluator.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Get a graph result (RIO) writer based upon the file name (extension), or return the console result writer when
 * path is null.
 *
 * @param path path or null
 * @param out  output stream or null
 * @return result writer
 * @throws IllegalArgumentException
 */
private RDFWriter getRDFWriter(Path path, OutputStream out) throws IllegalArgumentException {
	RDFWriter w;
	if (path == null) {
		w = new ConsoleRDFWriter(consoleIO, getConsoleWidth());
	} else {
		Optional<RDFFormat> fmt = Rio.getWriterFormatForFileName(path.toFile().toString());
		if (!fmt.isPresent()) {
			throw new IllegalArgumentException("No suitable result writer found");
		}
		w = Rio.createWriter(fmt.get(), out);
	}
	return w;
}
 
Example 17
Source File: RepositoryModel.java    From semweb4j with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public void writeTo(Writer writer, Syntax syntax) throws ModelRuntimeException {
	assertModel();
	RDFWriter rdfWriter = Rio.createWriter(getRDFFormat(syntax), writer);
	writeTo(rdfWriter);
}
 
Example 18
Source File: RdfWriter.java    From Wikidata-Toolkit with Apache License 2.0 4 votes vote down vote up
public RdfWriter(RDFFormat format, OutputStream output) throws UnsupportedRDFormatException {
	this(Rio.createWriter(format, output));
}
 
Example 19
Source File: HalyardStats.java    From Halyard with Apache License 2.0 4 votes vote down vote up
@Override
protected void setup(Context context) throws IOException, InterruptedException {
    Configuration conf = context.getConfiguration();
    statsGraphContext = SVF.createIRI(conf.get(TARGET_GRAPH, HALYARD.STATS_GRAPH_CONTEXT.stringValue()));
    String targetUrl = conf.get(TARGET);
    if (targetUrl == null) {
        sail = new HBaseSail(conf, conf.get(SOURCE), false, 0, true, 0, null, null);
        sail.initialize();
        sail.setNamespace(SD.PREFIX, SD.NAMESPACE);
        sail.setNamespace(VOID.PREFIX, VOID.NAMESPACE);
        sail.setNamespace(VOID_EXT.PREFIX, VOID_EXT.NAMESPACE);
        sail.setNamespace(HALYARD.PREFIX, HALYARD.NAMESPACE);
    } else {
        targetUrl = MessageFormat.format(targetUrl, context.getTaskAttemptID().getTaskID().getId());
        out = FileSystem.get(URI.create(targetUrl), conf).create(new Path(targetUrl));
        try {
            if (targetUrl.endsWith(".bz2")) {
                out = new CompressorStreamFactory().createCompressorOutputStream(CompressorStreamFactory.BZIP2, out);
                targetUrl = targetUrl.substring(0, targetUrl.length() - 4);
            } else if (targetUrl.endsWith(".gz")) {
                out = new CompressorStreamFactory().createCompressorOutputStream(CompressorStreamFactory.GZIP, out);
                targetUrl = targetUrl.substring(0, targetUrl.length() - 3);
            }
        } catch (CompressorException ce) {
            throw new IOException(ce);
        }
        Optional<RDFFormat> form = Rio.getWriterFormatForFileName(targetUrl);
        if (!form.isPresent()) throw new IOException("Unsupported target file format extension: " + targetUrl);
        writer = Rio.createWriter(form.get(), out);
        writer.handleNamespace(SD.PREFIX, SD.NAMESPACE);
        writer.handleNamespace(VOID.PREFIX, VOID.NAMESPACE);
        writer.handleNamespace(VOID_EXT.PREFIX, VOID_EXT.NAMESPACE);
        writer.handleNamespace(HALYARD.PREFIX, HALYARD.NAMESPACE);
        writer.startRDF();
    }
    if (conf.get(GRAPH_CONTEXT) == null) {
        writeStatement(HALYARD.STATS_ROOT_NODE, RDF.TYPE, VOID.DATASET);
        writeStatement(HALYARD.STATS_ROOT_NODE, RDF.TYPE, SD.DATASET);
        writeStatement(HALYARD.STATS_ROOT_NODE, RDF.TYPE, SD.GRAPH_CLASS);
        writeStatement(HALYARD.STATS_ROOT_NODE, SD.DEFAULT_GRAPH, HALYARD.STATS_ROOT_NODE);
    }
    graphs = new WeakHashMap<>();
}
 
Example 20
Source File: RepositoryModelSet.java    From semweb4j with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Writes the whole ModelSet to the OutputStream. Depending on the Syntax
 * the context URIs might or might not be serialized. TriX should be able to
 * serialize contexts.
 */
@Override
public void writeTo(OutputStream out, Syntax syntax) throws IOException, ModelRuntimeException {
	RDFWriter rdfWriter = Rio.createWriter(getRDFFormat(syntax), out);
	this.writeTo(rdfWriter);
}