3

I have a question about the deletion of elements from a triplestore (in my case a Virtuoso) using SPARQL. I have stored the following elements in a graph:

@prefix xy: <http://purl.oclc.org/xy/xy#> .
@prefix ssn: <http://purl.oclc.org/NET/ssnx/ssn#> .

<point> a xy:Point ;
    xy:value "10" ;
    ssn:observationResultTime <Rs_b8d4ae44-6083-4140-b4e3-11fcf38a53c8> ;
    ssn:observationSamplingTime <St_b8d4ae44-6083-4140-b4e3-11fcf38a53c8> ;
    ssn:observedBy <SensorID-b8d4ae44-6083-4140-b4e3-11fcf38a53c8> .

As you can see I have one xy:Point, which has some properties. In my database I have stored dozens of these points. Now my question: How to delete one point and all of its properties (even the possibly linked subproperties of observationSamplingTime, observationResultTime)? Is there any simple solution? By now I am deleting the point and its properties by giving all exact relations like:

@prefix xy: <http://purl.oclc.org/xy/xy#> .
@prefix ssn: <http://purl.oclc.org/NET/ssnx/ssn#> 

delete {
   ?observation a xy:Point .
   ?observation xy:value ?value .
   ?observation ssn:observationResultTime ?resultTime .
   ?observation ssn:observationSamplingTime ?samplingTime .
   ?observation ssn:observedBy ?sensor .
}
WHERE {
   ?observation xy:value ?value .
   ?observation ssn:observationResultTime ?resultTime .
   ?observation ssn:observationSamplingTime ?samplingTime .
   ?observation ssn:observedBy ?sensor .
}

What I would like to do is "Delete ?observation a xy:Point and all ob its subproperties". Is there any possibility to do that?

Thanks and kind regards

tanktoo

tanktoo
  • 181
  • 12

1 Answers1

5

(even the possibly linked subproperties of observationSamplingTime, observationResultTime)?

Note that something like this pretty dangerous, since you might delete from triples from a context that you're not expecting to. E.g., suppose you had something like

:pointX :hasTime :time1 ;
        :hasValue :valueX .

:pointY :hasTime :time1 ;
        :hasValue :valueY .

:time1 :hasLabel "time1" .

If you "delete" :pointX, and recursively delete :time1, then you lose information that was important for :pointY as well. Remember that a triple store stores sets of triples. Things only exist by virtue of being a subject, predicate, or object.

At any rate, what you're trying to do isn't too hard. You can just do:

delete { ?s ?p ?o }
where {
  :thing (<>|!<>)* ?s . 
  ?s ?p ?o .
}

(<>|!<>)* is a wildcard path, so ?s gets bound to anything reachable from :thing, including :thing itself. ?p and ?o are just the property and object of ?s. For more information about the wildcard, see SPARQL: is there any path between two nodes?.

Community
  • 1
  • 1
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
  • Hi, thanks for your answer. I have tried it but it works not completely. If "time1" has sub-properties they are not deleted. How could I delete them too? In my triplestore there should be no case where something linked is used by another point. If I have properties that are used by more than one point, they are stored in a different graph. – tanktoo Oct 15 '15 at 08:14
  • I'm not sure what you mean by `"time1"`. "time1" is a string, which is a literal, and can't be the subject of at triple. You can't have, e.g., `"time1" :property :value"`. – Joshua Taylor Oct 15 '15 at 11:20
  • If I use your command the triples "pointY hasTime time1" and "pointY hasvalue valueY" are deleted. The triple "time1 hasLabel time1" still exists. I want to delete this too. – tanktoo Oct 15 '15 at 12:31
  • I'm sorry, there was a typo in my query, it should have been `(<>|!<>)` (exclamation point, not caret). I'll update the answer – Joshua Taylor Oct 15 '15 at 21:26
  • Thanks! This solution is working in my test environment with JENA DB and Fuseki but not within Virtuoso 7. Both stores have the same graph but on Virtuoso the query returns an empty result... Don't know if this another serious Virtuoso bug. – tanktoo Oct 16 '15 at 10:07