{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import owlready2 as owl2\n", "from urllib import request\n", "import os" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Genereal Information\n", "\n", "\n", "### Problem 1:\n", "\n", "The IRI of the time ontology refers to a file in turtle syntax. This syntax is currently (2021-01-22) not supported by owlready. Thus, this ontology canont be loaded (directly) with owlready.\n", "\n", "### Problem 2:\n", "\n", "Another ontologies ABC which are in the supported \"RDF_XML\"-format might import that ontology (via ``). Owlready detects the import, tries to resolve it and fails due to problem 1.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Solution (Problem 1):\n", "\n", "Show how to convert that file programmatically (i.e. without using a GUI like protégé).\n", "\n", "- assumes https://github.com/sszuev/ont-converter to be installed (requires local build with *maven*)\n", "- `ont-converter.jar` must be located in current dir" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# download the original file\n", "\n", "url = \"http://www.w3.org/2006/time\"\n", "res = request.urlopen(url)\n", "t_path = \"time_turtle.txt\"\n", "with open(t_path, \"wb\") as t_file:\n", " # write plain bytes (without de- and encoding)\n", " t_file.write(res.read())" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# convert from Turtle to RDF_XML\n", "\n", "o_path = \"time.owl\"\n", "cmd = f\"java -jar ont-converter.jar -i {t_path} -if TURTLE -o {o_path} -of RDF_XML\"\n", "os.system(cmd)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "* Owlready2 * WARNING: DataProperty http://www.w3.org/2006/time#inXSDDateTime belongs to more than one entity types: [owl.DeprecatedProperty, owl.DatatypeProperty]; I'm trying to fix it...\n", "* Owlready2 * WARNING: DataProperty http://www.w3.org/2006/time#xsdDateTime belongs to more than one entity types: [owl.DeprecatedProperty, owl.DatatypeProperty]; I'm trying to fix it...\n" ] } ], "source": [ "fpath = \"time.owl\"\n", "onto = owl2.get_ontology(fpath).load()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[time.DayOfWeek,\n", " time.Duration,\n", " time.TemporalDuration,\n", " time.DateTimeInterval,\n", " time.ProperInterval,\n", " time.TimeZone,\n", " time.Instant,\n", " time.Interval,\n", " time.MonthOfYear,\n", " time.DateTimeDescription,\n", " time.TemporalUnit,\n", " time.TemporalEntity,\n", " time.GeneralDateTimeDescription,\n", " time.DurationDescription,\n", " time.GeneralDurationDescription,\n", " time.TemporalPosition,\n", " time.TRS,\n", " time.TimePosition,\n", " time.Year,\n", " time.January]" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(onto.classes())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Solution (Problem 2):" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "could not import 'http://ontology.eil.utoronto.ca/tove/ctime.owl'\n" ] } ], "source": [ "world2 = owl2.World()\n", "\n", "target = \"http://ontology.eil.utoronto.ca/tove/ctime.owl\"\n", "\n", "try: \n", " # while the following attempt fails, it is necessary to enable to load this ontology later\n", " world2.get_ontology(target).load()\n", "except owl2.OwlReadyOntologyParsingError:\n", " print(f\"could not import '{target}'\" )" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "* Owlready2 * WARNING: DataProperty http://www.w3.org/2006/time#inXSDDateTime belongs to more than one entity types: [owl.DeprecatedProperty, owl.DatatypeProperty]; I'm trying to fix it...\n", "* Owlready2 * WARNING: DataProperty http://www.w3.org/2006/time#xsdDateTime belongs to more than one entity types: [owl.DeprecatedProperty, owl.DatatypeProperty]; I'm trying to fix it...\n" ] } ], "source": [ "\n", "# load the RDF_XML-version of the time ontology first\n", "time_onto = world2.get_ontology(fpath).load()\n", "\n", "# load the ontology which imports the time ontology second (this seems to require the failed attempt above)\n", "test_onto = world2.get_ontology(target).load()" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "# manualy add the imported ontology to the respective data-structure\n", "\n", "test_onto.imported_ontologies.append(time_onto)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "from io import StringIO, BytesIO" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\n", "\n", " \n", " \n", " Constrained time is an extension of OWL-Time where a Cinterval is a constrained interval with hasBeginning and hasEnd ranges being a DateTime Interval that specify the min/max of the beginning and end of the interval.\n", " 2 February 2021 (recreation of work done a long time ago)\n", " Mark S. Fox\n", "Enterprise Integration Lab\n", "University of Toronto\n", "msf@eil.utoronto.ca\n", "\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " 1\n", " \n", " \n", " \n", " \n", " \n", " \n", " 1\n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", " \n", "\n", "\n", "\n", " \n", "\n", "\n", "\n", "\n", "\n" ] } ], "source": [ "# print the ctime-ontology\n", "file = BytesIO()\n", "test_onto.save(file)\n", "print(file.getvalue().decode(\"utf8\"))" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[ctime.CDateTimeInterval, ctime.ctimeThing]" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(test_onto.classes())" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[]" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(test_onto.properties())" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[ctime.ctimeThing,\n", " time.hasBeginning.exactly(1, time.DateTimeInterval),\n", " time.hasEnd.exactly(1, time.DateTimeInterval)]" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Short Version (works with freshly restarted kernel)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from owlready2 import *" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "could not import ontology on first attempt\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "* Owlready2 * WARNING: DataProperty http://www.w3.org/2006/time#inXSDDateTime belongs to more than one entity types: [owl.DeprecatedProperty, owl.DatatypeProperty]; I'm trying to fix it...\n", "* Owlready2 * WARNING: DataProperty http://www.w3.org/2006/time#xsdDateTime belongs to more than one entity types: [owl.DeprecatedProperty, owl.DatatypeProperty]; I'm trying to fix it...\n" ] } ], "source": [ "try: \n", " # while the following attempt fails, it is necessary to enable to load this ontology later\n", " ctime = default_world.get_ontology('http://ontology.eil.utoronto.ca/tove/ctime.owl').load() \n", "except OwlReadyOntologyParsingError:\n", " print(f\"could not import ontology on first attempt\" )\n", "\n", "\n", "time = default_world.get_ontology('time.owl').load()\n", "ctime = default_world.get_ontology('http://ontology.eil.utoronto.ca/tove/ctime.owl').load() \n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[ctime.ctimeThing,\n", " time.hasBeginning.exactly(1, time.DateTimeInterval),\n", " time.hasEnd.exactly(1, time.DateTimeInterval)]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# confirm that the class \"CDateTimeInterval\" from the ctime ontology is indeed a class-restriction\n", "# of the original time-ontology:\n", "\n", "ctime.CDateTimeInterval.is_a" ] } ], "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 }