我使用此自定义扩展器为Neo4j
嵌入了使用最短路径算法。然而,最短路径算法并不允许我查找它构造的路径中的最后一个关系,因为它们还没有实现它。我得到了一个不受支持的例外。
我只需要检查最后一个关系的属性以继续扩展。有没有办法做到这一点?
public class CustomExpander implements PathExpander {
private Integer conditionFromTime;
private Integer conditionToTime;
private Integer conditionDayInd;
private Node startNode;
private Node endNode;
public CustomExpander( Integer fromTime, Integer toTime, Integer dayInd, Node startNode ) {
this.conditionFromTime = fromTime;
this.conditionToTime = toTime;
this.conditionDayInd = dayInd;
this.startNode = startNode;
this.endNode = endNode;
}
public CustomExpander( Integer fromTime, Integer toTime, Integer dayInd, Node startNode,Node endNode ) {
this.conditionFromTime = fromTime;
this.conditionToTime = toTime;
this.conditionDayInd = dayInd;
this.startNode = startNode;
this.endNode = endNode;
}
public Iterable expand(Path path, BranchState bs) {
Iterable<Relationship> something = path.endNode().getRelationships(TripGraphRelTypes.VISITS,Direction.BOTH);
return new FilteringIterable<Relationship>( something
, new Predicate<Relationship>() {
public boolean accept(Relationship t) {
//boolean result = tripRange.contains((Integer)t.getProperty(TripGraphProperties.VisitedByRel.departureToDsec.name()));
if ( (Integer)t.getProperty("departureToDsec") <= conditionFromTime ) {
return false;
}
if ((Integer)t.getProperty("arrivalToDsec") >= conditionToTime) {
return false;
}
if ( path.length() > 0 && (Integer) path.lastRelationship().getProperty("arrivalToDsec") < (Integer)t.getProperty("departureToDsec")){
return false; }
return true;
}
});
}
@Override
public PathExpander reverse() {
//System.out.println("reverse");
return this;
}
}
PathFinder<Path> finderGood = GraphAlgoFactory.shortestPath(expander, 10);
validPaths = filterValid2(finderGood.findAllPaths(n1, n2));
java.lang.UnsupportedOperationException
at org.neo4j.graphalgo.impl.path.ShortestPath$DirectionDataPath.lastRelationship(ShortestPath.java:394)
at au.com.company.tripresolver.CustomExpander$1.accept(CustomExpander.java:104)
at org.neo4j.helpers.collection.FilteringIterator.fetchNextOrNull(FilteringIterator.java:49)
at org.neo4j.helpers.collection.PrefetchingIterator.peek(PrefetchingIterator.java:60)
at org.neo4j.helpers.collection.PrefetchingIterator.hasNext(PrefetchingIterator.java:46)
at org.neo4j.helpers.collection.NestingIterator.fetchNextOrNull(NestingIterator.java:69)
at org.neo4j.helpers.collection.PrefetchingIterator.peek(PrefetchingIterator.java:60)
at org.neo4j.helpers.collection.PrefetchingIterator.hasNext(PrefetchingIterator.java:46)
at org.neo4j.graphalgo.impl.path.ShortestPath$DirectionData.fetchNextRelOrNull(ShortestPath.java:351)
at org.neo4j.graphalgo.impl.path.ShortestPath$DirectionData.fetchNextOrNull(ShortestPath.java:296)
at org.neo4j.graphalgo.impl.path.ShortestPath$DirectionData.fetchNextOrNull(ShortestPath.java:234)
at org.neo4j.helpers.collection.PrefetchingIterator.peek(PrefetchingIterator.java:60)
at org.neo4j.helpers.collection.PrefetchingIterator.hasNext(PrefetchingIterator.java:46)
at org.neo4j.graphalgo.impl.path.ShortestPath.internalPaths(ShortestPath.java:138)
at org.neo4j.graphalgo.impl.path.ShortestPath.findAllPaths(ShortestPath.java:110)
答案 0 :(得分:1)
尚未为shortestPath实现Path
接口。有关正在使用的实施,请参阅https://github.com/neo4j/neo4j/blob/2.3/community/graph-algo/src/main/java/org/neo4j/graphalgo/impl/path/ShortestPath.java#L359。
我不知道它没有实现的原因,但我想这样的理由存在。
作为解决方法,您使用BranchState
存储最后一个关系。当您的Pathexpander使用Iterable时,您需要使用BranchState.setState(current)
存储当前关系。在accept
中,您可以使用getState()
来检索以前存储的状态。