Lab: Even More OWL: Difference between revisions

From info216
(Created page with " =Lab 12: Even more OWL= ==Topics== OWL ontology programming with owlready2. <!-- ==Tutorial== --> ==Classes and methods== In an earlier lab, you have already used these O...")
 
No edit summary
Line 57: Line 57:
Empty an ontology (otherwise owlready2 remembers ontologies between sessions!):
Empty an ontology (otherwise owlready2 remembers ontologies between sessions!):
<syntaxhighlight>
<syntaxhighlight>
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)
</syntaxhighlight>
</syntaxhighlight>


Print an ontology:
Print an ontology:
<syntaxhighlight>
<syntaxhighlight>
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'))
</syntaxhighlight>
</syntaxhighlight>


Print an ontology without standard triples:
To print an ontology without standard triples (you must first define ''_empty_graph'' at the beginning of the program:
<syntaxhighlight>
<syntaxhighlight>
</syntaxhighlight>
clean_onto(onto)
_empty_graph = onto2graph(onto)


To print an ontology without standard triples, you must first run this code at the beginning of the program:
def print_onto(onto):
<syntaxhighlight>
    g = onto2graph(onto)
    for t in _empty_graph:
        if t in g:
            g.remove(t)
    g.bind('', Namespace(BASE))
    print(g.serialize(format='ttl'))
</syntaxhighlight>
</syntaxhighlight>


Line 85: Line 109:


==Useful readings==
==Useful readings==
* [https://www.w3.org/TR/owl-primer/ OWL2 Primer]
* [https://pypi.org/project/Owlready2/ Project description] and ''What can I do with Owlready2?''
* [https://www.w3.org/TR/2012/REC-owl2-quick-reference-20121211/ OWL2 Quick Reference Guide]
* [https://owlready2.readthedocs.io/en/latest/ Welcome to Owlready2's Documentation]

Revision as of 11:28, 14 April 2022

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

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 reaosning over many types of restrictions.

Useful readings