-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalidate-examples
executable file
·73 lines (60 loc) · 2.71 KB
/
validate-examples
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python
import argparse
import os
from pyshacl import validate
from rdflib import Graph
def load_rdfs_files(directory):
rdfs_graph = Graph()
for root, _, files in os.walk(directory):
for filename in files:
if filename.endswith(".ttl"):
print(f"Loading RDFS file: {filename}")
rdfs_file = os.path.join(root, filename)
rdfs_graph.parse(rdfs_file, format="turtle")
return rdfs_graph
def load_context_files(context_files):
context_graph = Graph()
for file in context_files:
print(f"Loading context file: {file}")
context_graph.parse(file, format="turtle")
return context_graph
def validate_turtle_against_shacl(turtle_file, shacl_file, rdfs_directory=None, context_files=None):
# Load Turtle file
data_graph = Graph()
data_graph.parse(turtle_file, format="turtle")
# Load SHACL file
shacl_graph = Graph()
shacl_graph.parse(shacl_file, format="turtle")
# Load RDFS files if directory is provided
if rdfs_directory:
rdfs_graph = load_rdfs_files(rdfs_directory)
else:
rdfs_graph = Graph()
# Load context files
if context_files:
context_graph = load_context_files(context_files)
else:
context_graph = Graph()
# Validate
conforms, results_graph, results_text = validate(data_graph=data_graph+context_graph+rdfs_graph, shacl_graph=shacl_graph)
return conforms, results_text
def validate_turtle_files_in_directory(directory, shacl_file, rdfs_directory=None, context_files=None):
for root, _, files in os.walk(directory):
for filename in files:
if filename.endswith(".ttl"):
turtle_file = os.path.join(root, filename)
print(f"Validating {turtle_file} against SHACL...")
conforms, results_text = validate_turtle_against_shacl(turtle_file, shacl_file, rdfs_directory, context_files)
print(results_text)
print(f"{turtle_file} conforms to SHACL constraints: {conforms}")
print("=" * 50)
def main():
parser = argparse.ArgumentParser(description="Validate Turtle files against a SHACL file")
parser.add_argument("directory", help="Directory containing Turtle files")
parser.add_argument("shacl_file", help="SHACL file")
parser.add_argument("--rdfs-directory", help="Directory containing RDFS files for inference (optional)")
parser.add_argument("--context-files", nargs="+", help="Arbitrary list of context Turtle files for inference (optional)")
args = parser.parse_args()
validate_turtle_files_in_directory(args.directory, args.shacl_file, args.rdfs_directory, args.context_files)
if __name__ == "__main__":
main()