Py2neo:生成节点和关系网格

时间:2018-01-09 19:29:41

标签: python neo4j cypher py2neo

我正在使用Py2neo在Neo4j中生成节点和关系网格(2D Graph),也就是元胞自动机。我的以下代码有效但它只创建节点但它们之间没有关系。想知道我在哪里做错了,或者是否不可能使用Cypher或Python在Neo4j中创建节点和关系。



from py2neo import Graph, Node, Relationship
def grid(x,y):   
    nodes=list(range(0,x*y))
    for node in nodes:
        graph.create(Node("Cell"))
        if Node(node%x!=0): #if we're not at the start of a line, then we connect to the node before
            graph.create(Relationship(Node(node), "Neighbors", Node(node-1)))
        if Node(node%x!=x-1): #if we're not at the end of a line, then we connect to the node after
            graph.create(Relationship(Node(node), "Neighbors", Node(node+1)))
        if Node(node>=x): #if we're not in the top row, then we connect to the node above
            graph.create(Relationship(Node(node), "Neighbors", Node(node-x)))
        if Node(node<x*(y-1)): #if we're not in the bottom row, then we connect to the node under
            graph.create(Relationship(Node(node), "Neighbors", Node(node+x)))
grid(3,3)
&#13;
&#13;
&#13;

0 个答案:

没有答案