我是neo4j的新手,我需要一些建议和建议来建立关系:
我有70个节点,代表矩阵的每个槽[10] [7]。每个节点都有一个(i,j)作为属性
for(int i = S_N; i <= F_N; i++){//10 rows S_N= 5735, F_N=5744
for(int j = S_E; j <= F_E; j++){//7 columns S_E=589, F_E=595
Node gridNode = graphDb.createNode(label);
gridNode.setProperty("id", id);
gridNode.setProperty("name", "Grid" + id);
gridNode.setProperty("easting", j);
gridNode.setProperty("northing", i);
id++;
}
}
如何以简单的方式创建水平,垂直和对角线(包括对角线反向)关系?像这样:
我有超过200个关系,这只是考虑到一个方向(不是两个),而不是两个节点。
有什么建议吗? 谢谢
答案 0 :(得分:2)
从Neo4j的角度来看,不存在水平和垂直关系。
我建议您创建普通关系并添加标签,这将代表关系的类型。
Cypher中的示例
CREATE (e00:Element {x:0, y:0})
CREATE (e01:Element {x:0, y:1})
CREATE (e10:Element {x:1, y:0})
CREATE (e11:Element {x:1, y:1})
CREATE (e00)-[rh1:HORIZONTAL]->(e01)
CREATE (e00)-[rd1:DIAGONAL]->(e10)
CREATE (e00)-[rv1:VERTICAL]->(e11)
CREATE (e10)-[rh2:HORIZONTAL]->(e11)
CREATE (e10)-[rd2:DIAGONAL]->(e01)
CREATE (e10)-[rv2:VERTICAL]->(e00)
这是JAVA伪代码,以显示如何做到这一点
class Coordinates {
int x;
int y;
public Coordinates(int x, int y) {
this.x = x;
this.y = y;
}
}
private RelationshipType getRelationShipType(Coordinates coordinates, int x, int y) {
int currentX = coordinates.x;
int currentY = coordinates.y;
if (currentX == x && currentY < y) {
return DynamicRelationshipType.withName("Vertical");
}
if (currentX < x && currentY == y) {
return DynamicRelationshipType.withName("Horizontal");
}
return DynamicRelationshipType.withName("Diagonal");
}
int columns = 10;
int rows = 10;
String nodeLabel = "NodeLabel";
Map<Coordinates,Node> matrix = new HashMap<>();
for (int x = 0; x < columns; x++) {
for (int y = 0; y < rows; y++) {
Node node = database.createNode(DynamicLabel.label(nodeLabel));
node.setProperty("propertyName", "propertyValue");
matrix.put(new Coordinates(x, y), node);
}
}
for (Coordinates coordinates : matrix.keySet()) {
final Node startNode = matrix.get(coordinates);
for (int x = 0; x < columns; x++) {
for (int y = 0; y < rows; y++) {
final Node endNode = matrix.get(new Coordinates(x, y));
if (endNode != null) {
RelationshipType relationshipType = getRelationShipType(coordinates, x, y);
startNode.createRelationshipTo(endNode, relationshipType);
}
}
}
}