Search in sources :

Example 26 with Resource

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

the class TransactionReaderTest method testRoundtrip.

@Test
public void testRoundtrip() throws Exception {
    AddStatementOperation operation = new AddStatementOperation(bob, knows, alice, context1, context2);
    List<TransactionOperation> txn = new ArrayList<TransactionOperation>();
    txn.add(operation);
    ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
    TransactionWriter w = new TransactionWriter();
    w.serialize(txn, out);
    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    TransactionReader r = new TransactionReader();
    Collection<TransactionOperation> parsedTxn = r.parse(in);
    assertNotNull(parsedTxn);
    for (TransactionOperation op : parsedTxn) {
        assertTrue(op instanceof AddStatementOperation);
        AddStatementOperation addOp = (AddStatementOperation) op;
        Resource[] contexts = addOp.getContexts();
        assertEquals(2, contexts.length);
        assertTrue(contexts[0].equals(context1) || contexts[1].equals(context1));
        assertTrue(contexts[0].equals(context2) || contexts[1].equals(context2));
    }
}
Also used : AddStatementOperation(org.eclipse.rdf4j.http.protocol.transaction.operations.AddStatementOperation) TransactionOperation(org.eclipse.rdf4j.http.protocol.transaction.operations.TransactionOperation) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) Resource(org.eclipse.rdf4j.model.Resource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 27 with Resource

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

the class LinkedHashModel method find.

private Iterator find(Statement st) {
    Resource subj = st.getSubject();
    IRI pred = st.getPredicate();
    Value obj = st.getObject();
    Resource ctx = st.getContext();
    return matchPattern(subj, pred, obj, ctx);
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) Resource(org.eclipse.rdf4j.model.Resource) Value(org.eclipse.rdf4j.model.Value)

Example 28 with Resource

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

the class LinkedHashModel method writeObject.

private void writeObject(ObjectOutputStream s) throws IOException {
    // Write out any hidden serialization magic
    s.defaultWriteObject();
    // Write in size
    s.writeInt(statements.size());
    // Write in all elements
    for (ModelStatement st : statements) {
        Resource subj = st.getSubject();
        IRI pred = st.getPredicate();
        Value obj = st.getObject();
        Resource ctx = st.getContext();
        s.writeObject(new ContextStatement(subj, pred, obj, ctx));
    }
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) Resource(org.eclipse.rdf4j.model.Resource) Value(org.eclipse.rdf4j.model.Value)

Example 29 with Resource

use of org.eclipse.rdf4j.model.Resource 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 30 with Resource

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

the class SPARQLConnection method clear.

public void clear(Resource... contexts) throws RepositoryException {
    OpenRDFUtil.verifyContextNotNull(contexts);
    boolean localTransaction = startLocalTransaction();
    if (contexts.length == 0) {
        sparqlTransaction.append("CLEAR ALL ");
        sparqlTransaction.append("; ");
    } else {
        for (Resource context : contexts) {
            if (context == null) {
                sparqlTransaction.append("CLEAR DEFAULT ");
                sparqlTransaction.append("; ");
            } else if (context instanceof IRI) {
                sparqlTransaction.append("CLEAR GRAPH <" + context.stringValue() + "> ");
                sparqlTransaction.append("; ");
            } else {
                throw new RepositoryException("SPARQL does not support named graphs identified by blank nodes.");
            }
        }
    }
    try {
        conditionalCommit(localTransaction);
    } catch (RepositoryException e) {
        conditionalRollback(localTransaction);
        throw e;
    }
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) Resource(org.eclipse.rdf4j.model.Resource) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException)

Aggregations

Resource (org.eclipse.rdf4j.model.Resource)90 IRI (org.eclipse.rdf4j.model.IRI)37 Value (org.eclipse.rdf4j.model.Value)30 Test (org.junit.Test)16 Statement (org.eclipse.rdf4j.model.Statement)15 Model (org.eclipse.rdf4j.model.Model)12 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)12 BNode (org.eclipse.rdf4j.model.BNode)11 IOException (java.io.IOException)9 Literal (org.eclipse.rdf4j.model.Literal)9 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)7 StringWriter (java.io.StringWriter)6 ParsedIRI (org.eclipse.rdf4j.common.net.ParsedIRI)6 ValueFactory (org.eclipse.rdf4j.model.ValueFactory)6 SimpleValueFactory (org.eclipse.rdf4j.model.impl.SimpleValueFactory)6 TreeModel (org.eclipse.rdf4j.model.impl.TreeModel)6 RepositoryConnection (org.eclipse.rdf4j.repository.RepositoryConnection)6 RDFWriter (org.eclipse.rdf4j.rio.RDFWriter)6 LinkedHashModel (org.eclipse.rdf4j.model.impl.LinkedHashModel)5 ArrayList (java.util.ArrayList)4