Solution examples 2022

From info216

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

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:

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