4

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.

Stanislav Kralin
  • 11,070
  • 4
  • 35
  • 58
Veni_Vidi_Vici
  • 291
  • 1
  • 6
  • 16

1 Answers1

5

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.

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
user205512
  • 8,798
  • 29
  • 28
  • I have the other way around - i have `_:A68d23cacX3aX13f793fa898X3aXX2dX7ffd ` and I need to remove all its triples - how to do that ? – Antoniossss Sep 09 '20 at 08:28
  • `model.removeAll(res, null, null)` (when res is your bnode). However you can also use `res.removeProperties()`, which does the same thing (assuming your resource is from that model). – user205512 Sep 10 '20 at 15:27
  • The problem is that `ResourceFactory.createResource("_:A68d23cacX3aX13f793fa898X3aXX2dX7ffd")` and `A68d23cacX3aX13f793fa898X3aXX2dX7ffd` didnt work for me and `model.find(res,null,null)` was returning empty iterator. That was my main problem - how to match `bnode` as I would say it should match as mentioned. – Antoniossss Sep 10 '20 at 15:52
  • Ah, I see. Try `Resource res = model.createResource(new AnonId("A68d23cacX3aX13f793fa898X3aXX2dX7ffd"));` – user205512 Sep 10 '20 at 17:10
  • Interesting, I didnt try that one.Thanks in advance – Antoniossss Sep 10 '20 at 17:13