Solution examples 2021
From info216
*** Examples related to the "OWL in TTL" task from 2021: 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 ] . A city is located in exactly one country. @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 ] . A capital city is a city. @prefix : <http://ex.org/> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . :CapitalCity rdfs:subClassOf :City . A country has only one capital. @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 ] . A division is either a country or a region. @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 ) ] . Anything that is adjacent to something is a division. @prefix : <http://ex.org/> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . :adjacentTo rdfs:domain :Division ; rdfs:range :Division . A division cannot be adjacent to itself. @prefix : <http://ex.org/> . @prefix owl: <http://www.w3.org/2002/07/owl#> . :adjacentTo a owl:IrreflexiveProperty . A city is located in at most one region. @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 ] . A capital region is a region that has a capital city. @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 ] ) . If a city is in a region, it must be in the country of that region. @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 ) ] . An island state is a country that is next to no (other) country. @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 ] ] ) . A country with only one city and at most one region is a city state. @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 ] ) . *** Examples related to the "SPARQL" task from 2021: PREFIX : <http://ex.org/> INSERT DATA { :Norway :hasRegion :OsloRegion, :Vestland, :Trondelag, :Rogaland, :Viken . :OsloRegion :hasCity :Oslo . } PREFIX : <http://ex.org/> INSERT DATA { :Norway :citiesByPopulation ( :Oslo :Bergen :Trondheim :Stavanger :Drammen ) . } PREFIX : <http://ex.org/> PREFIX rdf: <{RDF}> SELECT ?city WHERE {{ :Norway (:citiesByPopulation / rdf:rest* / rdf:first) ?city . }} PREFIX : <http://ex.org/> INSERT { :Norway :hasCity ?city . } WHERE { :Norway (:citiesByPopulation / rdf:rest* / rdf:first) ?city . } 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 . } PREFIX : <http://ex.org/> SELECT ?region (SUM(?pop) AS ?cityPop) WHERE { ?region :regionalCity / :hasPopulation ?pop . } GROUP BY ?region ORDER BY DESC(?cityPop)