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

Make it possible to run HermiT on EMMO #740

Merged
merged 5 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 48 additions & 3 deletions ontopy/ontology.py
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,7 @@
write_catalog_file=False,
append_catalog=False,
catalog_file="catalog-v001.xml",
**kwargs,
) -> Path:
"""Writes the ontology to file.

Expand Down Expand Up @@ -915,6 +916,7 @@
# pylint: disable=redefined-builtin,too-many-arguments
# pylint: disable=too-many-statements,too-many-branches
# pylint: disable=too-many-locals,arguments-renamed,invalid-name

if not _validate_installed_version(
package="rdflib", min_version="6.0.0"
) and format == FMAP.get("ttl", ""):
Expand All @@ -940,7 +942,9 @@
raise TypeError("`filename` and `format` cannot both be None.")
else:
file = filename
filepath = os.path.join(dir, file)
filepath = os.path.join(
dir, file if isinstance(file, (str, Path)) else file.name
)
returnpath = filepath

dir = Path(filepath).resolve().parent
Expand Down Expand Up @@ -980,6 +984,7 @@
recursive=False,
squash=False,
write_catalog_file=False,
**kwargs,
)

if write_catalog_file:
Expand Down Expand Up @@ -1045,7 +1050,7 @@

graph.serialize(destination=filepath, format=format)
elif format in OWLREADY2_FORMATS:
super().save(file=filepath, format=fmt)
super().save(file=filepath, format=fmt, **kwargs)
else:
# The try-finally clause is needed for cleanup and because
# we have to provide delete=False to NamedTemporaryFile
Expand All @@ -1056,7 +1061,7 @@
suffix=".owl", delete=False
) as handle:
tmpfile = handle.name
super().save(tmpfile, format="ntriples")
super().save(tmpfile, format="ntriples", **kwargs)
graph = rdflib.Graph()
graph.parse(tmpfile, format="ntriples")
graph.namespace_manager.bind(
Expand Down Expand Up @@ -1331,12 +1336,46 @@

Keyword arguments are passed to the underlying owlready2 function.
"""
# pylint: disable=too-many-branches

removed_equivalent = defaultdict(list)
removed_subclasses = defaultdict(list)

if reasoner == "FaCT++":
sync = sync_reasoner_factpp
elif reasoner == "Pellet":
sync = owlready2.sync_reasoner_pellet
elif reasoner == "HermiT":
sync = owlready2.sync_reasoner_hermit

# Remove custom data propertyes, otherwise HermiT will crash
datatype_iri = "http://www.w3.org/2000/01/rdf-schema#Datatype"

for cls in self.classes(imported=include_imported):
remove_eq = []
for i, r in enumerate(cls.equivalent_to):
if isinstance(r, owlready2.Restriction):
if (

Check warning on line 1358 in ontopy/ontology.py

View check run for this annotation

Codecov / codecov/patch

ontopy/ontology.py#L1358

Added line #L1358 was not covered by tests
hasattr(r.value.__class__, "iri")
and r.value.__class__.iri == datatype_iri
):
remove_eq.append(i)
removed_equivalent[cls].append(r)

Check warning on line 1363 in ontopy/ontology.py

View check run for this annotation

Codecov / codecov/patch

ontopy/ontology.py#L1362-L1363

Added lines #L1362 - L1363 were not covered by tests
for i in reversed(remove_eq):
del cls.equivalent_to[i]

Check warning on line 1365 in ontopy/ontology.py

View check run for this annotation

Codecov / codecov/patch

ontopy/ontology.py#L1365

Added line #L1365 was not covered by tests

remove_subcls = []
for i, r in enumerate(cls.is_a):
if isinstance(r, owlready2.Restriction):
if (
hasattr(r.value.__class__, "iri")
and r.value.__class__.iri == datatype_iri
):
remove_subcls.append(i)
removed_subclasses[cls].append(r)

Check warning on line 1375 in ontopy/ontology.py

View check run for this annotation

Codecov / codecov/patch

ontopy/ontology.py#L1374-L1375

Added lines #L1374 - L1375 were not covered by tests
for i in reversed(remove_subcls):
del cls.is_a[i]

Check warning on line 1377 in ontopy/ontology.py

View check run for this annotation

Codecov / codecov/patch

ontopy/ontology.py#L1377

Added line #L1377 was not covered by tests

else:
raise ValueError(
f"Unknown reasoner '{reasoner}'. Supported reasoners "
Expand All @@ -1353,6 +1392,12 @@
else:
sync(self, **kwargs)

# Restore removed custom data properties
for cls, eqs in removed_equivalent.items():
cls.extend(eqs)

Check warning on line 1397 in ontopy/ontology.py

View check run for this annotation

Codecov / codecov/patch

ontopy/ontology.py#L1397

Added line #L1397 was not covered by tests
for cls, subcls in removed_subclasses.items():
cls.extend(subcls)

Check warning on line 1399 in ontopy/ontology.py

View check run for this annotation

Codecov / codecov/patch

ontopy/ontology.py#L1399

Added line #L1399 was not covered by tests

def sync_attributes( # pylint: disable=too-many-branches
self,
name_policy=None,
Expand Down
18 changes: 17 additions & 1 deletion tools/ontoconvert
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ from rdflib.util import guess_format
from ontopy import get_ontology
from ontopy.utils import annotate_source, rename_iris, copy_annotation

import owlready2 # pylint: disable=wrong-import-order
import owlready2.reasoning # pylint: disable=wrong-import-order


def main(argv: list = None):
"""Main run function.
Expand Down Expand Up @@ -116,7 +119,14 @@ def main(argv: list = None):
action="store_true",
help="Do not infer imported ontologies.",
)
# To be implemented...
parser.add_argument(
"--java-executable",
help="Path to Java executable to use. Default is `java`.",
)
parser.add_argument(
"--java-memory",
help="Maximum memory allocated to Java in MB. Default is 2000.",
)
parser.add_argument(
"--iri",
"-I",
Expand Down Expand Up @@ -218,6 +228,12 @@ def main(argv: list = None):
if not output_format:
output_format = "xml"

# Settings for running Java
if args.java_executable:
owlready2.JAVA_EXE = args.java_executable
if args.java_memory:
owlready2.reasoning.JAVA_MEMORY = int(args.java_memory)

# Annotations to copy with --copy-emmo-annotations
if args.copy_emmo_annotations:
args.copy_annotation.extend(
Expand Down
Loading