Difference between revisions of "Lab: OWL 1"

m
 
(47 intermediate revisions by 3 users not shown)
Line 1: Line 1:
=Lab 7: RDFS Plus=
+
=Lab 6: OWL 1 ("RDFS Plus / Basic OWL")=
  
 
==Topics==
 
==Topics==
RDF Plus sketching.
+
Basic OWL ontology programming with RDFlib and owlrl.
 +
 
 
WebVOWL visualisation.
 
WebVOWL visualisation.
Basic OWL ontology programming in Jena.
 
  
==Tutorial==
+
RDF and RDFS might be relevant too.
Tutorial for the Jena-programming part: https://jena.apache.org/documentation/ontology/
 
  
==Classes/interfaces (some in package org.apache.jena.ontology)==
+
==Classes/Vocabularies==
* ModelFactory (createOntologyModel),
+
 
 +
Vocabulary:
 
* OWL (sameAs, equivalentClass, equivalentProperty, differentFrom, disjointWith, inverseOf)
 
* OWL (sameAs, equivalentClass, equivalentProperty, differentFrom, disjointWith, inverseOf)
* OntModel (createClass, createIndividual, createObjectProperty, CreateDatatypeProperty, createAllDifferent, createSymmetricProperty, createTransitiveProperty, createInverseFunctionalProperty),
+
* OWL (SymmetricProperty, AsymmetricProperty, ReflexiveProperty, IrreflexiveProperty, TransitiveProperty, FunctionalProperty, InverseFunctionalProperty)
* Model (createList, write),
 
* OntClass, Individual, DatatypeProperty, ObjectProperty
 
 
 
Note that the OntModel interface extends InfModel and Model.
 
  
 
==Tasks==
 
==Tasks==
In earlier labs you modelled and programmed the following situations:
+
'''Task 1'''
  
In RDF: "Cade Tracy lives in 1516 Henry Street, Berkeley, California 94709, USA. He has a B.Sc. in biology
+
'''Write OWL triples that corresponds to the following text. '''.If you can, try to build on your example from labs 2 and 7, or extend the triples at the bottom of the page. OWL can be imported from rdflib.namespace.
from the University of California, Berkeley from 2011. His interests include birds, ecology, the environment,
 
photography and travelling. He has visited Canada and France. Emma Dominguez lives in Carrer de la Guardia
 
Civil 20, 46020 Valencia, Spain. She has a M.Sc. in chemistry from the University of Valencia from 2015.
 
Her areas of expertise include waste management, toxic waste, air pollution. Her interests include bike
 
riding, music and travelling. She has visited Portugal, Italy, France, Germany, Denmark and Sweden. Cade
 
knows Ines. They met in Paris in August 2014."
 
  
In RDFS: "University of California, Berkeley and University of Valencia are both Universities.
+
Cade and Emma are two different persons. <!-- All the countries mentioned above are different. --> The country USA above is
All universities are higher education instituttions (HEIs). Having a B.Sc. from a HEI and having a M.Sc.
 
from a HEI are special cases of gradutating from that HEI. That a person has a degree in a subject means
 
that the person has expertise in that subject. Only persons can have expertise, and what they have expertise
 
about is always a subject."
 
 
 
Extend the RDF and RDFS graphs from earlier to account for the following situation:
 
Cade and Emma are two different persons. All the countries mentioned above are different. The country USA above is
 
 
the same as the DBpedia resource http://dbpedia.org/resource/United_States (dbr:United_States) and the GeoNames
 
the same as the DBpedia resource http://dbpedia.org/resource/United_States (dbr:United_States) and the GeoNames
 
resource http://sws.geonames.org/6252001/ (gn:6252001). The person class (the RDF type the Cade and Emma resources)
 
resource http://sws.geonames.org/6252001/ (gn:6252001). The person class (the RDF type the Cade and Emma resources)
 
in your graph is the same as FOAF's, schema.org's and AKT's person classes (they are http://xmlns.com/foaf/0.1/Person,
 
in your graph is the same as FOAF's, schema.org's and AKT's person classes (they are http://xmlns.com/foaf/0.1/Person,
http://schema.org/Person, and http://www.aktors.org/ontology/portal#Person, respectively. Nothing can be any two of a person, a university, a city, and a person at the same time. The property
+
http://schema.org/Person, and http://www.aktors.org/ontology/portal#Person, respectively. Nothing can be any two of a person, a university, or a city at the same time. The property
 
you have used in your RDF/RDFS graph to represent that 94709 is the US zip code of Berkeley, California in US is
 
you have used in your RDF/RDFS graph to represent that 94709 is the US zip code of Berkeley, California in US is
 
a subproperty of VCard's postal code-property (http://www.w3.org/2006/vcard/ns#postal-code). No two US cities can
 
a subproperty of VCard's postal code-property (http://www.w3.org/2006/vcard/ns#postal-code). No two US cities can
have the same postal code. The property you have used for Ines living in Valencia is the same property as FOAF's
+
have the same postal code. The property you have used for Emma living in Valencia is the same property as FOAF's
 
based near-property (http://xmlns.com/foaf/0.1/based_near), and it is the inverse of DBpedia's hometown property
 
based near-property (http://xmlns.com/foaf/0.1/based_near), and it is the inverse of DBpedia's hometown property
 
(http://dbpedia.org/ontology/hometown, dbo:hometown). (This is not completely precise: but "hometown" is perhaps the
 
(http://dbpedia.org/ontology/hometown, dbo:hometown). (This is not completely precise: but "hometown" is perhaps the
 
inverse of a subproperty of "based near".)
 
inverse of a subproperty of "based near".)
  
Look through your graph and try to identify at least one of each: a reflexive and an irreflexive, a symmetric and
 
an asymmetric, as well as a transitive property.
 
  
Create and output the OWL ontology in RDFlib. If you can, try to build on your example from labs 2 and 3!
+
'''Task 2'''
 +
<syntaxhighlight>
 +
g.add((ex.Cade, ex.married, ex.Mary))
 +
g.add((ex.Cade, ex.livesWith, ex.Mary))
 +
g.add((ex.Cade, ex.sibling, ex.Andrew))
 +
g.add((ex.Cade, ex.hasFather, ex.Bob))
 +
g.add((ex.Bob, ex.fatherOf, ex.Cade))
 +
</syntaxhighlight>
 +
 
 +
Look through the predicates(properties) above and add new triples for each one that describes them as any of the following: a reflexive , irreflexive, symmetric, asymmetric, transitive, functional, or an Inverse Functional Property.
 +
e.g
 +
<syntaxhighlight>
 +
g.add((ex.married, RDF.type, OWL.SymmetricProperty))
 +
</syntaxhighlight>
 +
 
 +
'''Task 3'''
 +
 
 +
Print/Serialize the ontology. Then use owlrl like seen below to infer additional triples. Can you spot the many inferences?
 +
 
 +
<syntaxhighlight>
 +
# These three lines add inferred triples to the graph.
 +
owl = owlrl.CombinedClosure.RDFS_OWLRL_Semantics(g, False, False, False)
 +
owl.closure()
 +
owl.flush_stored_triples()
 +
</syntaxhighlight>
 +
 
 +
Finally write the ontology to a TURTLE file, and try to visualise it using http://visualdataweb.de/webvowl/ . WebVOWL is oriented towards visualising classes and their properties, so the individuals may not show.
 +
 
 +
 
 +
==Useful Reading==
 +
* [https://wiki.uib.no/info216/index.php/File:S06-OWL-1.pdf Lecture Notes]
 +
 
 +
* [https://wiki.uib.no/info216/index.php/Python_Examples#RDFS_Plus_.2F_OWL_inference_with_RDFLib Example page]
 +
 
 +
==Triples you can extend for the tasks==
 +
'''Python'''
 +
<syntaxhighlight>
 +
 
 +
import owlrl
 +
from rdflib import Graph, Namespace, Literal, URIRef
 +
from rdflib.namespace import RDF, RDFS, XSD, FOAF, OWL
 +
 
 +
g = Graph()
 +
 
 +
# Namespaces
 +
ex = Namespace("http://example.org/")
 +
dbp = Namespace("http://dbpedia.org/resource/")
 +
geo = Namespace("http://sws.geonames.org/")
 +
schema = Namespace("https://schema.org/")
 +
akt = Namespace("http://www.aktors.org/ontology/portal#")
 +
vcard = Namespace("http://www.w3.org/2006/vcard/ns#")
 +
 
 +
g.bind("ex", ex)
 +
g.bind("owl", OWL)
 +
 
 +
 
 +
# RDFS Tasks from last time.
 +
g.add((ex.Cade, ex.degreeFrom, ex.University_of_California))
 +
g.add((ex.Emma, ex.degreeFrom, ex.University_of_Valencia))
 +
g.add((ex.Cade, ex.degreeSubject, ex.Biology))
 +
g.add((ex.Emma, ex.degreeSubject, ex.Chemistry))
 +
g.add((ex.University_of_California, RDF.type, ex.University))
 +
g.add((ex.University_of_Valencia, RDF.type, ex.University))
 +
g.add((ex.University, RDFS.subClassOf, ex.Higher_Education_Institution))
 +
g.add((ex.expertise, RDFS.range, ex.Subject))
 +
g.add((ex.expertise, RDFS.domain, FOAF.Person))
 +
g.add((ex.degreeSubject, RDFS.subPropertyOf, ex.expertise))
 +
g.add((ex.graduated, RDFS.range, ex.Higher_Education_Institution))
 +
g.add((ex.graduated, RDFS.domain, FOAF.Person))
 +
g.add((ex.degreeFrom, RDFS.subPropertyOf, ex.graduated))
 +
g.add((ex.Biology, RDFS.label, Literal("Biology", lang="en")))
 +
g.add((ex.Biology, RDFS.label, Literal("La Biologie", lang="fr")))
 +
g.add((ex.Biology, RDFS.comment, Literal("Biology is a natural science concerned with the study of life and living organisms, including their structure, function, growth, evolution, distribution, identification and taxonomy.")))
 +
g.add((ex.Chemistry, RDFS.label, Literal("Chemistry", lang="en")))
 +
g.add((ex.Chemistry, RDFS.label, Literal("La Chimie", lang="fr")))
 +
g.add((ex.Chemistry, RDFS.comment, Literal("Chemistry is a branch of physical science that studies the composition, structure, properties and change of matter.", lang="en")))
 +
 
 +
# Write OWL triples here
 +
</syntaxhighlight>
 +
'''Turtle'''
 +
<syntaxhighlight>
 +
@prefix ex: <http://example.org/> .
 +
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
 +
 
 +
ex:Cade ex:degreeFrom ex:University_of_California ;
 +
    ex:degreeSubject ex:Biology .
 +
 
 +
ex:Emma ex:degreeFrom ex:University_of_Valencia ;
 +
    ex:degreeSubject ex:Chemistry .
 +
 
 +
ex:degreeFrom rdfs:subPropertyOf ex:graduated .
 +
 
 +
ex:degreeSubject rdfs:subPropertyOf ex:expertise .
 +
 
 +
ex:Biology rdfs:label "Biology"@en,
 +
        "La Biologie"@fr ;
 +
    rdfs:comment "Biology is a natural science concerned with the study of life and living organisms, including their structure, function, growth, evolution, distribution, identification and taxonomy." .
 +
 
 +
ex:Chemistry rdfs:label "Chemistry"@en,
 +
        "La Chimie"@fr ;
 +
    rdfs:comment "Chemistry is a branch of physical science that studies the composition, structure, properties and change of matter."@en .
 +
 
 +
ex:University_of_California a ex:University .
 +
 
 +
ex:University_of_Valencia a ex:University .
 +
 
 +
ex:expertise rdfs:domain <http://xmlns.com/foaf/0.1/Person> ;
 +
    rdfs:range ex:Subject .
 +
 
 +
ex:graduated rdfs:domain <http://xmlns.com/foaf/0.1/Person> ;
 +
    rdfs:range ex:Higher_Education_Institution .
 +
 
 +
ex:University rdfs:subClassOf ex:Higher_Education_Institution .
 +
 
 +
ex:Cade a foaf:Person ;
 +
    ex:address [ a ex:Address ;
 +
            ex:city ex:Berkeley ;
 +
            ex:country ex:USA ;
 +
            ex:postalCode "94709"^^xsd:string ;
 +
            ex:state ex:California ;
 +
            ex:street "1516_Henry_Street"^^xsd:string ] ;
 +
    ex:age 27 ;
 +
    ex:characteristic ex:Kind ;
 +
    ex:degree [ ex:degreeField ex:Biology ;
 +
            ex:degreeLevel "Bachelor"^^xsd:string ;
 +
            ex:degreeSource ex:University_of_California ;
 +
            ex:year "2011-01-01"^^xsd:gYear ] ;
 +
    ex:interest ex:Bird,
 +
        ex:Ecology,
 +
        ex:Environmentalism,
 +
        ex:Photography,
 +
        ex:Travelling ;
 +
    ex:married ex:Mary ;
 +
    ex:meeting ex:Meeting1 ;
 +
    ex:visit ex:Canada,
 +
        ex:France,
 +
        ex:Germany ;
 +
    foaf:knows ex:Emma ;
 +
    ex:name "Cade_Tracey"^^xsd:string .
 +
 
 +
ex:Mary a ex:Student;
 +
    ex:age 26 ;
 +
    ex:characteristic ex:Kind ;
 +
    ex:interest ex:Biology,
 +
        ex:Chocolate,
 +
        ex:Hiking .
 +
 
 +
ex:Emma a foaf:Person ;
 +
    ex:address [ a ex:Address ;
 +
            ex:city ex:Valencia ;
 +
            ex:country ex:Spain ;
 +
            ex:postalCode "46020"^^xsd:string ;
 +
            ex:street "Carrer_de_la Guardia_Civil_20"^^xsd:string ] ;
 +
    ex:age 26 ;
 +
    ex:degree [ ex:degreeField ex:Chemistry ;
 +
            ex:degreeLevel "Master"^^xsd:string ;
 +
            ex:degreeSource ex:University_of_Valencia ;
 +
            ex:year "2015-01-01"^^xsd:gYear ] ;
 +
    ex:expertise ex:Air_Pollution,
 +
        ex:Toxic_Waste,
 +
        ex:Waste_Management,
 +
        ex:Bananas ;
 +
    ex:interest ex:Bike_Riding,
 +
        ex:Music,
 +
        ex:Travelling ;
 +
    ex:meeting ex:Meeting1 ;
 +
    ex:visit ( ex:Portugal ex:Italy ex:France ex:Germany ex:Denmark ex:Sweden ) ;
 +
    ex:name "Emma_Dominguez"^^xsd:string .
 +
 
 +
ex:Meeting1 a ex:Meeting ;
 +
    ex:date "August, 2014"^^xsd:string ;
 +
    ex:involved ex:Cade,
 +
        ex:Emma ;
 +
    ex:location ex:Paris .
  
Write the ontology to a TURTLE file, and try to visualise it using http://visualdataweb.de/webvowl/ . WebVOWL is oriented towards visualising classes and their properties, so the individuals may not show.
+
ex:Paris a ex:City ;
 +
    ex:capitalOf ex:France ;
 +
    ex:locatedIn ex:France .
  
Use OntModel.writeAll() to write out the whole ontology, including OWL's built-in axioms (note that sending it to WebVOWL may not work.) Add a reasoner to your OntModel, for example ModelFactory.createOntology(OntModelSpec.OWL_MEM_RULE_INF), and writeAll() again. Can you spot any inferences?
+
ex:France ex:capital ex:Paris .
 +
</syntaxhighlight>

Latest revision as of 17:47, 4 March 2021

Lab 6: OWL 1 ("RDFS Plus / Basic OWL")

Topics

Basic OWL ontology programming with RDFlib and owlrl.

WebVOWL visualisation.

RDF and RDFS might be relevant too.

Classes/Vocabularies

Vocabulary:

  • OWL (sameAs, equivalentClass, equivalentProperty, differentFrom, disjointWith, inverseOf)
  • OWL (SymmetricProperty, AsymmetricProperty, ReflexiveProperty, IrreflexiveProperty, TransitiveProperty, FunctionalProperty, InverseFunctionalProperty)

Tasks

Task 1

Write OWL triples that corresponds to the following text. .If you can, try to build on your example from labs 2 and 7, or extend the triples at the bottom of the page. OWL can be imported from rdflib.namespace.

Cade and Emma are two different persons. The country USA above is the same as the DBpedia resource http://dbpedia.org/resource/United_States (dbr:United_States) and the GeoNames resource http://sws.geonames.org/6252001/ (gn:6252001). The person class (the RDF type the Cade and Emma resources) in your graph is the same as FOAF's, schema.org's and AKT's person classes (they are http://xmlns.com/foaf/0.1/Person, http://schema.org/Person, and http://www.aktors.org/ontology/portal#Person, respectively. Nothing can be any two of a person, a university, or a city at the same time. The property you have used in your RDF/RDFS graph to represent that 94709 is the US zip code of Berkeley, California in US is a subproperty of VCard's postal code-property (http://www.w3.org/2006/vcard/ns#postal-code). No two US cities can have the same postal code. The property you have used for Emma living in Valencia is the same property as FOAF's based near-property (http://xmlns.com/foaf/0.1/based_near), and it is the inverse of DBpedia's hometown property (http://dbpedia.org/ontology/hometown, dbo:hometown). (This is not completely precise: but "hometown" is perhaps the inverse of a subproperty of "based near".)


Task 2

g.add((ex.Cade, ex.married, ex.Mary))
g.add((ex.Cade, ex.livesWith, ex.Mary))
g.add((ex.Cade, ex.sibling, ex.Andrew))
g.add((ex.Cade, ex.hasFather, ex.Bob))
g.add((ex.Bob, ex.fatherOf, ex.Cade))

Look through the predicates(properties) above and add new triples for each one that describes them as any of the following: a reflexive , irreflexive, symmetric, asymmetric, transitive, functional, or an Inverse Functional Property. e.g

g.add((ex.married, RDF.type, OWL.SymmetricProperty))

Task 3

Print/Serialize the ontology. Then use owlrl like seen below to infer additional triples. Can you spot the many inferences?

# These three lines add inferred triples to the graph.
owl = owlrl.CombinedClosure.RDFS_OWLRL_Semantics(g, False, False, False)
owl.closure()
owl.flush_stored_triples()

Finally write the ontology to a TURTLE file, and try to visualise it using http://visualdataweb.de/webvowl/ . WebVOWL is oriented towards visualising classes and their properties, so the individuals may not show.


Useful Reading

Triples you can extend for the tasks

Python

import owlrl
from rdflib import Graph, Namespace, Literal, URIRef
from rdflib.namespace import RDF, RDFS, XSD, FOAF, OWL

g = Graph()

# Namespaces
ex = Namespace("http://example.org/")
dbp = Namespace("http://dbpedia.org/resource/")
geo = Namespace("http://sws.geonames.org/")
schema = Namespace("https://schema.org/")
akt = Namespace("http://www.aktors.org/ontology/portal#")
vcard = Namespace("http://www.w3.org/2006/vcard/ns#")

g.bind("ex", ex)
g.bind("owl", OWL)


# RDFS Tasks from last time.
g.add((ex.Cade, ex.degreeFrom, ex.University_of_California))
g.add((ex.Emma, ex.degreeFrom, ex.University_of_Valencia))
g.add((ex.Cade, ex.degreeSubject, ex.Biology))
g.add((ex.Emma, ex.degreeSubject, ex.Chemistry))
g.add((ex.University_of_California, RDF.type, ex.University))
g.add((ex.University_of_Valencia, RDF.type, ex.University))
g.add((ex.University, RDFS.subClassOf, ex.Higher_Education_Institution))
g.add((ex.expertise, RDFS.range, ex.Subject))
g.add((ex.expertise, RDFS.domain, FOAF.Person))
g.add((ex.degreeSubject, RDFS.subPropertyOf, ex.expertise))
g.add((ex.graduated, RDFS.range, ex.Higher_Education_Institution))
g.add((ex.graduated, RDFS.domain, FOAF.Person))
g.add((ex.degreeFrom, RDFS.subPropertyOf, ex.graduated))
g.add((ex.Biology, RDFS.label, Literal("Biology", lang="en")))
g.add((ex.Biology, RDFS.label, Literal("La Biologie", lang="fr")))
g.add((ex.Biology, RDFS.comment, Literal("Biology is a natural science concerned with the study of life and living organisms, including their structure, function, growth, evolution, distribution, identification and taxonomy.")))
g.add((ex.Chemistry, RDFS.label, Literal("Chemistry", lang="en")))
g.add((ex.Chemistry, RDFS.label, Literal("La Chimie", lang="fr")))
g.add((ex.Chemistry, RDFS.comment, Literal("Chemistry is a branch of physical science that studies the composition, structure, properties and change of matter.", lang="en")))

# Write OWL triples here

Turtle

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

ex:Cade ex:degreeFrom ex:University_of_California ;
    ex:degreeSubject ex:Biology .

ex:Emma ex:degreeFrom ex:University_of_Valencia ;
    ex:degreeSubject ex:Chemistry .

ex:degreeFrom rdfs:subPropertyOf ex:graduated .

ex:degreeSubject rdfs:subPropertyOf ex:expertise .

ex:Biology rdfs:label "Biology"@en,
        "La Biologie"@fr ;
    rdfs:comment "Biology is a natural science concerned with the study of life and living organisms, including their structure, function, growth, evolution, distribution, identification and taxonomy." .

ex:Chemistry rdfs:label "Chemistry"@en,
        "La Chimie"@fr ;
    rdfs:comment "Chemistry is a branch of physical science that studies the composition, structure, properties and change of matter."@en .

ex:University_of_California a ex:University .

ex:University_of_Valencia a ex:University .

ex:expertise rdfs:domain <http://xmlns.com/foaf/0.1/Person> ;
    rdfs:range ex:Subject .

ex:graduated rdfs:domain <http://xmlns.com/foaf/0.1/Person> ;
    rdfs:range ex:Higher_Education_Institution .

ex:University rdfs:subClassOf ex:Higher_Education_Institution .

ex:Cade a foaf:Person ;
    ex:address [ a ex:Address ;
            ex:city ex:Berkeley ;
            ex:country ex:USA ;
            ex:postalCode "94709"^^xsd:string ;
            ex:state ex:California ;
            ex:street "1516_Henry_Street"^^xsd:string ] ;
    ex:age 27 ;
    ex:characteristic ex:Kind ;
    ex:degree [ ex:degreeField ex:Biology ;
            ex:degreeLevel "Bachelor"^^xsd:string ;
            ex:degreeSource ex:University_of_California ;
            ex:year "2011-01-01"^^xsd:gYear ] ;
    ex:interest ex:Bird,
        ex:Ecology,
        ex:Environmentalism,
        ex:Photography,
        ex:Travelling ;
    ex:married ex:Mary ;
    ex:meeting ex:Meeting1 ;
    ex:visit ex:Canada,
        ex:France,
        ex:Germany ;
    foaf:knows ex:Emma ;
    ex:name "Cade_Tracey"^^xsd:string .

ex:Mary a ex:Student;
    ex:age 26 ;
    ex:characteristic ex:Kind ;
    ex:interest ex:Biology,
        ex:Chocolate,
        ex:Hiking .

ex:Emma a foaf:Person ;
    ex:address [ a ex:Address ;
            ex:city ex:Valencia ;
            ex:country ex:Spain ;
            ex:postalCode "46020"^^xsd:string ;
            ex:street "Carrer_de_la Guardia_Civil_20"^^xsd:string ] ;
    ex:age 26 ;
    ex:degree [ ex:degreeField ex:Chemistry ;
            ex:degreeLevel "Master"^^xsd:string ;
            ex:degreeSource ex:University_of_Valencia ;
            ex:year "2015-01-01"^^xsd:gYear ] ;
    ex:expertise ex:Air_Pollution,
        ex:Toxic_Waste,
        ex:Waste_Management,
        ex:Bananas ;
    ex:interest ex:Bike_Riding,
        ex:Music,
        ex:Travelling ;
    ex:meeting ex:Meeting1 ;
    ex:visit ( ex:Portugal ex:Italy ex:France ex:Germany ex:Denmark ex:Sweden ) ;
    ex:name "Emma_Dominguez"^^xsd:string .

ex:Meeting1 a ex:Meeting ;
    ex:date "August, 2014"^^xsd:string ;
    ex:involved ex:Cade,
        ex:Emma ;
    ex:location ex:Paris .

ex:Paris a ex:City ;
    ex:capitalOf ex:France ;
    ex:locatedIn ex:France .

ex:France ex:capital ex:Paris .