Sparql delete entity and all its linked elements - sparql

I have a Fuseki DB with triples like the following:
<https://tomcat.antudo.it/data/bucket.json-WSP1WS8#row=1>
<https://tomcat.antudo.it/data/bucket.json-WSP1WS8#ws7col0>
"ws6dat1aa"
...
<https://tomcat.antudo.it/data/bucket.json-WSP1WS8#row=2>
<https://tomcat.antudo.it/data/bucket.json-WSP1WS8#ws6col2>
"ws6dat2aa"
...
<http://www.w3.org/2002/07/owl#bottomDataProperty>
<http://www.w3.org/2002/07/owl#propertyDisjointWith>
<https://tomcat.antudo.it/data/bucket.json-WSP1WS8#ws7col0>
I want to delete everything related to:
<https://tomcat.antudo.it/data/bucket.json-WSP1WS8#row=1>
The problem is that i'm not able to write a query that also target triples like that one:
<http://www.w3.org/2002/07/owl#bottomDataProperty>
<http://www.w3.org/2002/07/owl#propertyDisjointWith>
<https://tomcat.antudo.it/data/bucket.json-WSP1WS8#ws7col0>
I have used the following query to delete triples about a list of entities:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
DELETE {
?x ?y ?z
}
WHERE {
{
?x ?y ?z.
?x rdfs:seeAlso <https://tomcat.antudo.it/data/#dcat_test.csv-WSP1WS6>.
}
union
{
?x ?y ?z.
?x rdfs:seeAlso <https://tomcat.antudo.it/data/#dcat_test.csv-WSP1WS7>.
}
}
I think i have to do something similar to that:
SPARQL: Delete instance and all of its properties with linked subproperties but i'm stuck. Any help will be appreciated

The following should work if you want to delete the resource entirely from the graph. Note that you need to remove the triples "both ways".
DELETE {
<https://tomcat.antudo.it/data/bucket.json-WSP1WS8#row=1> ?p ?o .
?s1 ?p1 <https://tomcat.antudo.it/data/bucket.json-WSP1WS8#row=1> .
}
WHERE {
<https://tomcat.antudo.it/data/bucket.json-WSP1WS8#row=1> ?p ?o .
OPTIONAL {
?s1 ?p1 <https://tomcat.antudo.it/data/bucket.json-WSP1WS8#row=1> .
}
}

Related

The mechanism of "FILTER NOT EXISTS" in SPARQL

Assuming the triples are following:
#prefix : <http://example/> .
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
#prefix foaf: <http://xmlns.com/foaf/0.1/> .
:alice rdf:type foaf:Person .
:alice foaf:name "Alice" .
:bob rdf:type foaf:Person .
and then we perform 3 queries based on SPARQL 1.1:
Q1:
SELECT ?s
WHERE
{
?s ?p ?o .
FILTER NOT EXISTS { ?s foaf:name ?y }
}
Q2:
SELECT ?s
WHERE
{
?s ?p ?o .
FILTER NOT EXISTS { ?x foaf:name ?y }
}
Q3:
SELECT ?s
WHERE
{
?s ?p ?o .
FILTER NOT EXISTS { ?x foaf:mailbox ?y }
}
These three queries return three different solutions. Could anyone help me figure out why Q2 evaluates to no query solution in contrast to Q1 and Q3? Many thanks in advance :)
Q2 returns no solution because in your data, there exists a statement that matches ?x foaf:name ?y: ?x = :alice and ?y = "Alice". You've put no further constraints on either ?x or ?y. So no matter what the other variables in your query (?s, ?p and ?o) are bound to, the NOT EXISTS condition will always fail and therefore the query returns no result.

SPARQL - FILTER NOT EXISTS vs MINUS

Could anyone please explain to me the difference between FILTER NOT EXISTS and MINUS in sparql. Specifically, when they don't contain WHERE variables in their body.
For example why does the following query returns anything?
SELECT * {
?s ?p ?o .
FILTER NOT EXISTS { ?x ?y ?z } .
}
and why does the following query returns everything?
SELECT * {
?s ?p ?o .
MINUS { ?x ?y ?z } .
}
Thanks in advance

What does !a mean in SPARQL?

I'm using SPARQL to construct a query and would like to rule out certain results.
I'm aware FILTER NOT EXISTS can be used to do the task. For example:
SELECT * {
?sub a ?type .
FILTER NOT EXISTS {?sub a :NotExpectedType.}
}
But during exploration, I accidentally found I could write !a. That's a valid SPARQL query, but the result is different from FILTER NOT EXISTS. For example:
SELECT * {
?sub a ?type .
?sub !a :NotExpectedType .
}
So what does the !a mean? (I'm aware a is a shortcut for rdf:type, but that doesn't help me understanding yet.)
!a is a property path, and it means “a path of length one with a property other than rdf:type”.
SELECT ?x {
?x a ?type.
FILTER NOT EXISTS { ?x a :MyClass }
}
The query above will find the ?x where no triple ?x rdf:type :MyClass exists.
SELECT ?x {
?x a ?type.
?x !a :MyClass.
}
This query will find the ?x where a triple ?x ?property :MyClass exists for some ?property other than rdf:type.

semantic web request result difference

PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT * WHERE {
?x foaf:primaryTopic ?n .
?x rdf:type ?z .
FILTER (regex(?x, '^http://en.wikipedia.org/wiki/Barack_Obama$')).
}
LIMIT 10
when i call this request from http://dbpedia.org/snorql, it works. but when i try below
SELECT * WHERE {
?x foaf:primaryTopic ?n .
?x rdf:type ?z .
?x owl:activeYearsStartDate ?v .
FILTER (regex(?x, '^http://en.wikipedia.org/wiki/Barack_Obama$')).
}
it doesn't work. but why?
I expect that you meant
?x dbpedia-owl:activeYearsStartDate ?v .
rather than
?x owl:activeYearsStartDate ?v .
Also, using regex here is more expensive than it needs to be. You could check whether the string value of a URI is something in particular with
filter( str(?x) = "…" )
but even that is a lot more work than you need. If you want ?x to be <http://en.wikipedia.org/wiki/Barack_Obama>, just use it instead of ?x, or bind ?x to it with values:
SELECT * WHERE {
<http://en.wikipedia.org/wiki/Barack_Obama> foaf:primaryTopic ?n .
<http://en.wikipedia.org/wiki/Barack_Obama> rdf:type ?z .
<http://en.wikipedia.org/wiki/Barack_Obama> owl:activeYearsStartDate ?v .
}
SELECT * WHERE {
values ?x { <http://en.wikipedia.org/wiki/Barack_Obama> }
?x foaf:primaryTopic ?n .
?x rdf:type ?z .
?x owl:activeYearsStartDate ?v .
}
That still looks suspicious, though. Are you sure you didn't want <http://dbpedia.org/resource/Barack_Obama>? And the use of foaf:primaryTopic stuff really makes me think that you want something more like:
select * {
?x foaf:isPrimaryTopicOf <http://en.wikipedia.org/wiki/Barack_Obama> ;
a ?type ;
dbpedia-owl:activeYearsStartDate ?v
}
limit 10
SPARQL results

Figuring out the right SPARQL query

I am trying to somehow find all the owl:sameAs properties from a resource link. A simple query would be like
SELECT ?x WHERE {
<http://dbpedia.org/resource/Tetris> owl:sameAs ?x
}
However i also would like to get the Yago link mentioned as is owl:sameAs of. Could any one help me out how to do this ?
You can get the YAGO link like so:
SELECT ?x WHERE {
?x owl:sameAs <http://dbpedia.org/resource/Tetris>
}
Or get both the “incoming” and “outgoing” links in one query:
SELECT ?x WHERE {
{ <http://dbpedia.org/resource/Tetris> owl:sameAs ?x }
UNION
{ ?x owl:sameAs <http://dbpedia.org/resource/Tetris> }
}