This content has been marked as final.
Show 4 replies
-
1. Re: Deleting Triples from a Model
alwu-Oracle Jan 17, 2012 8:16 PM (in response to 900576)Hi MichaelB,
You are not supposed to use wild card with graph.delete(Triple t) API. Jena's in memory graph does not support it. Oracle's graph does not support it either.
One way to remove a subset of triples is to find them and remove them iteratively.
Here is an example.
Oracle oracle = new Oracle(jdbcUrl, user, password);
Model model = ModelOracleSem.createInstance(oracle, modelName);
Node nodeSubject = Node.createURI("http://my.com/s1");
Node nodePredicate = Node.createURI("http://my.com/hasAlias");
model.getGraph().add(Triple.create(nodeSubject, nodePredicate,
Node.createLiteral("John")));
model.getGraph().add(Triple.create(nodeSubject, nodePredicate,
Node.createLiteral("Johnny")));
model.getGraph().add(Triple.create(nodeSubject,
Node.createURI("http://my.com/hasFriend"),
Node.createLiteral("Mary")));
{
// try out in-memory Jena model
Model modelInMem = ModelFactory.createDefaultModel();
modelInMem.add(model);
psOut.println("size of the in memory model " + model.getGraph().size());
modelInMem.getGraph().delete(Triple.create(nodeSubject, nodePredicate, Node.ANY));
psOut.println("size of the in memory model after deletion " + model.getGraph().size());
modelInMem.close();
}
psOut.println("size of the Oracle model " + model.getGraph().size());
ExtendedIterator<Triple> results = model.getGraph().find(nodeSubject, nodePredicate, null);
while (results.hasNext()) {
Triple t = results.next();
model.getGraph().delete(t);
}
psOut.println("size of the Oracle model after delete " + model.getGraph().size());
// After deletion
results = GraphUtil.findAll(model.getGraph());
while (results.hasNext()) {
psOut.println("triples after deletion: " + results.next());
}
model.close();
oracle.dispose();
Output of the above code:
size of the in memory model 3
size of the in memory model after deletion 3
size of the Oracle model 3
size of the Oracle model after delete 1
triples after deletion: http://my.com/s1 @http://my.com/hasFriend "Mary"
Hope it helps,
Zhe -
2. Re: Deleting Triples from a Model
alwu-Oracle Jan 17, 2012 9:01 PM (in response to 900576)Hi,
Sorry I missed the SPARQL Update question in your original post. Here is how one can use SPARQL Update.
Please check Section 7.8 of http://docs.oracle.com/cd/E11882_01/appdev.112/e11828/sem_jena.htm for
details.
Continue from my previous code snippet.
...
...
psOut.println("size of the Oracle model " + model.getGraph().size());
((GraphOracleSem) (model.getGraph())).commitTransaction();
ExtendedIterator<Triple> results = model.getGraph().find(nodeSubject, nodePredicate, null);
while (results.hasNext()) {
Triple t = results.next();
model.getGraph().delete(t);
}
psOut.println("size of the Oracle model after delete " + model.getGraph().size());
// After deletion
results = GraphUtil.findAll(model.getGraph());
while (results.hasNext()) {
psOut.println("triples after deletion: " + results.next());
}
// Undo the deletes so that we can try another approach.
//
((GraphOracleSem) (model.getGraph())).rollbackTransaction();
psOut.println("size of the Oracle model " + model.getGraph().size());
// Now use SPARQL Update
UpdateAction.parseExecute(
"DELETE { <http://my.com/s1> <http://my.com/hasAlias> ?o } " +
" WHERE { <http://my.com/s1> <http://my.com/hasAlias> ?o } ",
model);
psOut.println("size of the Oracle model after SPARQL UPDATE " + model.getGraph().size());
model.close();
oracle.dispose();
==> Output is
size of the in memory model 3
size of the in memory model after deletion 3
size of the Oracle model 3
size of the Oracle model after delete 1
triples after deletion: http://my.com/s1 @http://my.com/hasFriend "Mary"
size of the Oracle model 3
size of the Oracle model after SPARQL UPDATE 1
Thanks,
Zhe Wu -
3. Re: Deleting Triples from a Model
900576 Jan 18, 2012 3:04 PM (in response to alwu-Oracle)I am going to use
graph.getBulkUpdateHandler().delete(graph.find(null, null, o));
and
graph.getBulkUpdateHandler().delete(graph.find(null, p, o));
to detele the triples that match the s, p, o combination.
Is this better than iterating over the ExtendedIterator and calling graph.delete(Triple)?
Thanks. -
4. Re: Deleting Triples from a Model
alwu-Oracle Jan 18, 2012 4:56 PM (in response to 900576)Hi,
These two approaches are very, very similar. The only difference is OracleBulkUpdateHandler's logic will perform a little bit of buffering. I don't think the difference has a significant impact on performance.
Thanks,
Zhe Wu