【问题标题】:Neo4j Java Embedded ShortestpathNeo4j Java 嵌入式最短路径
【发布时间】:2015-08-12 05:10:12
【问题描述】:

我正在使用这个自定义扩展器来嵌入使用最短路径算法的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)

【问题讨论】:

  • 请添加完整的堆栈跟踪

标签: java neo4j


【解决方案1】:

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() 检索之前存储的状态。

【讨论】:

  • 谢谢 Stefan,你能写一个小代码 sn-p 吗?当您说 Pathexpander 中的 Iterable 已被消耗时,我不太确定您的意思。
  • 您能否为您的用例提供一个示例 graph.db 作为 tar.gz?
  • PathFinder finderGood = GraphAlgoFactory.shortestPath(expander, 10);
  • 我猜你误解了我的要求。我希望你的 graph.db 文件夹中包含一些合理的数据来编写一个合适的测试用例。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-21
相关资源
最近更新 更多