SPARQL MIN on string - sparql

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.

Related

Replace multiple value in SPARQL

I'm trying to use the BIND and REPLACE functions to create a new column in which there is the real title of an object code.
For example, I have a column with ENV_TOB and I would like to create a column that changes "ENV_TOB" to "SLIDE". I have several codes like "ENV_XXX" that I would also like to include in my query. So far, I've only managed to replace one value.
select ?code_type
{
GRAPH xxx:optimized {
OPTIONAL {?o xxx:code_type ?code_type}
FILTER (?code_type IN
("ENV_BAL"^^xsd:string, "ENV_BBF"^^xsd:string, "ENV_CAB"^^xsd:string, "ENV_JRG"^^xsd:string, "ENV_STO"^^xsd:string, "ENV_TJE"^^xsd:string, "ENV_TOB"^^xsd:string, "ENV_TPP"^^xsd:string ))
}
BIND (REPLACE(?code_type, "ENV_TOB", "Slide") AS ?TYPE_JEU)
}
If you just want to get a different value occurs in the data, and the mapping a direct mapping, that is, if you want "Slide" when the value is exactly "ENV_TOB", you can use a VALUES block. For instance:
#prefix : <urn:ex:>
:a :label "ENV_TOB" .
:b :label "ENV_TOB" .
:c :label "ENV_BBF" .
:d :label "ENV_CAB" .
:e :label "Other Label" .
:f :label "I went down the ENV_TOB" .
:g :label "I live in a ENV_CAB" .
prefix : <urn:ex:>
select ?subject ?label ?better_label where {
?subject :label ?label .
optional {
values (?label ?better_label) {
("ENV_TOB" "Slide")
("ENV_CAB" "House")
}
}
}
subject
label
better_label
:a
"ENV_TOB"
"Slide"
:b
"ENV_TOB"
"Slide"
:c
"ENV_BBF"
:d
"ENV_CAB"
"House"
:e
"Other Label"
:f
"I went down the ENV_TOB"
:g
"I live in a ENV_CAB"

SPARQL subquery with limit

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?

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)".

How to model a multiple choice possibility in a ontology?

I'm using protege 5 and trying to convert a questionnaire to an ontology. And I cant figure out how to model a multiple choice answer. I have a class (Class1) which has elements and for each element a person can select one or more of allowed values. Eg:
ElementOne : "someStringValue1", "someStringValue2", "someStringValue3", someStringValue4" .
And the user can pick "someStringValue1" and "someStringValue2".
Any ideas how to model this in protege and owl?
You can use enumerated classes to model a fixed set of choices. E.g., you can say things like
Question2Answers ≡ {"choice a", "choice b", "choice c", "none of the above"}
question2 &sqsubseteq; ∀ hasAnswer.Question2Answers
Here's an ontology you can download and see examples. In it, I've declared two question individuals, and three answer individuals. I've defined two properties hasObjectAnswer and hasDataAnswer, so you can either use individuals or datatyped literals as answers. I've said that the possible answers for question1 are answerA, answerB, and answerC, by asserting that
question1 &sqsubseteq; ∀ hasObjectAnswer.{answerA, answerB, answerC}
I've said that the possible answers for question2 are "answer one", "answer two", "answer three", and "none of the above" by using the axiom:
question2 &sqsubseteq; ∀ hasDataAnswer.{"answer one", …, "none of the above"}
#prefix : <urn:ex:> .
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
#prefix owl: <http://www.w3.org/2002/07/owl#> .
#prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
#prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
:answerB a owl:Thing , owl:NamedIndividual .
:question2 a owl:Thing , owl:NamedIndividual ;
a [ a owl:Restriction ;
owl:allValuesFrom [ a rdfs:Datatype ;
owl:oneOf ("answer one" "answer two" "answer three" "none of the above")
] ;
owl:onProperty :hasDataAnswer
] .
:answerA a owl:Thing , owl:NamedIndividual .
:question1 a owl:Thing , owl:NamedIndividual ;
a [ a owl:Restriction ;
owl:allValuesFrom [ a owl:Class ;
owl:oneOf ( :answerC :answerB :answerA )
] ;
owl:onProperty :hasObjectAnswer
] .
:hasObjectAnswer a owl:ObjectProperty .
:hasDataAnswer a owl:DatatypeProperty .
:answerC a owl:Thing , owl:NamedIndividual .