-
Notifications
You must be signed in to change notification settings - Fork 2
/
demo.py
104 lines (81 loc) · 2.07 KB
/
demo.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Proof-of-concept for RDFlib atop KùzuDB
"""
import json
import typing
from icecream import ic # pylint: disable=E0401
import pandas as pd # pylint: disable=E0401
import pyshacl # type: ignore # pylint: disable=E0401
import rdflib # type: ignore # pylint: disable=E0401
rdflib.plugin.register(
"kuzudb",
rdflib.store.Store,
"graph",
"PropertyGraph",
)
if __name__ == "__main__":
## load and configure the `RDFlib` integration
graph: rdflib.Graph = rdflib.Graph(
store = "kuzudb",
identifier = "kuzudb",
)
graph.open(
configuration = json.dumps({
"db_path": "db",
"db_name": "UniKG",
}),
create = True,
)
## confirm the number of RDF triples, which should be `16`
ic(len(graph))
## run a SPARQL query
QUERY: str = """
SELECT ?src ?rel ?dst
WHERE {
?src ?rel ?dst .
}"""
df: pd.DataFrame = pd.DataFrame([
r.asdict() for r in graph.query(QUERY)
])
print(QUERY.strip())
ic(df)
## run a SHACL report on the RDF data
SHAPES_GRAPH: str = """
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix kz: <http://kuzu.io/rdf-ex#> .
kz:PersonShape
a sh:NodeShape ;
sh:targetClass kz:student ;
sh:property [
sh:path kz:age ;
sh:datatype xsd:integer
] .
"""
results: typing.Tuple = pyshacl.validate(
graph,
shacl_graph = SHAPES_GRAPH,
data_graph_format = "json-ld",
shacl_graph_format = "ttl",
#inference = "rdfs",
inplace = False,
serialize_report_graph = "ttl",
debug = False,
)
conforms, report_graph, report_text = results
print(report_text)
## run a more constrained SPARQL query
QUERY = """
PREFIX kz: <http://kuzu.io/rdf-ex#>
SELECT DISTINCT ?src ?name
WHERE {
?src a kz:faculty .
?src kz:name ?name .
}"""
df = pd.DataFrame([
r.asdict() for r in graph.query(QUERY)
])
print(QUERY.strip())
ic(df)