-
Notifications
You must be signed in to change notification settings - Fork 68
/
graph_database.py
532 lines (429 loc) · 17 KB
/
graph_database.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
import logging
from typing import List, Dict, Any, Optional, Text
from grakn.client import GraknClient
logger = logging.getLogger(__name__)
class KnowledgeBase(object):
def get_entities(
self,
entity_type: Text,
attributes: Optional[List[Dict[Text, Text]]] = None,
limit: int = 5,
) -> List[Dict[Text, Any]]:
raise NotImplementedError("Method is not implemented.")
def get_attribute_of(
self, entity_type: Text, key_attribute: Text, entity: Text, attribute: Text
) -> List[Any]:
raise NotImplementedError("Method is not implemented.")
def validate_entity(
self, entity_type, entity, key_attribute, attributes
) -> Optional[Dict[Text, Any]]:
raise NotImplementedError("Method is not implemented.")
def map(self, mapping_type: Text, mapping_key: Text) -> Text:
raise NotImplementedError("Method is not implemented.")
class GraphDatabase(KnowledgeBase):
"""
GraphDatabase uses a grakn graph database to encode your domain knowledege. Make
sure to have the graph database set up and the grakn server running.
"""
def __init__(self, uri: Text = "localhost:48555", keyspace: Text = "banking"):
self.uri = uri
self.keyspace = keyspace
self.me = "mitchell.gillis@t-online.de"
def _thing_to_dict(self, thing):
"""
Converts a thing (a grakn object) to a dict for easy retrieval of the thing's
attributes.
"""
entity = {"id": thing.id, "type": thing.type().label()}
for each in thing.attributes():
entity[each.type().label()] = each.value()
return entity
def _execute_entity_query(self, query: Text) -> List[Dict[Text, Any]]:
"""
Executes a query that returns a list of entities with all their attributes.
"""
with GraknClient(uri=self.uri) as client:
with client.session(keyspace=self.keyspace) as session:
with session.transaction().read() as tx:
logger.debug("Executing Graql Query: " + query)
result_iter = tx.query(query)
concepts = result_iter.collect_concepts()
entities = []
for c in concepts:
entities.append(self._thing_to_dict(c))
return entities
def _execute_attribute_query(self, query: Text) -> List[Any]:
"""
Executes a query that returns the value(s) an entity has for a specific
attribute.
"""
with GraknClient(uri=self.uri) as client:
with client.session(keyspace=self.keyspace) as session:
with session.transaction().read() as tx:
print("Executing Graql Query: " + query)
result_iter = tx.query(query)
concepts = result_iter.collect_concepts()
return [c.value() for c in concepts]
def _execute_relation_query(
self, query: Text, relation_name: Text
) -> List[Dict[Text, Any]]:
"""
Execute a query that queries for a relation. All attributes of the relation and
all entities participating in the relation are part of the result.
"""
with GraknClient(uri=self.uri) as client:
with client.session(keyspace=self.keyspace) as session:
with session.transaction().read() as tx:
print("Executing Graql Query: " + query)
result_iter = tx.query(query)
relations = []
for concept in result_iter:
relation_entity = concept.map().get(relation_name)
relation = self._thing_to_dict(relation_entity)
for (
role_entity,
entity_set,
) in relation_entity.role_players_map().items():
role_label = role_entity.label()
thing = entity_set.pop()
relation[role_label] = self._thing_to_dict(thing)
relations.append(relation)
return relations
def _get_me_clause(self, entity_type: Text) -> Text:
"""
Construct the me clause. Needed to only list, for example, accounts that are
related to me.
:param entity_type: entity type
:return: me clause as string
"""
clause = ""
# do not add the me clause to a query asking for banks or people as they are
# independent of the accounts related to me
if entity_type not in ["person", "bank"]:
clause = (
f"$person isa person, has email '{self.me}';"
f"$contract(customer: $person, offer: $account, provider: $bank) isa contract;"
)
return clause
def _get_attribute_clause(
self, attributes: Optional[List[Dict[Text, Text]]] = None
) -> Text:
"""
Construct the attribute clause.
:param attributes: attributes
:return: attribute clause as string
"""
clause = ""
if attributes:
clause = ",".join([f"has {a['key']} '{a['value']}'" for a in attributes])
clause = ", " + clause
return clause
def get_attribute_of(
self, entity_type: Text, key_attribute: Text, entity: Text, attribute: Text
) -> List[Any]:
"""
Get the value of the given attribute for the provided entity.
:param entity_type: entity type
:param key_attribute: key attribute of entity
:param entity: name of the entity
:param attribute: attribute of interest
:return: the value of the attribute
"""
me_clause = self._get_me_clause(entity_type)
return self._execute_attribute_query(
f"""
match
{me_clause}
${entity_type} isa {entity_type},
has {key_attribute} '{entity}',
has {attribute} $a;
get $a;
"""
)
def _get_transaction_entities(
self, attributes: Optional[List[Dict[Text, Text]]] = None
) -> List[Dict[Text, Any]]:
"""
Query the graph database for transactions. Restrict the transactions
by the provided attributes, if any attributes are given.
As transaction is a relation, query also the related account entities.
:param attributes: list of attributes
:return: list of transactions
"""
attribute_clause = self._get_attribute_clause(attributes)
me_clause = self._get_me_clause("transaction")
return self._execute_relation_query(
f"match "
f"{me_clause} "
f"$transaction(account-of-receiver: $x, account-of-creator: $account) "
f"isa transaction{attribute_clause}; "
f"get $transaction;",
"transaction",
)
def _get_card_entities(
self, attributes: Optional[List[Dict[Text, Text]]] = None, limit: int = 5
) -> List[Dict[Text, Any]]:
"""
Query the graph database for cards. Restrict the cards
by the provided attributes, if any attributes are given.
:param attributes: list of attributes
:param limit: maximum number of cards to return
:return: list of cards
"""
attribute_clause = self._get_attribute_clause(attributes)
me_clause = self._get_me_clause("card")
return self._execute_entity_query(
f"match "
f"{me_clause} "
f"$represented-by(bank-account: $account, bank-card: $card) "
f"isa represented-by;"
f"$card isa card{attribute_clause}; "
f"get $card;"
)[:limit]
def _get_account_entities(
self, attributes: Optional[List[Dict[Text, Text]]] = None, limit: int = 5
) -> List[Dict[Text, Any]]:
"""
Query the graph database for accounts. Restrict the accounts
by the provided attributes, if any attributes are given.
Query the related relation contract, to obtain additional information
about the bank and the person who owns the account.
:param attributes: list of attributes
:param limit: maximum number of accounts to return
:return: list of accounts
"""
attribute_clause = self._get_attribute_clause(attributes)
me_clause = self._get_me_clause("account")
entities = self._execute_relation_query(
f"""
match
$account isa account{attribute_clause};
{me_clause}
get $contract;
""",
"contract",
)[:limit]
for entity in entities:
for k, v in entity["offer"].items():
entity[k] = v
entity.pop("offer")
return entities
def get_entities(
self,
entity_type: Text,
attributes: Optional[List[Dict[Text, Text]]] = None,
limit: int = 10,
) -> List[Dict[Text, Any]]:
"""
Query the graph database for entities of the given type. Restrict the entities
by the provided attributes, if any attributes are given.
:param entity_type: the entity type
:param attributes: list of attributes
:param limit: maximum number of entities to return
:return: list of entities
"""
if entity_type == "transaction":
return self._get_transaction_entities(attributes)
if entity_type == "account":
return self._get_account_entities(attributes, limit)
if entity_type == "card":
return self._get_card_entities(attributes, limit)
me_clause = self._get_me_clause(entity_type)
attribute_clause = self._get_attribute_clause(attributes)
return self._execute_entity_query(
f"match "
f"{me_clause} "
f"${entity_type} isa {entity_type}{attribute_clause}; "
f"get ${entity_type};"
)[:limit]
def map(self, mapping_type: Text, mapping_key: Text) -> Text:
"""
Query the given mapping table for the provided key.
:param mapping_type: the name of the mapping table
:param mapping_key: the mapping key
:return: the mapping value
"""
value = self._execute_attribute_query(
f"match "
f"$mapping isa {mapping_type}, "
f"has mapping-key '{mapping_key}', "
f"has mapping-value $v;"
f"get $v;"
)
if value and len(value) == 1:
return value[0]
def validate_entity(
self, entity_type, entity, key_attribute, attributes
) -> Dict[Text, Any]:
"""
Validates if the given entity has all provided attribute values.
:param entity_type: entity type
:param entity: name of the entity
:param key_attribute: key attribute of entity
:param attributes: attributes
:return: the found entity
"""
attribute_clause = self._get_attribute_clause(attributes)
value = self._execute_entity_query(
f"match "
f"${entity_type} isa {entity_type}{attribute_clause}, "
f"has {key_attribute} '{entity}'; "
f"get ${entity_type};"
)
if value and len(value) == 1:
return value[0]
class InMemoryGraph(KnowledgeBase):
"""
If you don't want to use a graph database and you just have a few data points, you
can also store your domain knowledge, for example, in a dictionary.
This class is an example class that uses a python dictionary to encode some domain
knowledge about banks.
"""
def __init__(self):
self.graph = {
"bank": [
{
"name": "N26",
"headquarters": "Berlin",
"country": "Germany",
"free-accounts": "true",
},
{
"name": "bunq",
"headquarters": "Amsterdam",
"country": "Netherlands",
"free-accounts": "false",
},
{
"name": "Deutsche Bank",
"headquarters": "Frankfurt am Main",
"country": "Germany",
"free-accounts": "false",
},
{
"name": "Commerzbank",
"headquarters": "Frankfurt am Main",
"country": "Germany",
"free-accounts": "true",
},
{
"name": "Targobank",
"headquarters": "Düsseldorf",
"country": "Germany",
"free-accounts": "true",
},
{
"name": "DKB",
"headquarters": "Berlin",
"country": "Germany",
"free-accounts": "true",
},
{
"name": "Comdirect",
"headquarters": "Quickborn",
"country": "Germany",
"free-accounts": "true",
},
]
}
self.attribute_mapping = {
"headquarters": "headquarters",
"HQ": "headquarters",
"main office": "headquarters",
"city": "headquarters",
"name": "name",
"country": "country",
"free-accounts": "free-accounts",
"free accounts": "free-accounts",
}
self.entity_type_mapping = {"banks": "bank", "bank": "bank"}
def get_entities(
self,
entity_type: Text,
attributes: Optional[List[Dict[Text, Text]]] = None,
limit: int = 5,
) -> List[Dict[Text, Any]]:
"""
Query the graph database for entities of the given type. Restrict the entities
by the provided attributes, if any attributes are given.
:param entity_type: the entity type
:param attributes: list of attributes
:param limit: maximum number of entities to return
:return: list of entities
"""
if entity_type not in self.graph:
return []
entities = self.graph[entity_type]
# filter entities by attributes
if attributes:
entities = list(
filter(
lambda e: [e[a["key"]] == a["value"] for a in attributes].count(
False
)
== 0,
entities,
)
)
return entities[:limit]
def get_attribute_of(
self, entity_type: Text, key_attribute: Text, entity: Text, attribute: Text
) -> List[Any]:
"""
Get the value of the given attribute for the provided entity.
:param entity_type: entity type
:param key_attribute: key attribute of entity
:param entity: name of the entity
:param attribute: attribute of interest
:return: the value of the attribute
"""
if entity_type not in self.graph:
return []
entities = self.graph[entity_type]
entity_of_interest = list(
filter(lambda e: e[key_attribute] == entity, entities)
)
if not entity_of_interest or len(entity_of_interest) > 1:
return []
return [entity_of_interest[0][attribute]]
def validate_entity(
self, entity_type, entity, key_attribute, attributes
) -> Optional[Dict[Text, Any]]:
"""
Validates if the given entity has all provided attribute values.
:param entity_type: entity type
:param entity: name of the entity
:param key_attribute: key attribute of entity
:param attributes: attributes
:return: the found entity
"""
if entity_type not in self.graph:
return None
entities = self.graph[entity_type]
entity_of_interest = list(
filter(lambda e: e[key_attribute] == entity, entities)
)
if not entity_of_interest or len(entity_of_interest) > 1:
return None
entity_of_interest = entity_of_interest[0]
for a in attributes:
if entity_of_interest[a["key"]] != a["value"]:
return None
return entity_of_interest
def map(self, mapping_type: Text, mapping_key: Text) -> Text:
"""
Query the given mapping table for the provided key.
:param mapping_type: the name of the mapping table
:param mapping_key: the mapping key
:return: the mapping value
"""
if (
mapping_type == "attribute-mapping"
and mapping_key in self.attribute_mapping
):
return self.attribute_mapping[mapping_key]
if (
mapping_type == "entity-type-mapping"
and mapping_key in self.entity_type_mapping
):
return self.entity_type_mapping[mapping_key]