-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgraph.py
326 lines (260 loc) · 9.48 KB
/
graph.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
RDFlib `Store` plugin for adapting RDF/W3C functionality into KùzuDB
"""
import json
import typing
from icecream import ic # pylint: disable=E0401
import chocolate # type: ignore # pylint: disable=E0401
import kuzu # pylint: disable=E0401
from rdflib.plugins.sparql.sparql import Query, Update # pylint: disable=E0401
import rdflib # type: ignore # pylint: disable=E0401
######################################################################
## class definitions
class PropertyGraph (rdflib.store.Store):
"""
A subclass of `rdflib.store.Store` to use as a plugin, to integrate
the W3C stack in Python.
"""
QUERY_TRIPLES: str = """
MATCH (s)-[p:{}_rt|{}_lt]->(o)
RETURN s.iri, p.iri, o.iri, o.val
"""
QUERY_COUNT: str = """
MATCH (s)-[p:{}_rt]-(o)
RETURN count(*)
"""
def __init__ (
self,
*,
configuration: typing.Optional[ str ] = None,
identifier: typing.Optional[ rdflib.term.Identifier ] = None,
) -> None:
"""
Instance constructor
"""
super().__init__(configuration)
self.identifier: rdflib.term.Identifier = identifier
self.__namespace: typing.Dict[ str, rdflib.term.URIRef ] = {}
self.__prefix: typing.Dict[ rdflib.term.URIRef, str ] = {}
self.conn: typing.Optional[ kuzu.Connection ] = None
self.db_name: str = "UniKG"
######################################################################
## rdflib.store.Store implementation
@classmethod
def get_graph (
cls,
graph: rdflib.Graph,
) -> "PropertyGraph":
"""
An accessor method to extract the PropertyGraph from an RDF graph,
which is a private member of the `rdflib.Graph` object.
"""
return graph._Graph__store # type: ignore # pylint: disable=W0212
def open (
self,
configuration: str,
create: bool = False, # pylint: disable=W0613
) -> typing.Optional[ int ]:
"""
Opens the Store/connection specified by the configuration string.
"""
config_data: dict = json.loads(configuration)
db: kuzu.Database = kuzu.Database(config_data["db_path"]) # pylint: disable=C0103
self.conn = kuzu.Connection(db)
self.db_name = config_data["db_name"]
return rdflib.store.VALID_STORE
def add ( # type: ignore # pylint: disable=W0221
self,
triple: typing.Tuple,
*,
context: typing.Optional[ rdflib.graph._ContextType ] = None, # pylint: disable=W0613
quoted: bool = False, # pylint: disable=W0613
) -> None:
"""
Adds the given statement to a specific context or to the model.
The quoted argument is interpreted by formula-aware stores to indicate
this statement is quoted/hypothetical.
It should be an error to not specify a context and have the quoted
argument be `True`.
It should also be an error for the quoted argument to be `True` when
the store is not formula-aware.
"""
s, p, o = triple # pylint: disable=C0103,W0612
# DO SOMETHING?
# We're ignoring this operation, for now
def remove ( # type: ignore # pylint: disable=W0221
self,
triple_pattern: typing.Tuple,
*,
context: typing.Optional[ rdflib.graph._ContextType ] = None, # pylint: disable=W0613
) -> None:
"""
Remove the set of triples matching the pattern from the store.
"""
s, p, o = triple_pattern # pylint: disable=C0103,W0612
# DO SOMETHING?
# We're ignoring this operation, for now
def triples ( # type: ignore # pylint: disable=W0221
self,
triple_pattern: rdflib.graph._TriplePatternType,
*,
context: typing.Optional[ rdflib.graph._ContextType ] = None, # pylint: disable=W0613
debug: bool = False,
) -> typing.Iterator[
typing.Tuple[
rdflib.graph._TripleType,
typing.Iterator[typing.Optional[ rdflib.graph._ContextType ]]
]
]:
"""
A generator over all the triples matching the pattern.
triple_pattern:
Can include any objects for used for comparing against nodes in the
store, for example, REGEXTerm, URIRef, Literal, BNode, Variable, Graph,
QuotedGraph, Date? DateRange?
context:
A conjunctive query can be indicated by either providing a value of
None, or a specific context can be queries by passing a Graph instance
(if store is context aware). (currently IGNORED)
"""
query: str = self.QUERY_TRIPLES.format(self.db_name, self.db_name)
results: kuzu.query_result.QueryResult = self.conn.execute(query) # type: ignore
if debug:
ic(triple_pattern)
while results.has_next():
s_iri, p_iri, o_iri, o_val = results.get_next()
if debug:
ic(s_iri, p_iri, o_iri, o_val)
if o_iri is not None:
triple = (
rdflib.term.URIRef(s_iri),
rdflib.term.URIRef(p_iri),
rdflib.term.URIRef(o_iri),
)
else:
triple = (
rdflib.term.URIRef(s_iri),
rdflib.term.URIRef(p_iri),
rdflib.term.Literal(o_val),
)
is_match: bool = True
for i in range(3):
if triple_pattern[i] is not None and triple_pattern[i] != triple[i]:
if debug:
print("fail", i, triple)
is_match = False
break
if is_match:
if debug:
print(" !!YIELD", triple)
yield triple, self.__contexts()
def __len__ ( # type: ignore # pylint: disable=W0221,W0222
self,
*,
context: typing.Optional[ rdflib.graph._ContextType ] = None, # pylint: disable=W0613
) -> int:
"""
Number of statements in the store. This should only account for
non-quoted (asserted) statements if the context is not specified,
otherwise it should return the number of statements in the formula or
context given.
context:
a graph instance to query or None
"""
query: str = self.QUERY_COUNT.format(self.db_name)
results: kuzu.query_result.QueryResult = self.conn.execute(query) # type: ignore
count: int = results.get_next()[0]
return count
def __contexts (
self
) -> typing.Generator[ rdflib.graph._ContextType, None, None ]:
"""
A no-op, since contexts are not yet supported.
"""
# best way to return an empty generator
context_list: list = []
return (c for c in context_list)
def bind (
self,
prefix: str,
namespace: rdflib.term.URIRef,
*,
override: bool = True, # pylint: disable=W0613
) -> None:
"""
Should be identical to `Memory.bind`
"""
self.__prefix[namespace] = prefix
self.__namespace[prefix] = namespace
def namespace (
self,
prefix: str,
) -> typing.Optional[ rdflib.term.URIRef ]:
"""
Should be identical to `Memory.namespace`
"""
return self.__namespace.get(prefix, None)
def prefix (
self,
namespace: rdflib.term.URIRef,
) -> typing.Optional[ str ]:
"""
Should be identical to `Memory.prefix`
"""
return self.__prefix.get(namespace, None)
def namespaces (
self
) -> typing.Iterator[typing.Tuple[ str, rdflib.term.URIRef ]]:
"""
Should be identical to `Memory.namespaces`
"""
for prefix, namespace in self.__namespace.items():
yield prefix, namespace
def query ( # pylint: disable=W0235
self,
query: typing.Union[ Query, str ],
initNs: typing.Mapping[ str, typing.Any ], # pylint: disable=C0103
initBindings: typing.Mapping[ str, rdflib.term.Identifier ], # pylint: disable=C0103
queryGraph: typing.Any, # pylint: disable=C0103
**kwargs: typing.Any,
) -> rdflib.query.Result:
"""
queryGraph is None, a URIRef or '__UNION__'
If None the graph is specified in the query-string/object
If URIRef it specifies the graph to query,
If '__UNION__' the union of all named graphs should be queried
This is used by ConjunctiveGraphs
Values other than None obviously only makes sense for context-aware stores.)
"""
super().query(
query,
initNs,
initBindings,
queryGraph,
**chocolate.filter_args(kwargs, super().query),
)
def update ( # pylint: disable=W0235
self,
update: typing.Union[ Update, str ],
initNs: typing.Mapping[ str, typing.Any ], # pylint: disable=C0103
initBindings: typing.Mapping[ str, rdflib.term.Identifier ], # pylint: disable=C0103
queryGraph: typing.Any, # pylint: disable=C0103
**kwargs: typing.Any,
) -> None:
"""
queryGraph is None, a URIRef or '__UNION__'
If None the graph is specified in the query-string/object
If URIRef it specifies the graph to query,
If '__UNION__' the union of all named graphs should be queried
This is used by ConjunctiveGraphs
Values other than None obviously only makes sense for context-aware stores.)
"""
super().update(
update,
initNs,
initBindings,
queryGraph,
**chocolate.filter_args(kwargs, super().update),
)