-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphDBcommitter.py
190 lines (170 loc) · 7.77 KB
/
graphDBcommitter.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
from unicodedata import name
from neo4j import GraphDatabase
import logging
from neo4j.exceptions import ServiceUnavailable
class App:
def __init__(self, uri, user, password):
self.driver = GraphDatabase.driver(uri, auth=(user, password))
def close(self):
# Don't forget to close the driver connection when you are finished with it
self.driver.close()
def create_equivalence_relation(self, expression1, expression2):
# two expressions are equivalent if their difference = 0 modulo variable permutation
with self.driver.session() as session:
# Write transactions allow the driver to handle retries and transient errors
result = session.write_transaction(
self._create_and_return_equivalence_relation, expression1, expression2)
#for row in result:
# print("expressions: {expr1}, {expr2} are equivalent".format(expr1=row['expr1'], expr2=row['expr2']))
@staticmethod
def _create_and_return_equivalence_relation(tx, expression1, expression2):
query = (
"MATCH (expr1:Expression { name: $expression1 }) "
"MATCH (expr2:Expression { name: $expression2 }) "
"CREATE (expr1)-[:EQUALS]->(expr2) "
"CREATE (expr2)-[:EQUALS]->(expr1) "
"RETURN expr1, expr2"
)
result = tx.run(query, expression1=expression1, expression2=expression2)
# "tx" is a transaction
try:
return [{"expr1": row["expr1"]["name"], "expr2": row["expr2"]["name"]}
for row in result]
# Capture any errors along with the query and data for traceability
except ServiceUnavailable as exception:
logging.error("{query} raised an error: \n {exception}".format(
query=query, exception=exception))
raise
def create_dependency(self, expr, type):
with self.driver.session() as session:
# Write transactions allow the driver to handle retries and transient errors
result = session.write_transaction(
self._create_and_return_dependency, expr, type)
#for row in result:
# print("expression: {expr} is of type {node}, connection created".format(expr=row['expr'], node=row['node']))
@staticmethod
def _create_and_return_dependency(tx, exprName, type):
query = (
"MATCH (expr:Expression), (node:Node)"
"WHERE (expr.name=$exprName) AND (node.type=$type)"
"CREATE (expr)-[:IS]->(node)"
"RETURN expr,node"
)
result = tx.run(query, exprName=exprName, type=type)
# "tx" is a transaction
try:
return [{"expr": row["expr"]["name"], "node": row["node"]["type"]} for row in result]
# Capture any errors along with the query and data for traceability
except ServiceUnavailable as exception:
logging.error("{query} raised an error: \n {exception}".format(
query=query, exception=exception))
raise
def createOperationNode(self, nodeType):
with self.driver.session() as session:
# Write transactions allow the driver to handle retries and transient errors
result = session.write_transaction(
self._create_and_return_OperationNode, nodeType)
for row in result:
print("node of type: {nodeType} created".format(nodeType=row['node']))
@staticmethod
def _create_and_return_OperationNode(tx, nodeType):
query = (
"CREATE (node:Node { type: $nodeType }) "
"RETURN node"
)
result = tx.run(query, nodeType=nodeType)
# "tx" is a transaction
try:
return [{"node": row["node"]["type"]} for row in result]
# Capture any errors along with the query and data for traceability
except ServiceUnavailable as exception:
logging.error("{query} raised an error: \n {exception}".format(
query=query, exception=exception))
raise
def createExpressionNode(self, exprName, stdExprName):
with self.driver.session() as session:
# Write transactions allow the driver to handle retries and transient errors
result = session.write_transaction(
self._create_and_return_ExpressionNode, exprName, stdExprName)
#for row in result:
# print("expression: {exprName} created".format(exprName=row['expr']))
@staticmethod
def _create_and_return_ExpressionNode(tx, exprName, stdExprName):
query = (
"CREATE (expr:Expression { name: $exprName, stdName: $stdExprName }) "
"RETURN expr"
)
result = tx.run(query, exprName=exprName, stdExprName=stdExprName)
# "tx" is a transaction
try:
return [{"expr": row["expr"]["name"], "expr": row["expr"]["stdName"]} for row in result]
# Capture any errors along with the query and data for traceability
except ServiceUnavailable as exception:
logging.error("{query} raised an error: \n {exception}".format(
query=query, exception=exception))
raise
def createBatchExpressionNode(self, batch):
with self.driver.session() as session:
# Write transactions allow the driver to handle retries and transient errors
session.write_transaction(self._create_and_return_BatchExpressionNode, batch)
@staticmethod
def _create_and_return_BatchExpressionNode(tx, batch):
query = (
"WITH $batch AS batch "
"UNWIND batch AS singleExpression "
"CREATE (expr:Expression) "
"SET expr += singleExpression;"
)
tx.run(query, batch=batch)
def find_person(self, person_name):
with self.driver.session() as session:
result = session.read_transaction(self._find_and_return_person, person_name)
for row in result:
print("Found person: {row}".format(row=row))
@staticmethod
def _find_and_return_person(tx, person_name):
query = (
"MATCH (p:Person) "
"WHERE p.name = $person_name "
"RETURN p.name AS name"
)
result = tx.run(query, person_name=person_name)
return [row["name"] for row in result]
def getAllExpressions(self):
with self.driver.session() as session:
result = session.read_transaction(self._returnExpressions)
return result
@staticmethod
def _returnExpressions(tx):
query = (
"MATCH (e:Expression) "
"RETURN e.name AS name"
)
result = tx.run(query)
return [row["name"] for row in result]
def checkIfLinked(self, expr1Name, expr2Name):
with self.driver.session() as session:
result = session.read_transaction(self._returnEquivalentRelationships, expr1Name, expr2Name)
return result
@staticmethod
def _returnEquivalentRelationships(tx, expr1Name, expr2Name):
query = (
"MATCH (expr1:Expression)-[r:EQUALS]->(expr2:Expression)"
"WHERE (expr1.name=$expr1Name) AND (expr2.name=$expr2Name)"
"RETURN r"
)
result = tx.run(query,expr1Name=expr1Name,expr2Name=expr2Name)
return [row for row in result]
if __name__ == "__main__":
# Aura queries use an encrypted connection using the "neo4j+s" URI scheme
uri = "neo4j+s://cb231e56.databases.neo4j.io"
user = "neo4j"
password = "5631ZHUiFDJ1kLH96wfoVdCzdF4kAqKCwPiXgFwucKI"
app = App(uri, user, password)
# app.createOperationNode("POW")
# app.create_dependency("x**2","POW")
# app.create_equivalence_relation("A", "B")
# r = app.checkIfLinked("A","B")
# print(len(r))
# app.find_person("Alice")
app.close()