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

The following examples show how to use org.eclipse.rdf4j.rio.Rio#parse() . 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: CustomTurtleParserTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testSES2165LiteralSpaceDatatypeSpace() throws Exception {
	Model model = Rio.parse(new StringReader("<urn:a> <urn:b> \"testliteral\"^^ <urn:datatype> ."), "",
			RDFFormat.TURTLE);

	assertEquals(1, model.size());
	assertTrue(model.contains(vf.createIRI("urn:a"), vf.createIRI("urn:b"),
			vf.createLiteral("testliteral", vf.createIRI("urn:datatype"))));
}
 
Example 2
Source File: RdfConverter.java    From timbuctoo with GNU General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {
  final Model model = Rio.parse(
      new FileReader("/home/martijnm/Desktop/emdates_places_opole.ttl"),
      "http://example.com",
      RDFFormat.TURTLE
  );

  Rio.write(model, new FileWriter("/home/martijnm/Desktop/emdates_places_opole.nq"), RDFFormat.NQUADS);
}
 
Example 3
Source File: HalyardStatsTest.java    From Halyard with Apache License 2.0 5 votes vote down vote up
@Test
public void testStatsTarget() throws Exception {
    final HBaseSail sail = new HBaseSail(HBaseServerTestInstance.getInstanceConfig(), "statsTable", true, -1, true, 0, null, null);
    sail.initialize();
    try (InputStream ref = HalyardStatsTest.class.getResourceAsStream("testData.trig")) {
        RDFParser p = Rio.createParser(RDFFormat.TRIG);
        p.setPreserveBNodeIDs(true);
        p.setRDFHandler(new AbstractRDFHandler() {
            @Override
            public void handleStatement(Statement st) throws RDFHandlerException {
                sail.addStatement(st.getSubject(), st.getPredicate(), st.getObject(), st.getContext());
            }
        }).parse(ref, "");
    }
    sail.commit();
    sail.close();

    File root = File.createTempFile("test_stats", "");
    root.delete();
    root.mkdirs();

    assertEquals(0, ToolRunner.run(HBaseServerTestInstance.getInstanceConfig(), new HalyardStats(),
            new String[]{"-s", "statsTable", "-t", root.toURI().toURL().toString() + "stats{0}.trig", "-r", "100", "-g", "http://whatever/myStats"}));

    File stats = new File(root, "stats0.trig");
    assertTrue(stats.isFile());
    try (InputStream statsStream = new FileInputStream(stats)) {
        try (InputStream refStream = HalyardStatsTest.class.getResourceAsStream("testStatsTarget.trig")) {
            Model statsM = Rio.parse(statsStream, "", RDFFormat.TRIG, new ParserConfig().set(BasicParserSettings.PRESERVE_BNODE_IDS, true), SimpleValueFactory.getInstance(), new ParseErrorLogger());
            Model refM = Rio.parse(refStream, "", RDFFormat.TRIG, new ParserConfig().set(BasicParserSettings.PRESERVE_BNODE_IDS, true), SimpleValueFactory.getInstance(), new ParseErrorLogger(), SimpleValueFactory.getInstance().createIRI("http://whatever/myStats"));
            assertEqualModels(refM, statsM);
        }
    }
}
 
Example 4
Source File: CustomTurtleParserTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testParsingNamespacesWithOverride() throws Exception {
	ParserConfig aConfig = new ParserConfig();

	aConfig.set(BasicParserSettings.NAMESPACES,
			Collections.<Namespace>singleton(new NamespaceImpl("foo", SKOS.NAMESPACE)));

	Model model = Rio.parse(new StringReader("@prefix skos : <urn:not_skos:> ." + "<urn:a> skos:broader <urn:b>."),
			"", RDFFormat.TURTLE, aConfig, vf, new ParseErrorLogger());

	assertEquals(1, model.size());
	assertTrue(model.contains(vf.createIRI("urn:a"), vf.createIRI("urn:not_skos:broader"), vf.createIRI("urn:b")));
}
 
Example 5
Source File: TriGParserCustomTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testGraph() throws Exception {
	Model model = Rio.parse(new StringReader("<urn:a> { [] <http://www.example.net/test> \"Foo\" }"), "",
			RDFFormat.TRIG);

	assertEquals(1, model.size());
	assertNotNull(model.contexts().iterator().next());
	assertEquals("urn:a", model.contexts().iterator().next().stringValue());
	assertTrue(model.subjects().iterator().next() instanceof BNode);
	assertEquals("http://www.example.net/test", model.predicates().iterator().next().stringValue());
	assertEquals("Foo", model.objects().iterator().next().stringValue());
}
 
Example 6
Source File: CustomTurtleParserTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testSES1887Period() throws Exception {
	Model model = Rio.parse(new StringReader("<urn:a> <http://www.example.net/test> \"Foo\"@fr-FR."), "",
			RDFFormat.TURTLE);

	assertEquals(1, model.size());
	assertTrue(model.contains(null, null, vf.createLiteral("Foo", "fr-FR")));
}
 
Example 7
Source File: RDFXMLParserCustomTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void tset() throws IOException {
	ValueFactory vf = SimpleValueFactory.getInstance();
	Model mm = new LinkedHashModel();
	mm.add(vf.createStatement(vf.createIRI("urn:foo:момтен.т=2-"), RDF.TYPE, vf.createIRI("urn:foo:момтен")));
	try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
		RDFXMLPrettyWriter rdfWriter = new RDFXMLPrettyWriter(System.out);
		Rio.write(mm, rdfWriter);
		RDFXMLPrettyWriter rdfWriter2 = new RDFXMLPrettyWriter(bos);
		Rio.write(mm, rdfWriter2);
		Model mmm = Rio.parse(new ByteArrayInputStream(bos.toByteArray()), "urn:base:", RDFFormat.RDFXML);
		mmm.forEach(System.out::println);
	}
}
 
Example 8
Source File: RemoteValidation.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
Model asModel() {
	if (model == null) {
		try {
			model = Rio.parse(stringReader, baseUri, format);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	return model;
}
 
Example 9
Source File: CustomTurtleParserTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testSES1988BlankNodePeriodBNode() throws Exception {
	Model model = Rio.parse(new StringReader("<urn:a> <urn:b> _:blank._:blank <urn:d> <urn:e>."), "",
			RDFFormat.TURTLE);

	assertEquals(2, model.size());
}
 
Example 10
Source File: TriGParserCustomTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testGraphLocalNameIntegerNotGraph() throws Exception {
	Model model = Rio.parse(
			new StringReader("@prefix ex: <urn:> .\n ex:1 { [] <http://www.example.net/test> \"Foo\" }"), "",
			RDFFormat.TRIG);

	assertEquals(1, model.size());
	assertNotNull(model.contexts().iterator().next());
	assertEquals("urn:1", model.contexts().iterator().next().stringValue());
	assertTrue(model.subjects().iterator().next() instanceof BNode);
	assertEquals("http://www.example.net/test", model.predicates().iterator().next().stringValue());
	assertEquals("Foo", model.objects().iterator().next().stringValue());
}
 
Example 11
Source File: ExportTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public final void testExportAll() throws RepositoryException, IOException {
	File nq = LOCATION.newFile("all.nq");
	cmd.execute("export", nq.getAbsolutePath());
	Model exp = Rio.parse(Files.newReader(nq, StandardCharsets.UTF_8), "http://example.com", RDFFormat.NQUADS);

	assertTrue("File is empty", nq.length() > 0);
	assertEquals("Number of contexts incorrect", 3, exp.contexts().size());

	nq.delete();
}
 
Example 12
Source File: SparqlTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public final void testOutputFileConstruct() throws IOException {
	File f = LOCATION.newFile("out.ttl");

	cmd.executeQuery("sparql OUTFILE=\"" + f.getAbsolutePath() + "\" construct { ?s ?p ?o } where { ?s ?p ?o }",
			"sparql");
	verify(mockConsoleIO, never()).writeError(anyString());

	assertTrue("File does not exist", f.exists());
	assertTrue("Empty file", f.length() > 0);

	Model m = Rio.parse(new FileReader(f), "", RDFFormat.TURTLE);
	assertTrue("Empty model", m.size() > 0);
}
 
Example 13
Source File: CustomTurtleParserTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testSES1887NoLangTagFailure() throws Exception {
	try {
		Rio.parse(new StringReader("<urn:a> <http://www.example.net/test> \"Foo\"@."), "", RDFFormat.TURTLE);
		fail("Did not receive an exception");
	} catch (RDFParseException e) {
		assertTrue(e.getMessage().contains("Expected a letter, found '.'"));
	}
}
 
Example 14
Source File: CustomTurtleParserTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void testParseDoubleLiteral() throws Exception {
	String ttl = "<http://www.ex.com/s> <http://www.ex.com/b> 1.2.\n";
	Model m = Rio.parse(new StringReader(ttl), "http://ex/", RDFFormat.TURTLE);
	assertEquals(1.2d, Models.objectLiteral(m).get().doubleValue(), 0.01);
}
 
Example 15
Source File: CustomTurtleParserTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void testSES1988BlankNodePeriodTab() throws Exception {
	Model model = Rio.parse(new StringReader("<urn:a> <urn:b> _:blank.\t"), "", RDFFormat.TURTLE);

	assertEquals(1, model.size());
}
 
Example 16
Source File: TriGParserCustomTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void testParseTruePrefix() throws Exception {
	Rio.parse(new StringReader("@prefix true: <http://example/c/> . {true:s true:p true:o .}"), "", RDFFormat.TRIG);
}
 
Example 17
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 18
Source File: TriGParserCustomTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void testTrailingSemicolon() throws Exception {
	Rio.parse(new StringReader("{<http://example/s> <http://example/p> <http://example/o> ;}"), "", RDFFormat.TRIG);
}
 
Example 19
Source File: TriGParserCustomTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void testParseTrig_booleanLiteral_space() throws Exception {
	String trig = "{\n" + "  <http://www.ex.com/s> <http://www.ex.com/b> true .\n" + "}";
	Model m = Rio.parse(new StringReader(trig), "http://ex/", RDFFormat.TRIG);
	assertEquals(1, m.size());
}
 
Example 20
Source File: JSONLDHierarchicalWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void verifyModelIsNotChanged(File file) throws IOException {
	Model model2 = Rio.parse(new FileInputStream(file), null, RDFFormat.JSONLD);
	assertTrue(model2.equals(model));
}