Skip to content

Commit

Permalink
add parameter to get valid OWL profiles from input
Browse files Browse the repository at this point in the history
  • Loading branch information
muddymudskipper committed Jul 9, 2024
1 parent e9b0622 commit 783a061
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
47 changes: 41 additions & 6 deletions cmem_plugin_reason/plugin_reason.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Reasoning workflow plugin module"""

from collections.abc import Sequence
from datetime import UTC, datetime
from pathlib import Path
from tempfile import TemporaryDirectory
Expand All @@ -9,6 +10,7 @@
from cmem.cmempy.dp.proxy.graph import get
from cmem_plugin_base.dataintegration.context import ExecutionContext
from cmem_plugin_base.dataintegration.description import Icon, Plugin, PluginParameter
from cmem_plugin_base.dataintegration.entity import Entities
from cmem_plugin_base.dataintegration.parameter.graph import GraphParameterType
from cmem_plugin_base.dataintegration.plugins import WorkflowPlugin
from cmem_plugin_base.dataintegration.types import BoolParameterType, StringParameterType
Expand All @@ -26,7 +28,6 @@
get_provenance,
post_profiles,
post_provenance,
remove_temp,
robot,
send_result,
validate_profiles,
Expand Down Expand Up @@ -162,6 +163,15 @@
description="",
default_value=False,
),
PluginParameter(
param_type=BoolParameterType(),
name="input_profiles",
label="Process valid OWL profiles from input",
description="""If the "validate OWL profiles" is enabled, take values from the input on
path "profile" instead of running the validation in the plugin.""",
default_value=False,
advanced=True,
),
],
)
class ReasonPlugin(WorkflowPlugin):
Expand All @@ -188,6 +198,7 @@ def __init__( # noqa: PLR0913
sub_data_property: bool = False,
sub_object_property: bool = False,
validate_profile: bool = False,
input_profiles: bool = False,
max_ram_percentage: int = MAX_RAM_PERCENTAGE_DEFAULT,
) -> None:
self.axioms = {
Expand Down Expand Up @@ -244,6 +255,7 @@ def __init__( # noqa: PLR0913
self.output_graph_iri = output_graph_iri
self.reasoner = reasoner
self.validate_profile = validate_profile
self.input_profiles = input_profiles
self.max_ram_percentage = max_ram_percentage

def get_graphs(self, graphs: dict, context: ExecutionContext) -> None:
Expand Down Expand Up @@ -296,8 +308,32 @@ def reason(self, graphs: dict) -> None:
raise OSError(response.stderr.decode())
raise OSError("ROBOT error")

def _execute(self, context: ExecutionContext) -> None:
def post_valid_profiles(self, inputs: Sequence[Entities], graphs: dict) -> None:
"""Post valid profiles. Optionally get valid profiles from input."""
if self.input_profiles:
values = next(inputs[0].entities).values
paths = [p.path for p in inputs[0].schema.paths]
validated_ontology = values[paths.index("ontology")][0]
valid_profiles = values[paths.index("profile")]
if validated_ontology != self.ontology_graph_iri:
raise ValueError(
"The ontology IRI validated with Validate differs from the input ontology IRI."
)
else:
valid_profiles = validate_profiles(self, graphs)
post_profiles(self, valid_profiles)

def _execute(self, inputs: Sequence[Entities], context: ExecutionContext) -> None:
"""`Execute plugin"""
if self.input_profiles:
if not inputs:
raise OSError(
'Input entities needed if "Process valid OWL profiles from input" is enabled'
)
paths = [p.path for p in inputs[0].schema.paths]
if "profile" not in paths or "ontology" not in paths:
raise ValueError("Invalid input for processing OWL profiles")

setup_cmempy_user_access(context.user)
graphs = get_graphs_tree((self.data_graph_iri, self.ontology_graph_iri))
self.get_graphs(graphs, context)
Expand All @@ -306,11 +342,10 @@ def _execute(self, context: ExecutionContext) -> None:
setup_cmempy_user_access(context.user)
send_result(self.output_graph_iri, Path(self.temp) / "result.ttl")
if self.validate_profile:
post_profiles(self, validate_profiles(self, graphs))
self.post_valid_profiles(inputs, graphs)
post_provenance(self, get_provenance(self, context))
remove_temp(self)

def execute(self, inputs: tuple, context: ExecutionContext) -> None: # noqa: ARG002
def execute(self, inputs: Sequence[Entities], context: ExecutionContext) -> None:
"""Remove temp files on error"""
with TemporaryDirectory() as self.temp:
self._execute(context)
self._execute(inputs, context)
4 changes: 2 additions & 2 deletions cmem_plugin_reason/plugin_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ def add_profiles(self, valid_profiles: list) -> list:

def make_entities(self, text: str, valid_profiles: list) -> Entities:
"""Make entities"""
values = [[text]]
paths = [EntityPath(path="markdown")]
values = [[text], [self.ontology_graph_iri]]
paths = [EntityPath(path="markdown"), EntityPath(path="ontology")]
if self.validate_profile:
values.append(valid_profiles)
paths.append(EntityPath(path="profile"))
Expand Down
2 changes: 1 addition & 1 deletion tests/test_reason.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def test_validate(errors: str) -> str:
if next(iter(result.entities)).values[0][0] != md_test: # type: ignore[union-attr]
val_errors += 'EntityPath "markdown" output error. '

if next(iter(result.entities)).values[1] != ["Full", "DL", "EL", "QL", "RL"]: # type: ignore[union-attr]
if next(iter(result.entities)).values[2] != ["Full", "DL", "EL", "QL", "RL"]: # type: ignore[union-attr]
val_errors += 'EntityPath "profile" output error. '

if md_test != get_resource(PROJECT_ID, MD_FILENAME).decode():
Expand Down

0 comments on commit 783a061

Please sign in to comment.