Solution examples 2022

From info216
Revision as of 12:46, 6 May 2024 by Sinoa (talk | contribs)

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


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

Question 40:

Organization rdfs:subClassOf :Agent .

Question 41:

affiliation rdfs:domain :Author .

Question 42:

affiliation rdfs:range :Organization .

Question 43:

publication rdf:type owl:FunctionalProperty .

Question 44:

year rdf:type owl:FunctionalProperty .

Question 45:

publication rdf:type owl:IrreflexiveProperty .

Question 46:

publication rdf:type owl:TransitiveProperty .

Question 47:

name rdf:type owl:InverseFunctionalProperty .

Question 48:

Author owl:disjointWith :Organization .

Question 49:

title rdfs:range xsd:string .


Question 50:

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

]

Question 51:

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

]

Question 52:

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

]

Question 53: [

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

]

Question 54:

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

)


      • Examples related to Part 6 - SPARQL from 2022:

Question 62: SELECT ?title WHERE {

   ?paper rdf:type :Paper ;
       :title ?title .

}

Question 63: SELECT DISTINCT ?name WHERE {

   ?publ rdf:type :Publication ;
       :publisher / :name ?name

} ORDER BY ?name

Question 64: SELECT ?author ?title WHERE {

   ?author ^:name / ^:author / :title ?title

}

Question 65: SELECT ?country (COUNT(?paper) AS ?number) WHERE {

   ?paper rdf:type :Paper ;
       :author / :country / :name ?country

} GROUP BY ?country

Question 66: SELECT ?author (COUNT(?paper) AS ?number) WHERE {

   ?author ^:name / ^:author ?paper

} GROUP BY ?author

Question 67: 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: SELECT ?name WHERE {

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

}

Question 69: ASK {

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

}

Question 70: 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: 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: INSERT {

   ?org rdf:type :Institution

} WHERE {

   ?author rdf:type :Author ;
       :affiliation ?org

}

Question 73: INSERT {

   ?org :locatedIn ?country

} WHERE {

   ?author rdf:type :Author ;
       :affiliation ?org ;
       :country ?country

}

Question 74: INSERT {

   ?paper :producedBy ?org

} WHERE {

   ?paper :author / :affiliation ?org

}

Question 75: INSERT {

   ?paper :producedIn ?country

} WHERE {

   ?paper :author / :country ?country

}

Question 76: DELETE {

   ?author :country ?country

} WHERE {

   ?author :country ?country

}

Question 77: DELETE {

   ?paper :year ?year

} WHERE {

   ?paper :year ?year ;
       :year ?earlier .
   FILTER(?year > ?earlier)

}


      • Example related to Task 78 - Error detection from 2022:

The errors are:

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