From 69875d88dbd9b08a9f863fa290abaa8f85147e33 Mon Sep 17 00:00:00 2001 From: Jamie-SA Date: Thu, 13 Aug 2026 16:27:17 -0600 Subject: [PATCH 1/2] Bug fix --- .../materialize_subclass_inferences.py | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/tools/subclass_inferences/materialize_subclass_inferences.py b/tools/subclass_inferences/materialize_subclass_inferences.py index 18ee03ce..96a4dfd6 100644 --- a/tools/subclass_inferences/materialize_subclass_inferences.py +++ b/tools/subclass_inferences/materialize_subclass_inferences.py @@ -1,7 +1,8 @@ """Materialize rdfs:subClassOf inferences to help OWL-RL reasoners.""" -import io +import os import sys +import tempfile from argparse import ArgumentParser @@ -55,15 +56,23 @@ def _run_reasoner(inputs: list[str], output_ttl: str, version: str): # Format will be guessed from the name suffix input_graph.parse(one_input) - # owlready2 does not accept TTL files. Serialize to N-Triples and stream in memory. - nt_data = input_graph.serialize(format='nt', encoding='utf-8') - nt_stream = io.BytesIO(nt_data) - - ontology = get_ontology('file:///tmp/placeholder.nt').load(fileobj=nt_stream) - - # Run OWL DL reasoner (HermiT reasoner is used by default in owlready2). - with ontology: - sync_reasoner() + # owlready2 does not accept TTL files. Convert to N-Triples. + # Close the temp file before owlready2 reopens it by path: a still-open + # NamedTemporaryFile may not have flushed its writes, and Windows refuses + # to open a file that is already open elsewhere. + temp_file = tempfile.NamedTemporaryFile('w', suffix='.nt', delete=False) + try: + input_graph_str = input_graph.serialize(format='nt') + temp_file.write(input_graph_str) + temp_file.close() + + ontology = get_ontology(f'file://{temp_file.name}').load() + + # Run OWL DL reasoner (HermiT reasoner is used by default in owlready2). + with ontology: + sync_reasoner() + finally: + os.unlink(temp_file.name) raw_output_graph = Graph().parse( data=default_world.as_rdflib_graph().serialize(format='turtle'), From a62f834ff09e8fecfc38c19b0c4a045ed3d7ba5d Mon Sep 17 00:00:00 2001 From: Jamie-SA Date: Thu, 13 Aug 2026 16:41:44 -0600 Subject: [PATCH 2/2] Add release note --- .../1499-bug-fix-materialize_subclass_inferences.py | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 docs/release_notes/1499-bug-fix-materialize_subclass_inferences.py diff --git a/docs/release_notes/1499-bug-fix-materialize_subclass_inferences.py b/docs/release_notes/1499-bug-fix-materialize_subclass_inferences.py new file mode 100644 index 00000000..29dd74a2 --- /dev/null +++ b/docs/release_notes/1499-bug-fix-materialize_subclass_inferences.py @@ -0,0 +1,4 @@ +### Patch Updates + +- Fixed a bug in materialize_subclass_inferences.py. Issue [#1498](https://github.com/semanticarts/gist/issues/1498). +