Question

I have been trying to delete a triple from a model using Jena with no success. Things work well when the subject, predicate and object are URIs or literals, but for anonymous nodes it doesn't seem to work. For example, consider this triple in a model:

_:A68d23cacX3aX13f793fa898X3aXX2dX7ffd <http://www.w3.org/1999/02/22-rdf-syntax-ns#value> "class" .

I would like to delete it using:

Node nodeSubject = Node.createAnon(); //or Node.ANY
Node nodePredicate = Node.createURI("http://www.w3.org/1999/02/22-rdf-syntax-ns#value");
Node nodeObject = Node.createLiteral("class");
Triple triple = Triple.create(nodeSubject, nodePredicate,  nodeObject);
inMemModel.getGraph().delete(triple);

I can't delete the triple regardless if I use createAnon or Node.ANY. I wouldn't want to use a AnonId just cause if I run my code on another machine I doubt that the same anonymous id will be generated.

Was it helpful?

Solution

Simple answer:

inMemModel.removeAll(null, RDF.value, ResourceFactory.createPlainLiteral("class"));

That will remove all triples where the predicate is rdf:value and object is "class".

Internally — at the SPI level you were trying — you could have used inMemModel.remove(Node.ANY, nodePredicate, nodeObject), which finds and deletes (using delete) matching triples. delete takes a ground triple and hence doesn't do a find.

createAnon() doesn't work simply because it's a different subject, so there is nothing to delete.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top