From dfac802954d2e780f7d36b143821e5844a239a97 Mon Sep 17 00:00:00 2001 From: muddymudskipper Date: Tue, 2 Jul 2024 12:53:05 +0100 Subject: [PATCH] fix Reason parameter provenance, new iri for plugin in provenance data --- cmem_plugin_reason/plugin_reason.py | 14 ++++++++++++++ cmem_plugin_reason/utils.py | 14 ++++++++++---- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/cmem_plugin_reason/plugin_reason.py b/cmem_plugin_reason/plugin_reason.py index 9f3dc76..7666285 100644 --- a/cmem_plugin_reason/plugin_reason.py +++ b/cmem_plugin_reason/plugin_reason.py @@ -187,6 +187,20 @@ def __init__( # noqa: PLR0913 sub_object_property: bool = False, max_ram_percentage: int = MAX_RAM_PERCENTAGE_DEFAULT, ) -> None: + self.sub_class = sub_class + self.equivalent_class = equivalent_class + self.disjoint_classes = disjoint_classes + self.data_property_characteristic = data_property_characteristic + self.equivalent_data_properties = equivalent_data_properties + self.sub_data_property = sub_data_property + self.class_assertion = class_assertion + self.property_assertion = property_assertion + self.equivalent_object_property = equivalent_object_property + self.inverse_object_properties = inverse_object_properties + self.object_property_characteristic = object_property_characteristic + self.sub_object_property = sub_object_property + self.object_property_range = object_property_range + self.object_property_domain = object_property_domain self.axioms = { "SubClass": sub_class, "EquivalentClass": equivalent_class, diff --git a/cmem_plugin_reason/utils.py b/cmem_plugin_reason/utils.py index bc54abe..c243d2e 100644 --- a/cmem_plugin_reason/utils.py +++ b/cmem_plugin_reason/utils.py @@ -5,6 +5,7 @@ import unicodedata from collections import OrderedDict from pathlib import Path +from secrets import token_hex from shutil import rmtree from xml.etree.ElementTree import Element, SubElement, tostring @@ -128,9 +129,10 @@ def post_provenance(plugin: WorkflowPlugin, context: ExecutionContext) -> None: project_graph = f"http://di.eccenca.com/project/{context.task.project_id()}" type_query = f""" - SELECT ?type {{ + SELECT ?type ?label {{ GRAPH <{project_graph}> {{ <{plugin_iri}> a ?type . + <{plugin_iri}> ?label . FILTER(STRSTARTS(STR(?type), "https://vocab.eccenca.com/di/functions/")) }} }}""" @@ -142,6 +144,7 @@ def post_provenance(plugin: WorkflowPlugin, context: ExecutionContext) -> None: except IndexError: plugin.log.warning("Could not add provenance data to output graph.") return + plugin_label = result["results"]["bindings"][0]["label"]["value"] param_split = ( plugin_type.replace( @@ -159,17 +162,20 @@ def post_provenance(plugin: WorkflowPlugin, context: ExecutionContext) -> None: }} }}""" + new_plugin_iri = f'{"_".join(plugin_iri.split("_")[:-1])}_{token_hex(8)}' result = json.loads(post_select(query=parameter_query)) - param_sparql = f"<{plugin_iri}> a <{plugin_type}> . " + param_sparql = "" for binding in result["results"]["bindings"]: param_iri = binding["parameter"]["value"] param_val = plugin.__dict__[binding["parameter"]["value"].split(param_split)[1]] - param_sparql += f'\n<{plugin_iri}> <{param_iri}> "{param_val}" . ' + param_sparql += f'\n<{new_plugin_iri}> <{param_iri}> "{param_val}" .' insert_query = f""" INSERT DATA {{ GRAPH <{plugin.output_graph_iri}> {{ - <{plugin.output_graph_iri}> <{plugin_iri}> . + <{plugin.output_graph_iri}> <{new_plugin_iri}> . + <{new_plugin_iri}> a <{plugin_type}>, . + <{new_plugin_iri}> "{plugin_label}" . {param_sparql} }} }}"""