Skip to content

Commit

Permalink
parameters for generated axioms
Browse files Browse the repository at this point in the history
  • Loading branch information
muddymudskipper committed Jun 24, 2024
1 parent 5319634 commit af44cd8
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 4 deletions.
178 changes: 174 additions & 4 deletions cmem_plugin_robotreason/plugin_robotreason.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
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 StringParameterType
from cmem_plugin_base.dataintegration.types import BoolParameterType, StringParameterType
from cmem_plugin_base.dataintegration.utils import setup_cmempy_user_access
from defusedxml import minidom

Expand Down Expand Up @@ -94,20 +94,164 @@ def convert_iri_to_filename(value: str) -> str:
description="Reasoner option.",
default_value="elk",
),
PluginParameter(
param_type=BoolParameterType(),
name="sub_class",
label="SubClass",
description="Generated Axioms",
default_value=False,
advanced=True,
),
PluginParameter(
param_type=BoolParameterType(),
name="equivalent_class",
label="EquivalentClass",
description="",
default_value=False,
advanced=True,
),
PluginParameter(
param_type=BoolParameterType(),
name="disjoint_class",
label="Disjoint_Class",
description="",
default_value=False,
advanced=True,
),
PluginParameter(
param_type=BoolParameterType(),
name="Data_property_characteristic",
label="DataPropertyCharacteristic",
description="",
default_value=False,
advanced=True,
),
PluginParameter(
param_type=BoolParameterType(),
name="equivalent_data_properties",
label="EquivalentDataProperties",
description="",
default_value=False,
advanced=True,
),
PluginParameter(
param_type=BoolParameterType(),
name="sub_data_property",
label="SubDataProperty",
description="",
default_value=False,
advanced=True,
),
PluginParameter(
param_type=BoolParameterType(),
name="class_assertion",
label="ClassAssertion",
description="",
default_value=False,
advanced=True,
),
PluginParameter(
param_type=BoolParameterType(),
name="property_assertion",
label="PropertyAssertion",
description="",
default_value=False,
advanced=True,
),
PluginParameter(
param_type=BoolParameterType(),
name="equivalent_object_property",
label="EquivalentObjectProperty",
description="",
default_value=False,
advanced=True,
),
PluginParameter(
param_type=BoolParameterType(),
name="inverse_bject_properties",
label="InverseObjectProperties",
description="",
default_value=False,
advanced=True,
),
PluginParameter(
param_type=BoolParameterType(),
name="object_property_characteristic",
label="ObjectPropertyCharacteristic",
description="",
default_value=False,
advanced=True,
),
PluginParameter(
param_type=BoolParameterType(),
name="sub_object_property",
label="SubObjectProperty",
description="",
default_value=False,
advanced=True,
),
PluginParameter(
param_type=BoolParameterType(),
name="object_property_range",
label="ObjectPropertyRange",
description="",
default_value=False,
advanced=True,
),
PluginParameter(
param_type=BoolParameterType(),
name="object_property_domain",
label="ObjectPropertyDomain",
description="",
default_value=False,
advanced=True,
),
],
)
class RobotReasonPlugin(WorkflowPlugin):
"""Robot reasoning plugin"""

def __init__(
self, data_graph_iri: str, ontology_graph_iri: str, result_iri: str, reasoner: str
def __init__( # noqa: PLR0913
self,
data_graph_iri: str = "",
ontology_graph_iri: str = "",
result_iri: str = "",
reasoner: str = "elk",
sub_class: bool = True,
equivalent_class: bool = False,
disjoint_class: bool = False,
data_property_characteristic: bool = False,
equivalent_data_properties: bool = False,
sub_data_property: bool = False,
class_assertion: bool = False,
property_assertion: bool = False,
equivalent_object_property: bool = False,
inverse_object_properties: bool = False,
object_property_characteristic: bool = False,
sub_object_property: bool = False,
object_property_range: bool = False,
object_property_domain: bool = False,
) -> None:
"""Init"""
self.data_graph_iri = data_graph_iri
self.ontology_graph_iri = ontology_graph_iri
self.result_iri = result_iri
self.reasoner = reasoner
self.temp = f"robot_{uuid4().hex}"
self.sub_class = sub_class
self.equivalent_class = equivalent_class
self.disjoint_class = disjoint_class
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

def create_xml_catalog_file(self, graphs: dict) -> None:
"""Create XML catalog file"""
Expand Down Expand Up @@ -151,14 +295,40 @@ def get_graphs_tree(self) -> dict:
graphs[iri] = convert_iri_to_filename(iri)
return graphs

def set_axioms(self) -> str:
"""Set asserted axioms"""
axioms_dict = {
"sub_class": "SubClass",
"equivalent_class": "EquivalentClass",
"disjoint_class": "DisjointClass",
"data_property_characteristic": "DataPropertyCharacteristic",
"equivalent_data_properties": "EquivalentDataProperties",
"sub_data_property": "SubDataProperty",
"class_assertion": "ClassAssertion",
"property_assertion": "PropertyAssertion",
"equivalent_object_property": "EquivalentObjectProperty",
"inverse_object_properties": "InverseObjectProperties",
"object_property_characteristic": "ObjectPropertyCharacteristic",
"sub_object_property": "SubObjectProperty",
"object_property_range": "ObjectPropertyRange",
"object_property_domain": "ObjectPropertyDomain",
}

axioms = " ".join(v for k, v in axioms_dict.items() if self.__dict__[k])

if not axioms:
raise ValueError("No axioms selected.")

return axioms

def reason(self, graphs: dict) -> None:
"""Reason"""
inputs = f'--input "{self.temp}/{graphs[self.data_graph_iri]}"'
utctime = str(datetime.fromtimestamp(int(time()), tz=UTC))[:-6].replace(" ", "T") + "Z"
cmd = (
f"{ROBOT} merge {inputs} --collapse-import-closure false "
f"reason --reasoner {self.reasoner} "
f'--axiom-generators "ClassAssertion PropertyAssertion" '
f'--axiom-generators "{self.set_axioms()}" '
f"--include-indirect true "
f"--exclude-duplicate-axioms true "
f"--exclude-owl-thing true "
Expand Down
3 changes: 3 additions & 0 deletions tests/test_robotreason.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ def test_reasoner(reasoner: str, err_list: list) -> list:
ontology_graph_iri=ONTOLOGY_GRAPH_IRI,
result_iri=RESULT_GRAPH_IRI,
reasoner=reasoner,
sub_class=False,
class_assertion=True,
property_assertion=True,
).execute((), context=TestExecutionContext())

result = Graph().parse(
Expand Down

0 comments on commit af44cd8

Please sign in to comment.