{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "* Owlready2 * Running Pellet...\n", " 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\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "* Owlready * Adding relation test.person3 isParentOf test.person5\n", "* Owlready * Adding relation test.person3 hasSibling test.person1\n", "* Owlready * Adding relation test.person2 hasSibling test.person4\n", "* Owlready * Adding relation test.person1 hasSibling test.person3\n", "* Owlready * Adding relation test.person4 isParentOf test.person5\n", "* Owlready * Adding relation test.person4 hasSibling test.person2\n", "* Owlready * Adding relation test.person5 hasParent test.person3\n", "* Owlready * Adding relation test.person5 hasParent test.person4\n", "* Owlready * Adding relation test.person5 hasUncle test.person2\n", "* Owlready * Adding relation test.person5 hasUncle test.person1\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "* Owlready2 * Pellet took 1.0330870151519775 seconds\n", "* Owlready * Reparenting test.person2: {test.Person} => {test.Man}\n", "* Owlready * Reparenting test.person1: {test.Person} => {test.Man}\n", "* Owlready * (NB: only changes on entities loaded in Python are shown, other changes are done but not listed)\n" ] } ], "source": [ "# source: https://owlready2.readthedocs.io/en/latest/intro.html#short-example-what-can-i-do-with-owlready\n", "\n", "from owlready2 import Thing, FunctionalProperty, Imp, sync_reasoner_pellet, get_ontology, SymmetricProperty\n", "import owlready2 as owl2\n", "\n", "\n", "# the url currently has no meaning\n", "onto = get_ontology(\"http://example.org/test.owl#\")\n", "\n", "# provide namespace for classes via `with` statement\n", "with onto:\n", " class Person(Thing):\n", " pass\n", " \n", " class Gender(Thing):\n", " pass\n", "\n", " class Woman(Person):\n", " pass\n", " \n", " class Man(Person):\n", " pass\n", "\n", " # syntax ‘domain >> range’ works by overloading the >> Operator to return an `ObjectProperty`\n", " # A functional property is a property that has a single value for a given instance.\n", " # Functional properties are created by inheriting the FunctionalProperty class.\n", "\n", " class hasName(Person >> str, FunctionalProperty): pass\n", " class hasGender(Person >> Gender, FunctionalProperty): pass\n", "\n", "\n", " class hasChild(Person >> Person):\n", " pass\n", " \n", " class hasParent(Person >> Person):\n", " pass\n", " \n", " class hasFather(hasParent):\n", " pass\n", " \n", " class hasMother(hasParent):\n", " pass\n", "\n", " class isParentOf(Person >> Person):\n", " pass\n", "\n", " class isMotherOf(isParentOf):\n", " inverse_property = hasMother\n", "\n", " class isFartherOf(isParentOf):\n", " inverse_property = hasFather\n", "\n", "\n", " class hasSibling(Person >> Person, SymmetricProperty):\n", " pass\n", "\n", " # sub-properties\n", " class hasBrother(hasSibling):\n", " pass\n", " \n", " class hasSister(hasSibling):\n", " pass\n", "\n", " class hasAunt(Person >> Person):\n", " pass\n", " \n", " class hasUncle(Person >> Person):\n", " pass\n", "\n", "\n", " male_g = Gender()\n", " female_g = Gender()\n", "\n", " rule = Imp()\n", " rule.set_as_rule(\"\"\"Person(?p), hasBrother(?p, ?b) -> Man(?b)\"\"\")\n", "\n", " rule2 = Imp()\n", " rule2.set_as_rule(\"\"\"Person(?p), hasParent(?p, ?r), hasBrother(?r, ?b) -> hasUncle(?p, ?b)\"\"\")\n", "\n", "\n", "fred = Person()\n", "legolas = Person()\n", "\n", "alice = Person(hasName=\"alice\", hasBrother=[fred])\n", "bob = Person(hasName=\"bob\", )\n", "\n", "bob.hasBrother.append(legolas)\n", "\n", "\n", "susan = Person(hasMother=[alice], hasFather=[bob])\n", "\n", "\n", "sync_reasoner_pellet(infer_property_values=True, infer_data_property_values=True)\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[test.Man]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fred.is_a # this is a consequence of rule1" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "legolas in susan.hasUncle # this is a consequence of rule2" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.6" } }, "nbformat": 4, "nbformat_minor": 4 }