In [1]:
# source: https://owlready2.readthedocs.io/en/latest/intro.html#short-example-what-can-i-do-with-owlready

from owlready2 import Thing, FunctionalProperty, Imp, sync_reasoner_pellet, get_ontology, SymmetricProperty
import owlready2 as owl2


# the url currently has no meaning
onto = get_ontology("http://example.org/test.owl#")

# provide namespace for classes via `with` statement
with onto:
    class Person(Thing):
        pass
    
    class Gender(Thing):
        pass

    class Woman(Person):
        pass
    
    class Man(Person):
        pass

    # syntax ‘domain >> range’ works by overloading the >> Operator to return an `ObjectProperty`
    # A functional property is a property that has a single value for a given instance.
    # Functional properties are created by inheriting the FunctionalProperty class.

    class hasName(Person >> str, FunctionalProperty): pass
    class hasGender(Person >> Gender, FunctionalProperty): pass


    class hasChild(Person >> Person):
        pass
    
    class hasParent(Person >> Person):
        pass
    
    class hasFather(hasParent):
        pass
    
    class hasMother(hasParent):
        pass

    class isParentOf(Person >> Person):
        pass

    class isMotherOf(isParentOf):
        inverse_property = hasMother

    class isFartherOf(isParentOf):
        inverse_property = hasFather


    class hasSibling(Person >> Person, SymmetricProperty):
        pass

    # sub-properties
    class hasBrother(hasSibling):
        pass
    
    class hasSister(hasSibling):
        pass

    class hasAunt(Person >> Person):
        pass
    
    class hasUncle(Person >> Person):
        pass


    male_g = Gender()
    female_g = Gender()

    rule = Imp()
    rule.set_as_rule("""Person(?p), hasBrother(?p, ?b) -> Man(?b)""")

    rule2 = Imp()
    rule2.set_as_rule("""Person(?p), hasParent(?p, ?r), hasBrother(?r, ?b) -> hasUncle(?p, ?b)""")


fred = Person()
legolas = Person()

alice = Person(hasName="alice", hasBrother=[fred])
bob = Person(hasName="bob", )

bob.hasBrother.append(legolas)


susan = Person(hasMother=[alice], hasFather=[bob])


sync_reasoner_pellet(infer_property_values=True, infer_data_property_values=True)
* Owlready2 * Running Pellet...
    java -Xmx2000M -cp /media/workcard/workstickdir/RST/expertise_system/ontologie_experimente/owlready2_cvk_fork/owlready2/pellet/pellet-2.3.1.jar:/media/workcard/workstickdir/RST/expertise_system/ontologie_experimente/owlready2_cvk_fork/owlready2/pellet/jena-core-2.10.0.jar:/media/workcard/workstickdir/RST/expertise_system/ontologie_experimente/owlready2_cvk_fork/owlready2/pellet/jena-arq-2.10.0.jar:/media/workcard/workstickdir/RST/expertise_system/ontologie_experimente/owlready2_cvk_fork/owlready2/pellet/xml-apis-1.4.01.jar:/media/workcard/workstickdir/RST/expertise_system/ontologie_experimente/owlready2_cvk_fork/owlready2/pellet/owlapi-distribution-3.4.3-bin.jar:/media/workcard/workstickdir/RST/expertise_system/ontologie_experimente/owlready2_cvk_fork/owlready2/pellet/log4j-1.2.16.jar:/media/workcard/workstickdir/RST/expertise_system/ontologie_experimente/owlready2_cvk_fork/owlready2/pellet/xercesImpl-2.10.0.jar:/media/workcard/workstickdir/RST/expertise_system/ontologie_experimente/owlready2_cvk_fork/owlready2/pellet/jena-iri-0.9.5.jar:/media/workcard/workstickdir/RST/expertise_system/ontologie_experimente/owlready2_cvk_fork/owlready2/pellet/httpcore-4.2.2.jar:/media/workcard/workstickdir/RST/expertise_system/ontologie_experimente/owlready2_cvk_fork/owlready2/pellet/commons-codec-1.6.jar:/media/workcard/workstickdir/RST/expertise_system/ontologie_experimente/owlready2_cvk_fork/owlready2/pellet/jena-tdb-0.10.0.jar:/media/workcard/workstickdir/RST/expertise_system/ontologie_experimente/owlready2_cvk_fork/owlready2/pellet/antlr-runtime-3.2.jar:/media/workcard/workstickdir/RST/expertise_system/ontologie_experimente/owlready2_cvk_fork/owlready2/pellet/aterm-java-1.6.jar:/media/workcard/workstickdir/RST/expertise_system/ontologie_experimente/owlready2_cvk_fork/owlready2/pellet/slf4j-log4j12-1.6.4.jar:/media/workcard/workstickdir/RST/expertise_system/ontologie_experimente/owlready2_cvk_fork/owlready2/pellet/antlr-3.2.jar:/media/workcard/workstickdir/RST/expertise_system/ontologie_experimente/owlready2_cvk_fork/owlready2/pellet/httpclient-4.2.3.jar:/media/workcard/workstickdir/RST/expertise_system/ontologie_experimente/owlready2_cvk_fork/owlready2/pellet/jgrapht-jdk1.5.jar:/media/workcard/workstickdir/RST/expertise_system/ontologie_experimente/owlready2_cvk_fork/owlready2/pellet/jcl-over-slf4j-1.6.4.jar:/media/workcard/workstickdir/RST/expertise_system/ontologie_experimente/owlready2_cvk_fork/owlready2/pellet/slf4j-api-1.6.4.jar pellet.Pellet realize --loader Jena --input-format N-Triples --infer-prop-values --infer-data-prop-values --ignore-imports /tmp/tmpyahg1ha8
* Owlready * Adding relation test.person3 isParentOf test.person5
* Owlready * Adding relation test.person3 hasSibling test.person1
* Owlready * Adding relation test.person2 hasSibling test.person4
* Owlready * Adding relation test.person1 hasSibling test.person3
* Owlready * Adding relation test.person4 isParentOf test.person5
* Owlready * Adding relation test.person4 hasSibling test.person2
* Owlready * Adding relation test.person5 hasParent test.person3
* Owlready * Adding relation test.person5 hasParent test.person4
* Owlready * Adding relation test.person5 hasUncle test.person2
* Owlready * Adding relation test.person5 hasUncle test.person1
* Owlready2 * Pellet took 1.0330870151519775 seconds
* Owlready * Reparenting test.person2: {test.Person} => {test.Man}
* Owlready * Reparenting test.person1: {test.Person} => {test.Man}
* Owlready * (NB: only changes on entities loaded in Python are shown, other changes are done but not listed)
In [2]:
fred.is_a # this is a consequence of rule1
Out[2]:
[test.Man]
In [3]:
legolas in susan.hasUncle # this is a consequence of rule2
Out[3]:
True