import owlready2 as owl2
from urllib import request
import os
The IRI of the time ontology http://www.w3.org/2006/time 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.
Another ontologies ABC which are in the supported "RDF_XML"-format might import that ontology (via <owl:imports rdf:resource="http://www.w3.org/2006/time"/>
). Owlready detects the import, tries to resolve it and fails due to problem 1.
Show how to convert that file programmatically (i.e. without using a GUI like protégé).
ont-converter.jar
must be located in current dir# download the original file
url = "http://www.w3.org/2006/time"
res = request.urlopen(url)
t_path = "time_turtle.txt"
with open(t_path, "wb") as t_file:
# write plain bytes (without de- and encoding)
t_file.write(res.read())
# convert from Turtle to RDF_XML
o_path = "time.owl"
cmd = f"java -jar ont-converter.jar -i {t_path} -if TURTLE -o {o_path} -of RDF_XML"
os.system(cmd)
0
fpath = "time.owl"
onto = owl2.get_ontology(fpath).load()
* 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... * 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...
list(onto.classes())
[time.DayOfWeek, time.Duration, time.TemporalDuration, time.DateTimeInterval, time.ProperInterval, time.TimeZone, time.Instant, time.Interval, time.MonthOfYear, time.DateTimeDescription, time.TemporalUnit, time.TemporalEntity, time.GeneralDateTimeDescription, time.DurationDescription, time.GeneralDurationDescription, time.TemporalPosition, time.TRS, time.TimePosition, time.Year, time.January]
world2 = owl2.World()
target = "http://ontology.eil.utoronto.ca/tove/ctime.owl"
try:
# while the following attempt fails, it is necessary to enable to load this ontology later
world2.get_ontology(target).load()
except owl2.OwlReadyOntologyParsingError:
print(f"could not import '{target}'" )
could not import 'http://ontology.eil.utoronto.ca/tove/ctime.owl'
# load the RDF_XML-version of the time ontology first
time_onto = world2.get_ontology(fpath).load()
# load the ontology which imports the time ontology second (this seems to require the failed attempt above)
test_onto = world2.get_ontology(target).load()
* 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... * 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...
# manualy add the imported ontology to the respective data-structure
test_onto.imported_ontologies.append(time_onto)
from io import StringIO, BytesIO
# print the ctime-ontology
file = BytesIO()
test_onto.save(file)
print(file.getvalue().decode("utf8"))
<?xml version="1.0"?> <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:xsd="http://www.w3.org/2001/XMLSchema#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns:owl="http://www.w3.org/2002/07/owl#" xml:base="http://ontology.eil.utoronto.ca/tove/ctime" xmlns="http://ontology.eil.utoronto.ca/tove/ctime#" xmlns:term="http://purl.org/dc/terms/"> <owl:Ontology rdf:about="http://ontology.eil.utoronto.ca/tove/ctime"> <owl:imports rdf:resource="http://www.w3.org/2006/time#2016"/> <owl:imports rdf:resource="http://www.w3.org/2006/time"/> <rdfs:comment xml:lang="en">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.</rdfs:comment> <term:created xml:lang="en">2 February 2021 (recreation of work done a long time ago)</term:created> <term:creator xml:lang="en">Mark S. Fox Enterprise Integration Lab University of Toronto msf@eil.utoronto.ca</term:creator> </owl:Ontology> <owl:Class rdf:about="#CDateTimeInterval"> <rdfs:subClassOf rdf:resource="#ctimeThing"/> <rdfs:subClassOf> <owl:Restriction> <owl:onProperty rdf:resource="http://www.w3.org/2006/time#hasBeginning"/> <owl:onClass rdf:resource="http://www.w3.org/2006/time#DateTimeInterval"/> <owl:qualifiedCardinality rdf:datatype="http://www.w3.org/2001/XMLSchema#nonNegativeInteger">1</owl:qualifiedCardinality> </owl:Restriction> </rdfs:subClassOf> <rdfs:subClassOf> <owl:Restriction> <owl:onProperty rdf:resource="http://www.w3.org/2006/time#hasEnd"/> <owl:onClass rdf:resource="http://www.w3.org/2006/time#DateTimeInterval"/> <owl:qualifiedCardinality rdf:datatype="http://www.w3.org/2001/XMLSchema#nonNegativeInteger">1</owl:qualifiedCardinality> </owl:Restriction> </rdfs:subClassOf> </owl:Class> <owl:Class rdf:about="#ctimeThing"/> <rdf:Description rdf:about="http://www.w3.org/2006/time#hasBeginning"> <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#FunctionalProperty"/> </rdf:Description> <rdf:Description rdf:about="http://www.w3.org/2006/time#hasEnd"> <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#FunctionalProperty"/> </rdf:Description> </rdf:RDF>
list(test_onto.classes())
[ctime.CDateTimeInterval, ctime.ctimeThing]
list(test_onto.properties())
[]
[ctime.ctimeThing, time.hasBeginning.exactly(1, time.DateTimeInterval), time.hasEnd.exactly(1, time.DateTimeInterval)]
from owlready2 import *
try:
# while the following attempt fails, it is necessary to enable to load this ontology later
ctime = default_world.get_ontology('http://ontology.eil.utoronto.ca/tove/ctime.owl').load()
except OwlReadyOntologyParsingError:
print(f"could not import ontology on first attempt" )
time = default_world.get_ontology('time.owl').load()
ctime = default_world.get_ontology('http://ontology.eil.utoronto.ca/tove/ctime.owl').load()
could not import ontology on first attempt
* 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... * 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...
# confirm that the class "CDateTimeInterval" from the ctime ontology is indeed a class-restriction
# of the original time-ontology:
ctime.CDateTimeInterval.is_a
[ctime.ctimeThing, time.hasBeginning.exactly(1, time.DateTimeInterval), time.hasEnd.exactly(1, time.DateTimeInterval)]