Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check first versionIRI then versionInfo in ontology.get_version() #301

Merged
merged 9 commits into from
Dec 7, 2021
35 changes: 25 additions & 10 deletions ontopy/ontology.py
Original file line number Diff line number Diff line change
Expand Up @@ -1051,23 +1051,38 @@ def is_defined(self, entity):
entity = self.get_by_label(entity)
return hasattr(entity, "equivalent_to") and bool(entity.equivalent_to)

def get_version(self, as_iri=False):
def get_version(self, as_iri=False) -> str:
"""Returns the version number of the ontology as inferred from the
owl:versionIRI tag.
owl:versionINFO tag or owl:versionIRI tag.

If `as_iri` is True, the full versionIRI is returned.
"""
version_iri_storid = self.world._abbreviate(
"http://www.w3.org/2002/07/owl#versionIRI"
version_info_storid = self.world._abbreviate(
"http://www.w3.org/2002/07/owl#versionInfo"
)
tokens = self.get_triples(s=self.storid, p=version_iri_storid)
tokens = self.get_triples(s=self.storid, p=version_info_storid)
version_iri_storid = None
if not tokens:
raise TypeError(f"No versionIRI in Ontology {self.base_iri!r}")
version_iri_storid = self.world._abbreviate(
"http://www.w3.org/2002/07/owl#versionIRI"
)
tokens = self.get_triples(s=self.storid, p=version_iri_storid)
if not tokens:
raise TypeError(
"No versionIRI or versionInfo "
f"in Ontology {self.base_iri!r}"
)
_, _, obj = tokens[0]
version_iri = self.world._unabbreviate(obj)
if as_iri:
return version_iri
return infer_version(self.base_iri, version_iri)
if isinstance(obj, int):
version_iri = self.world._unabbreviate(obj)
else:
version_iri = obj.strip('"').strip("'")

if as_iri and not version_iri_storid:
francescalb marked this conversation as resolved.
Show resolved Hide resolved
return self.base_iri + version_iri
if not as_iri and version_iri_storid:
return infer_version(self.base_iri, version_iri)
return version_iri

def set_version(self, version=None, version_iri=None):
"""Assign version to ontology by asigning owl:versionIRI.
Expand Down
34 changes: 17 additions & 17 deletions tools/ontoversion
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,9 @@ import argparse
import sys

import rdflib
from rdflib.util import guess_format


def infer_version(iri, version_iri):
"""Infer version from IRI and versionIRI."""
if str(version_iri[: len(iri)]) == str(iri):
version = version_iri[len(iri) :]
else:
counter = 0
version_parts = []
for index, _ in enumerate(iri):
while iri[index] != version_iri[index + counter]:
version_parts.append(version_iri[index + counter])
counter += 1
version = "".join(version_parts)
return version.lstrip("/").rstrip("/#")
from ontopy.utils import infer_version, FMAP


def main():
Expand All @@ -34,16 +22,28 @@ def main():
help="IRI/file to OWL source to extract the version from.",
)
parser.add_argument(
"--format", "-f", default="xml", help='OWL format. Default is "xml".'
)
"--format",
"-f",
choices=set(list(FMAP.keys()) + list(FMAP.values())),
help="Ontology format. Default: Guess format with rdflib.guess_format.",
) # add accepted formats
try:
args = parser.parse_args()
except SystemExit as exc:
sys.exit(exc.code) # Exit without traceback on invalid arguments

# Extract base IRI and versionIRI
graph = rdflib.Graph()
graph.parse(args.iri.rstrip("/#"), format=args.format)

# Guess format if format not given
fmt = args.format if args.format else guess_format(args.iri, fmap=FMAP)
try:
graph.parse(args.iri, format=fmt)
except Exception as err:
print("rdflib could not parse the ontology.")
print(err)
sys.exit()

iri, version_iri = list(
graph.subject_objects(
rdflib.URIRef("http://www.w3.org/2002/07/owl#versionIRI")
Expand Down