Search in sources :

Example 21 with BNode

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

the class SPARQLConnection method createInsertDataCommand.

private String createInsertDataCommand(Iterable<? extends Statement> statements, Resource... contexts) {
    StringBuilder qb = new StringBuilder();
    qb.append("INSERT DATA \n");
    qb.append("{ \n");
    if (contexts.length > 0) {
        for (Resource context : contexts) {
            if (context != null) {
                String namedGraph = context.stringValue();
                if (context instanceof BNode) {
                    // SPARQL does not allow blank nodes as named graph
                    // identifiers, so we need to skolemize
                    // the blank node id.
                    namedGraph = "urn:nodeid:" + context.stringValue();
                }
                qb.append("    GRAPH <" + namedGraph + "> { \n");
            }
            createDataBody(qb, statements, true);
            if (context != null) {
                qb.append(" } \n");
            }
        }
    } else {
        createDataBody(qb, statements, false);
    }
    qb.append("}");
    return qb.toString();
}
Also used : BNode(org.eclipse.rdf4j.model.BNode) Resource(org.eclipse.rdf4j.model.Resource)

Example 22 with BNode

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

the class SPARQLConnection method createDeleteDataCommand.

private String createDeleteDataCommand(Iterable<? extends Statement> statements, Resource... contexts) {
    StringBuilder qb = new StringBuilder();
    qb.append("DELETE DATA \n");
    qb.append("{ \n");
    if (contexts.length > 0) {
        for (Resource context : contexts) {
            if (context != null) {
                String namedGraph = context.stringValue();
                if (context instanceof BNode) {
                    // SPARQL does not allow blank nodes as named graph
                    // identifiers, so we need to skolemize
                    // the blank node id.
                    namedGraph = "urn:nodeid:" + context.stringValue();
                }
                qb.append("    GRAPH <" + namedGraph + "> { \n");
            }
            createDataBody(qb, statements, true);
            if (context != null) {
                qb.append(" } \n");
            }
        }
    } else {
        createDataBody(qb, statements, false);
    }
    qb.append("}");
    return qb.toString();
}
Also used : BNode(org.eclipse.rdf4j.model.BNode) Resource(org.eclipse.rdf4j.model.Resource)

Example 23 with BNode

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

the class RDFJSONWriter method writeObject.

/**
 * Helper method to reduce complexity of the JSON serialisation algorithm Any null contexts will only be
 * serialised to JSON if there are also non-null contexts in the contexts array
 *
 * @param object
 *        The RDF value to serialise
 * @param contexts
 *        The set of contexts that are relevant to this object, including null contexts as they are found.
 * @param jg
 *        the {@link JsonGenerator} to write to.
 * @throws IOException
 * @throws JsonGenerationException
 * @throws JSONException
 */
public static void writeObject(final Value object, final Set<Resource> contexts, final JsonGenerator jg) throws JsonGenerationException, IOException {
    jg.writeStartObject();
    if (object instanceof Literal) {
        jg.writeObjectField(RDFJSONUtility.VALUE, object.stringValue());
        jg.writeObjectField(RDFJSONUtility.TYPE, RDFJSONUtility.LITERAL);
        final Literal l = (Literal) object;
        if (Literals.isLanguageLiteral(l)) {
            jg.writeObjectField(RDFJSONUtility.LANG, l.getLanguage().orElse(null));
        } else {
            jg.writeObjectField(RDFJSONUtility.DATATYPE, l.getDatatype().stringValue());
        }
    } else if (object instanceof BNode) {
        jg.writeObjectField(RDFJSONUtility.VALUE, resourceToString((BNode) object));
        jg.writeObjectField(RDFJSONUtility.TYPE, RDFJSONUtility.BNODE);
    } else if (object instanceof IRI) {
        jg.writeObjectField(RDFJSONUtility.VALUE, resourceToString((IRI) object));
        jg.writeObjectField(RDFJSONUtility.TYPE, RDFJSONUtility.URI);
    }
    if (contexts != null && !contexts.isEmpty() && !(contexts.size() == 1 && contexts.iterator().next() == null)) {
        jg.writeArrayFieldStart(RDFJSONUtility.GRAPHS);
        for (final Resource nextContext : contexts) {
            if (nextContext == null) {
                jg.writeNull();
            } else {
                jg.writeString(resourceToString(nextContext));
            }
        }
        jg.writeEndArray();
    }
    jg.writeEndObject();
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) BNode(org.eclipse.rdf4j.model.BNode) Literal(org.eclipse.rdf4j.model.Literal) Resource(org.eclipse.rdf4j.model.Resource)

Example 24 with BNode

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

the class TriGParserCustomTest method testGraphLocalNameNotGraph.

@Test
public void testGraphLocalNameNotGraph() throws Exception {
    Model model = Rio.parse(new StringReader("@prefix ex: <urn:> .\n ex: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());
}
Also used : BNode(org.eclipse.rdf4j.model.BNode) LinkedHashModel(org.eclipse.rdf4j.model.impl.LinkedHashModel) Model(org.eclipse.rdf4j.model.Model) StringReader(java.io.StringReader) Test(org.junit.Test)

Example 25 with BNode

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

the class TriGParserCustomTest method testSPARQLGraphKeyword.

@Test
public void testSPARQLGraphKeyword() throws Exception {
    Model model = Rio.parse(new StringReader("GRAPH <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());
}
Also used : BNode(org.eclipse.rdf4j.model.BNode) LinkedHashModel(org.eclipse.rdf4j.model.impl.LinkedHashModel) Model(org.eclipse.rdf4j.model.Model) StringReader(java.io.StringReader) Test(org.junit.Test)

Aggregations

BNode (org.eclipse.rdf4j.model.BNode)40 IRI (org.eclipse.rdf4j.model.IRI)16 Literal (org.eclipse.rdf4j.model.Literal)14 Test (org.junit.Test)12 Resource (org.eclipse.rdf4j.model.Resource)10 Model (org.eclipse.rdf4j.model.Model)9 Value (org.eclipse.rdf4j.model.Value)7 LinkedHashModel (org.eclipse.rdf4j.model.impl.LinkedHashModel)7 StringReader (java.io.StringReader)6 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)6 Statement (org.eclipse.rdf4j.model.Statement)5 Binding (org.eclipse.rdf4j.query.Binding)4 BindingSet (org.eclipse.rdf4j.query.BindingSet)4 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 ParsedIRI (org.eclipse.rdf4j.common.net.ParsedIRI)3 ValueFactory (org.eclipse.rdf4j.model.ValueFactory)3 SimpleValueFactory (org.eclipse.rdf4j.model.impl.SimpleValueFactory)3 InputStream (java.io.InputStream)2 ModelBuilder (org.eclipse.rdf4j.model.util.ModelBuilder)2