Solution examples 2021: Difference between revisions

From info216
(Created page with "<pre> *** 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...")
 
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
<pre>
*** Examples related to the "OWL in TTL" task from 2021:


==Questions 77-88: OWL in TTL==


'''Question 77:'''
A country has one or more regions.
A country has one or more regions.


 
<pre>
@prefix : <http://ex.org/> .
@prefix : <http://ex.org/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
Line 14: Line 13:
             owl:onProperty :hasRegion ;
             owl:onProperty :hasRegion ;
             owl:someValuesFrom :Region ] .
             owl:someValuesFrom :Region ] .
</pre>


   
   
 
'''Question 78:'''
A city is located in exactly one country.
A city is located in exactly one country.




<pre>
@prefix : <http://ex.org/> .
@prefix : <http://ex.org/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
Line 28: Line 29:
             owl:cardinality "1"^^xsd:nonNegativeInteger ;
             owl:cardinality "1"^^xsd:nonNegativeInteger ;
             owl:onProperty :inCountry ] .
             owl:onProperty :inCountry ] .
</pre>


   
   
 
'''Question 79:'''
A capital city is a city.
A capital city is a city.




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


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


   
   
 
'''Question 80:'''
A country has only one capital.
A country has only one capital.




<pre>
@prefix : <http://ex.org/> .
@prefix : <http://ex.org/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
Line 53: Line 58:
             owl:onProperty [ owl:inverseOf :inCountry ] ;
             owl:onProperty [ owl:inverseOf :inCountry ] ;
             owl:qualifiedCardinality "1"^^xsd:nonNegativeInteger ] .
             owl:qualifiedCardinality "1"^^xsd:nonNegativeInteger ] .
</pre>


   
   
 
'''Question 81:'''
A division is either a country or a region.
A division is either a country or a region.




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


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


   
   
 
'''Question 82:'''
Anything that is adjacent to something is a division.
Anything that is adjacent to something is a division.




<pre>
@prefix : <http://ex.org/> .
@prefix : <http://ex.org/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
Line 75: Line 84:
:adjacentTo rdfs:domain :Division ;
:adjacentTo rdfs:domain :Division ;
     rdfs:range :Division .
     rdfs:range :Division .
</pre>


   
   
 
'''Question 83:'''
A division cannot be adjacent to itself.
A division cannot be adjacent to itself.




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


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


   
   
 
'''Question 84:'''
A city is located in at most one region.
A city is located in at most one region.




<pre>
@prefix : <http://ex.org/> .
@prefix : <http://ex.org/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
Line 99: Line 112:
             owl:maximumCardinality "1"^^xsd:nonNegativeInteger ;
             owl:maximumCardinality "1"^^xsd:nonNegativeInteger ;
             owl:onProperty :inRegion ] .
             owl:onProperty :inRegion ] .
</pre>


   
   
 
'''Question 85:'''
A capital region is a region that has a capital city.
A capital region is a region that has a capital city.




<pre>
@prefix : <http://ex.org/> .
@prefix : <http://ex.org/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
Line 112: Line 127:
                 owl:onProperty [ owl:inverseOf :inRegion ] ;
                 owl:onProperty [ owl:inverseOf :inRegion ] ;
                 owl:someValuesFrom :CapitalCity ] ) .
                 owl:someValuesFrom :CapitalCity ] ) .
</pre>


   
   
 
'''Question 86:'''
If a city is in a region, it must be in the country of that region.
If a city is in a region, it must be in the country of that region.




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


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


   
   
 
'''Question 87:'''
An island state is a country that is next to no (other) country.
An island state is a country that is next to no (other) country.




<pre>
@prefix : <http://ex.org/> .
@prefix : <http://ex.org/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
Line 137: Line 156:
                         owl:onProperty :adjancentTo ;
                         owl:onProperty :adjancentTo ;
                         owl:someValuesFrom :Country ] ] ) .
                         owl:someValuesFrom :Country ] ] ) .
</pre>


   
   
 
'''Question 88:'''
A country with only one city and at most one region is a city state.
A country with only one city and at most one region is a city state.




<pre>
@prefix : <http://ex.org/> .
@prefix : <http://ex.org/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
Line 155: Line 176:
                 owl:onClass :Region ;
                 owl:onClass :Region ;
                 owl:onProperty :hasRegion ] ) .
                 owl:onProperty :hasRegion ] ) .
</pre>




*** Examples related to the "SPARQL" task from 2021:
==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/>
     PREFIX : <http://ex.org/>


Line 168: Line 192:
         :OsloRegion :hasCity :Oslo .
         :OsloRegion :hasCity :Oslo .
     }
     }
</pre>


   
   
'''Question 95:'''


    PREFIX : <http://ex.org/>
11
 
    INSERT DATA {
        :Norway :citiesByPopulation ( :Oslo :Bergen :Trondheim :Stavanger :Drammen ) .
    }


   
   
'''Question 96:'''
Complete this single-line SPARQL query so that it returns these 5 Norwegian cities: ...


<pre>
     PREFIX : <http://ex.org/>
     PREFIX : <http://ex.org/>
     PREFIX rdf: <{RDF}>
     PREFIX rdf: <{RDF}>
Line 185: Line 210:
         :Norway (:citiesByPopulation / rdf:rest* / rdf:first) ?city .
         :Norway (:citiesByPopulation / rdf:rest* / rdf:first) ?city .
     }}
     }}
</pre>


   
   
'''Question 97:'''
Write a SPARQL Update statement that uses the :citiesByPopulation
list to add five corresponding unordered :hasCity triples.


<pre>
     PREFIX : <http://ex.org/>
     PREFIX : <http://ex.org/>


Line 195: Line 225:
         :Norway (:citiesByPopulation / rdf:rest* / rdf:first) ?city .
         :Norway (:citiesByPopulation / rdf:rest* / rdf:first) ?city .
     }
     }
</pre>




Preparation:
<pre>
     INSERT DATA {
     INSERT DATA {
         :Norway :hasCity :Os, :Voss, :Sandnes, :Fredrikstad, :Sarpsborg .
         :Norway :hasCity :Os, :Voss, :Sandnes, :Fredrikstad, :Sarpsborg .
Line 218: Line 250:
         :Sarpsborg :hasPopulation 52159 .
         :Sarpsborg :hasPopulation 52159 .
     }
     }
</pre>


'''Question 98:'''
Write a SPARQL query that counts the number of cities in each region in Norway.
<pre>
TBD.
</pre>
   
   
'''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.


<pre>
     PREFIX : <http://ex.org/>
     PREFIX : <http://ex.org/>


Line 228: Line 271:
     GROUP BY ?region
     GROUP BY ?region
     ORDER BY DESC(?cityPop)
     ORDER BY DESC(?cityPop)
</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)