I've cleaned it up, will try running it again today, but for now >
import uuid
from json import dumps, loads
from py2neo import Node, Relationship, Graph, NodeMatcher
import time
graph = Graph("http://localhost:7474", password="graph")
globalStart = time.time()
print(graph)
def make3Manual():
print("Deleting old graph")
graph.delete_all()
print("Deleted old graph")
nData = {}
nData["type"] = ""
nData["name"] = "name"
nData["reference"] = 1
nData["icId"] = ""
nodeSize = 100000 * [None]
matSize = 100 * [None]
containerSize = 100 * [None]
sceneSize = 200 * [None]
for id in range(0, len(nodeSize)):
n = nData.copy()
n["type"] = "Obj"
n["name"] = "geo_" + str(id)
n["refType"] = 1
n["icId"] = str(uuid.uuid4())
nodeSize[id] = n
for id in range(0, len(matSize)):
n = nData.copy()
n["type"] = "animal"
n["name"] = "ani_" + str(id)
n["refType"] = 1
n["icId"] = str(uuid.uuid4())
matSize[id] = n
for id in range(0, len(containerSize)):
n = nData.copy()
n["type"] = "Cont"
n["name"] = "Cont_" + str(id)
n["refType"] = 1
n["icId"] = str(uuid.uuid4())
containerSize[id] = n
for id in range(0, len(sceneSize)):
n = nData.copy()
n["type"] = "Scene"
n["name"] = "someani_" + str(id)
n["refType"] = 1
n["icId"] = str(uuid.uuid4())
sceneSize[id] = n
allData = nodeSize + matSize + containerSize + sceneSize
print("Allocated arrays")
tx = graph.begin()
start = time.time()
tx.run('''
UNWIND $mapEntry AS mItem
CALL apoc.create.node([mItem["type"]], {name:mItem["name"], art:mItem["atr"],icId:mItem["icId"], refType:mItem["refType"]})
YIELD node
RETURN node
'''
, mapEntry=allData)
tx.commit()
print("Processed in : ", time.time() - start)
def getNodes(nodeClass, lim=-1):
nMatcher = NodeMatcher(graph)
if (lim):
a = nMatcher.match(nodeClass).limit(lim)
else:
a = nMatcher.match(nodeClass)
print("Found nodes : ", len(a), " ", nodeClass)
return list(a)
def buildRelation(source, key, dest):
rela = len(source) * [None]
x = 0
for src in source:
rela[x] = Relationship(src, key, dest)
x = x + 1
tx = graph.begin()
for r in rela:
tx.create(r)
tx.commit()
def makeConnectionQuery(source, key, dest, sourceType, destType):
for s in source:
r = graph.run('''
MATCH a={s}, b={d}
CREATE UNIQUE (a)-[r-{k}]-()b
RETURN a,b
'''.format(s=s, k=key, d=dest))
print("new con, key : ", key, " Reply : ", r)
def getNodesByType(type, limit=-1):
if limit > 0:
a = graph.run('''
MATCH (n:{type})
RETURN n
LIMIT {limit}'''.format(type=type, limit=limit))
return a
return graph.run('''
MATCH (n:{type})
RETURN n'''.format(type=type))
def makeConnection(source, key, dest, sourceType, destType):
data = len(source) * [None]
for a, item in enumerate(source):
data[a] = {"id": item[0]["icId"]}
print(dest[0]["icId"])
print(data[0]["id"])
print("processing count : ", len(source))
tx = graph.begin() #
cmd = '''
MATCH (dest:{destTag}{{icId:"{destId}"}})
UNWIND $arrSource AS row
MATCH (source:{srcType}{{icId:row.id}})
CALL apoc.create.relationship(source, "{k}", {{relation:"usedIn"}}, dest)
YIELD rel
RETURN rel
'''.format(destId=dest[0]["icId"], destTag=destType, srcType=sourceType, k=key)
# WHERE NOT EXISTS((source)-[:usedIn]->(dest))
print(cmd)
c = tx.run(cmd, arrSource=data)
print("con result : ", c)
tx.commit()
print("Building DB")
make3Manual()
print("Getting data back")
geos = getNodesByType("Obj").to_ndarray()
containers = getNodesByType("Cont", 3).to_ndarray()
print(containers)
makeConnection(geos, "usedIn", containers[0], "Obj", "Cont")
print("End job!")
globalEnd = time.time()
print("Done in :", globalEnd - globalStart)