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