Solution examples 2022: Difference between revisions

From info216
No edit summary
No edit summary
Line 141: Line 141:




*** Examples related to the Part 4 - Restrictions and reasoning task from 2022:


Question 40:
==Task 4: Restrictions and reasoning==
:Organization rdfs:subClassOf :Agent .


Question 41:
'''Question 40:'''
:affiliation rdfs:domain :Author .
An Organization is a kind of Agent.


Question 42:
:Organization rdfs:subClassOf :Agent .
:affiliation rdfs:range :Organization .


Question 43:
'''Question 41:'''
:publication rdf:type owl:FunctionalProperty .
The object in an affiliation-triple is an Organization.


Question 44:
:affiliation rdfs:range :Organization .
:year rdf:type owl:FunctionalProperty .


Question 45:
'''Question 42:'''
:publication rdf:type owl:IrreflexiveProperty .
The subject in an affiliation-triple is an Author.


Question 46:
:affiliation rdfs:domain :Author .
:publication rdf:type owl:TransitiveProperty .


Question 47:
'''Question 43:'''
:name rdf:type owl:InverseFunctionalProperty .
A Paper is published in only one Publication.


Question 48:
:publication rdf:type owl:FunctionalProperty .
:Author owl:disjointWith :Organization .


Question 49:
'''Question 44:'''
:title rdfs:range xsd:string .
A Paper cannot have more than one publication year.


   
  :year rdf:type owl:FunctionalProperty .
 
'''Question 45:'''
A Paper cannot be its own publication.
 
:publication rdf:type owl:IrreflexiveProperty .
 
'''Question 46:'''
publication is a transitive relation.
 
:publication rdf:type owl:TransitiveProperty .
 
'''Question 47:'''
Different Author-s have different name-s.
 
:name rdf:type owl:InverseFunctionalProperty .
 
'''Question 48:'''
An Author is not an Organization.
 
:Author owl:disjointWith :Organization .
 
'''Question 49:'''
A Publication title is a string.
 
:title rdfs:range xsd:string .
 
 
'''Question 50:'''
A Paper must have at least one author.
 
:Paper rdfs:subClassOf [
    owl:someValuesFrom :Author ;
    owl:onProperty :author
]
 
'''Question 51:'''
A Paper has exactly one title.
 
:Paper rdfs:subClassOf [
    owl:cardinality 1 ;
    owl:onProperty :author
]


Question 50:
'''Question 52:'''
:Paper rdfs:subClassOf [
A year must be in range from 1900 to 2050 (inclusive).
    owl:someValuesFrom :Author ;
    owl:onProperty :author
]


Question 51:
:year rdfs:range [
:Paper rdfs:subClassOf [
    a rdfs:Datatype ;
    owl:cardinality 1 ;
    owl:onDatatype xsd:int ;
    owl:onProperty :author
    owl:withRestrictions (
]
        [xsd:minInclusive 1900]
        [xsd:maxInclusive 2050]
    )
]


Question 52:
'''Question 53:'''
:year rdfs:range [
Author-s, Organization-s, and Country-s are disjoint.
    a rdfs:Datatype ;
    owl:onDatatype xsd:int ;
    owl:withRestrictions (
        [xsd:minInclusive 1900]
        [xsd:maxInclusive 2050]
    )
]


Question 53:
[
[
     rdf:type owl:AllDisjointClasses ;
     rdf:type owl:AllDisjointClasses ;
     owl:members (
     owl:members (
         :Author :Organization :Country       
         :Author :Organization :Country       
     )
     )
]
]
 
'''Question 54:'''
A Publisher must be either of ACM, IEEE CS, Springer Nature, or IGI Global.
 
:Publisher owl:equivalentClass (
    :ACM :IEEE_CS :Springer_Nature :IGI_Global             
)


Question 54:
:Publisher owl:equivalentClass (
    :ACM :IEEE_CS :Springer_Nature :IGI_Global             
)


==Task 6: SPARQL==


*** Examples related to Part 6 - SPARQL from 2022:
*** Examples related to Part 6 - SPARQL from 2022:


Question 62:
'''Question 62:'''
List all Paper title-s.
 
<pre>
SELECT ?title WHERE {
SELECT ?title WHERE {
     ?paper rdf:type :Paper ;
     ?paper rdf:type :Paper ;
         :title ?title .
         :title ?title .
}
}
</pre>
   
   
Question 63:
'''Question 63:'''
List all publisher Organization-s in alphabetical order.
 
<pre>
SELECT DISTINCT ?name WHERE {
SELECT DISTINCT ?name WHERE {
     ?publ rdf:type :Publication ;
     ?publ rdf:type :Publication ;
Line 225: Line 264:
}
}
ORDER BY ?name
ORDER BY ?name
</pre>
Question 64:
 
'''Question 64:'''
List all Author-s along with the title-s of the Paper-s they have written.
 
<pre>
SELECT ?author ?title WHERE {
SELECT ?author ?title WHERE {
     ?author ^:name / ^:author / :title ?title
     ?author ^:name / ^:author / :title ?title
}
}
</pre>
   
   
Question 65:
'''Question 65:'''
List all Country-s and numbers of Paper-s from that Country.
 
<pre>
SELECT ?country (COUNT(?paper) AS ?number) WHERE {
SELECT ?country (COUNT(?paper) AS ?number) WHERE {
     ?paper rdf:type :Paper ;
     ?paper rdf:type :Paper ;
Line 237: Line 284:
}
}
GROUP BY ?country
GROUP BY ?country
</pre>
   
   
Question 66:
'''Question 66:'''
List all Author-s of more than one Paper in descending order (of paper numbers).
 
<pre>
SELECT ?author (COUNT(?paper) AS ?number) WHERE {
SELECT ?author (COUNT(?paper) AS ?number) WHERE {
     ?author ^:name / ^:author ?paper
     ?author ^:name / ^:author ?paper
}
}
GROUP BY ?author
GROUP BY ?author
</pre>
   
   
Question 67:
'''Question 67:'''
For each Author, write the first and last year they have published a Paper.
 
<pre>
SELECT ?name (MIN(?year) AS ?min) (MAX(?year) AS ?max) WHERE {
SELECT ?name (MIN(?year) AS ?min) (MAX(?year) AS ?max) WHERE {
     ?author rdf:type :Author ;
     ?author rdf:type :Author ;
Line 251: Line 306:
}
}
GROUP BY ?name
GROUP BY ?name
</pre>
   
   
Question 68:
'''Question 68:'''
List all Authors that are not affiliated with an Organization in Germany.
 
<pre>
SELECT ?name WHERE {
SELECT ?name WHERE {
     ?author rdf:type :Author ;
     ?author rdf:type :Author ;
Line 260: Line 319:
     }
     }
}
}
</pre>
   
   
Question 69:
'''Question 69:'''
Is James Hendler the author of more than one Paper?
 
<pre>
ASK {
ASK {
     "James Hendler" ^:name / ^:author ?paper1 ;
     "James Hendler" ^:name / ^:author ?paper1 ;
Line 267: Line 330:
     FILTER (?paper1 != ?paper2)
     FILTER (?paper1 != ?paper2)
}
}
</pre>
   
   
Question 70:
'''Question 70:'''
Create a new graph of all Author-s that have author-ed a Paper with Christian Bizer, including their affiliation-s and country-s.
 
<pre>
CONSTRUCT {
CONSTRUCT {
     ?author rdf:type :Author ;
     ?author rdf:type :Author ;
Line 281: Line 348:
         ^:author / :author / :name "Christian Bizer"  
         ^:author / :author / :name "Christian Bizer"  
}
}
</pre>
Question 71:
 
'''Question 71:'''
Create a new graph of all Author-s that have author-ed a Paper with Christian Bizer, including their Paper-s, affiliation-s and country-s.
 
<pre>
CONSTRUCT {
CONSTRUCT {
     ?author rdf:type :Author ;
     ?author rdf:type :Author ;
Line 300: Line 371:
     ?paper :title ?title
     ?paper :title ?title
}
}
</pre>


'''Question 72:'''
An Organization that is an Author's affiliation is an Institution.


Question 72:
<pre>
INSERT {
INSERT {
     ?org rdf:type :Institution
     ?org rdf:type :Institution
Line 309: Line 383:
         :affiliation ?org
         :affiliation ?org
}
}
</pre>
   
   
Question 73:
'''Question 73:'''
If an Author has a country, the then that Author's affiliation Organization is locatedIn the same Country.
 
<pre>
INSERT {
INSERT {
     ?org :locatedIn ?country
     ?org :locatedIn ?country
Line 318: Line 396:
         :country ?country
         :country ?country
}
}
</pre>
   
   
Question 74:
'''Question 74:'''
A Paper is producedBy the Organization-s its Author-s are affiliated with.
 
<pre>
INSERT {
INSERT {
     ?paper :producedBy ?org
     ?paper :producedBy ?org
Line 325: Line 407:
     ?paper :author / :affiliation ?org
     ?paper :author / :affiliation ?org
}
}
</pre>
   
   
Question 75:
'''Question 75:'''
A Paper is producedIn the Country-s of its Author-s Institutions are located in.
 
<pre>
INSERT {
INSERT {
     ?paper :producedIn ?country
     ?paper :producedIn ?country
Line 332: Line 418:
     ?paper :author / :country ?country
     ?paper :author / :country ?country
}
}
</pre>
   
   
Question 76:
'''Question 76:'''
Delete all direct (country) connections between Author-s and Country-s.
 
<pre>
DELETE {
DELETE {
     ?author :country ?country
     ?author :country ?country
Line 339: Line 429:
     ?author :country ?country
     ?author :country ?country
}
}
</pre>
Question 77:
 
'''Question 77:'''
If a Paper has more than one year, delete the most recent ones.
 
<pre>
DELETE {
DELETE {
     ?paper :year ?year
     ?paper :year ?year
Line 348: Line 442:
     FILTER(?year > ?earlier)
     FILTER(?year > ?earlier)
}
}
</pre>
   
   


*** Example related to Task 78 - Error detection from 2022:
==Task 7: rdflib understanding==


The errors are:
'''Question 78:'''
 
* Graph() without assignment
    Graph() without assignment
* BASE not a Namespace
    BASE not a Namespace
* Parse 'owl' format
    Parse 'owl' format
* No tuples in add
    No tuples in add
* No Literal()
    No Literal()
* No owlrl import
    No owlrl import
* No closure
    No closure
* DELETE without WHERE
    DELETE without WHERE
 
 
</pre>

Revision as of 13:02, 6 May 2024

Task 2: Programming

Question 26: Ontology programming

owlready2 is no longer in the curriculum.

from rdflib import Graph

g = Graph
g.update("""
    PREFIX ex: <http://ex.org#>
    PREFIX rdf: <...>
    # ...and so on

    INSERT DATA {
        :Agent          rdf:type        owl:Class .
        :Author         rdfs:subClassOf :Agent .
        :Organization   rdfs:subClassOf :Agent .
        :Country        rdfs:subClassOf :Agent .
        :Publication    rdf:type        owl:Class .
        :Paper          rdfs:subClassOf :Publication .

        :name           rdfs:domain     :Agent ;
                        rdfs:range      xsd:string .
        :affiliation    rdfs:domain     :Author ;
                        rdfs:range      :Organization .
        :country        rdfs:domain     :Author ;
                        rdfs:range      :Country .
        :title          rdfs:domain     :Publication ;
                        rdfs:range      xsd:string .
        :author         rdfs:domain     :Publication ;
                        rdfs:range      :Author .
        :publication    rdfs:domain     :Paper ;
                        rdfs:range      :Publication .
        :publisher      rdfs:domain     :Publication ;
                        rdfs:range      :Organization .
        :year           rdfs:domain     :Publication ;
                        rdfs:range      xsd:int .
""")

Alternative style (also see Robin's suggestions for how to program this):

from rdflib import Graph, Namespace, RDF, RDFS, OWL, XSD

EX = Namespace('http://ex.org#')

g = Graph
g.add(EX.Agent, RDF.type, OWL.Class)
g.add(EX.Author, RDFS.subClassOf, EX.Agent)

# ...and so on


Question 27: KG programming

You can use the same styles as in question 26, either 'INSERT DATA ... or Graph.add(...).

These are the triples to insert. It is much more important to show you understand the general idea than to be complete.

:DBpedia_A_nucleus a :Paper ;
    :author :Christian_Bizer,
        :Soren_Auer ;
    :publication :The_semantic_web_book ;
    :publisher :Springer_Nature ;
    :title "DBpedia A nucleus" ;
    :year 2007 .

:Linked_data_The_story_so_far a :Paper ;
    :author :Christian_Bizer,
        :Tim_Berners-Lee ;
    :publication :Semantic_services_interoperability_and_web_applications ;
    :publisher :IGI_Global ;
    :title "Linked data The story so far" ;
    :year 2011 .

:The_semantic_web a :Paper ;
    :author :James_Hendler,
        :Tim_Berners-Lee ;
    :publication :Scientific_American ;
    :publisher :Springer_Nature ;
    :title "The semantic web" ;
    :year 2001 .

:James_Hendler a :Author ;
    :affiliation :Rensselaer_Polytechnic_Institute ;
    :country :United_States ;
    :name "James Hendler" .

:Soren_Auer a :Author ;
    :affiliation :Leibniz_University_Hannover ;
    :country :Germany ;
    :name "Soren Auer" .

:IGI_Global a :Organization ;
    :name "IGI Global" .

:Leibniz_University_Hannover a :Organization ;
    :name "Leibniz University Hannover" .

:Massachusetts_Institute_of_Technology a :Organization ;
    :name "Massachusetts Institute of Technology" .

:Rensselaer_Polytechnic_Institute a :Organization ;
    :name "Rensselaer Polytechnic Institute" .

:University_of_Mannheim a :Organization ;
    :name "University of Mannheim" .

:Scientific_American a :Publication ;
    :title "Scientific American" .

:Semantic_services_interoperability_and_web_applications a :Publication ;
    :title "Semantic services interoperability and web applications" .

:The_semantic_web_book a :Publication ;
    :title "The semantic web book" .

:Christian_Bizer a :Author ;
    :affiliation :University_of_Mannheim ;
    :country :Germany ;
    :name "Christian Bizer" .

:Tim_Berners-Lee a :Author ;
    :affiliation :Massachusetts_Institute_of_Technology ;
    :country :United_States ;
    :name "Tim Berners-Lee" .

:Germany a :Country ;
    :name "Germany" .

:United_States a :Country ;
    :name "United States" .

:Springer_Nature a :Organization ;
    :name "Springer Nature" .


Task 4: Restrictions and reasoning

Question 40: An Organization is a kind of Agent.

:Organization rdfs:subClassOf :Agent .

Question 41: The object in an affiliation-triple is an Organization.

:affiliation rdfs:range :Organization .

Question 42: The subject in an affiliation-triple is an Author.

:affiliation rdfs:domain :Author .

Question 43: A Paper is published in only one Publication.

:publication rdf:type owl:FunctionalProperty .

Question 44: A Paper cannot have more than one publication year.

:year rdf:type owl:FunctionalProperty .

Question 45: A Paper cannot be its own publication.

:publication rdf:type owl:IrreflexiveProperty .

Question 46: publication is a transitive relation.

:publication rdf:type owl:TransitiveProperty .

Question 47: Different Author-s have different name-s.

:name rdf:type owl:InverseFunctionalProperty .

Question 48: An Author is not an Organization.

:Author owl:disjointWith :Organization .

Question 49: A Publication title is a string.

:title rdfs:range xsd:string .


Question 50: A Paper must have at least one author.

:Paper rdfs:subClassOf [
    owl:someValuesFrom :Author ;
    owl:onProperty :author 
]

Question 51: A Paper has exactly one title.

:Paper rdfs:subClassOf [
    owl:cardinality 1 ;
    owl:onProperty :author
]

Question 52: A year must be in range from 1900 to 2050 (inclusive).

:year rdfs:range [
    a rdfs:Datatype ;
    owl:onDatatype xsd:int ;
    owl:withRestrictions ( 
        [xsd:minInclusive 1900]
        [xsd:maxInclusive 2050]
    )
]

Question 53: Author-s, Organization-s, and Country-s are disjoint.

[
   rdf:type owl:AllDisjointClasses ;
   owl:members (
       :Author :Organization :Country       
   )
]

Question 54: A Publisher must be either of ACM, IEEE CS, Springer Nature, or IGI Global.

:Publisher owl:equivalentClass (
    :ACM :IEEE_CS :Springer_Nature :IGI_Global               
)


Task 6: SPARQL

      • Examples related to Part 6 - SPARQL from 2022:

Question 62: List all Paper title-s.

SELECT ?title WHERE {
    ?paper rdf:type :Paper ;
        :title ?title .
}

Question 63: List all publisher Organization-s in alphabetical order.

SELECT DISTINCT ?name WHERE {
    ?publ rdf:type :Publication ;
        :publisher / :name ?name
}
ORDER BY ?name

Question 64: List all Author-s along with the title-s of the Paper-s they have written.

SELECT ?author ?title WHERE {
    ?author ^:name / ^:author / :title ?title
}

Question 65: List all Country-s and numbers of Paper-s from that Country.

SELECT ?country (COUNT(?paper) AS ?number) WHERE {
    ?paper rdf:type :Paper ;
        :author / :country / :name ?country
}
GROUP BY ?country

Question 66: List all Author-s of more than one Paper in descending order (of paper numbers).

SELECT ?author (COUNT(?paper) AS ?number) WHERE {
    ?author ^:name / ^:author ?paper
}
GROUP BY ?author

Question 67: For each Author, write the first and last year they have published a Paper.

SELECT ?name (MIN(?year) AS ?min) (MAX(?year) AS ?max) WHERE {
    ?author rdf:type :Author ;
        :name ?name ;
        ^:author / :year ?year 
}
GROUP BY ?name

Question 68: List all Authors that are not affiliated with an Organization in Germany.

SELECT ?name WHERE {
    ?author rdf:type :Author ;
        :name ?name
    MINUS {
        ?author :country / :name "Germany"
    }
}

Question 69: Is James Hendler the author of more than one Paper?

ASK {
    "James Hendler" ^:name / ^:author ?paper1 ;
        ^:name / ^:author ?paper2 
    FILTER (?paper1 != ?paper2)
}

Question 70: Create a new graph of all Author-s that have author-ed a Paper with Christian Bizer, including their affiliation-s and country-s.

CONSTRUCT {
    ?author rdf:type :Author ;
        :name ?name ;
        :affiliation ?affiliation ;
        :country ?country
} WHERE {
    ?author rdf:type :Author ;
        :name ?name ;
        :affiliation ?affiliation ;
        :country ?country ;
        ^:author / :author / :name "Christian Bizer" 
}

Question 71: Create a new graph of all Author-s that have author-ed a Paper with Christian Bizer, including their Paper-s, affiliation-s and country-s.

CONSTRUCT {
    ?author rdf:type :Author ;
        :name ?name ;
        :affiliation ?affiliation ;
        :country ?country .
    ?paper rdf:type :Paper ;
        :author ?author ;
        :title ?title
} WHERE {
    ?author rdf:type :Author ;
        :name ?name ;
        :affiliation ?affiliation ;
        :country ?country ;
        ^:author / :author / :name "Christian Bizer" ;
        ^:author ?paper .
    ?paper :title ?title
}

Question 72: An Organization that is an Author's affiliation is an Institution.

INSERT {
    ?org rdf:type :Institution
} WHERE {
    ?author rdf:type :Author ;
        :affiliation ?org
}

Question 73: If an Author has a country, the then that Author's affiliation Organization is locatedIn the same Country.

INSERT {
    ?org :locatedIn ?country
} WHERE {
    ?author rdf:type :Author ;
        :affiliation ?org ;
        :country ?country
}

Question 74: A Paper is producedBy the Organization-s its Author-s are affiliated with.

INSERT {
    ?paper :producedBy ?org
} WHERE {
    ?paper :author / :affiliation ?org
}

Question 75: A Paper is producedIn the Country-s of its Author-s Institutions are located in.

INSERT {
    ?paper :producedIn ?country
} WHERE {
    ?paper :author / :country ?country
}

Question 76: Delete all direct (country) connections between Author-s and Country-s.

DELETE {
    ?author :country ?country
} WHERE {
    ?author :country ?country
}

Question 77: If a Paper has more than one year, delete the most recent ones.

DELETE {
    ?paper :year ?year
} WHERE {
    ?paper :year ?year ;
        :year ?earlier .
    FILTER(?year > ?earlier)
}


Task 7: rdflib understanding

Question 78:

  • Graph() without assignment
  • BASE not a Namespace
  • Parse 'owl' format
  • No tuples in add
  • No Literal()
  • No owlrl import
  • No closure
  • DELETE without WHERE