Search in sources :

Example 26 with BNode

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());
}
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 27 with BNode

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);
}
Also used : Binding(org.eclipse.rdf4j.query.Binding) IRI(org.eclipse.rdf4j.model.IRI) BNode(org.eclipse.rdf4j.model.BNode) Dataset(org.eclipse.rdf4j.query.Dataset) Literal(org.eclipse.rdf4j.model.Literal)

Example 28 with BNode

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");
        }
    }
}
Also used : BNode(org.eclipse.rdf4j.model.BNode) Statement(org.eclipse.rdf4j.model.Statement) Literal(org.eclipse.rdf4j.model.Literal) Resource(org.eclipse.rdf4j.model.Resource)

Example 29 with BNode

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();
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) BNode(org.eclipse.rdf4j.model.BNode) Resource(org.eclipse.rdf4j.model.Resource)

Example 30 with BNode

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;
}
Also used : BNode(org.eclipse.rdf4j.model.BNode) Literal(org.eclipse.rdf4j.model.Literal)

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