-
Notifications
You must be signed in to change notification settings - Fork 24
SHACL Validation #767
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
SHACL Validation #767
Changes from 8 commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
d5be7fe
Adds shacl validation to renku doctor
f8625b3
Updates SHACL schema to validate current KG
6a83b7b
Adds Unit tests for dataset and project jsonld structure
7a08904
Improves SHACL tests for project and dataset jsonld
9a34f7f
Merge branch 'master' into 755-kg-validation
d1243f3
Fixes SHACl validation and adds validation to renku log
e3bc017
Adds renku log tests
52c5bcc
Adds some provenance types to the SHACL graph
db33fc0
Merge branch 'master' into 755-kg-validation
39b2e9a
fixes rdf log output and pyshacl dependency
b9535a0
Fixes Project and Dataset SHACL tests
5339b6d
Adds workflow SHACl validation
5624fdd
Fix SHACL schema and renku log validation
f0f20d4
Fix memory leak in integration tests
b8ddbd5
Merge branch 'master' into 755-kg-validation
rokroskar 8370636
Merge branch 'master' into 755-kg-validation
Panaetius c827f3a
Fixes Python 3.5 path
bcb441b
Merge branch '755-kg-validation' of github.com:SwissDataScienceCenter…
a760d2f
Fixes sort order
901c67c
Fixes python 3.5 tests
5e72d13
Merge branch 'master' into 755-kg-validation
Panaetius 835cff1
Patches pyld to prevent contexts from overwhelming its cache
fda27ad
Merge branch '755-kg-validation' of github.com:SwissDataScienceCenter…
b776273
Merge branch 'master' into 755-kg-validation
Panaetius 04b07eb
Cleanup of pyld patch
ed81d9f
Updates documentation
b299b49
Cleanup: remove hardcoded versions from shacl shape
86e0456
Merge branch 'master' into 755-kg-validation
jsam 328f5b5
Cleanup and addresses PR comments
e868ba0
Merge branch 'master' into 755-kg-validation
Panaetius c0468b4
fix: sanitize author name for nt output
rokroskar cd13981
refactor: Creator --> Person
rokroskar 35296da
Merge branch 'master' into 755-kg-validation
rokroskar 7537693
Merge branch 'master' into 755-kg-validation
Panaetius 3a4a74d
Adapts SHACL and tests to changes on master
877113e
Changed to proper objects on loading and fix DatasetFile ids for old…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| # -*- coding: utf-8 -*- | ||
| # | ||
| # Copyright 2019 - Swiss Data Science Center (SDSC) | ||
| # A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and | ||
| # Eidgenössische Technische Hochschule Zürich (ETHZ). | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| """Check KG structure using SHACL.""" | ||
| import yaml | ||
| from pyld import jsonld as ld | ||
| from rdflib.namespace import Namespace | ||
| from rdflib.term import BNode | ||
|
|
||
| from renku.core.commands.echo import WARNING | ||
| from renku.core.models.jsonld import NoDatesSafeLoader | ||
| from renku.core.utils.shacl import validate_graph | ||
|
|
||
|
|
||
| def _shacl_graph_to_string(graph): | ||
| """Converts a shacl validation graph into human readable format.""" | ||
| sh = Namespace('http://www.w3.org/ns/shacl#') | ||
|
|
||
| problems = [] | ||
|
|
||
| for _, result in graph.subject_objects(sh.result): | ||
| path = graph.value(result, sh.resultPath) | ||
| res = graph.value(result, sh.resultMessage) | ||
|
|
||
| if res: | ||
| message = '{}: {}'.format(path, res) | ||
| else: | ||
| kind = graph.value(result, sh.sourceConstraintComponent) | ||
| focusNode = graph.value(result, sh.focusNode) | ||
|
|
||
| if isinstance(focusNode, BNode): | ||
| focusNode = '<Anonymous>' | ||
|
|
||
| message = '{}: Type: {}, Node ID: {}'.format(path, kind, focusNode) | ||
|
|
||
| problems.append(message) | ||
|
|
||
| return '\n\t'.join(problems) | ||
|
|
||
|
|
||
| def check_project_structure(client): | ||
| """Validate project metadata against SHACL.""" | ||
| project_path = client.renku_metadata_path | ||
|
|
||
| conform, graph, t = check_shacl_structure(project_path) | ||
|
|
||
| if conform: | ||
| return True, None | ||
|
|
||
| problems = ( | ||
| WARNING + 'Invalid structure of project metadata\n\t' + | ||
| _shacl_graph_to_string(graph) | ||
| ) | ||
|
|
||
| return False, problems | ||
|
|
||
|
|
||
| def check_datasets_structure(client): | ||
| """Validate dataset metadata against SHACL.""" | ||
| ok = True | ||
|
|
||
| problems = WARNING + 'Invalid structure of dataset metadata\n' | ||
|
|
||
| for path in client.renku_datasets_path.rglob(client.METADATA): | ||
| try: | ||
| conform, graph, t = check_shacl_structure(path) | ||
| except (Exception, BaseException) as e: | ||
| problems += 'Couldn\'t validate {}: {}\n\n'.format(path, e) | ||
| continue | ||
|
|
||
| if conform: | ||
| continue | ||
|
|
||
| ok = False | ||
|
|
||
| problems += str(path) + '\n\t' + _shacl_graph_to_string(graph) + '\n\n' | ||
Panaetius marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if ok: | ||
| return True, None | ||
|
|
||
| return False, problems | ||
|
|
||
|
|
||
| def check_shacl_structure(path): | ||
| """Validates all metadata aginst the SHACL schema.""" | ||
| with path.open(mode='r') as fp: | ||
| source = yaml.load(fp, Loader=NoDatesSafeLoader) or {} | ||
|
|
||
| rdf = ld.to_rdf( | ||
| source, | ||
| options={ | ||
| 'format': 'application/n-quads', | ||
| 'produceGeneralizedRdf': True | ||
| } | ||
| ) | ||
|
|
||
| return validate_graph(rdf) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| # -*- coding: utf-8 -*- | ||
| # | ||
| # Copyright 2018-2019- Swiss Data Science Center (SDSC) | ||
| # A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and | ||
| # Eidgenössische Technische Hochschule Zürich (ETHZ). | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| """JSON-LD SHACL validation.""" | ||
Panaetius marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| import json | ||
|
|
||
| from pkg_resources import resource_string | ||
| from pyshacl import validate | ||
|
|
||
|
|
||
| def validate_graph(graph, shacl_path=None, format='nquads'): | ||
| """Validate the current graph with a SHACL schema. | ||
|
|
||
| uses default schema if not supplied. | ||
Panaetius marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """ | ||
| if shacl_path: | ||
| with open(shacl_path, 'r', encoding='utf-8') as f: | ||
| shacl = f.read() | ||
| else: | ||
| shacl = resource_string('renku', 'data/shacl_shape.json') | ||
|
|
||
| return validate( | ||
| graph, | ||
| shacl_graph=shacl, | ||
| inference='rdfs', | ||
| meta_shacl=True, | ||
| debug=False, | ||
| data_graph_format=format, | ||
| shacl_graph_format='json-ld', | ||
| advanced=True | ||
| ) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.