Lab: OWL 1

From Info216
Revision as of 10:10, 19 February 2021 by Sinoa (talk | contribs) (Sinoa moved page Lab: RDFS plus to Lab: OWL 1)
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Lab 8: RDFS Plus / Basic OWL


Basic OWL ontology programming with RDFlib and owlrl.

WebVOWL visualisation.

RDF and RDFS might be relevant too.



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


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 (dbr:United_States) and the GeoNames resource (gn:6252001). The person class (the RDF type the Cade and Emma resources) in your graph is the same as FOAF's,'s and AKT's person classes (they are,, and, 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 ( 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 (, and it is the inverse of DBpedia's hometown property (, 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)

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

Useful Reading

Triples you can extend for the tasks

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

g = Graph()

# Namespaces
ex = Namespace("")
dbp = Namespace("")
geo = Namespace("")
schema = Namespace("")
akt = Namespace("")
vcard = Namespace("")

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