![](/img/trans.png)
[英]Neo4j Java register gds.alpha.shortestPath.stream to embedded neo4j
[英]Neo4j Java Embedded Shortestpath
我正在使用最短路徑算法為嵌入式的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)
尚未為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)
存儲當前的Relationship。 在accept
,可以使用getState()
檢索以前存儲的狀態。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.