Skip to content

Commit

Permalink
use GraphParameterType(allow_only_autocompleted_values=False) instead…
Browse files Browse the repository at this point in the history
… of custom parameter for output graph IRI
  • Loading branch information
muddymudskipper committed Aug 8, 2024
1 parent 73ee56b commit dd5a654
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 111 deletions.
10 changes: 2 additions & 8 deletions cmem_plugin_reason/plugin_reason.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
MAX_RAM_PERCENTAGE_DEFAULT,
MAX_RAM_PERCENTAGE_PARAMETER,
ONTOLOGY_GRAPH_IRI_PARAMETER,
OUTPUT_GRAPH_IRI_PARAMETER,
REASONERS,
VALIDATE_PROFILES_PARAMETER,
GraphParameterTypeNew,
create_xml_catalog_file,
get_graphs_tree,
get_provenance,
Expand All @@ -51,6 +51,7 @@
ELK, Expression Materializing Reasoner, HermiT, JFact, Structural Reasoner and Whelk.""",
parameters=[
ONTOLOGY_GRAPH_IRI_PARAMETER,
OUTPUT_GRAPH_IRI_PARAMETER,
VALIDATE_PROFILES_PARAMETER,
MAX_RAM_PERCENTAGE_PARAMETER,
PluginParameter(
Expand All @@ -72,13 +73,6 @@
label="Data graph IRI",
description="The IRI of the input data graph.",
),
PluginParameter(
param_type=GraphParameterTypeNew(),
name="output_graph_iri",
label="Result graph IRI",
description="The IRI of the output graph for the reasoning result. ⚠️ Existing graphs "
"will be overwritten.",
),
PluginParameter(
param_type=BoolParameterType(),
name="sub_class",
Expand Down
10 changes: 2 additions & 8 deletions cmem_plugin_reason/plugin_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
MAX_RAM_PERCENTAGE_DEFAULT,
MAX_RAM_PERCENTAGE_PARAMETER,
ONTOLOGY_GRAPH_IRI_PARAMETER,
OUTPUT_GRAPH_IRI_PARAMETER,
REASONERS,
VALIDATE_PROFILES_PARAMETER,
GraphParameterTypeNew,
create_xml_catalog_file,
get_graphs_tree,
get_provenance,
Expand Down Expand Up @@ -55,20 +55,14 @@
ONTOLOGY_GRAPH_IRI_PARAMETER,
MAX_RAM_PERCENTAGE_PARAMETER,
VALIDATE_PROFILES_PARAMETER,
OUTPUT_GRAPH_IRI_PARAMETER,
PluginParameter(
param_type=ChoiceParameterType(REASONERS),
name="reasoner",
label="Reasoner",
description="Reasoner option.",
default_value="",
),
PluginParameter(
param_type=GraphParameterTypeNew(),
name="output_graph_iri",
label="Output graph IRI",
description="The IRI of the output graph for the inconsistency validation. ⚠️ Existing "
"graphs will be overwritten.",
),
PluginParameter(
param_type=StringParameterType(),
name="md_filename",
Expand Down
106 changes: 11 additions & 95 deletions cmem_plugin_reason/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,16 @@
from pathlib import Path
from secrets import token_hex
from subprocess import CompletedProcess, run
from typing import Any
from xml.etree.ElementTree import Element, SubElement, tostring

import validators.url
from cmem.cmempy.dp.proxy.graph import get_graph_import_tree, get_graphs_list, post_streamed
from cmem.cmempy.dp.proxy.graph import get_graph_import_tree, post_streamed
from cmem.cmempy.dp.proxy.sparql import post as post_select
from cmem.cmempy.dp.proxy.update import post as post_update
from cmem_plugin_base.dataintegration.context import ExecutionContext, PluginContext
from cmem_plugin_base.dataintegration.context import ExecutionContext
from cmem_plugin_base.dataintegration.description import PluginParameter
from cmem_plugin_base.dataintegration.parameter.graph import GraphParameterType
from cmem_plugin_base.dataintegration.plugins import WorkflowPlugin
from cmem_plugin_base.dataintegration.types import (
Autocompletion,
BoolParameterType,
IntParameterType,
StringParameterType,
)
from cmem_plugin_base.dataintegration.utils import setup_cmempy_user_access
from cmem_plugin_base.dataintegration.types import BoolParameterType, IntParameterType
from defusedxml import minidom

from . import __path__
Expand Down Expand Up @@ -67,6 +59,14 @@
default_value=False,
)

OUTPUT_GRAPH_IRI_PARAMETER = PluginParameter(
param_type=GraphParameterType(allow_only_autocompleted_values=False),
name="output_graph_iri",
label="Output graph IRI",
description="""The IRI of the output graph for the inconsistency validation. ⚠️ Existing graphs
graphs will be overwritten.""",
)


def create_xml_catalog_file(dir_: str, graphs: dict) -> None:
"""Create XML catalog file"""
Expand Down Expand Up @@ -230,87 +230,3 @@ def post_profiles(plugin: WorkflowPlugin, valid_profiles: list) -> None:
}}
"""
post_update(query=query)


class GraphParameterTypeNew(StringParameterType):
"""Knowledge Graph parameter type."""

allow_only_autocompleted_values: bool = False
autocomplete_value_with_labels: bool = True
classes: set[str] | None = None

def __init__( # noqa: PLR0913
self,
show_di_graphs: bool = False,
show_system_graphs: bool = False,
show_graphs_without_class: bool = False,
classes: list[str] | None = None,
allow_only_autocompleted_values: bool = True,
):
"""Knowledge Graph parameter type.
:param show_di_graphs: show DI project graphs
:param show_system_graphs: show system graphs such as shape and query catalogs
:param classes: allowed classes of the shown graphs
- if None -> defaults to di:Dataset and void:Dataset
:param allow_only_autocompleted_values: allow entering new graph URLs
"""
self.show_di_graphs = show_di_graphs
self.show_system_graphs = show_system_graphs
self.show_graphs_without_class = show_graphs_without_class
self.allow_only_autocompleted_values = allow_only_autocompleted_values
if classes:
self.classes = set(classes)
else:
self.classes = {
"https://vocab.eccenca.com/di/Dataset",
"http://rdfs.org/ns/void#Dataset",
}

def autocomplete( # noqa: C901
self,
query_terms: list[str],
depend_on_parameter_values: list[Any], # noqa: ARG002
context: PluginContext,
) -> list[Autocompletion]:
"""Autocomplete"""
setup_cmempy_user_access(context=context.user)
graphs = get_graphs_list()
result = []
for _ in graphs:
iri = _["iri"]
title = _["label"]["title"]
label = f"{title} ({iri})"
assigned_classes = set(_["assignedClasses"])
# ignore DI project graphs
if self.show_di_graphs is False and _["diProjectGraph"]:
continue
# ignore system resource graphs
if self.show_system_graphs is False and _["systemResource"]:
continue
# show graphs without assigned classes only if explicitly wanted
if not assigned_classes:
if self.show_graphs_without_class:
result.append(Autocompletion(value=iri, label=label))
continue
# ignore graphs which do not match the requested classes
if (
self.classes
and assigned_classes
and not self.classes.intersection(assigned_classes)
):
continue
str_match = True
for term in query_terms:
if term.lower() not in label.lower():
str_match = False
break
if str_match:
result.append(Autocompletion(value=iri, label=label))
if not result and len(query_terms) == 1 and validators.url(query_terms[0]):
result.append(
Autocompletion(value=query_terms[0], label=f"new graph: {query_terms[0]}")
)

result.sort(key=lambda x: x.label)
return list(set(result))

0 comments on commit dd5a654

Please sign in to comment.