neo4j启动java路径,java – Neo4j中的定向路径

详细介绍了Wes的想法.在自定义PathEvaluator中,您需要设置分支状态以记住第一个关系的方向.所有后续访问都会检查最后一个关系是否与方向匹配.在伪代码中:

class SameDirectionPathEvaluator implements PathEvaluator {

public Evaluation evaluate(Path path, BranchState state) {

if (path.length()==0) {

return Evaluation.EXCLUDE_AND_CONTINUE;

} else if (path.length()==1) {

state.setState(getDirectionOfLastRelationship(path));

return Evaluation.INCLUDE_AND_CONTINUE;

} else {

if (state.getState().equals(getDirectionOfLastRelationship(path)) {

return Evaluation.INCLUDE_AND_CONTINUE;

} else {

return Evaluation.EXCLUDE_AND_PRUNE;

}

}

}

private Direction getDirectionOfLastRelationship(Path path) {

assert path.length() > 0;

Direction direction = Direction.INCOMING

if (path.endNode().equals(path.lastRelationship().getEndNode()) {

direction = Direction.OUTGOING;

}

return direction;

}

}

请注意我没有编译或测试上面的代码 – 它只是为了草拟这个想法.

UPDATE

似乎有一种更有效的方法来做到这一点.由于遍历在调用赋值器之前使用扩展器,因此在扩展器中实现此行为更有意义:

class ConstantDirectionExpander implements PathExpander() {

@Override

public Iterable expand(Path path, BranchState state) {

if (path.length()==0) {

return path.endNode().getRelationships(types);

} else {

Direction direction = getDirectionOfLastRelationship(path);

return path.endNode().getRelationships(direction, types);

}

}

@Override

public PathExpander reverse() {

return this;

}

private Direction getDirectionOfLastRelationship(Path path) {

assert path.length() > 0;

Direction direction = Direction.INCOMING;

if (path.endNode().equals(path.lastRelationship().getEndNode())) {

direction = Direction.OUTGOING;

}

return direction;

}

}

在遍历中,您需要使用InitialBranchSate:

TraversersDescription td = graphDatabaseService.traversalDescriptioin().

.expand(new ConstantDirectionExpander(reltype))

.traverse(startNode)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值