Solution examples 2021: Difference between revisions

From info216
No edit summary
 
Line 13: Line 13:
             owl:onProperty :hasRegion ;
             owl:onProperty :hasRegion ;
             owl:someValuesFrom :Region ] .
             owl:someValuesFrom :Region ] .
<pre>
</pre>


   
   
Line 29: Line 29:
             owl:cardinality "1"^^xsd:nonNegativeInteger ;
             owl:cardinality "1"^^xsd:nonNegativeInteger ;
             owl:onProperty :inCountry ] .
             owl:onProperty :inCountry ] .
<pre>
</pre>


   
   
Line 41: Line 41:


:CapitalCity rdfs:subClassOf :City .
:CapitalCity rdfs:subClassOf :City .
<pre>
</pre>


   
   
Line 58: Line 58:
             owl:onProperty [ owl:inverseOf :inCountry ] ;
             owl:onProperty [ owl:inverseOf :inCountry ] ;
             owl:qualifiedCardinality "1"^^xsd:nonNegativeInteger ] .
             owl:qualifiedCardinality "1"^^xsd:nonNegativeInteger ] .
<pre>
</pre>


   
   
Line 71: Line 71:


:Division owl:EquivalentClass [ owl:unionOf ( :Country :Region ) ] .
:Division owl:EquivalentClass [ owl:unionOf ( :Country :Region ) ] .
<pre>
</pre>


   
   
Line 84: Line 84:
:adjacentTo rdfs:domain :Division ;
:adjacentTo rdfs:domain :Division ;
     rdfs:range :Division .
     rdfs:range :Division .
<pre>
</pre>


   
   
Line 96: Line 96:


:adjacentTo a owl:IrreflexiveProperty .
:adjacentTo a owl:IrreflexiveProperty .
<pre>
</pre>


   
   
Line 112: Line 112:
             owl:maximumCardinality "1"^^xsd:nonNegativeInteger ;
             owl:maximumCardinality "1"^^xsd:nonNegativeInteger ;
             owl:onProperty :inRegion ] .
             owl:onProperty :inRegion ] .
<pre>
</pre>


   
   
Line 127: Line 127:
                 owl:onProperty [ owl:inverseOf :inRegion ] ;
                 owl:onProperty [ owl:inverseOf :inRegion ] ;
                 owl:someValuesFrom :CapitalCity ] ) .
                 owl:someValuesFrom :CapitalCity ] ) .
<pre>
</pre>


   
   
Line 141: Line 141:


:inRegion rdfs:subPropertyOf [ owl:propertyChainAxiom ( :inCountry :hasRegion ) ] .
:inRegion rdfs:subPropertyOf [ owl:propertyChainAxiom ( :inCountry :hasRegion ) ] .
<pre>
</pre>


   
   
Line 156: Line 156:
                         owl:onProperty :adjancentTo ;
                         owl:onProperty :adjancentTo ;
                         owl:someValuesFrom :Country ] ] ) .
                         owl:someValuesFrom :Country ] ] ) .
<pre>
</pre>


   
   
Line 176: Line 176:
                 owl:onClass :Region ;
                 owl:onClass :Region ;
                 owl:onProperty :hasRegion ] ) .
                 owl:onProperty :hasRegion ] ) .
<pre>
</pre>





Latest revision as of 13:16, 6 May 2024

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 ] .


Question 78: 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 ] .


Question 79: A capital city is a city.


@prefix : <http://ex.org/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .

:CapitalCity rdfs:subClassOf :City .


Question 80: 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 ] .


Question 81: 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 ) ] .


Question 82: 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 .


Question 83: A division cannot be adjacent to itself.


@prefix : <http://ex.org/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .

:adjacentTo a owl:IrreflexiveProperty .


Question 84: 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 ] .


Question 85: 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 ] ) .


Question 86: 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 ) ] .


Question 87: 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 ] ] ) .


Question 88: 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 ] ) .


Questions 94-99: SPARQL

Question 94: Write a SPARQL Update that adds the triples written below in Turtle to a triple store: ...

    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)