Skip to content

Commit

Permalink
define schemas, remove write_md and produc_graph bool parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
muddymudskipper committed Jul 25, 2024
1 parent e49b099 commit 5f8263e
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 124 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p

## [Unreleased]


### Added

- defined input and output schema

### Changed

- raise OSError on post result graph error
- removed write_md and produce_graph bool parameters

## [1.0.0beta4] 2024-07-12

Expand Down
20 changes: 17 additions & 3 deletions cmem_plugin_reason/plugin_reason.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
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.entity import Entities, EntityPath, EntitySchema
from cmem_plugin_base.dataintegration.parameter.graph import GraphParameterType
from cmem_plugin_base.dataintegration.plugins import WorkflowPlugin
from cmem_plugin_base.dataintegration.ports import FixedNumberOfInputs, FixedSchemaPort
from cmem_plugin_base.dataintegration.types import BoolParameterType, StringParameterType
from cmem_plugin_base.dataintegration.utils import setup_cmempy_user_access
from urllib3.exceptions import InsecureRequestWarning
Expand Down Expand Up @@ -184,12 +185,12 @@
class ReasonPlugin(WorkflowPlugin):
"""Reason plugin"""

def __init__( # noqa: PLR0913
def __init__( # noqa: PLR0913, C901
self,
data_graph_iri: str = "",
ontology_graph_iri: str = "",
output_graph_iri: str = "",
reasoner: str = "elk",
reasoner: str = "",
class_assertion: bool = False,
data_property_characteristic: bool = False,
disjoint_classes: bool = False,
Expand Down Expand Up @@ -265,6 +266,19 @@ def __init__( # noqa: PLR0913
self.input_profiles = input_profiles
self.max_ram_percentage = max_ram_percentage

if input_profiles:
self.input_ports = FixedNumberOfInputs([FixedSchemaPort(self.generate_input_schema())])
else:
self.input_ports = FixedNumberOfInputs([])
self.output_port = FixedSchemaPort(EntitySchema(type_uri="", paths=[]))

def generate_input_schema(self) -> EntitySchema:
"""Generate the output schema."""
return EntitySchema(
type_uri="urn:row",
paths=[EntityPath(path="profile"), EntityPath(path="ontology")],
)

def get_graphs(self, graphs: dict, context: ExecutionContext) -> None:
"""Get graphs from CMEM"""
for graph in graphs:
Expand Down
65 changes: 26 additions & 39 deletions cmem_plugin_reason/plugin_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,9 @@
from cmem.cmempy.workspace.projects.resources.resource import create_resource
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,
Entity,
EntityPath,
EntitySchema,
)
from cmem_plugin_base.dataintegration.entity import Entities, Entity, EntityPath, EntitySchema
from cmem_plugin_base.dataintegration.plugins import WorkflowPlugin
from cmem_plugin_base.dataintegration.ports import FixedNumberOfInputs, FixedSchemaPort
from cmem_plugin_base.dataintegration.types import BoolParameterType, StringParameterType
from cmem_plugin_base.dataintegration.utils import setup_cmempy_user_access
from pathvalidate import is_valid_filename
Expand Down Expand Up @@ -59,20 +55,6 @@
ONTOLOGY_GRAPH_IRI_PARAMETER,
MAX_RAM_PERCENTAGE_PARAMETER,
VALIDATE_PROFILES_PARAMETER,
PluginParameter(
param_type=BoolParameterType(),
name="write_md",
label="Write Markdown explanation file",
description="Write Markdown file with explanation to project.",
default_value=False,
),
PluginParameter(
param_type=BoolParameterType(),
name="produce_graph",
label="Produce output graph",
description="Produce explanation graph.",
default_value=False,
),
PluginParameter(
param_type=StringParameterType(),
name="output_graph_iri",
Expand Down Expand Up @@ -104,9 +86,7 @@ def __init__( # noqa: PLR0913
self,
ontology_graph_iri: str = "",
reasoner: str = "elk",
produce_graph: bool = False,
output_graph_iri: str = "",
write_md: bool = False,
md_filename: str = "",
validate_profile: bool = False,
stop_at_inconsistencies: bool = False,
Expand All @@ -115,28 +95,30 @@ def __init__( # noqa: PLR0913
errors = ""
if not validators.url(ontology_graph_iri):
errors += 'Invalid IRI for parameter "Ontology graph IRI." '
if produce_graph and not validators.url(output_graph_iri):
if output_graph_iri and not validators.url(output_graph_iri):
errors += 'Invalid IRI for parameter "Output graph IRI". '
if produce_graph and output_graph_iri == ontology_graph_iri:
if output_graph_iri and output_graph_iri == ontology_graph_iri:
errors += "Output graph IRI cannot be the same as the Ontology graph IRI. "
if reasoner not in REASONERS:
errors += 'Invalid value for parameter "Reasoner". '
if write_md and not is_valid_filename(md_filename):
if md_filename and not is_valid_filename(md_filename):
errors += 'Invalid filename for parameter "Output filename". '
if max_ram_percentage not in range(1, 101):
errors += 'Invalid value for parameter "Maximum RAM Percentage". '
if errors:
raise ValueError(errors[:-1])
self.ontology_graph_iri = ontology_graph_iri
self.reasoner = reasoner
self.produce_graph = produce_graph
self.output_graph_iri = output_graph_iri
self.write_md = write_md
self.stop_at_inconsistencies = stop_at_inconsistencies
self.md_filename = md_filename if write_md else "mdfile.md"
self.md_filename = md_filename if md_filename else "mdfile.md"
self.validate_profile = validate_profile
self.max_ram_percentage = max_ram_percentage

self.input_ports = FixedNumberOfInputs([])
self.schema = self.generate_output_schema()
self.output_port = FixedSchemaPort(self.schema)

def get_graphs(self, graphs: dict, context: ExecutionContext) -> None:
"""Get graphs from CMEM"""
for graph in graphs:
Expand All @@ -156,7 +138,7 @@ def explain(self, graphs: dict) -> None:
f'--explanation "{self.temp}/{self.md_filename}"'
)

if self.produce_graph:
if self.output_graph_iri:
cmd += (
f' annotate --ontology-iri "{self.output_graph_iri}" '
f'--language-annotation rdfs:label "Ontology Validation Result {utctime}" en '
Expand Down Expand Up @@ -191,28 +173,32 @@ def add_profiles(self, valid_profiles: list) -> list:
if valid_profiles:
profiles_str = "\n- ".join(valid_profiles)
mdfile.write(f"- {profiles_str}\n")
if self.produce_graph:
if self.output_graph_iri:
post_profiles(self, valid_profiles)
return valid_profiles

def generate_output_schema(self) -> EntitySchema:
"""Generate the output schema."""
paths = [EntityPath(path="markdown"), EntityPath(path="ontology")]
if self.validate_profile:
paths.append(EntityPath(path="profile"))
return EntitySchema(
type_uri="https://eccenca.com/plugin_validateontology/type",
paths=paths,
)

def make_entities(self, text: str, valid_profiles: list) -> Entities:
"""Make entities"""
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"))
entities = [
Entity(
uri="https://eccenca.com/plugin_validateontology/result",
values=values,
),
]
schema = EntitySchema(
type_uri="https://eccenca.com/plugin_validateontology/type",
paths=paths,
)
return Entities(entities=entities, schema=schema)
return Entities(entities=entities, schema=self.schema)

def _execute(self, context: ExecutionContext) -> Entities:
"""Run the workflow operator."""
Expand All @@ -222,7 +208,7 @@ def _execute(self, context: ExecutionContext) -> Entities:
create_xml_catalog_file(self.temp, graphs)
self.explain(graphs)

if self.produce_graph:
if self.output_graph_iri:
setup_cmempy_user_access(context.user)
send_result(self.output_graph_iri, Path(self.temp) / "output.ttl")
setup_cmempy_user_access(context.user)
Expand All @@ -232,7 +218,8 @@ def _execute(self, context: ExecutionContext) -> Entities:
self.add_profiles(validate_profiles(self, graphs)) if self.validate_profile else []
)

if self.write_md:
# if self.write_md:
if self.md_filename:
setup_cmempy_user_access(context.user)
self.make_resource(context)

Expand Down
2 changes: 1 addition & 1 deletion cmem_plugin_reason/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
name="reasoner",
label="Reasoner",
description="Reasoner option.",
default_value="elk",
default_value="",
)

MAX_RAM_PERCENTAGE_PARAMETER = PluginParameter(
Expand Down
Loading

0 comments on commit 5f8263e

Please sign in to comment.