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();
}
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();
}
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();
}
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());
}
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());
}
Aggregations