Search in sources :

Example 61 with Statement

use of org.eclipse.rdf4j.model.Statement in project rdf4j by eclipse.

the class DAWGTestResultSetUtil method toBooleanQueryResult.

public static boolean toBooleanQueryResult(Iterable<? extends Statement> dawgGraph) throws DAWGTestResultSetParseException {
    DAWGTestBooleanParser parser = new DAWGTestBooleanParser();
    try {
        parser.startRDF();
        for (Statement st : dawgGraph) {
            parser.handleStatement(st);
        }
        parser.endRDF();
        return parser.getValue();
    } catch (RDFHandlerException e) {
        throw new DAWGTestResultSetParseException(e.getMessage(), e);
    }
}
Also used : RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) Statement(org.eclipse.rdf4j.model.Statement)

Example 62 with Statement

use of org.eclipse.rdf4j.model.Statement in project rdf4j by eclipse.

the class BinaryHandlingTest method writeBinary.

/**
 * Helper method to write the given model to N-Triples and return an InputStream containing the results.
 *
 * @param statements
 * @return An {@link InputStream} containing the results.
 * @throws RDFHandlerException
 */
private InputStream writeBinary(Model statements) throws RDFHandlerException {
    ByteArrayOutputStream output = new ByteArrayOutputStream(8096);
    RDFWriter binaryWriter = new BinaryRDFWriter(output);
    binaryWriter.startRDF();
    for (Statement nextStatement : statements) {
        binaryWriter.handleStatement(nextStatement);
    }
    binaryWriter.endRDF();
    return new ByteArrayInputStream(output.toByteArray());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) BinaryRDFWriter(org.eclipse.rdf4j.rio.binary.BinaryRDFWriter) Statement(org.eclipse.rdf4j.model.Statement) BinaryRDFWriter(org.eclipse.rdf4j.rio.binary.BinaryRDFWriter) RDFWriter(org.eclipse.rdf4j.rio.RDFWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 63 with Statement

use of org.eclipse.rdf4j.model.Statement in project rdf4j by eclipse.

the class RDFJSONHandlingTest method writeRDFJSON.

/**
 * Helper method to write the given model to RDFJSON and return an InputStream containing the results.
 *
 * @param statements
 * @return An {@link InputStream} containing the results.
 * @throws RDFHandlerException
 */
private InputStream writeRDFJSON(Model statements) throws RDFHandlerException {
    StringWriter writer = new StringWriter();
    RDFWriter rdfjsonWriter = new RDFJSONWriter(writer, RDFFormat.RDFJSON);
    rdfjsonWriter.startRDF();
    for (Statement nextStatement : statements) {
        rdfjsonWriter.handleStatement(nextStatement);
    }
    rdfjsonWriter.endRDF();
    return new ByteArrayInputStream(writer.toString().getBytes(Charset.forName("UTF-8")));
}
Also used : RDFJSONWriter(org.eclipse.rdf4j.rio.rdfjson.RDFJSONWriter) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) Statement(org.eclipse.rdf4j.model.Statement) RDFWriter(org.eclipse.rdf4j.rio.RDFWriter)

Example 64 with Statement

use of org.eclipse.rdf4j.model.Statement in project rdf4j by eclipse.

the class JSONLDParserHandlerTest method writeJSONLD.

/**
 * Helper method to write the given model to JSON-LD and return an InputStream containing the results.
 *
 * @param statements
 * @return An {@link InputStream} containing the results.
 * @throws RDFHandlerException
 */
private InputStream writeJSONLD(Model statements) throws RDFHandlerException {
    final StringWriter writer = new StringWriter();
    final RDFWriter jsonldWriter = new JSONLDWriter(writer);
    jsonldWriter.startRDF();
    for (final Namespace prefix : statements.getNamespaces()) {
        jsonldWriter.handleNamespace(prefix.getPrefix(), prefix.getName());
    }
    for (final Statement nextStatement : statements) {
        jsonldWriter.handleStatement(nextStatement);
    }
    jsonldWriter.endRDF();
    return new ByteArrayInputStream(writer.toString().getBytes(Charset.forName("UTF-8")));
}
Also used : JSONLDWriter(org.eclipse.rdf4j.rio.jsonld.JSONLDWriter) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) Statement(org.eclipse.rdf4j.model.Statement) RDFWriter(org.eclipse.rdf4j.rio.RDFWriter) Namespace(org.eclipse.rdf4j.model.Namespace)

Example 65 with Statement

use of org.eclipse.rdf4j.model.Statement in project rdf4j by eclipse.

the class JSONLDWriterTest method testRoundTripNamespaces.

@Test
public void testRoundTripNamespaces() throws Exception {
    String exNs = "http://example.org/";
    IRI uri1 = vf.createIRI(exNs, "uri1");
    IRI uri2 = vf.createIRI(exNs, "uri2");
    Literal plainLit = vf.createLiteral("plain", XMLSchema.STRING);
    Statement st1 = vf.createStatement(uri1, uri2, plainLit);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    RDFWriter rdfWriter = rdfWriterFactory.getWriter(out);
    rdfWriter.getWriterConfig().set(JSONLDSettings.JSONLD_MODE, JSONLDMode.COMPACT);
    rdfWriter.handleNamespace("ex", exNs);
    rdfWriter.startRDF();
    rdfWriter.handleStatement(st1);
    rdfWriter.endRDF();
    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    RDFParser rdfParser = rdfParserFactory.getParser();
    ParserConfig config = new ParserConfig();
    config.set(BasicParserSettings.FAIL_ON_UNKNOWN_DATATYPES, true);
    config.set(BasicParserSettings.FAIL_ON_UNKNOWN_LANGUAGES, true);
    rdfParser.setParserConfig(config);
    rdfParser.setValueFactory(vf);
    Model model = new LinkedHashModel();
    rdfParser.setRDFHandler(new StatementCollector(model));
    rdfParser.parse(in, "foo:bar");
    assertEquals("Unexpected number of statements, found " + model.size(), 1, model.size());
    assertTrue("missing namespaced statement", model.contains(st1));
    if (rdfParser.getRDFFormat().supportsNamespaces()) {
        assertTrue("Expected at least one namespace, found " + model.getNamespaces().size(), model.getNamespaces().size() >= 1);
        assertEquals(exNs, model.getNamespace("ex").get().getName());
    }
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) ByteArrayInputStream(java.io.ByteArrayInputStream) Statement(org.eclipse.rdf4j.model.Statement) StatementCollector(org.eclipse.rdf4j.rio.helpers.StatementCollector) Literal(org.eclipse.rdf4j.model.Literal) Model(org.eclipse.rdf4j.model.Model) LinkedHashModel(org.eclipse.rdf4j.model.impl.LinkedHashModel) RDFWriter(org.eclipse.rdf4j.rio.RDFWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LinkedHashModel(org.eclipse.rdf4j.model.impl.LinkedHashModel) RDFParser(org.eclipse.rdf4j.rio.RDFParser) ParserConfig(org.eclipse.rdf4j.rio.ParserConfig) RDFWriterTest(org.eclipse.rdf4j.rio.RDFWriterTest) Test(org.junit.Test)

Aggregations

Statement (org.eclipse.rdf4j.model.Statement)74 IRI (org.eclipse.rdf4j.model.IRI)17 Test (org.junit.Test)17 Model (org.eclipse.rdf4j.model.Model)16 Literal (org.eclipse.rdf4j.model.Literal)15 Resource (org.eclipse.rdf4j.model.Resource)15 Value (org.eclipse.rdf4j.model.Value)13 RDFWriter (org.eclipse.rdf4j.rio.RDFWriter)11 ByteArrayInputStream (java.io.ByteArrayInputStream)10 ArrayList (java.util.ArrayList)10 LinkedHashModel (org.eclipse.rdf4j.model.impl.LinkedHashModel)8 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)8 RDFParseException (org.eclipse.rdf4j.rio.RDFParseException)8 StringWriter (java.io.StringWriter)7 BNode (org.eclipse.rdf4j.model.BNode)6 ValueFactory (org.eclipse.rdf4j.model.ValueFactory)6 SimpleValueFactory (org.eclipse.rdf4j.model.impl.SimpleValueFactory)6 RepositoryConnection (org.eclipse.rdf4j.repository.RepositoryConnection)6 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)6 IOException (java.io.IOException)5