Solution examples 2021
From info216
Questions 77-88: OWL in TTL
Question 77: A country has one or more regions.
@prefix : <http://ex.org/> . @prefix owl: <http://www.w3.org/2002/07/owl#> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . :Country rdfs:subClassOf [ a owl:Restriction ; owl:onProperty :hasRegion ; owl:someValuesFrom :Region ] . <pre> '''Question 78:''' A city is located in exactly one country. <pre> @prefix : <http://ex.org/> . @prefix owl: <http://www.w3.org/2002/07/owl#> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . :City rdfs:subClassOf [ a owl:Restriction ; owl:cardinality "1"^^xsd:nonNegativeInteger ; owl:onProperty :inCountry ] . <pre> '''Question 79:''' A capital city is a city. <pre> @prefix : <http://ex.org/> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . :CapitalCity rdfs:subClassOf :City . <pre> '''Question 80:''' A country has only one capital. <pre> @prefix : <http://ex.org/> . @prefix owl: <http://www.w3.org/2002/07/owl#> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . :Country rdfs:subClassOf [ a owl:Restriction ; owl:onClass :CapitalCity ; owl:onProperty [ owl:inverseOf :inCountry ] ; owl:qualifiedCardinality "1"^^xsd:nonNegativeInteger ] . <pre> '''Question 81:''' A division is either a country or a region. <pre> @prefix : <http://ex.org/> . @prefix owl: <http://www.w3.org/2002/07/owl#> . @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . :Division owl:EquivalentClass [ owl:unionOf ( :Country :Region ) ] . <pre> '''Question 82:''' Anything that is adjacent to something is a division. <pre> @prefix : <http://ex.org/> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . :adjacentTo rdfs:domain :Division ; rdfs:range :Division . <pre> '''Question 83:''' A division cannot be adjacent to itself. <pre> @prefix : <http://ex.org/> . @prefix owl: <http://www.w3.org/2002/07/owl#> . :adjacentTo a owl:IrreflexiveProperty . <pre> '''Question 84:''' A city is located in at most one region. <pre> @prefix : <http://ex.org/> . @prefix owl: <http://www.w3.org/2002/07/owl#> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . :City rdfs:subClassOf [ a owl:Restriction ; owl:maximumCardinality "1"^^xsd:nonNegativeInteger ; owl:onProperty :inRegion ] . <pre> '''Question 85:''' A capital region is a region that has a capital city. <pre> @prefix : <http://ex.org/> . @prefix owl: <http://www.w3.org/2002/07/owl#> . @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . :CapitalRegion owl:intersectionOf ( :Region [ a owl:Restriction ; owl:onProperty [ owl:inverseOf :inRegion ] ; owl:someValuesFrom :CapitalCity ] ) . <pre> '''Question 86:''' If a city is in a region, it must be in the country of that region. <pre> @prefix : <http://ex.org/> . @prefix owl: <http://www.w3.org/2002/07/owl#> . @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . :inRegion rdfs:subPropertyOf [ owl:propertyChainAxiom ( :inCountry :hasRegion ) ] . <pre> '''Question 87:''' An island state is a country that is next to no (other) country. <pre> @prefix : <http://ex.org/> . @prefix owl: <http://www.w3.org/2002/07/owl#> . @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . :IslandState owl:intersectionOf ( :Country [ owl:complementOf [ a owl:Restriction ; owl:onProperty :adjancentTo ; owl:someValuesFrom :Country ] ] ) . <pre> '''Question 88:''' A country with only one city and at most one region is a city state. <pre> @prefix : <http://ex.org/> . @prefix owl: <http://www.w3.org/2002/07/owl#> . @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . :CityState owl:intersectionOf ( :Country [ a owl:Restriction ; owl:onClass :City ; owl:onProperty [ owl:inverseOf :inCountry ] ; owl:qualifiedCardinality "1"^^xsd:NonNegativeInteger ] [ a owl:Restriction ; owl:maxQualifiedCardinality "1"^^xsd:NonNegativeInteger ; owl:onClass :Region ; owl:onProperty :hasRegion ] ) . <pre> ==Questions 94-99: SPARQL== '''Question 94:''' Write a SPARQL Update that adds the triples written below in Turtle to a triple store: ... <pre> PREFIX : <http://ex.org/> INSERT DATA { :Norway :hasRegion :OsloRegion, :Vestland, :Trondelag, :Rogaland, :Viken . :OsloRegion :hasCity :Oslo . }
Question 95:
11
Question 96:
Complete this single-line SPARQL query so that it returns these 5 Norwegian cities: ...
PREFIX : <http://ex.org/> PREFIX rdf: <{RDF}> SELECT ?city WHERE {{ :Norway (:citiesByPopulation / rdf:rest* / rdf:first) ?city . }}
Question 97:
Write a SPARQL Update statement that uses the :citiesByPopulation
list to add five corresponding unordered :hasCity triples.
PREFIX : <http://ex.org/> INSERT { :Norway :hasCity ?city . } WHERE { :Norway (:citiesByPopulation / rdf:rest* / rdf:first) ?city . }
Preparation:
INSERT DATA { :Norway :hasCity :Os, :Voss, :Sandnes, :Fredrikstad, :Sarpsborg . :OsloRegion :regionalCity :Oslo . :Vestland :regionalCity :Bergen, :Os, :Voss . :Trondelag :regionalCity :Trondheim . :Rogaland :regionalCity :Stavanger, :Sandnes . :Viken :regionalCity :Drammen, :Fredrikstad, :Sarpsborg . :Oslo :hasPopulation 580000 . :Bergen :hasPopulation 213585 . :Os :hasPopulation 14046 . :Voss :hasPopulation 6043 . :Trondheim :hasPopulation 147139 . :Stavanger :hasPopulation 121610 . :Drammen :hasPopulation 90722 . :Fredrikstad :hasPopulation 72760 . :Sandnes :hasPopulation 63032 . :Sarpsborg :hasPopulation 52159 . }
Question 98:
Write a SPARQL query that counts the number of cities in each region in Norway.
TBD.
Question 99: Continue with the same triple store. Extend the previous SPARQL query so that it lists the city population in each region in Norway in descending order.
PREFIX : <http://ex.org/> SELECT ?region (SUM(?pop) AS ?cityPop) WHERE { ?region :regionalCity / :hasPopulation ?pop . } GROUP BY ?region ORDER BY DESC(?cityPop)