Lab: Even More OWL

From info216

Lab 12: Even more OWL

Topics

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)


Owlready2

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)]


Tasks

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 = 'http://info216.uib.no/owlready2-lab/'
onto = get_ontology(BASE)

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

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

Print an ontology:

def onto2graph(onto):
    graph = Graph()
    onto.save('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))
    print(g.serialize(format='ttl'))

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

clean_onto(onto)
_empty_graph = onto2graph(onto)

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

If You Have More Time

Populate the ontology with individals, such as:

with onto:
    cade = Student()
    infosci = Degree()
    cade.hasDegree.append(infosci)

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