SPARQL: Delete instance and all of its properties

2019-03-01 08:08发布

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

1条回答
冷血范
2楼-- · 2019-03-01 08:53

(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?.

查看更多
登录 后发表回答