Neo4j Java嵌入式短路径



我使用这个自定义扩展器嵌入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)来存储当前的关系。在accept中,您可以使用getState()来检索先前存储的状态。

相关内容

最新更新