Lab: Even More OWL

From Info216
Revision as of 12:30, 14 April 2022 by Sinoa (talk | contribs) (→‎If You Have More Time)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Lab 12: Even more OWL


OWL ontology programming with owlready2.

Classes and methods

In an earlier lab, you have already used these OWL concepts:

  • (sameAs, equivalentClass, equivalentProperty, differentFrom, disjointWith, inverseOf)
  • (ReflexiveProperty, IrreflexiveProperty, SymmetricProperty, AsymmetricProperty, TransitiveProperty, FunctionalProperty, InverseFunctionalProperty),
  • (oneOf, unionOf, intersectionOf. complementOf)
  • (Restriction, onProperty)
  • (someValuesFrom, allValuesFrom, hasValue)
  • (cardinality, minCardinality, maxCardinality)
  • (qualifiedCardinality, minQualifiedCardinality, maxQualifiedCardinality, onClass)


This lab will re-write the same OWL expressions as in an earlier lab, but using owlready2 instead of rdflib.

The Project description and section What can I do with Owlready2? gives a brief introduction to installing and getting started with owlready2. You will find more documentation at Welcome to Owlready2's Documentation

For example:

# A graduate is a student with at least one degree.
with onto:
    class Student(Thing): pass
    class Degree(Thing): pass
    class hasDegree(Student >> Degree): pass
    class Graduate(Student): 
        is_a = [hasDegree.some(Degree)]


Re-write the same OWL expressions as in an earlier lab, but using owlready2 instead of rdflib:

  • anyone who is a graduate has at least one degree
  • anyone who is a university graduate has at least one degree from a university
  • a grade is either an A, B, C, D, E or F
  • a straight A student is a student that has only A grades
  • a graduate has no F grades
  • a student has a unique student number
  • each student has exactly one average grade
  • a course is either a bachelor, a master or a Ph.D course
  • a bachelor student takes only bachelor courses
  • a master student takes only master courses, except for at most one bachelor course
  • a Ph.D student takes only Ph.D courses, except for at most two masters courses
  • a Ph.D. student cannot take any bachelor course

Code to get started

(These need more testing!)

Load an ontology (will remember ontologies between sessions):

BASE = ''
onto = get_ontology(BASE)

Empty an ontology (otherwise owlready2 remembers ontologies between sessions!):

def clean_onto(onto):
    with onto:
        for ind in onto.individuals():
        for prop in
        for cls in onto.classes():

Print an ontology:

def onto2graph(onto):
    graph = Graph()'temp_owlready2/temp.nt', format='ntriples')
    graph.parse('temp_owlready2/temp.nt', format='ntriples')
    return graph

def print_onto(onto):
    g = onto2graph(onto)
    g.bind('', Namespace(BASE))

To print an ontology without standard triples (you must first define _empty_graph at the beginning of the program:

_empty_graph = onto2graph(onto)

def print_onto(onto):
    g = onto2graph(onto)
    for t in _empty_graph:
        if t in g:
    g.bind('', Namespace(BASE))

If You Have More Time

Populate the ontology with individals, such as:

with onto:
    cade = Student()
    infosci = Degree()

Try to use Hermit as in the lecture to infer additional triples. IMPORANT: Neither Hermit/Pellet nor OWL-RL are able to reason with the full OWL-DL. But unlike OWL-RL, Owlready2 supports reasoning over many types of restrictions.

Useful readings