aesnxs
aesnxs

Reputation: 11

Neo4j Java Embedded Shortestpath

I'm using this custom expander for Neo4j embedded using the shortestpath algorithm. However the shortestpath algorithm doesn't allow me to look up the last relationship in the path it's constructed because they haven't implemented it. I get an unsupported exception.

I just need to check the property of the last relationship to keep expanding. Is there a way to do this?

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)

Upvotes: 0

Views: 407

Answers (1)

Stefan Armbruster
Stefan Armbruster

Reputation: 39915

The Path interfaces hasn't been implemented for shortestPath. See https://github.com/neo4j/neo4j/blob/2.3/community/graph-algo/src/main/java/org/neo4j/graphalgo/impl/path/ShortestPath.java#L359 for the implemenation being used.

I don't know the reason why it's not implemented, but I guess such a reason exists.

As a workaround you use BranchState to store the last relationship. When the Iterable from your Pathexpander is consumed you need to store the current Relationship using BranchState.setState(current). In accept you can use getState() to retrieve the previously stored state.

Upvotes: 1

Related Questions