Virtuoso Open-Source Wiki
Virtuoso Open-Source, OpenLink Data Spaces, and OpenLink Ajax Toolkit
Advanced Search
Help?
Location: / Dashboard / Main / VirtTipsAndTricksGuide / VirtTipsAndTricksGuideDeleteTriplesWithBlanknodes

How Can I Delete Triples containing blank nodes?

There are two ways to delete a particular blank node:

  1. To refer to it via some properties or:
  2. To convert it to it's internal "serial number", a long integer, and back.

Assume the following sample scenario:

Important Note: IDs of bnodes will vary from server to server and even from run to run on the same server, so the application should identify bnodes by properties before doing bif:iri_id_XXX tricks.

  1. Clear the graph:

    SPARQL CLEAR GRAPH <http://sample/>; Done. -- 4 msec.

  2. Insert three blank nodes with two related triples each:

    SPARQL INSERT IN GRAPH <http://sample/> { [] <p> <o1a> , <o1b> . [] <p> <o2a> , <o2b> . [] <p> <o3a> , <o3b> } Done. -- 15 msec.

  3. Delete one pair of triples:

    SPARQL WITH <http://sample/> DELETE { ?s ?p ?o } WHERE { ?s ?p ?o ; <p> <o1a> . } Done. -- 7 msec.

  4. Ensure that we still have two bnodes, two triple per bnode:

    SPARQL SELECT * FROM <http://sample/> WHERE { ?s ?p ?o } s p o VARCHAR VARCHAR VARCHAR ________________ nodeID://b10006 p o3a nodeID://b10006 p o3b nodeID://b10007 p o2a nodeID://b10007 p o2b 4 Rows. -- 4 msec.

  5. Each bnode, as well as any "named" node, is identified internally as an integer:

    SPARQL SELECT (<LONG::bif:iri_id_num>(?s)) AS ?s_num, ?p, ?o FROM <http://sample/> WHERE { ?s ?p ?o }; s_num p o INTEGER VARCHAR VARCHAR _____________________________ 4611686018427397910 p o3a 4611686018427397910 p o3b 4611686018427397911 p o2a 4611686018427397911 p o2b 4 Rows. -- 5 msec.

  6. The integer can be converted back to internal identifier. Say, here we try to delete a triple that does not exist (even if the ID integer is valid):

    SPARQL DELETE FROM <http://sample/> { `bif:iri_id_from_num(4611686018427397911)` <p> <o3a> }; Done. -- 5 msec.

  7. Should have no effect, because the "46..11" IRI has <o2a> and <o2b>, and was not requested <o3a>:

    SPARQL SELECT * FROM <http://sample/> WHERE { ?s ?p ?o }; s p o VARCHAR VARCHAR VARCHAR ________________ nodeID://b10006 p o3a nodeID://b10006 p o3b nodeID://b10007 p o2a nodeID://b10007 p o2b 4 Rows. -- 5 msec.

  8. Now let's try to delete a triple that does actually exist. Note the use of backquotes to insert an expression into template:

    SPARQL DELETE FROM <http://sample/> { `bif:iri_id_from_num(4611686018427397911)` <p> <o2a> }; Done. -- 4 msec.

  9. So there's an effect:

    SPARQL SELECT * FROM <http://sample/> WHERE { ?s ?p ?o }; s p o VARCHAR VARCHAR VARCHAR _________________ nodeID://b10006 p o3a nodeID://b10006 p o3b nodeID://b10007 p o2b 3 Rows. -- 2 msec.

  10. Now delete everything related to nodeID://b10006 subject:

    SPARQL WITH <http://sample/> DELETE { ?s ?p ?o } WHERE { ?s ?p ?o . FILTER (?s = bif:iri_id_from_num(4611686018427397910)) }; Done. -- 18 msec.

  11. Three minus two gives one triple remaining:

    SQL> SPARQL SELECT * FROM <http://sample/> WHERE { ?s ?p ?o }; s p o VARCHAR VARCHAR VARCHAR _________________ nodeID://b10007 p o2b 1 Rows. -- 4 msec.

Related

Powered By Virtuoso