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 )
}
}
Related
I have an RDF graph G with several classes assuming for simplicity (Person and Parrot).
The class Person is connected to the class Parrot by the property hasAnimal, e.g.:
#PREFIX : <http://example.org/>
:Hugo rdf:type :Person .
:Hugo rdfs:label "Hugo" .
:Hugo :hasAnimal :Birdy.
:Birdy rdf:type :Parrot .
:Birdy rdfs:label :"Birdy" .
:LonleyBrido rdf:type :Parrot .
What is wanted is a subgraph of G that contains all the triples from Person and Parrot that are directly connected with each other, starting from Person. The initial Person does not matter to me, the important part is that only connected triples are extracted i.e. that only persons that do have a parrot or don't get outputted. What I have already tried is the following:
construct {
?person ?p ?o .
?parrot ?p2 ?o2 .
} where {
?person rdf:type :Person .
?person ?p ?o .
?person :hasAnimal ?parrot .
?parrot rdf:type :Parrot .
?parrot ?p2 ?o2 .
}
So the expected output would be:
:Hugo rdf:type :Person .
:Hugo rdfs:label "Hugo" .
:Hugo :hasAnimal :Birdy.
:Birdy rdf:type :Parrot .
:Birdy rdfs:label :"Birdy" .
I am executing this query on a rdflib graph.
Does anyone have a solution to this problem?
The solution is already described above:
import rdflib
from rdflib.namespace import RDF, RDFS
query = """
construct {
?person ?p ?o .
?parrot ?p2 ?o2 .
} where {
?person rdf:type :Person .
?person ?p ?o .
?person :hasAnimal ?parrot .
?parrot rdf:type :Parrot .
?parrot ?p2 ?o2 .
}
"""
g = rdflib.Graph()
g.parse("example.ttl", format="ttl")
g.bind("rdf", RDF)
g.bind("rdfs", RDFS)
EX= rdflib.Namespace("http://example.org/")
g.bind("example", EX)
result = g.query(query)
Why can get results with this query:
SELECT *
WHERE {
?person rdfs:label "Kirk Douglas"#en;
dbo:birthPlace ?place. # With dbp: too
}
but not with another one:
SELECT *
WHERE {
?person rdfs:label "Kirk Douglas"#en;
dbo:starring ?film.
}
I'm following tags in https://dbpedia.org/page/Kirk_Douglas
Some tip to understand it.
Thx!
In dbr:Kirk_Douglas you can read:
dbo:birthPlace dbr:Amsterdam_(city),_New_York
...
is dbo:starring of dbr:The_Light_at_the_Edge_of_the_World
dbr:Cast_a_Giant_Shadow
dbr:Two-Fisted_Tales_(film)
...
where is dbo:starring of is a way of simulating an inverse property for dbo:starring.
Indeed, in dbo:starring you can read:
rdfs:domain dbo:Work
...
rdfs:range dbo:Actor
This means that you shouldn't build triples like ?actor dbo:starring ?work, while ?work dbo:starring ?actor is a valid triple.
So your query should be something like:
SELECT *
WHERE {
?person rdfs:label "Kirk Douglas"#en .
?film dbo:starring ?person .
}
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.
I uploaded a set of RDF triples onto a local Virtuoso endpoint.
Of all these triples, I would like to extract only those whose subjects have at least the predicates http://www.w3.org/2000/01/rdf-schema#label and http://www.w3.org/2000/01/rdf-schema#comment.
For example, from these triples:
<http://dbpedia.org/resource/AccessibleComputing> <http://www.w3.org/2000/01/rdf-schema#label> "AccessibleComputing"#en .
<http://dbpedia.org/resource/AfghanistanGeography> <http://www.w3.org/2000/01/rdf-schema#label> "AfghanistanGeography"#en .
<http://dbpedia.org/resource/AfghanistanGeography> <http://www.w3.org/2000/01/rdf-schema#comment> " ... " .
<http://dbpedia.org/resource/Austroasiatic_languages> <http://www.w3.org/2000/01/rdf-schema#comment> " ... " .
<http://dbpedia.org/resource/AccessibleComputing> <http://dbpedia.org/ontology/wikiPageWikiLink> <http://dbpedia.org/resource/Computer_accessibility> .
<http://dbpedia.org/resource/AfghanistanGeography> <http://dbpedia.org/ontology/wikiPageWikiLink> <http://dbpedia.org/resource/Afghanistan_Geography> .
I would like to get:
<http://dbpedia.org/resource/AfghanistanGeography> <http://www.w3.org/2000/01/rdf-schema#label> "AfghanistanGeography"#en .
<http://dbpedia.org/resource/AfghanistanGeography> <http://www.w3.org/2000/01/rdf-schema#comment> " ... " .
<http://dbpedia.org/resource/AfghanistanGeography> <http://dbpedia.org/ontology/wikiPageWikiLink> <http://dbpedia.org/resource/Afghanistan_Geography> .
Is it possible to do this with one (or more) SPARQL query?
Thank you for helping
This can be done with a CONSTRUCT WHERE query:
CONSTRUCT WHERE {
?s rdfs:label ?label.
?s rdfs:comment ?comment.
?s ?p ?o
}
This is a simplified form of CONSTRUCT that can be used when the CONSTRUCT {} part and the WHERE {} part are identical.
One way is to use DESCRIBE, e.g.:
DESCRIBE ?s
WHERE {
?s rdfs:label ?label .
?s rdfs:comment ?comment .
}
or alternatively with CONSTRUCT :
CONSTRUCT { ?subject ?predicate ?object}
WHERE {
?subject ?predicate ?object .
FILTER EXISTS {
?subject rdfs:label ?label .
?subject rdfs:comment ?comment .
}
}
I'm developing my own Fuseki endpoint from some DBpedia data.
I'm in doubt on how to aggregate properties related to a single resource.
SELECT ?name ?website ?abstract ?genre ?image
WHERE{
VALUES ?s {<http://dbpedia.org/resource/Attack_Attack!>}
?s foaf:name ?name ;
dbo:abstract ?abstract .
OPTIONAL { ?s dbo:genre ?genre } .
OPTIONAL { ?s dbp:website ?website } .
OPTIONAL { ?s dbo:image ?image } .
FILTER LANGMATCHES(LANG(?abstract ), "en")
}
SPARQL endpoint: http://dbpedia.org/sparql/
This query returns 2 matching results. They are different just for the dbo:genre value. There is a way I can query the knowledge base and retrieving a single result with a list of genres?
#chrisis's query works well on the DBpedia SPARQL Endpoint, which is based on Virtuoso.
However, if you are using Jena Fuseki, you should use more conformant syntax:
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
SELECT
?name
(SAMPLE(?website) AS ?sample_website)
(SAMPLE(?abstract) AS ?sample_abstract)
(SAMPLE(?image) AS ?sample_image)
(GROUP_CONCAT(?genre; separator=', ') AS ?genres)
WHERE {
VALUES (?s) {(<http://dbpedia.org/resource/Attack_Attack!>)}
?s foaf:name ?name ;
dbo:abstract ?abstract .
OPTIONAL { ?s dbo:genre ?genre } .
OPTIONAL { ?s dbp:website ?website } .
OPTIONAL { ?s dbo:image ?image} .
FILTER LANGMATCHES(LANG(?abstract ), "en")
} GROUP BY ?name
The differences from the #chrisis's query are:
Since GROUP_CONCAT is an aggregation function, it might be used with GROUP BY only;
Since GROUP BY is used, all non-grouping variables should be aggregated (e.g. via SAMPLE);
GROUP_CONCAT syntax is slightly different.
In Fuseki, these AS in the projection are in fact superfluous: see this question and comments.
Yes, the GROUP_CONCAT() function is what you want.
SELECT ?name ?website ?abstract (GROUP_CONCAT(?genre,',') AS ?genres) ?image
WHERE{
<http://dbpedia.org/resource/Attack_Attack!> a dbo:Band ;
foaf:name ?name;
dbo:abstract ?abstract .
OPTIONAL{ <http://dbpedia.org/resource/Attack_Attack!> dbo:genre ?genre } .
OPTIONAL{ <http://dbpedia.org/resource/Attack_Attack!> dbp:website ?website} .
OPTIONAL{ <http://dbpedia.org/resource/Attack_Attack!> dbo:image ?image} .
FILTER LANGMATCHES(LANG(?abstract ), "en")
}