Figuring out the right SPARQL query - sparql

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> }
}

Related

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.

Sparql delete entity and all its linked elements

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> .
}
}

SPARQL different operator

SPARQL Query
I have some SPARQL query shown below:
SELECT DISTINCT ?name1
WHERE {
GRAPH <blabla>
{
?k swrc:author ?x .
?x foaf:name ?name1 .
} .
GRAPH <blabla2>
{
?l swrc:author ?y .
?y foaf:name ?name2 .
} .
FILTER(?x != ?y) .
}
I want to get the names that exist only in the first graph blabla.
Problem
Counter intuitively I get some names that actually belong to the intersection. This happens because b (of set A) = b (of set B)?
Question
What are exactly the semantics of != ? How can I surpass this problem?
The semantics of != are exactly that its left argument is not equal to its right argument. But a FILTER is evaluated for every possible combination of values - so the query as you formulated it will return all name-values of ?x for which some value of ?y is not equal to it.
If you want to get back only name-values of ?x for which all values of ?y are not equal to it, you should be using a NOT EXISTS clause:
SELECT DISTINCT ?name1
WHERE {
GRAPH <blabla>
{
?k swrc:author ?x.
?x foaf:name ?name1.
}
FILTER NOT EXISTS {
GRAPH <blabla2>
{
?l swrc:author ?x.
}
}
}
Note that using this approach you can actually get rid of variable ?y altogether: you change the condition to just check that author ?x as found in the first graph does not also occur in the second graph.

Query to display all classes and all predicates of DBpedia

How to display all classes and all predicates of DBpedia using sparql in dbpedia.org/snorql
How about:
SELECT * { ?x a owl:Class }
and:
SELECT * { ?x a rdf:Property }
I don't know if these are all classes/properties.