Skip to content
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

Create python module to validate processed data #18

Closed
wants to merge 28 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
8c1dd85
add modularized validation
horstf Apr 13, 2022
d181201
add shape to test
horstf Apr 13, 2022
fc37a5e
add old dodo.py code from ont_dev branch (have to update)
horstf Apr 13, 2022
68aed79
update requirements and environment
horstf Apr 13, 2022
f49e040
add __init__.py for modularization of code
horstf May 3, 2022
644a4c9
rename emodul_validation.py to validation.py to imporve python modula…
horstf May 3, 2022
f752d17
add validation to dodo.py
horstf May 3, 2022
c4dd67b
add shape as dep to dodo.py
horstf May 3, 2022
230ac90
move and rename shape
horstf May 3, 2022
458da5e
merge main
horstf Jul 26, 2022
1e3697e
move validation into own lebedigital submodule
horstf Jul 26, 2022
f35255b
validation didnt need additional submodule structure
horstf Jul 26, 2022
379c107
add comments
horstf Jul 26, 2022
2a3e61c
add comments
horstf Aug 4, 2022
b9b3175
remove superflous comments
horstf Aug 4, 2022
c150d15
remove unused file from concrete
horstf Sep 5, 2022
8b25331
add files to test validation
horstf Sep 7, 2022
da2cebf
add validation tests
horstf Sep 7, 2022
de56554
add validation tests
horstf Sep 7, 2022
1a49eab
remove empty lines
horstf Sep 7, 2022
26e0925
add validation tests
horstf Sep 7, 2022
9e92d70
undo changes in requirements.txt
horstf Sep 8, 2022
3133735
undo changes in requirements.txt
horstf Sep 8, 2022
f0d889a
Merge remote-tracking branch 'origin/main' into validation_modulariza…
horstf Oct 7, 2022
0d501bc
add some dodo code
horstf Oct 7, 2022
3cd8a72
add output code
horstf Oct 7, 2022
a3917f9
add output code
horstf Oct 7, 2022
2678023
update dodo
horstf Jan 16, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add comments
horstf committed Aug 4, 2022

Verified

This commit was signed with the committer’s verified signature.
ExampleWasTaken ExampleWasTaken
commit 2a3e61c5d46a5f55700dc9639582ca45969f2485
62 changes: 42 additions & 20 deletions lebedigital/validation.py
Original file line number Diff line number Diff line change
@@ -3,24 +3,25 @@
from rdflib.util import guess_format
from rdflib.namespace import SH, RDF

"""
baseDir0 = Path(__file__).resolve().parents[0]
baseDir1 = Path(__file__).resolve().parents[1]
baseDir2 = Path(__file__).resolve().parents[2]
ontologyPath = os.path.join(baseDir2,'ConcreteOntology')
metadataPath = os.path.join(baseDir0,'E-modul-processed-data/emodul_metadata.csv')
graphPath = os.path.join(baseDir0,'E-modul-processed-data/EM_Graph.ttl')
processedDataPath = os.path.join(baseDir0,'E-modul-processed-data')
"""

SCHEMA = Namespace('http://schema.org/')

"""
Given a path to a shacl shape and a path to an rdf file, this function tests the rdf data against the specified shacl shapes.
The result is an rdflib graph containing the validation report, if it is empty the validation was successful.
"""

def test_graph(rdf_graph: Graph, shapes_graph: Graph) -> Graph:
"""
Tests an RDF graph against a SHACL shapes graph.
Parameters
----------
rdf_graph
An rdflib Graph object containing the triples to test against.
shapes_graph
An rdflib Graph object containing the shapes to test.
Returns
-------
result_graph
An rdflib Graph object containing the SHACL validation report (which is empty if no SHACl shapes were violated).
"""
conforms, result_graph, _ = validate(
rdf_graph,
shapes_graph,
@@ -42,11 +43,21 @@ def test_graph(rdf_graph: Graph, shapes_graph: Graph) -> Graph:

return result_graph

"""
Returns true if the given shape is violated in the report.
"""
def violates_shape(validation_report: Graph, shape: URIRef) -> bool:
"""
Returns true if the given shape is violated in the report.
Parameters
----------
validation_report
An rdflib Graph object containing a validation report from the test_graph function.
shape
A URIRef object containing the URI of a shape.
Returns
-------
True, if the specified shape appears as violated in the validation report, False otherwise.
"""
# get the class that is targeted by the specified shape
target_class = validation_report.value(shape, SH.targetClass, None, any=False)
if target_class is None:
@@ -63,10 +74,21 @@ def violates_shape(validation_report: Graph, shape: URIRef) -> bool:
# no violated class is targeted by the specified shape, thus the shape is not violated
return False

"""
Reads a graph from a file into a Graph object.
"""

def read_graph_from_file(filepath: str) -> Graph:
"""
Reads a file containing an RDF graph into an rdflib Graph object.
Parameters
----------
filepath
The path to the file containing the graph.
Returns
-------
graph
The rdflib Graph object containing the triples from the file.
"""
with open(filepath, 'r') as f:
graph = Graph()
graph.parse(file=f, format=guess_format(filepath))