The mechanism of "FILTER NOT EXISTS" in SPARQL - 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.

Related

SPARQL CONSTRUCT subClassOf

i want to create a construct query with triples of subjects, that has a certain subclass.
That works fine:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX AS: <http://www.w3.org/ns/activitystreams#>
CONSTRUCT {?s ?p ?o}
WHERE {
?s rdf:type/rdfs:subClassOf* AS:Create ;
?p ?o .
}
But now i want to ask for more than one type!
Something like
WHERE {
?s rdf:type/rdfs:subClassOf* AS:Create|AS:Announce ;
?p ?o .
}
any idea ?
You can use a VALUES clause for this:
VALUES ?cls {AS:Create AS:Announce} ?s rdf:type/rdfs:subClassOf* ?cls ;

Delete all triples about event time except earliest event time

I have several triples like this
:event1 :hasTimeStamp "2017-06-30T00:01:00Z" .
:event1 :hasTimeStamp "2017-06-30T00:02:00Z" .
:event1 :hasTimeStamp "2017-06-30T00:03:00Z" .
I would like to delete all of the assertions about :event1's timestamp except the earliest.
I know how to select the earliest, insert it into a scratch named graph, delete all :event1 timestamps, and then copy back from the scratch graph.
Is there a way to do the deletion in place, with no utilization of a temporary/scratch graph?
Here's a nested select, where the inner subselect gets the minimum time, which is then be compared with the individual times form the outer select.
Now I just have to wrap that in the delete.
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT *
WHERE
{ ?s rdf:type <http://turbo.org/procStartTimeMeas> ;
<http://purl.obolibrary.org/obo/IAO_0000136> ?something .
?something <http://purl.obolibrary.org/obo/RO_0002223> ?another .
?another rdf:type <http://turbo.org/R2RInstantiation> .
?s <http://purl.obolibrary.org/obo/IAO_0000004> ?o
{ SELECT ?s (MIN(?o) AS ?earliest)
WHERE
{ ?s rdf:type <http://turbo.org/procStartTimeMeas> ;
<http://purl.obolibrary.org/obo/IAO_0000136> ?something .
?something <http://purl.obolibrary.org/obo/RO_0002223> ?another .
?another rdf:type <http://turbo.org/R2RInstantiation> .
?s <http://purl.obolibrary.org/obo/IAO_0000004> ?o
}
GROUP BY ?s
}
FILTER ( ?o != ?earliest )
}
Try this (not in the production environment):
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
DELETE {
?s <http://purl.obolibrary.org/obo/IAO_0000004> ?o2 .
}
WHERE {
?s <http://purl.obolibrary.org/obo/IAO_0000004> ?o2 .
?s rdf:type <http://turbo.org/procStartTimeMeas> .
FILTER EXISTS {
?s <http://purl.obolibrary.org/obo/IAO_0000004> ?o1 .
?s rdf:type <http://turbo.org/procStartTimeMeas> .
FILTER (?o2 > ?o1)
}
}
I'm not sure I understand correctly what these predicates mean.
I suppose <http://purl.obolibrary.org/obo/IAO_0000004> is :hasTimeStamp of the initial example.
In my answer, ?s rdf:type <http://turbo.org/procStartTimeMeas> is the only selection criterion. Please add other criteria.
(An alternative to the nice solution of #StansilavKralin)
I just did it based on the sample data
#prefix : <http://example.org/> .
#prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
:event1 :hasTimeStamp "2017-06-30T00:01:00Z"^^xsd:dateTime .
:event1 :hasTimeStamp "2017-06-30T00:02:00Z"^^xsd:dateTime .
:event1 :hasTimeStamp "2017-06-30T00:03:00Z"^^xsd:dateTime .
Not sure whether this is what you want, but at least it's pretty compact and I'm a big fan of MINUS which is at least more human readable (but maybe less performant):
PREFIX : <http://example.org/>
DELETE {
?event :hasTimeStamp ?ts .
}
WHERE
{ ?event :hasTimeStamp ?ts
MINUS
{ { SELECT ?event (MIN(?_ts) AS ?ts)
WHERE
{ ?event :hasTimeStamp ?_ts }
GROUP BY ?event
}
}
}
I think this does what I want, but I'd like to see suggestions from others. I don't want to be reckless with a deletion.
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
DELETE {
?s <http://purl.obolibrary.org/obo/IAO_0000004> ?o .
}
WHERE
{ SELECT *
WHERE
{ ?s rdf:type <http://turbo.org/procStartTimeMeas> ;
<http://purl.obolibrary.org/obo/IAO_0000136> ?something .
?something <http://purl.obolibrary.org/obo/RO_0002223> ?another .
?another rdf:type <http://turbo.org/R2RInstantiation> .
?s <http://purl.obolibrary.org/obo/IAO_0000004> ?o
{ SELECT ?s (MIN(?o) AS ?earliest)
WHERE
{ ?s rdf:type <http://turbo.org/procStartTimeMeas> ;
<http://purl.obolibrary.org/obo/IAO_0000136> ?something .
?something <http://purl.obolibrary.org/obo/RO_0002223> ?another .
?another rdf:type <http://turbo.org/R2RInstantiation> .
?s <http://purl.obolibrary.org/obo/IAO_0000004> ?o
}
GROUP BY ?s
}
FILTER ( ?o != ?earliest )
}
}

SPARQL in Protege 4.3

I need to return individuals for my query:
SELECT ?subject ?class
WHERE { ?subject rdfs:subClassOf ?class.
?class rdfs:comment "linear"#en}
But it works only with subclasses. Should I replace rdfs:subClassOf on different operator?
Your query specifically asks for ?subjects that are subclasses of ?class (where ?class has the rdfs:comment "linear"#en). To retrieve instances of type ?class, you'd use
?subject rdf:type ?class
or, since SPARQL allows abbreviating rdf:type by a,
?subject a ?class
If you can't share details about the body of data, you are querying, you might want to get an idea yourself by checking
SELECT ?s ?p ?subject ?class
WHERE
{ ?s ?p ?class .
?subject rdfs:subClassOf ?class .
?class rdfs:comment "linear"#en .
} ORDER BY ?s ?p ?subject ?class
and/or
SELECT ?subject ?class ?p ?o
WHERE
{ ?subject ?p ?o .
?subject rdfs:subClassOf ?class .
?class rdfs:comment "linear"#en .
} ORDER BY ?subject ?class ?p ?o
from where you can expand in the same manner until you get a handle.

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

Restrict property from being rdf:type

In a query like this, how can I avoid ?p being bound to rdf:type?
select ?p ?c where {
<http://dbpedia.org/resource/Istance_1> ?p ?c.
<http://dbpedia.org/resource/Istance_2> ?p ?c.
}
Add a filter to your query:
select ?p ?c where {
<http://dbpedia.org/resource/Istance_1> ?p ?c.
<http://dbpedia.org/resource/Istance_2> ?p ?c.
filter( ?p != rdf:type )
}
It's typical to use the prefix dbpedia: for http://dbpedia.org/resource/, and I expect that Istance is suppose to be Instance, so with a bit of cleanup, you'll have
prefix dbpedia: <http://dbpedia.org/resource>
select ?p ?c where {
dbpedia:Instance_1 ?p ?c .
dbpedia:Instance_2 ?p ?c .
filter( ?p != rdf:type )
}
If you're copying and pasting into the public DBpedia SPARQL endpoint, you won't need to define that prefix because it, and bunch of others are predefined, but if you'll calling it some other fashion, you will need to.