Skip to content

Commit

Permalink
individual bool parameters for removing additional graph types
Browse files Browse the repository at this point in the history
  • Loading branch information
muddymudskipper committed Dec 1, 2023
1 parent c734e1f commit 3427167
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 36 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
### Added

- Compatibility with cmem-plugin-base v4
- Added parameter "Remove additional graph types from data graph"
- Parameters for removing additionnal graph types from the data graph"

### Fixed

Expand Down
8 changes: 1 addition & 7 deletions README-public.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,6 @@ If enabled, OWL inferencing expansion of the data graph is performed before vali

Enable SHACL Advanced Features. Default value: *false*.

### Remove additional graph types from data graph

Before validating, remove graph types that may have been added by CMEM to the data graph. Removes triples of the form `<graph> a <type>`, where `<type>` is one
of `<http://rdfs.org/ns/void#Dataset>`, `<https://vocab.eccenca.com/shui/ShapeCatalog>`, `<https://vocab.eccenca.com/dsm/ThesaurusProject>`. Default value: *false*.

## Parameter Input

In order to set options via the input the following parameter names can be used:
Expand All @@ -102,5 +97,4 @@ In order to set options via the input the following parameter names can be used:
| Meta-SHACL | meta_shacl |
| Ontology graph URI | ontology_graph_uri |
| Inference | inference |
| SHACL Advanced Features | advanced |
| Remove additional graph types from data graph | remove_graph_types |
| SHACL Advanced Features | advanced |
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,6 @@ If enabled, OWL inferencing expansion of the data graph is performed before vali

Enable SHACL Advanced Features. Default value: *false*.

### Remove additional graph types from data graph

Before validating, remove graph types that may have been added by CMEM to the data graph. Removes triples of the form `<graph> a <type>`, where `<type>` is one
of `<http://rdfs.org/ns/void#Dataset>`, `<https://vocab.eccenca.com/shui/ShapeCatalog>`, `<https://vocab.eccenca.com/dsm/ThesaurusProject>`. Default value: *false*.

## Parameter Input

In order to set options via the input the following parameter names can be used:
Expand All @@ -105,4 +100,3 @@ In order to set options via the input the following parameter names can be used:
| Ontology graph URI | ontology_graph_uri |
| Inference | inference |
| SHACL Advanced Features | advanced |
| Remove additional graph types from data graph | remove_graph_types |
67 changes: 49 additions & 18 deletions cmem_plugin_pyshacl/plugin_pyshacl.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,13 +275,32 @@ def langfilter(lbl):
),
PluginParameter(
param_type=BoolParameterType(),
name="remove_graph_types",
label="Remove additional graph types from data graph",
description="Before validating, remove graph types that may have been "
"added by CMEM to the data graph. Removes triples of the form <graph> a "
"<type>, where type is one of <http://rdfs.org/ns/void#Dataset>, "
"<https://vocab.eccenca.com/shui/ShapeCatalog>, "
"<https://vocab.eccenca.com/dsm/ThesaurusProject>.",
name="remove_dataset_graph_type",
label="Remove graph type <http://rdfs.org/ns/void#Dataset> from data graph",
description="Before validating, remove the triple <data_graph_uri> a "
"<http://rdfs.org/ns/void#Dataset> from the loaded data graph.",
default_value=False,
advanced=True,
),
PluginParameter(
param_type=BoolParameterType(),
name="remove_thesaurus_graph_type",
label="Remove graph type <https://vocab.eccenca.com/dsm/ThesaurusProject> "
"from data graph",
description="Before validating, remove the triple <data_graph_uri> a "
"<https://vocab.eccenca.com/dsm/ThesaurusProject> from the loaded data "
"graph.",
default_value=False,
advanced=True,
),
PluginParameter(
param_type=BoolParameterType(),
name="remove_shape_catalog_graph_type",
label="Remove graph type <https://vocab.eccenca.com/shui/ShapeCatalog> "
"from data graph",
description="Before validating, remove the triple <data_graph_uri> a "
"<https://vocab.eccenca.com/shui/ShapeCatalog> from the loaded data "
"graph.",
default_value=False,
advanced=True,
),
Expand Down Expand Up @@ -312,7 +331,9 @@ def __init__(
meta_shacl,
inference,
advanced,
remove_graph_types,
remove_dataset_graph_type,
remove_thesaurus_graph_type,
remove_shape_catalog_graph_type,
) -> None:
self.data_graph_uri = data_graph_uri
self.shacl_graph_uri = shacl_graph_uri
Expand All @@ -329,7 +350,9 @@ def __init__(
self.meta_shacl = meta_shacl
self.inference = inference
self.advanced = advanced
self.remove_graph_types = remove_graph_types
self.remove_dataset_graph_type = remove_dataset_graph_type
self.remove_thesaurus_graph_type = remove_thesaurus_graph_type
self.remove_shape_catalog_graph_type = remove_shape_catalog_graph_type

discover_plugins_in_module("cmem_plugin_pyshacl")
this_plugin = Plugin.plugins[0]
Expand Down Expand Up @@ -655,6 +678,13 @@ def check_parameters(self):
for param in self.graph_parameters + self.bool_parameters:
self.log.info(f"{param}: {self.__dict__[param]}")

def remove_graph_type(self, data_graph, iri):
"""
Remove triple <data_graph_uri> a <iri>
"""
self.log.info(f"Removing graph type <{iri}> from data graph")
data_graph.remove((URIRef(self.data_graph_uri), RDF.type, URIRef(iri)))

def execute(self, inputs=(), context: ExecutionContext = ExecutionContext()):
"""
execute plugin
Expand All @@ -672,15 +702,16 @@ def execute(self, inputs=(), context: ExecutionContext = ExecutionContext()):
data_graph = self.get_graph(self.data_graph_uri)
self.log.info(f"Finished loading data graph in {e_t(start)} seconds")

if self.remove_graph_types:
self.log.info("Removing additional graph types from data graph")
graph_type_iris = [
"http://rdfs.org/ns/void#Dataset",
"https://vocab.eccenca.com/shui/ShapeCatalog",
"https://vocab.eccenca.com/dsm/ThesaurusProject",
]
for iri in graph_type_iris:
data_graph.remove((URIRef(self.data_graph_uri), RDF.type, URIRef(iri)))
if self.remove_dataset_graph_type:
self.remove_graph_type(data_graph, "http://rdfs.org/ns/void#Dataset")
if self.remove_thesaurus_graph_type:
self.remove_graph_type(
data_graph, "https://vocab.eccenca.com/dsm/ThesaurusProject"
)
if self.remove_shape_catalog_graph_type:
self.remove_graph_type(
data_graph, "https://vocab.eccenca.com/shui/ShapeCatalog"
)

self.log.info(f"Loading SHACL graph <{self.shacl_graph_uri}> into memory...")
start = time()
Expand Down
10 changes: 6 additions & 4 deletions tests/test_pyshacl.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ def test_workflow_execution():
add_labels=True,
include_graphs_labels=True,
add_shui_conforms=True,
meta_shacl=False,
inference="none",
advanced=False,
remove_graph_types=False,
meta_shacl=True,
inference="both",
advanced=True,
remove_dataset_graph_type=True,
remove_thesaurus_graph_typ=True,
remove_shape_catalog_graph_type=True,
)
plugin.execute(inputs=(), context=TestExecutionContext())
res = get(validation_graph_uri)
Expand Down

0 comments on commit 3427167

Please sign in to comment.