我正在玩Neo4j,到目前为止,我有一个地理图
AIRPORT
连接到
CITY
这个
城市
到
COUNTRY
以及
国家
到
CONTINENT
,如图所示
箭头上的标签转换为
org.neo4j.graphdb.RelationshipType
输入我的代码。到目前为止,我可以在开始节点之间构建路径
MXP
到结束节点
LTN
使用以下单向遍历。
Traverser traverse = database.traversalDescription().depthFirst()
.relationships(CITY, BOTH)
.relationships(CONTINENT, BOTH)
.relationships(COUNTRY, BOTH)
.relationships(REGION, BOTH)
.evaluator(Evaluators.includeWhereEndNodeIs(endNode)).traverse(startNode);
有了这个,我得到了一条路
MXP -> Milan -> Italy -> Europe <- England <- London <- LTN
,这是正确的,给出了图形描述,遍历描述,当然,我理解我对这种描述的理解。
我试图更改此代码以执行双向遍历,这意味着我希望从两者开始
MXP公司
和
LTN公司
并在碰撞点停止。我尝试了以下代码段,其中的注释意味着我的理解,因此可能更容易指出问题。
TraversalDescription startSide = database.traversalDescription().depthFirst() //Depth first algorithm
.relationships(CITY, OUTGOING) //consider CITY relationship, only outgoing
.relationships(REGION, OUTGOING) //consider REGION relationship, only outgoing
.relationships(COUNTRY, OUTGOING) //consider COUNTRY relationship, only outgoing
.relationships(CONTINENT, OUTGOING) //consider CONTINENT relationship, only outgoing
.evaluator(Evaluators.excludeStartPosition()); //do not consider the starting point.
//Here I tried also with all, with the same result
//with includeWhereEndNodeIs(endNode), again with same result
//and combining includeWhereEndNodeIs and excludeStartPosition, once more with same result.
//All tries I mirrored for the endSide description, changing endNode to startNode where I feel it was needed
TraversalDescription endSide = database.traversalDescription().depthFirst()
.relationships(CITY, OUTGOING)
.relationships(REGION, OUTGOING)
.relationships(COUNTRY, OUTGOING)
.relationships(CONTINENT, OUTGOING)
.evaluator(Evaluators.excludeStartPosition());
List<Node> asList = Arrays.asList(startNode, endNode);
Traverser traverse = database.bidirectionalTraversalDescription().endSide(endSide).startSide(startSide).traverse(asList, asList);
在这里,我得到了两条路径,一条路径只有
MXP公司
一个只有
LTN公司
.
在这一点上,我认真地认为我完全误解了双向遍历,甚至可能误解了它的目的。我的错误在哪里?为什么我没有得到相同的输出?