Java Code Examples for org.eclipse.rdf4j.rio.WriterConfig#set()

The following examples show how to use org.eclipse.rdf4j.rio.WriterConfig#set() . 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 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 2
Source File: JSONParserParseTest.java    From Halyard with Apache License 2.0 6 votes vote down vote up
@Test
public void testParse() throws Exception {
    Model transformedModel = new LinkedHashModel();
    RDFParser parser = new JSONParser();
    parser.setValueFactory(SimpleValueFactory.getInstance());
    parser.set(JSONParser.GENERATE_ONTOLOGY, true);
    parser.setRDFHandler(new ContextStatementCollector(transformedModel, SimpleValueFactory.getInstance()));
    parser.parse(JSONParserParseTest.class.getResourceAsStream(parameter + ".json"), "http://testParse/"+parameter + "/");

    WriterConfig wc = new WriterConfig();
    wc.set(BasicWriterSettings.PRETTY_PRINT, true);
    System.out.println("-------------- " + parameter + " ------------------");
    Rio.write(transformedModel, System.out, RDFFormat.TURTLE, wc);

    Model expectedModel = Rio.parse(JSONParserParseTest.class.getResourceAsStream(parameter + ".ttl"), "http://testParse/" + parameter + "/", RDFFormat.TURTLE);

    JSONParserParseTest.assertEquals(expectedModel, transformedModel);
}
 
Example 3
Source File: Verify.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Write SHACL validation report to a file. File extension is used to select the serialization format, TTL is used
 * as default.
 *
 * @param model      report
 * @param reportFile file name
 */
private void writeReport(Model model, String reportFile) {
	WriterConfig cfg = new WriterConfig();
	cfg.set(BasicWriterSettings.PRETTY_PRINT, true);
	cfg.set(BasicWriterSettings.INLINE_BLANK_NODES, true);

	RDFFormat format = Rio.getParserFormatForFileName(reportFile).orElse(RDFFormat.TURTLE);

	try (Writer w = Files.newBufferedWriter(Paths.get(reportFile))) {
		Rio.write(model, w, format, cfg);
	} catch (IOException ex) {
		writeError("Could not write report to " + reportFile, ex);
	}
}
 
Example 4
Source File: AbstractShaclTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static void printCurrentState(SailRepository shaclRepository) {
	if (!fullLogging) {
		return;
	}

	try (SailRepositoryConnection connection = shaclRepository.getConnection()) {

		if (connection.isEmpty()) {
			System.out.println("### CURRENT REPOSITORY STATE ###");
			System.out.println("   EMPTY!");
			System.out.println("################################################\n");
		} else {

			try (Stream<Statement> stream = connection.getStatements(null, null, null, false).stream()) {
				LinkedHashModel model = stream.collect(Collectors.toCollection(LinkedHashModel::new));
				model.setNamespace("ex", "http://example.com/ns#");
				model.setNamespace(FOAF.PREFIX, FOAF.NAMESPACE);
				model.setNamespace(XMLSchema.PREFIX, XMLSchema.NAMESPACE);
				model.setNamespace(RDF.PREFIX, RDF.NAMESPACE);
				model.setNamespace(RDFS.PREFIX, RDFS.NAMESPACE);

				WriterConfig writerConfig = new WriterConfig();
				writerConfig.set(BasicWriterSettings.PRETTY_PRINT, true);
				writerConfig.set(BasicWriterSettings.INLINE_BLANK_NODES, true);
				System.out.println("### CURRENT REPOSITORY STATE ###");
				Rio.write(model, System.out, RDFFormat.TURTLE, writerConfig);
				System.out.println("################################################\n");

			}
		}

	}
}
 
Example 5
Source File: AbstractShaclTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static void printResults(ValidationReport report) {
	if (!fullLogging) {
		return;
	}
	System.out.println("\n############################################");
	System.out.println("\tValidation Report\n");
	Model validationReport = report.asModel();

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

	Rio.write(validationReport, System.out, RDFFormat.TURTLE, writerConfig);
	System.out.println("\n############################################");
}
 
Example 6
Source File: ArrangedWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testWriteRepeatedInlineBlankNode() {
	Model model = new ModelBuilder().subject(exNs + "subject")
			.add(vf.createIRI(exNs, "rel1"), bnode1)
			.add(vf.createIRI(exNs, "rel2"), bnode1)
			.add(vf.createIRI(exNs, "rel3"), bnode2)
			.subject(bnode1)
			.add(RDFS.LABEL, "the bnode1")
			.subject(bnode2)
			.add(RDFS.LABEL, "the bnode2")
			.build();

	model.setNamespace(RDFS.NS);
	model.setNamespace("ex", exNs);

	StringWriter stringWriter = new StringWriter();
	BufferedWriter writer = new BufferedWriter(stringWriter);
	WriterConfig config = new WriterConfig();
	config.set(BasicWriterSettings.INLINE_BLANK_NODES, true);
	Rio.write(model, writer, RDFFormat.TURTLE, config);

	String sep = System.lineSeparator();
	String expectedResult = "@prefix ex: <http://example.org/> ." + sep +
			"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> ." + sep +
			"@prefix xsd: <http://www.w3.org/2001/XMLSchema#> ." + sep + sep +
			"ex:subject ex:rel1 _:bnode1 ." + sep + sep +
			"_:bnode1 rdfs:label \"the bnode1\" ." + sep + sep +
			"ex:subject ex:rel2 _:bnode1;" + sep +
			"  ex:rel3 [" + sep +
			"      rdfs:label \"the bnode2\"" + sep +
			"    ] ." + sep;

	assertEquals(expectedResult, stringWriter.toString());
}
 
Example 7
Source File: RDFXMLWriterBackgroundTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected void setupWriterConfig(WriterConfig config) {
	config.set(BasicWriterSettings.PRETTY_PRINT, false);
}
 
Example 8
Source File: RDFXMLPrettyWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected void setupWriterConfig(WriterConfig config) {
	config.set(BasicWriterSettings.PRETTY_PRINT, true);
}
 
Example 9
Source File: RDFXMLPrettyWriterBackgroundTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected void setupWriterConfig(WriterConfig config) {
	config.set(BasicWriterSettings.PRETTY_PRINT, true);
}
 
Example 10
Source File: RDFXMLWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected void setupWriterConfig(WriterConfig config) {
	config.set(BasicWriterSettings.PRETTY_PRINT, false);
}
 
Example 11
Source File: JSONLDWriterBackgroundTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected void setupWriterConfig(WriterConfig config) {
	super.setupWriterConfig(config);
	config.set(JSONLDSettings.JSONLD_MODE, JSONLDMode.COMPACT);
}
 
Example 12
Source File: JSONLDHierarchicalWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Before
public void setup() {
	model = new LinkedHashModel();
	writerConfig = new WriterConfig();
	writerConfig.set(JSONLDSettings.HIERARCHICAL_VIEW, true);
}
 
Example 13
Source File: TriGPrettyWriterBackgroundTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected void setupWriterConfig(WriterConfig config) {
	config.set(BasicWriterSettings.PRETTY_PRINT, true);
}
 
Example 14
Source File: TriGStarPrettyWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected void setupWriterConfig(WriterConfig config) {
	config.set(BasicWriterSettings.PRETTY_PRINT, true);
}
 
Example 15
Source File: TriGStarWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected void setupWriterConfig(WriterConfig config) {
	config.set(BasicWriterSettings.PRETTY_PRINT, false);
}
 
Example 16
Source File: TurtleStarPrettyWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected void setupWriterConfig(WriterConfig config) {
	config.set(BasicWriterSettings.PRETTY_PRINT, true);
}
 
Example 17
Source File: TurtleStarWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected void setupWriterConfig(WriterConfig config) {
	config.set(BasicWriterSettings.PRETTY_PRINT, false);
}
 
Example 18
Source File: TurtleWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected void setupWriterConfig(WriterConfig config) {
	config.set(BasicWriterSettings.PRETTY_PRINT, false);
}
 
Example 19
Source File: ValidationReportTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void nestedLogicalOrSupport() throws IOException {

	SailRepository shaclSail = Utils.getInitializedShaclRepository("test-cases/or/datatype/shacl.ttl", false);

	try (SailRepositoryConnection connection = shaclSail.getConnection()) {

		connection.begin();
		connection.prepareUpdate(IOUtils.toString(ValidationReportTest.class.getClassLoader()
				.getResourceAsStream("test-cases/or/datatype/invalid/case1/query1.rq"), StandardCharsets.UTF_8))
				.execute();
		connection.commit();
		fail();

	} catch (RepositoryException e) {
		ShaclSailValidationException cause = (ShaclSailValidationException) e.getCause();
		Model actual = cause.validationReportAsModel();

		actual.setNamespace(RDF.PREFIX, RDF.NAMESPACE);
		actual.setNamespace(RDFS.PREFIX, RDFS.NAMESPACE);
		actual.setNamespace("ex", "http://example.com/ns#");

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

		Rio.write(actual, System.out, RDFFormat.TURTLE, writerConfig);

		Model expected = Rio.parse(new StringReader(""
				+ "@prefix ex: <http://example.com/ns#> .\n"
				+ "@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n"
				+ "@prefix sh: <http://www.w3.org/ns/shacl#> .\n" + "\n"
				+ "[] a sh:ValidationReport;\n" +
				"  sh:conforms false;\n" +
				"  sh:result [ a sh:ValidationResult;\n" +
				"      sh:detail [ a sh:ValidationResult;\n" +
				"          sh:detail [ a sh:ValidationResult;\n" +
				"              sh:focusNode ex:validPerson1;\n" +
				"              sh:resultPath ex:age;\n" +
				"              sh:sourceConstraintComponent sh:DatatypeConstraintComponent;\n" +
				"              sh:sourceShape ex:personShapeAgeLong\n" +
				"            ];\n" +
				"          sh:focusNode ex:validPerson1;\n" +
				"          sh:resultPath ex:age;\n" +
				"          sh:sourceConstraintComponent sh:DatatypeConstraintComponent;\n" +
				"          sh:sourceShape ex:personShapeAgeInteger\n" +
				"        ];\n" +
				"      sh:focusNode ex:validPerson1;\n" +
				"      sh:resultPath ex:age;\n" +
				"      sh:sourceConstraintComponent sh:OrConstraintComponent;\n" +
				"      sh:sourceShape ex:personShapeOr\n" +
				"    ] ." + ""), "", RDFFormat.TURTLE);

		assertTrue(Models.isomorphic(expected, actual));

	}
}
 
Example 20
Source File: JSONLDWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected void setupWriterConfig(WriterConfig config) {
	super.setupWriterConfig(config);
	config.set(JSONLDSettings.JSONLD_MODE, JSONLDMode.COMPACT);
}