What does !a mean in SPARQL? - 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.

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

how to find classes from dbpedia?

I need a sparql query that given a free text (user input),
it finds me from dbpedia all the classes related to it.
How do it?
Also asked here. Accepted answer said --
When you say classes, are you mean about types? If yes, try something like
SELECT ?uri ?label ?type
WHERE {
?uri rdfs:label ?label .
?uri <http://dbpedia.org/ontology/type> ?type .
FILTER regex(str(?label), "Leipzig") .
}
limit 10
I couldn't let this go...
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX virtdrf: <http://www.openlinksw.com/schemas/virtrdf#>
SELECT ?s1c AS ?c1
COUNT (*) AS ?c2
?c3
WHERE
{
QUAD MAP virtrdf:DefaultQuadMap
{
GRAPH ?g
{
?s1 ?s1textp ?o1 .
?o1 bif:contains '"dbpedia"' .
}
}
?s1 a ?s1c .
OPTIONAL { ?s1c rdfs:label ?c3
FILTER(langMatches(LANG(?c3),"EN"))}
}
GROUP BY ?s1c ?c3
ORDER BY DESC (2) ASC (3)
The earlier answer gets you partial results.

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

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

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.