Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
muddymudskipper committed Aug 13, 2024
1 parent 304abf7 commit 7cb5c87
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
36 changes: 27 additions & 9 deletions cmem_plugin_reason/plugin_reason.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@
class ReasonPlugin(WorkflowPlugin):
"""Reason plugin"""

def __init__( # noqa: PLR0913, C901
def __init__( # noqa: PLR0913 C901
self,
data_graph_iri: str = "",
ontology_graph_iri: str = "",
Expand Down Expand Up @@ -314,16 +314,20 @@ def get_graphs(self, graphs: dict, context: ExecutionContext) -> None:
f"<http://www.w3.org/2002/07/owl#imports> <{self.output_graph_iri}> ."
):
file.write(line + "\n")
if iri == self.data_graph_iri:
file.write(
f"\n<{iri}> "
f"<http://www.w3.org/2002/07/owl#imports> <{self.ontology_graph_iri}> ."
)

def reason(self, graphs: dict) -> None:
"""Reason"""
axioms = " ".join(k for k, v in self.axioms.items() if v)
data_location = f"{self.temp}/{graphs[self.data_graph_iri]}"
ontology_location = f"{self.temp}/{graphs[self.ontology_graph_iri]}"
utctime = str(datetime.fromtimestamp(int(time()), tz=UTC))[:-6].replace(" ", "T") + "Z"
cmd = (
f'merge --input "{data_location}" --input "{ontology_location}" '
f"reason --reasoner {self.reasoner} "
f'reason --input "{data_location}" '
f"--reasoner {self.reasoner} "
f'--axiom-generators "{axioms}" '
f"--include-indirect true "
f"--exclude-duplicate-axioms true "
Expand All @@ -341,10 +345,8 @@ def reason(self, graphs: dict) -> None:
f'--link-annotation dc:source "{self.data_graph_iri}" '
f'--link-annotation dc:source "{self.ontology_graph_iri}" '
f'--typed-annotation dc:created "{utctime}" xsd:dateTime '
f'--output "{self.temp}/result.ttl"'
)
if self.import_ontology:
cmd += f'--link-annotation owl:imports "{self.ontology_graph_iri}" '
cmd += f'--output "{self.temp}/result.ttl"'
response = robot(cmd, self.max_ram_percentage)
if response.returncode != 0:
if response.stdout:
Expand All @@ -365,6 +367,18 @@ def add_result_import(self) -> None:
"""
post(query=query)

def remove_ontology_import(self) -> None:
"""Add result graph import to ontology graph"""
query = f"""
DELETE DATA {{
GRAPH <{self.output_graph_iri}> {{
<{self.output_graph_iri}> <http://www.w3.org/2002/07/owl#imports>
<{self.ontology_graph_iri}>
}}
}}
"""
post(query=query)

def _execute(self, context: ExecutionContext) -> None:
"""`Execute plugin"""
setup_cmempy_user_access(context.user)
Expand All @@ -383,9 +397,13 @@ def _execute(self, context: ExecutionContext) -> None:
valid_profiles = validate_profiles(self, graphs)
post_profiles(self, valid_profiles)
post_provenance(self, get_provenance(self, context))
if self.import_result:

if self.import_result or not self.import_ontology:
setup_cmempy_user_access(context.user)
self.add_result_import()
if self.import_result:
self.add_result_import()
if not self.import_ontology:
self.remove_ontology_import()

context.report.update(
ExecutionReport(
Expand Down
7 changes: 4 additions & 3 deletions cmem_plugin_reason/plugin_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
class ValidatePlugin(WorkflowPlugin):
"""Validate plugin"""

def __init__( # noqa: PLR0913
def __init__( # noqa: PLR0913 C901
self,
ontology_graph_iri: str = "",
reasoner: str = "elk",
Expand Down Expand Up @@ -213,7 +213,7 @@ def make_entities(self, text: str, valid_profiles: list) -> Entities:
]
return Entities(entities=entities, schema=self.schema)

def _execute(self, context: ExecutionContext) -> Entities:
def _execute(self, context: ExecutionContext) -> Entities | None:
"""Run the workflow operator."""
setup_cmempy_user_access(context.user)
graphs = get_graphs_tree((self.ontology_graph_iri, self.output_graph_iri))
Expand Down Expand Up @@ -258,8 +258,9 @@ def _execute(self, context: ExecutionContext) -> Entities:
)
if self.output_entities:
return self.make_entities(text, valid_profiles)
return None

def execute(self, inputs: None, context: ExecutionContext) -> Entities: # noqa: ARG002
def execute(self, inputs: None, context: ExecutionContext) -> Entities | None: # noqa: ARG002
"""Remove temp files on error"""
context.report.update(
ExecutionReport(
Expand Down
2 changes: 2 additions & 0 deletions tests/test_reason.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def test_reasoner(reasoner: str, err_list: list) -> list:
class_assertion=True,
property_assertion=True,
validate_profile=True,
import_ontology=True,
).execute(None, context=TestExecutionContext())

result = get_remote_graph(REASON_RESULT_GRAPH_IRI)
Expand All @@ -91,6 +92,7 @@ def test_validate(errors: str) -> str:
output_graph_iri=OUTPUT_GRAPH_IRI,
validate_profile=True,
md_filename=MD_FILENAME,
output_entities=True,
).execute(None, context=TestExecutionContext(PROJECT_ID))

val_errors = ""
Expand Down

0 comments on commit 7cb5c87

Please sign in to comment.