SPARQL: Delete instance and all of its properties with linked subproperties

守給你的承諾、 提交于 2019-12-02 07:22:24

问题


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:


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



来源:https://stackoverflow.com/questions/33129752/sparql-delete-instance-and-all-of-its-properties-with-linked-subproperties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!