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") }
Related
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"
This is my example.ttl:
#prefix : <http://example.com#> .
:NewYork a :City .
:London a :City .
:bob a :Person, :Man;
:name "Bob";
:bornIn :London.
:susan a :Person, :Woman;
:name "Susan";
:bornIn :NewYork.
:jane a :Person, :Woman;
:name "Jane".
<<:bob :meet :susan>> :venue :NewYork;
:date "2020-10-12" .
<<:jane :told :susan>> :about <<:bob :love :susan>>;
:date "2021-06-15" .
I import it into GraphDB, and Create visual graph config : I choose "Start with graph query results", I want get all nodes and edges in the graph, but I still don't know how to set the Graph expansion, Node basics and Edge basics.
Would anyone show me how to do it?
Thanks a lot!
The Visual Graph doesn't support RDF*, but you could expand all nodes by creating a construct query, using Standard Reification.
Go to Create graph config -> Start with graph query results and write following construct query:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix : <http://example.com#>
CONSTRUCT {
?s a ?o;
a ?o1;
:name ?name;
:bornIn ?cityBorn.
:meeting rdf:subject ?pers1;
rdf:predicate :meet;
rdf:object ?pers2;
:venue ?city.
:gossip rdf:subject ?pers5;
rdf:predicate :love;
rdf:object ?pers6.
:story rdf:subject ?pers3;
rdf:predicate :told;
rdf:object ?pers4.
:story :about :gossip;
:date ?date .
} WHERE {
?s a ?o;
a ?o1;
:name ?name.
<<?pers1 :meet ?pers2>> :venue ?city.
<<?pers3 :told ?pers4>> :about <<?pers5 :love ?pers6>>;
:date ?date .
OPTIONAL {
?s :bornIn ?cityBorn.
}
}
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?
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.
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!