use of org.eclipse.rdf4j.model.BNode in project rdf4j by eclipse.
the class TriGParserCustomTest method testGraph.
@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());
}
use of org.eclipse.rdf4j.model.BNode in project rdf4j by eclipse.
the class TransactionWriter method serialize.
protected void serialize(SPARQLUpdateOperation op, XMLWriter xmlWriter) throws IOException {
String baseURI = op.getBaseURI();
if (baseURI != null) {
xmlWriter.setAttribute(TransactionXMLConstants.BASE_URI_ATT, baseURI);
}
xmlWriter.setAttribute(TransactionXMLConstants.INCLUDE_INFERRED_ATT, op.isIncludeInferred());
xmlWriter.startTag(TransactionXMLConstants.SPARQL_UPDATE_TAG);
// serialize update string
String updateString = op.getUpdateString();
xmlWriter.textElement(TransactionXMLConstants.UPDATE_STRING_TAG, updateString);
// serialize dataset definition (if any)
Dataset dataset = op.getDataset();
if (dataset != null) {
xmlWriter.startTag(TransactionXMLConstants.DATASET_TAG);
xmlWriter.startTag(TransactionXMLConstants.DEFAULT_GRAPHS_TAG);
for (IRI defaultGraph : dataset.getDefaultGraphs()) {
xmlWriter.textElement(TransactionXMLConstants.GRAPH_TAG, defaultGraph.stringValue());
}
xmlWriter.endTag(TransactionXMLConstants.DEFAULT_GRAPHS_TAG);
xmlWriter.startTag(TransactionXMLConstants.NAMED_GRAPHS_TAG);
for (IRI namedGraph : dataset.getNamedGraphs()) {
xmlWriter.textElement(TransactionXMLConstants.GRAPH_TAG, namedGraph.stringValue());
}
xmlWriter.endTag(TransactionXMLConstants.NAMED_GRAPHS_TAG);
xmlWriter.startTag(TransactionXMLConstants.DEFAULT_REMOVE_GRAPHS_TAG);
for (IRI defaultRemoveGraph : dataset.getDefaultRemoveGraphs()) {
xmlWriter.textElement(TransactionXMLConstants.GRAPH_TAG, defaultRemoveGraph.stringValue());
}
xmlWriter.endTag(TransactionXMLConstants.DEFAULT_REMOVE_GRAPHS_TAG);
if (dataset.getDefaultInsertGraph() != null) {
xmlWriter.textElement(TransactionXMLConstants.DEFAULT_INSERT_GRAPH, dataset.getDefaultInsertGraph().stringValue());
}
xmlWriter.endTag(TransactionXMLConstants.DATASET_TAG);
}
if (op.getBindings() != null && op.getBindings().length > 0) {
xmlWriter.startTag(TransactionXMLConstants.BINDINGS);
for (Binding binding : op.getBindings()) {
if (binding.getName() != null && binding.getValue() != null && binding.getValue().stringValue() != null) {
if (binding.getValue() instanceof IRI) {
xmlWriter.setAttribute(TransactionXMLConstants.NAME_ATT, binding.getName());
xmlWriter.textElement(TransactionXMLConstants.BINDING_URI, binding.getValue().stringValue());
}
if (binding.getValue() instanceof BNode) {
xmlWriter.setAttribute(TransactionXMLConstants.NAME_ATT, binding.getName());
xmlWriter.textElement(TransactionXMLConstants.BINDING_BNODE, binding.getValue().stringValue());
}
if (binding.getValue() instanceof Literal) {
xmlWriter.setAttribute(TransactionXMLConstants.NAME_ATT, binding.getName());
Literal literal = (Literal) binding.getValue();
if (Literals.isLanguageLiteral(literal)) {
xmlWriter.setAttribute(TransactionXMLConstants.LANGUAGE_ATT, literal.getLanguage().get());
} else {
xmlWriter.setAttribute(TransactionXMLConstants.DATA_TYPE_ATT, literal.getDatatype().stringValue());
}
xmlWriter.textElement(TransactionXMLConstants.BINDING_LITERAL, binding.getValue().stringValue());
}
}
}
xmlWriter.endTag(TransactionXMLConstants.BINDINGS);
}
xmlWriter.endTag(TransactionXMLConstants.SPARQL_UPDATE_TAG);
}
use of org.eclipse.rdf4j.model.BNode in project rdf4j by eclipse.
the class SPARQLConnection method createDataBody.
private void createDataBody(StringBuilder qb, Iterable<? extends Statement> statements, boolean ignoreContext) {
for (Statement st : statements) {
final Resource context = st.getContext();
if (!ignoreContext) {
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");
}
}
if (st.getSubject() instanceof BNode) {
qb.append("_:" + st.getSubject().stringValue() + " ");
} else {
qb.append("<" + st.getSubject().stringValue() + "> ");
}
qb.append("<" + st.getPredicate().stringValue() + "> ");
if (st.getObject() instanceof Literal) {
Literal lit = (Literal) st.getObject();
qb.append("\"");
qb.append(SPARQLUtil.encodeString(lit.getLabel()));
qb.append("\"");
if (Literals.isLanguageLiteral(lit)) {
qb.append("@");
qb.append(lit.getLanguage().get());
} else {
qb.append("^^<" + lit.getDatatype().stringValue() + ">");
}
qb.append(" ");
} else if (st.getObject() instanceof BNode) {
qb.append("_:" + st.getObject().stringValue() + " ");
} else {
qb.append("<" + st.getObject().stringValue() + "> ");
}
qb.append(". \n");
if (!ignoreContext && context != null) {
qb.append(" }\n");
}
}
}
use of org.eclipse.rdf4j.model.BNode in project rdf4j by eclipse.
the class SPARQLConnection method createDeletePatternCommand.
private String createDeletePatternCommand(Resource subject, IRI predicate, Value object, Resource[] contexts) {
StringBuilder qb = new StringBuilder();
qb.append("DELETE WHERE \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");
}
createBGP(qb, subject, predicate, object);
if (context != null && context instanceof IRI) {
qb.append(" } \n");
}
}
} else {
createBGP(qb, subject, predicate, object);
}
qb.append("}");
return qb.toString();
}
use of org.eclipse.rdf4j.model.BNode in project rdf4j by eclipse.
the class TupleExprs method getConstVarName.
public static String getConstVarName(Value value) {
if (value == null) {
throw new IllegalArgumentException("value can not be null");
}
// We use toHexString to get a more compact stringrep.
String uniqueStringForValue = Integer.toHexString(value.stringValue().hashCode());
if (value instanceof Literal) {
uniqueStringForValue += "_lit";
// we need to append datatype and/or language tag to ensure a unique
// var name (see SES-1927)
Literal lit = (Literal) value;
if (lit.getDatatype() != null) {
uniqueStringForValue += "_" + Integer.toHexString(lit.getDatatype().hashCode());
}
if (lit.getLanguage() != null) {
uniqueStringForValue += "_" + Integer.toHexString(lit.getLanguage().hashCode());
}
} else if (value instanceof BNode) {
uniqueStringForValue += "_node";
} else {
uniqueStringForValue += "_uri";
}
return "_const_" + uniqueStringForValue;
}
Aggregations