0
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#>
PREFIX my: <http://www.ex.com#>
SELECT distinct ?person ?nationality
WHERE{
    ?person rdf:type ?p.
    ?person my:Nationality ?nationality.
    filter (?nationality = "Some nationality")
}

I have created an ontology in Protege which describes a music production company. I have trouble understanding the SPARQL queries and the way they work.

Some explanations

?person: variable in which I want to output people. They are individuals in Protege

?nationality a variable in which I want to output nationalities. They are data properties in Protege

Nationality: data property which contains nationalities as strings and every person has one

my: prefix I created

How does this work ?person rdf:type ?p and how does it select the right type? Does it work automatically? I don't feel like I have set the ?person variable as a type Person variable (which is a class I have created and it describes a person as an entity), even though it outputs exactly the outcome I need.

TallTed
  • 9,069
  • 2
  • 22
  • 37
Ionized
  • 1
  • 2
  • SPARQL -> RDF -> it doesn't query about "individuals", "properties", "classes", etc. which exist in the OWL world. It's just about subject, predicate, object – UninformedUser May 25 '18 at 06:19

1 Answers1

1

How does this work "?person rdf:type ?p" and how does it select the right type? I don't feel like I have set the ?person variable as a type Person variable (which is a class I have created and it describes a person as an entity), even though it outputs exactly the outcome I need.

It doesn't select the right type. It selects a type, any type (and also any individual). Presumably the reason you are seeing the expect outcome is the second part of your query:

?person my:Nationality ?nationality.

In your data, only person-individuals have this property, so only they will match the full query (even if there are other individuals that have a different type).

How SPARQL works is essentially pattern matching. You specify a template for your RDF graph, the variables in the query are the "holes" in that template. Whichever parts of your graph fit the entire template get returned.

Put another way, your query asks the following: "give me all things that have both a type, and a nationality". There may be many things that have a type, but since only persons have a nationality, only persons are returned.

If you want, you can make it explicit that you are only interested in individuals of type Person, by replacing the variable ?p with the class identifier for persons, for example:

?person rdf:type my:Person.
?person my:Nationality ?nationality.
Jeen Broekstra
  • 21,642
  • 4
  • 51
  • 73