SPARQL subquery with limit - sparql

I'm using Apache Jena Fuseki to query my graph using SPARQL.
I need to do subquery with limit, and I found one example similar to my requirements here.
The example looks like this.
Data
#prefix : <http://people.example/> .
:alice :name "Alice", "Alice Foo", "A. Foo" .
:alice :knows :bob, :carol .
:bob :name "Bob", "Bob Bar", "B. Bar" .
:carol :name "Carol", "Carol Baz", "C. Baz" .
Query
PREFIX : <http://people.example/>
SELECT ?y ?name WHERE {
:alice :knows ?y .
{
SELECT ?y ?name WHERE {
?y :name ?name
}
ORDER BY ?name
LIMIT 1
}
}
Result
?y ?name
:bob "B. Bar"
:carol "C. Baz"
I did the same query with the same data on Fuseki, but the result is empty.
Is it problem of SPARQL implementation in Fuseki?
Which SPARQL engine can I use to get the same result as showed in the example?

Related

How do I write a SPARQL query for this

How do i write a SPARQL query to retrieve all the movies and co-directors of a movie having director with name "George".
triples are
#prefix : <http://org.example/> .
:movie1 :hasDirectors :d1, :d2, :d3 .
:movie2 :hasDirectors :d3, :d4 .
:movie3 :hasDirectors :d1, :d3
:d1 :name "George" .
:d2 :name "Jack" .
:d6 :name "Raj" .
:d3 :name "Henry" .
:d4 :name "Williams" .
As posted in the comments the query should be as follows:
SELECT ?movie ?name ?coDirectors_name WHERE { ?movie :hasDirectors ?d. ?d :name ?
name. FILTER(?name="George") ?movie :hasDirectors ?coDirectors . ?coDirectors :name
?coDirectors_name FILTER(?coDirectors_name != "George") }

SPARQL When do we use the "a"

I'm new on SPARQL. I read the tutorial on Jena (https://jena.apache.org/tutorials/sparql.html) and I understand most of the example. But I don't really understand when we have to use the a
For example with this triples :
#prefix vCard: <http://www.w3.org/2001/vcard-rdf/3.0#> .
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
<http://somewhere/MattJones/> vCard:FN "Matt Jones" .
<http://somewhere/MattJones/> vCard:N _:b0 .
_:b0 vCard:Family "Jones" .
_:b0 vCard:Given "Matthew" .
<http://somewhere/RebeccaSmith/> vCard:FN "Becky Smith" .
<http://somewhere/RebeccaSmith/> vCard:N _:b1 .
_:b1 vCard:Family "Smith" .
_:b1 vCard:Given "Rebecca" .
<http://somewhere/JohnSmith/> vCard:FN "John Smith" .
<http://somewhere/JohnSmith/> vCard:N _:b2 .
_:b2 vCard:Family "Smith" .
_:b2 vCard:Given "John" .
<http://somewhere/SarahJones/> vCard:FN "Sarah Jones" .
<http://somewhere/SarahJones/> vCard:N _:b3 .
_:b3 vCard:Family "Jones" .
_:b3 vCard:Given "Sarah" .
We can write something like this :
PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#>
SELECT ?y ?givenName
WHERE
{ ?y vcard:Family "Smith" .
?y vcard:Given ?givenName .
}
But I the slideshow of my course I found something like this :
SELECT ?label_du_président ?âge_du_président ? label_du_pays
WHERE {
?une_élection a ex:electionPrésidentielle .
?une_élection ex:gagnéePar ?un_president .
?une_élection ex:alieuDans ?un_pays .
?un_president rdfs:label ?label_du_president .
?un_president ex:âge ?âge_du_president .
?un_pays rdfs:label ?label_du_pays
}
Why and when use the a ?
It's a shorthand notation for the rdf:type property. From the SPARQL Query Language specs:
The keyword "a" can be used as a predicate in a triple pattern and is an alternative for the IRI http://www.w3.org/1999/02/22-rdf-syntax-ns#type. This keyword is case-sensitive.
Think of it as meaning "is a (kind of)".

SPARQL MIN on string

I am studying SPARQL and there is this example on the documentation page:
PREFIX : <http://people.example/>
PREFIX : <http://people.example/>
SELECT ?y ?minName
WHERE {
:alice :knows ?y .
{
SELECT ?y (MIN(?name) AS ?minName)
WHERE {
?y :name ?name .
} GROUP BY ?y
}
}
From this dabase:
#prefix : <http://people.example/> .
:alice :name "Alice", "Alice Foo", "A. Foo" .
:alice :knows :bob, :carol .
:bob :name "Bob", "Bob Bar", "B. Bar" .
:carol :name "Carol", "Carol Baz", "C. Baz" .
There is says that this query:
"Return a name (the one with the lowest sort order) for all the people that know Alice and have a name."
and the output is:
y minName
:bob "B. Bar"
:carol "C. Baz"
But I can't understand how "B. Bar" is smaller than "Bob", for example. I cannot understand what the docs means with "lowest sort order".
For me the output should be:
y minName
:bob "Bob"
:carol "Carol"
Thank you for your help.

Encountered " "<" "< "" at line 1, column 15. Was expecting: <IRIref>

Trying to use the query in the endpoint. The query was created in SPARQL. The error coming like
Encountered " "<" "< "" at line 1, column 15.
Was expecting:
<IRIref> ...
Query:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?Class ?Title ?Definition
WHERE {
?Value rdfs:label ?Class
FILTER regex(?Class, "Motion") .
?def rdfs:domain ?Value .
?def rdfs:label ?Title .
?def rdfs:comment ?Definition}
The url
http://localhost:3030/skosmos/query?query=
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?Class ?Title ?Definition
WHERE {
?Value rdfs:label ?Class
FILTER regex(?Class, "Motion") .
?def rdfs:domain ?Value .
?def rdfs:label ?Title .
?def rdfs:comment ?Definition
}
I took your original query, URL-encoded it with one of many services and tools you might use, randomly selected from a web search, and appended it to the start of what you had as "the URL", http://localhost:3030/skosmos/query?query= ... and you verified that this worked --
http://localhost:3030/skosmos/query?query=PREFIX+rdfs%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F2000%2F01%2Frdf-schema%23%3E%0D%0A%0D%0ASELECT+%3FClass+%3FTitle+%3FDefinition%0D%0AWHERE+%7B%0D%0A%3FValue+rdfs%3Alabel+%3FClass%0D%0AFILTER+regex%28%3FClass%2C+%22Motion%22%29+.%0D%0A%3Fdef+rdfs%3Adomain+%3FValue+.%0D%0A%3Fdef+rdfs%3Alabel+%3FTitle+.%0D%0A%3Fdef+rdfs%3Acomment+%3FDefinition%7D

querying child data-properties on RDF-store

I have, in my ontology, an entity named "person" that has a class child named "member". Some like this (indenting for indicate the hierarchy),
person
member
Also, I have some attributes (dataProperty), associated with "person", and another attributes associated with "member". Specifically, that attributes could be phone numbers. I propose a hierarchy of attributes in this way,
phone (domain: person)
office-phone (domain: member)
office-phone-1 (domain: member)
office-phone-2 (domain: member)
personal-phone (domain: person)
I'm doing the following,
SELECT ?s ?attr ?data
WHERE
{
value ?attr {:phone} .
?s rdf:type :member .
?s ?attr ?data .
}
to get all phone-numbers,
instance-member :phone "value-of-personal-phone"
instance-member :phone "value-of-office-phone-1"
instance-member :phone "value-of-office-phone-2"
but,
how could I ONLY to get the two office-phone WITHOUT USE the specific attribute ":office-phone"?? Some owl: restriction or definition??
Thanks!
First, you have to use the property hierarchy in your data, otherwise, this can't work (it would be good if you provide proper sample data next time...):
#prefix : <http://example.org/> .
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
#prefix rdfs: <http://www.w3.org/2000/01/rdf-schema/> .
:officePhone rdfs:subPropertyOf :phone .
:officePhone1 rdfs:subPropertyOf :officePhone .
:officePhone2 rdfs:subPropertyOf :officePhone .
:personalPhone rdfs:subPropertyOf :phone .
:instance-member rdf:type :member .
:instance-member :personalPhone "value-of-personal-phone" .
:instance-member :officePhone1 "value-of-office-phone-1" .
:instance-member :officePhone2 "value-of-office-phone-2" .
Regarding the querying, there are at least two options:
1) The triple store supports reasoning, RDFS would be enough in your example - then your query would be sufficient.
2) You rewrite your query such that the property hierarchy is taken into account using SPARQL 1.1 property paths:
prefix : <http://example.org/>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema/>
SELECT ?s ?attr ?data
WHERE
{
values ?p {:officePhone}
?s rdf:type :member .
?attr rdfs:subPropertyOf* ?p .
?s ?attr ?data .
}
Result:
----------------------------------------------------------------
| s | attr | data |
================================================================
| :instance-member | :officePhone1 | "value-of-office-phone-1" |
| :instance-member | :officePhone2 | "value-of-office-phone-2" |
----------------------------------------------------------------
...I got it in a similar way, showing the superproperty, restrincting the domain and including a distinct...
SELECT distinct ?s ?attr_father ?data
WHERE
{
values ?attr_father {:phone} .
?s rdf:type :member .
?attr rdfs:subPropertyOf* ?attr_father .
?attr rdfs:domain :member .
?s ?attr ?data .
}
In this way, we get,
instance-member :phone "value-of-office-phone-1" .
instance-member :phone "value-of-office-phone-2" .
Regards!