Skip to content

Commit

Permalink
add max ram percentage parameter, axiom parameters not advanced, move…
Browse files Browse the repository at this point in the history
… common parameters to utils
  • Loading branch information
muddymudskipper committed Jun 28, 2024
1 parent 8cc6248 commit 8d692e4
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 47 deletions.
46 changes: 16 additions & 30 deletions cmem_plugin_reason/plugin_reason.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@
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.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 BoolParameterType, StringParameterType
from cmem_plugin_base.dataintegration.types import (
BoolParameterType,
StringParameterType,
)
from cmem_plugin_base.dataintegration.utils import setup_cmempy_user_access

from cmem_plugin_reason.utils import (
MAX_RAM_PERCENTAGE_PARAMETER,
ONTOLOGY_GRAPH_IRI_PARAMETER,
REASONER_PARAMETER,
REASONERS,
ROBOT,
create_xml_catalog_file,
Expand All @@ -37,6 +42,9 @@
to a specified graph. The following reasoner options are supported: ELK, Expression
Materializing Reasoner, HermiT, JFact, Structural Reasoner and Whelk.""",
parameters=[
REASONER_PARAMETER,
ONTOLOGY_GRAPH_IRI_PARAMETER,
MAX_RAM_PERCENTAGE_PARAMETER,
PluginParameter(
param_type=GraphParameterType(
classes=[
Expand All @@ -49,137 +57,110 @@
label="Data graph IRI",
description="The IRI of the input data graph.",
),
PluginParameter(
param_type=GraphParameterType(classes=["http://www.w3.org/2002/07/owl#Ontology"]),
name="ontology_graph_iri",
label="Ontology_graph_IRI",
description="The IRI of the input ontology graph.",
),
PluginParameter(
param_type=StringParameterType(),
name="result_graph_iri",
label="Result graph IRI",
description="The IRI of the output graph for the reasoning result. "
"WARNING: existing graph will be overwritten!",
),
PluginParameter(
param_type=ChoiceParameterType(REASONERS),
name="reasoner",
label="Reasoner",
description="Reasoner option.",
default_value="elk",
),
PluginParameter(
param_type=BoolParameterType(),
name="sub_class",
label="SubClass",
description="",
default_value=True,
advanced=True,
),
PluginParameter(
param_type=BoolParameterType(),
name="equivalent_class",
label="EquivalentClass",
description="",
default_value=False,
advanced=True,
),
PluginParameter(
param_type=BoolParameterType(),
name="disjoint_classes",
label="DisjointClasses",
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="Generated Axioms",
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_object_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,
),
],
)
Expand All @@ -206,6 +187,7 @@ def __init__( # noqa: PLR0913
sub_class: bool = True,
sub_data_property: bool = False,
sub_object_property: bool = False,
max_ram_percentage: int = 15,
) -> None:
"""Init"""
self.axioms = {
Expand Down Expand Up @@ -242,13 +224,16 @@ def __init__( # noqa: PLR0913
errors += "Invalid value for parameter Reasoner. "
if True not in self.axioms.values():
errors += "No axiom generator selected. "
if max_ram_percentage not in range(1, 100):
errors += "Invalid value for parameter Maximum RAM Percentage. "
if errors:
raise ValueError(errors[:-1])

self.data_graph_iri = data_graph_iri
self.ontology_graph_iri = ontology_graph_iri
self.result_graph_iri = result_graph_iri
self.reasoner = reasoner
self.max_ram_percentage = max_ram_percentage
self.temp = f"reason_{uuid4().hex}"

def get_graphs(self, graphs: dict, context: ExecutionContext) -> None:
Expand All @@ -271,7 +256,8 @@ def reason(self, graphs: dict) -> None:
data_location = f"{self.temp}/{graphs[self.data_graph_iri]}"
utctime = str(datetime.fromtimestamp(int(time()), tz=UTC))[:-6].replace(" ", "T") + "Z"
cmd = (
f'java -XX:MaxRAMPercentage=15 -jar {ROBOT} merge --input "{data_location}" '
f"java -XX:MaxRAMPercentage={self.max_ram_percentage} -jar {ROBOT} "
f'merge --input "{data_location}" '
"--collapse-import-closure false "
f"reason --reasoner {self.reasoner} "
f'--axiom-generators "{axioms}" '
Expand Down
30 changes: 13 additions & 17 deletions cmem_plugin_reason/plugin_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@
EntityPath,
EntitySchema,
)
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 BoolParameterType, StringParameterType
from cmem_plugin_base.dataintegration.utils import setup_cmempy_user_access
from pathvalidate import validate_filename

from cmem_plugin_reason.utils import (
MAX_RAM_PERCENTAGE_PARAMETER,
ONTOLOGY_GRAPH_IRI_PARAMETER,
REASONER_PARAMETER,
REASONERS,
ROBOT,
create_xml_catalog_file,
Expand All @@ -41,19 +42,9 @@
documentation="""""",
icon=Icon(package=__package__, file_name="obofoundry.png"),
parameters=[
PluginParameter(
param_type=GraphParameterType(classes=["http://www.w3.org/2002/07/owl#Ontology"]),
name="ontology_graph_iri",
label="Ontology_graph_IRI",
description="The IRI of the input ontology graph.",
),
PluginParameter(
param_type=ChoiceParameterType(REASONERS),
name="reasoner",
label="Reasoner",
description="Reasoner option.",
default_value="elk",
),
REASONER_PARAMETER,
ONTOLOGY_GRAPH_IRI_PARAMETER,
MAX_RAM_PERCENTAGE_PARAMETER,
PluginParameter(
param_type=BoolParameterType(),
name="write_md",
Expand Down Expand Up @@ -103,6 +94,7 @@ def __init__( # noqa: PLR0913
write_md: bool = False,
md_filename: str = "",
stop_at_inconsistencies: bool = False,
max_ram_percentage: int = 15,
) -> None:
errors = ""
if not validators.url(ontology_graph_iri):
Expand All @@ -116,6 +108,8 @@ def __init__( # noqa: PLR0913
validate_filename(md_filename)
except: # noqa: E722
errors += "Invalid filename for parameter Output filename. "
if max_ram_percentage not in range(1, 100):
errors += "Invalid value for parameter Maximum RAM Percentage. "
if errors:
raise ValueError(errors[:-1])

Expand All @@ -125,8 +119,9 @@ def __init__( # noqa: PLR0913
self.output_graph_iri = output_graph_iri
self.write_md = write_md
self.stop_at_inconsistencies = stop_at_inconsistencies
self.temp = f"reason_{uuid4().hex}"
self.md_filename = md_filename if md_filename and write_md else "mdfile.md"
self.max_ram_percentage = max_ram_percentage
self.temp = f"reason_{uuid4().hex}"

def get_graphs(self, graphs: dict, context: ExecutionContext) -> None:
"""Get graphs from CMEM"""
Expand All @@ -143,7 +138,8 @@ def validate(self, graphs: dict) -> None:
utctime = str(datetime.fromtimestamp(int(time()), tz=UTC))[:-6].replace(" ", "T") + "Z"

cmd = (
f'java -XX:MaxRAMPercentage=15 -jar {ROBOT} merge --input "{data_location}" '
f"java -XX:MaxRAMPercentage={self.max_ram_percentage} -jar {ROBOT} "
f'merge --input "{data_location}" '
f"explain --reasoner {self.reasoner} -M inconsistency "
f'--explanation "{self.temp}/{self.md_filename}"'
)
Expand Down
29 changes: 29 additions & 0 deletions cmem_plugin_reason/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
)

from cmem.cmempy.dp.proxy.graph import get_graph_import_tree, post_streamed
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.types import IntParameterType
from defusedxml import minidom

from . import __path__
Expand All @@ -28,6 +32,31 @@
}
)

ONTOLOGY_GRAPH_IRI_PARAMETER = PluginParameter(
param_type=GraphParameterType(classes=["http://www.w3.org/2002/07/owl#Ontology"]),
name="ontology_graph_iri",
label="Ontology_graph_IRI",
description="The IRI of the input ontology graph.",
)

REASONER_PARAMETER = PluginParameter(
param_type=ChoiceParameterType(REASONERS),
name="reasoner",
label="Reasoner",
description="Reasoner option.",
default_value="elk",
)

MAX_RAM_PERCENTAGE_PARAMETER = PluginParameter(
param_type=IntParameterType(),
name="max_ram_percentage",
label="Maximum RAM Percentage",
description="Maximum heap size for the Java virtual machine running the reasoning "
"process. ⚠️ Setting the RAM usage too high may result in an out of memory error.",
default_value=15,
advanced=True,
)


def convert_iri_to_filename(value: str) -> str:
"""Convert IRI to filename"""
Expand Down

0 comments on commit 8d692e4

Please sign in to comment.