Skip to content

Commit

Permalink
move cleanup to utils.py, annotate inferred axioms not advanced param…
Browse files Browse the repository at this point in the history
…eter
  • Loading branch information
muddymudskipper committed Jun 30, 2024
1 parent ee90828 commit b66f3c5
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 39 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p

- valid range of "Maximum RAM Percentage" parameter in Validate plugin (1-100)

### Change
### Changed

- completed validation for IRI parameters
- "Annnotate inferred subclass axioms" is now not an advanced parameter

## [1.0.0alpha3] 2024-06-28

Expand Down
25 changes: 5 additions & 20 deletions cmem_plugin_reason/plugin_reason.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
ROBOT,
create_xml_catalog_file,
get_graphs_tree,
remove_temp,
send_result,
)

Expand Down Expand Up @@ -57,7 +58,7 @@
param_type=StringParameterType(),
name="result_graph_iri",
label="Result graph IRI",
description="The IRI of the output graph for the reasoning result. ⚠️ Existing graph "
description="The IRI of the output graph for the reasoning result. ⚠️ Existing graphs "
"will be overwritten.",
),
PluginParameter(
Expand Down Expand Up @@ -162,10 +163,9 @@
param_type=BoolParameterType(),
name="annotate_inferred_axioms",
label="Annnotate inferred subclass axioms",
description="Annotate inferred subclass axioms. ⚠️ This parameter can only be enabled "
"if the only enabled axiom generator is SubClass.",
description="⚠️ This parameter can only be enabled if the only enabled axiom generator "
"is SubClass.",
default_value=False,
advanced=True,
),
],
)
Expand Down Expand Up @@ -211,7 +211,6 @@ def __init__( # noqa: PLR0913, C901
"ObjectPropertyRange": object_property_range,
"ObjectPropertyDomain": object_property_domain,
}

errors = ""
if not validators.url(data_graph_iri):
errors += 'Invalid IRI for parameter "Data graph IRI". '
Expand Down Expand Up @@ -299,20 +298,6 @@ def reason(self, graphs: dict) -> None:
raise OSError(response.stderr.decode())
raise OSError("ROBOT error")

def clean_up(self, graphs: dict) -> None:
"""Remove temporary files"""
files = ["catalog-v001.xml", "result.ttl"]
files += list(graphs.values())
for file in files:
try:
(Path(self.temp) / file).unlink()
except (OSError, FileNotFoundError) as err:
self.log.warning(f"Cannot remove file {file} ({err})")
try:
Path(self.temp).rmdir()
except (OSError, FileNotFoundError) as err:
self.log.warning(f"Cannot remove directory {self.temp} ({err})")

def execute(self, inputs: tuple, context: ExecutionContext) -> None: # noqa: ARG002
"""Execute plugin"""
setup_cmempy_user_access(context.user)
Expand All @@ -322,4 +307,4 @@ def execute(self, inputs: tuple, context: ExecutionContext) -> None: # noqa: AR
self.reason(graphs)
setup_cmempy_user_access(context.user)
send_result(self.result_graph_iri, Path(self.temp) / "result.ttl")
self.clean_up(graphs)
remove_temp(self, ["catalog-v001.xml", "result.ttl", *graphs.values()])
23 changes: 6 additions & 17 deletions cmem_plugin_reason/plugin_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
ROBOT,
create_xml_catalog_file,
get_graphs_tree,
remove_temp,
send_result,
)

Expand Down Expand Up @@ -112,7 +113,6 @@ def __init__( # noqa: PLR0913
errors += 'Invalid value for parameter "Maximum RAM Percentage". '
if errors:
raise ValueError(errors[:-1])

self.ontology_graph_iri = ontology_graph_iri
self.reasoner = reasoner
self.produce_graph = produce_graph
Expand Down Expand Up @@ -174,31 +174,20 @@ def make_resource(self, context: ExecutionContext) -> None:
replace=True,
)

def clean_up(self, graphs: dict) -> None:
"""Remove temporary files"""
files = ["catalog-v001.xml", "output.ttl", self.md_filename]
files += list(graphs.values())
for file in files:
try:
(Path(self.temp) / file).unlink()
except (OSError, FileNotFoundError) as err:
self.log.warning(f"Cannot remove file {file} ({err})")
try:
Path(self.temp).rmdir()
except (OSError, FileNotFoundError) as err:
self.log.warning(f"Cannot remove directory {self.temp} ({err})")

def execute(self, inputs: tuple, context: ExecutionContext) -> Entities | None: # noqa: ARG002
"""Run the workflow operator."""
setup_cmempy_user_access(context.user)
graphs = get_graphs_tree((self.ontology_graph_iri,))
self.get_graphs(graphs, context)
create_xml_catalog_file(self.temp, graphs)
self.validate(graphs)
files = ["catalog-v001.xml", self.md_filename, *graphs.values()]
if self.produce_graph:
files.append("output.ttl")

text = (Path(self.temp) / self.md_filename).read_text()
if text == "No explanations found.":
self.clean_up(graphs)
remove_temp(self, files)
return None

if self.produce_graph:
Expand All @@ -209,7 +198,7 @@ def execute(self, inputs: tuple, context: ExecutionContext) -> Entities | None:
setup_cmempy_user_access(context.user)
self.make_resource(context)

self.clean_up(graphs)
remove_temp(self, files)

if self.stop_at_inconsistencies:
raise RuntimeError("Inconsistencies found in Ontology.")
Expand Down
16 changes: 15 additions & 1 deletion cmem_plugin_reason/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Common functions"""
"""Common constants and functions"""

import re
import unicodedata
Expand All @@ -10,6 +10,7 @@
from cmem_plugin_base.dataintegration.description import PluginParameter
from cmem_plugin_base.dataintegration.parameter.choice import ChoiceParameterType
from cmem_plugin_base.dataintegration.parameter.graph import GraphParameterType
from cmem_plugin_base.dataintegration.plugins import WorkflowPlugin
from cmem_plugin_base.dataintegration.types import IntParameterType
from defusedxml import minidom

Expand Down Expand Up @@ -105,3 +106,16 @@ def send_result(iri: str, filepath: Path) -> None:
replace=True,
content_type="text/turtle",
)


def remove_temp(plugin: WorkflowPlugin, files: list) -> None:
"""Remove temproray files"""
for file in files:
try:
(Path(plugin.temp) / file).unlink()
except (OSError, FileNotFoundError) as err:
plugin.log.warning(f"Cannot remove file {file} ({err})")
try:
Path(plugin.temp).rmdir()
except (OSError, FileNotFoundError) as err:
plugin.log.warning(f"Cannot remove directory {plugin.temp} ({err})")

0 comments on commit b66f3c5

Please sign in to comment.