Skip to content

Commit

Permalink
Updated to grakn 1.5.7
Browse files Browse the repository at this point in the history
  • Loading branch information
tabergma committed Jul 18, 2019
1 parent 5af4d3a commit 6c54ba9
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 78 deletions.
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -27,7 +27,7 @@ In this repository [Grakn](https://grakn.ai/) is used as a graph database.
However, you can also use any other graph database or use an alternative (see below).

In order to use this code example, you need to install [Grakn](https://grakn.ai/).
To be able to run this bot, you need version 1.4.2.
To be able to run this bot, you need version 1.5.7.
Please check the [installation instruction](https://dev.grakn.ai/docs/running-grakn/install-and-run)
of Grakn in order to install it.
Once you installed Grakn, you need to start the Grakn server by executing
Expand All @@ -37,9 +37,9 @@ grakn server start
You can stop the server by running `grakn server stop`.

In order to get some data into the graph database you need to execute the following steps:
1. Create the schema by executing
1. Create the schema by executing
```bash
graql console --keyspace banking --file knowledge_base/schema.gql
grakn console --keyspace banking --file <absolute-path-to-knowledge_base/schema.gql>
```
This will create a keyspace `banking` in your Grakn graph database with the schema defined in `knowledge_base/schema.gql`.
2. Load data into your schema by running
Expand Down
72 changes: 38 additions & 34 deletions graph_database.py
@@ -1,12 +1,13 @@
import logging
from typing import List, Dict, Any, Optional, Text

import grakn
from grakn.client import GraknClient

logger = logging.getLogger(__name__)


class KnowledgeBase(object):

def get_entities(
self,
entity_type: Text,
Expand Down Expand Up @@ -40,7 +41,7 @@ class GraphDatabase(KnowledgeBase):
"""

def __init__(self, uri: Text = "localhost:48555", keyspace: Text = "banking"):
self.client = grakn.Grakn(uri=uri)
self.uri = uri
self.keyspace = keyspace
self.me = "mitchell.gillis@t-online.de"

Expand All @@ -58,27 +59,29 @@ 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 self.client.session(keyspace=self.keyspace) as session:
with session.transaction(grakn.TxType.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
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 self.client.session(keyspace=self.keyspace) as session:
with session.transaction(grakn.TxType.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]
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
Expand All @@ -87,28 +90,29 @@ def _execute_relation_query(
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 self.client.session(keyspace=self.keyspace) as session:
with session.transaction(grakn.TxType.READ) as tx:
print("Executing Graql Query: " + query)
result_iter = tx.query(query)
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 = []
relations = []

for concept in result_iter:
relation_entity = concept.map().get(relation_name)
relation = self._thing_to_dict(relation_entity)
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)
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)
relations.append(relation)

return relations
return relations

def _get_me_clause(self, entity_type: Text) -> Text:
"""
Expand Down
2 changes: 1 addition & 1 deletion knowledge_base/data/contract.csv
@@ -1,4 +1,4 @@
_id,sign-date,provider,customer,offer
identifier,sign-date,provider,customer,offer
1,2019-01-13T10:49:31.641721,Commerzbank,catalinasargent@googlemail.com,DE82444435329779109646
2,2017-09-11T10:49:31.641763,bunq,mitchell.gillis@t-online.de,DE70334119137743514093
3,2018-05-21T10:49:31.641784,Comdirect,mitchell.gillis@t-online.de,DE76894768662419673111
Expand Down
2 changes: 1 addition & 1 deletion knowledge_base/data/represented-by.csv
@@ -1,4 +1,4 @@
_id,bank-account,bank-card
identifier,bank-account,bank-card
1,DE82444435329779109646,70120805493
2,DE70334119137743514093,55604431442
3,DE76894768662419673111,20771854028
Expand Down
2 changes: 1 addition & 1 deletion knowledge_base/data/transaction.csv
@@ -1,4 +1,4 @@
_id,amount,execution-date,reference,category,account-of-receiver,account-of-creator
identifier,amount,execution-date,reference,category,account-of-receiver,account-of-creator
9,803.41,2019-12-16T10:49:31.641890,thanks,transfer,DE33510125629974889896,DE10985785971549145687
30,701.30,2021-03-26T10:49:31.641890,Pasta Bar,food,DE76894768662419673111,DE10985785971549145687
42,612.69,2021-06-24T10:49:31.641890,thanks,transfer,DE93852973047302670654,DE10985785971549145687
Expand Down
26 changes: 13 additions & 13 deletions knowledge_base/migrate.py
@@ -1,23 +1,23 @@
import grakn
from grakn.client import GraknClient
import csv


def build_banking_graph(inputs):
client = grakn.Grakn(uri="localhost:48555")
with client.session(keyspace="banking") as session:
for input in inputs:
print(f"Loading data from [{input['data_path']}] into Grakn ...")
load_data_into_grakn(input, session)
with GraknClient(uri="localhost:48555") as client:
with client.session(keyspace="banking") as session:
for input in inputs:
print("Loading from [" + input["data_path"] + "] into Grakn ...")
load_data_into_grakn(input, session)


def load_data_into_grakn(input, session):
items = parse_data_to_dictionaries(input)

for item in items:
with session.transaction(grakn.TxType.WRITE) as tx:
with session.transaction().write() as transaction:
graql_insert_query = input["template"](item)
tx.query(graql_insert_query)
tx.commit()
transaction.query(graql_insert_query)
transaction.commit()

print(f"Inserted {str(len(items))} items from [{input['data_path']}] into Grakn.")

Expand Down Expand Up @@ -108,7 +108,7 @@ def contract_template(contract):
' $account isa account, has account-number "' + contract["offer"] + '"; '
)
graql_insert_query += " insert $contract(provider: $bank, customer: $customer, offer: $account) isa contract; "
graql_insert_query += "$contract has _id " + str(contract["_id"]) + "; "
graql_insert_query += "$contract has identifier " + str(contract["identifier"]) + "; "
graql_insert_query += "$contract has sign-date " + contract["sign-date"] + "; "

return graql_insert_query
Expand All @@ -124,7 +124,7 @@ def represented_by_template(represented_by):
" $card isa card, has card-number " + represented_by["bank-card"] + "; "
)
graql_insert_query += " insert $representation(bank-card: $card, bank-account: $account) isa represented-by; "
graql_insert_query += "$representation has _id " + represented_by["_id"] + "; "
graql_insert_query += "$representation has identifier " + represented_by["identifier"] + "; "
return graql_insert_query


Expand All @@ -141,8 +141,8 @@ def transaction_template(transaction):
)
graql_insert_query += (
"insert $transaction(account-of-receiver: $account-of-receiver, account-of-creator: $account-of-creator) isa transaction; "
+ "$transaction has _id "
+ str(transaction["_id"])
+ "$transaction has identifier "
+ str(transaction["identifier"])
+ "; "
+ "$transaction has amount "
+ str(transaction["amount"])
Expand Down
44 changes: 22 additions & 22 deletions knowledge_base/schema.gql
Expand Up @@ -3,15 +3,16 @@ define
# entities

person sub entity,
plays customer,
key email,
has first-name,
has last-name,
has city,
has phone-number,
has gender,
plays customer;
has gender;

bank sub entity,
plays provider,
key name,
has country,
has headquarters,
Expand All @@ -20,25 +21,24 @@ bank sub entity,
has english-website,
has english-mobile-app,
has free-worldwide-withdrawals,
has allowed-residents
plays provider;
has allowed-residents;

account sub entity,
plays bank-account,
key account-number,
has opening-date,
has balance,
has account-type,
plays offer,
plays account-of-receiver,
plays account-of-creator,
plays bank-account;
plays account-of-creator;

card sub entity,
has name-on-card,
plays bank-card,
key card-number,
has name-on-card,
has expiry-date,
has created-date,
plays bank-card;
has created-date;

mapping sub entity,
has mapping-key,
Expand All @@ -50,26 +50,26 @@ attribute-mapping sub mapping;

# relationships

contract sub relationship,
has _id,
has sign-date,
contract sub relation,
relates customer,
relates offer,
relates provider;
relates provider,
key identifier,
has sign-date;

transaction sub relationship,
has _id,
transaction sub relation,
relates account-of-receiver,
relates account-of-creator,
key identifier,
has amount,
has reference,
has category,
has execution-date,
relates account-of-receiver,
relates account-of-creator;
has execution-date;

represented-by sub relationship,
has _id,
represented-by sub relation,
relates bank-account,
relates bank-card;
relates bank-card,
key identifier;

# attribute

Expand Down Expand Up @@ -160,7 +160,7 @@ english-mobile-app sub attribute,
free-worldwide-withdrawals sub attribute,
datatype boolean;

_id sub attribute,
identifier sub attribute,
datatype long;

name-on-card sub attribute,
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
@@ -1,3 +1,3 @@
rasa-sdk==1.1
rasa==1.1.4
grakn==1.4.2
grakn-client==1.5.3
4 changes: 2 additions & 2 deletions schema.py
@@ -1,15 +1,15 @@
schema = {
"transaction": {
"attributes": ["category", "execution-date", "amount", "reference"],
"key": "_id",
"key": "identifier",
"representation": [
"execution-date",
"reference",
"account-of-receiver.account-number",
"amount",
],
},
"contract": {"attributes": ["sign-date"], "key": "_id", "representation": ["_id"]},
"contract": {"attributes": ["sign-date"], "key": "identifier", "representation": ["identifier"]},
"account": {
"attributes": ["balance", "account-type", "opening-date", "account-number"],
"key": "account-number",
Expand Down

0 comments on commit 6c54ba9

Please sign in to comment.