private MonoDirectionalTraversalDescription( PathExpander expander, UniquenessFactory uniqueness, Object uniquenessParameter, PathEvaluator evaluator, InitialBranchState initialState, BranchOrderingPolicy branchOrdering, Comparator<? super Path> sorting, Collection<Node> endNodes, Supplier<? extends Resource> statementSupplier ) { this.expander = expander; this.uniqueness = uniqueness; this.uniquenessParameter = uniquenessParameter; this.evaluator = evaluator; this.branchOrdering = branchOrdering; this.sorting = sorting; this.endNodes = endNodes; this.initialState = initialState; this.statementSupplier = statementSupplier; }
private PathFinder<Path> newFinder() { return new ExactDepthPathFinder( PathExpanders.allTypesAndDirections(), 4, 4, true ); }
protected PathFinder<Path> instantiatePathFinder( int maxDepth ) { return allPaths( PathExpanders.allTypesAndDirections(), maxDepth ); }
protected PathFinder<Path> instantiatePathFinder( int maxDepth ) { return GraphAlgoFactory.allSimplePaths( PathExpanders.allTypesAndDirections(), maxDepth ); }
@Parameters public static Collection<Object[]> data() { return Arrays.asList( new Object[][] { { GraphAlgoFactory.aStar( PathExpanders.allTypesAndDirections(), doubleCostEvaluator( "length" ), ESTIMATE_EVALUATOR ) }, { new TraversalAStar( PathExpanders.allTypesAndDirections(), doubleCostEvaluator( "length" ), ESTIMATE_EVALUATOR ) } } ); }
@Test public void shouldHandleNeighbouringNodes() { // (a) - (b) graph.makeEdgeChain( "a,b" ); Node a = graph.getNode( "a" ); Node b = graph.getNode( "b" ); ExactDepthPathFinder pathFinder = new ExactDepthPathFinder( PathExpanders.allTypesAndDirections(), 1, Integer.MAX_VALUE, false ); Iterable<Path> allPaths = pathFinder.findAllPaths( a, b ); assertPaths( new ExactDepthPathFinder( PathExpanders.allTypesAndDirections(), 1, Integer.MAX_VALUE, false ).findAllPaths( a, b ), "a,b" ); assertPaths( new ExactDepthPathFinder( PathExpanders.allTypesAndDirections(), 1, Integer.MAX_VALUE, false ).findAllPaths( a, b ), "a,b" ); }
@Test public void shouldHandleSimpleChainOddDepth() { // (a) - (b) - (c) - (d) graph.makeEdgeChain( "a,b,c,d" ); Node a = graph.getNode( "a" ); Node d = graph.getNode( "d" ); assertPaths( new ExactDepthPathFinder( PathExpanders.allTypesAndDirections(), 3, Integer.MAX_VALUE, false ).findAllPaths( a, d ), "a,b,c,d" ); assertPaths( new ExactDepthPathFinder( PathExpanders.allTypesAndDirections(), 3, Integer.MAX_VALUE, false ).findAllPaths( a, d ), "a,b,c,d" ); }
@Test public void shouldHandleSimpleChainEvenDepth() { // (a) - (b) - (c) graph.makeEdgeChain( "a,b,c" ); Node a = graph.getNode( "a" ); Node c = graph.getNode( "c" ); assertPaths( new ExactDepthPathFinder( PathExpanders.allTypesAndDirections(), 2, Integer.MAX_VALUE, false ).findAllPaths( a, c ), "a,b,c" ); assertPaths( new ExactDepthPathFinder( PathExpanders.allTypesAndDirections(), 2, Integer.MAX_VALUE, false ).findAllPaths( a, c ), "a,b,c" ); }
@Test public void shouldHandleNeighbouringNodesWhenNotAlone() { // (a) - (b) // | // (c) graph.makeEdge( "a", "b" ); graph.makeEdge( "a", "c" ); Node a = graph.getNode( "a" ); Node b = graph.getNode( "b" ); ExactDepthPathFinder pathFinder = new ExactDepthPathFinder( PathExpanders.allTypesAndDirections(), 1, Integer.MAX_VALUE, false ); Iterable<Path> allPaths = pathFinder.findAllPaths( a, b ); assertPaths( new ExactDepthPathFinder( PathExpanders.allTypesAndDirections(), 1, Integer.MAX_VALUE, false ).findAllPaths( a, b ), "a,b" ); assertPaths( new ExactDepthPathFinder( PathExpanders.allTypesAndDirections(), 1, Integer.MAX_VALUE, false ).findAllPaths( a, b ), "a,b" ); }
@PluginTarget( Node.class ) public Path pathToReference( @Source Node me ) { PathFinder<Path> finder = GraphAlgoFactory.shortestPath( PathExpanders.allTypesAndDirections(), 6 ); return finder.findSinglePath( me.getGraphDatabase() .createNode(), me ); } }
@Test public void canFindNeighbourMultipleIncorrectPaths() { /* * - 2.0 - * / \ * (A) - 1 - (B) */ Node nodeA = graph.makeNode( "A" ); Node nodeB = graph.makeNode( "B" ); graph.makeEdge( "A", "B", "length", 2.0 ); graph.makeEdge( "A", "B", "length", 1 ); PathFinder finder = factory.dijkstra( PathExpanders.allTypesAndDirections() ); Iterator<WeightedPath> paths = finder.findAllPaths( nodeA, nodeB ).iterator(); assertTrue( "Expect at least one path", paths.hasNext() ); WeightedPath path = paths.next(); assertPath( path, nodeA, nodeB ); assertEquals( "Expect weight 1", 1, path.weight(), 0.0 ); assertFalse( "Expected at most one path", paths.hasNext() ); }
@Test public void shouldHandleNeighbouringNodesMultiplePaths() { // (a) = (b) // | // (c) graph.makeEdgeChain( "a,b" ); graph.makeEdgeChain( "a,b" ); graph.makeEdgeChain( "a,c" ); Node a = graph.getNode( "a" ); Node b = graph.getNode( "b" ); ExactDepthPathFinder pathFinder = new ExactDepthPathFinder( PathExpanders.allTypesAndDirections(), 1, Integer.MAX_VALUE, false ); Iterable<Path> allPaths = pathFinder.findAllPaths( a, b ); assertPaths( new ExactDepthPathFinder( PathExpanders.allTypesAndDirections(), 1, Integer.MAX_VALUE, false ).findAllPaths( a, b ), "a,b", "a,b" ); assertPaths( new ExactDepthPathFinder( PathExpanders.allTypesAndDirections(), 1, Integer.MAX_VALUE, false ).findAllPaths( a, b ), "a,b", "a,b" ); }
@Test public void canFindOrphanGraph() { /* * * (A)=1 (relationship to self) * * Should not find (A)-(A). Should find (A) */ Node nodeA = graph.makeNode( "A" ); graph.makeEdge( "A", "A", "length", 1d ); PathFinder finder = factory.dijkstra( PathExpanders.allTypesAndDirections() ); assertPaths( finder.findAllPaths( nodeA, nodeA ), "A" ); }
@Test public void canFindNeighbour() { /* * (A) - 1 -(B) */ Node nodeA = graph.makeNode( "A" ); Node nodeB = graph.makeNode( "B" ); graph.makeEdge( "A", "B", "length", 1 ); PathFinder finder = factory.dijkstra( PathExpanders.allTypesAndDirections() ); assertPaths( finder.findAllPaths( nodeA, nodeB ), "A,B" ); }
private Traverser traverseToDepth( GraphDatabaseService graphDb, final Node startNode, final int depth ) { TraversalDescription traversalDescription = graphDb.traversalDescription() .expand( PathExpanders.allTypesAndDirections() ) .depthFirst() .evaluator( path -> { if ( path.length() < depth ) { return Evaluation.INCLUDE_AND_CONTINUE; } else { return Evaluation.INCLUDE_AND_PRUNE; } } ); return traversalDescription.traverse( startNode ); }
@Test public void shouldFindShortestPathWhenOneSideFindsLongerPathFirst() { /* The order in which nodes are created matters when reproducing the original problem */ graph.makeEdge( "start", "c" ); graph.makeEdge( "start", "a" ); graph.makeEdge( "b", "end" ); graph.makeEdge( "d", "end" ); graph.makeEdge( "c", "e" ); graph.makeEdge( "f", "end" ); graph.makeEdge( "c", "b" ); graph.makeEdge( "e", "end" ); graph.makeEdge( "a", "end" ); final Node start = graph.getNode( "start" ); final Node end = graph.getNode( "end" ); assertThat( new ShortestPath( 2, allTypesAndDirections(), 42 ).findSinglePath( start, end ).length(), is( 2 ) ); assertThat( new ShortestPath( 3, allTypesAndDirections(), 42 ).findSinglePath( start, end ).length(), is( 2 ) ); }
@Test public void shouldMakeSureResultLimitIsRespectedForMultiPathHits() { /* _____ * / \ * (a)-----(b) * \_____/ */ for ( int i = 0; i < 3; i++ ) { graph.makeEdge( "a", "b" ); } Node a = graph.getNode( "a" ); Node b = graph.getNode( "b" ); testShortestPathFinder( finder -> assertEquals( 1, count( finder.findAllPaths( a, b ) ) ), allTypesAndDirections(), 2, 1 ); }
@Test public void canFindNeighbourMultipleCorrectPaths() { /* * - 1.0 - * / \ * (A) - 1 - (B) */ Node nodeA = graph.makeNode( "A" ); Node nodeB = graph.makeNode( "B" ); graph.makeEdge( "A", "B", "length", 1.0 ); graph.makeEdge( "A", "B", "length", 1 ); PathFinder finder = factory.dijkstra( PathExpanders.allTypesAndDirections() ); assertPaths( finder.findAllPaths( nodeA, nodeB ), "A,B","A,B" ); }
@Test public void pathToSelfReturnsZero() { // GIVEN Node start = graph.makeNode( "A" ); // WHEN PathFinder<WeightedPath> finder = factory.dijkstra( PathExpanders.allTypesAndDirections() ); WeightedPath path = finder.findSinglePath( start, start ); // THEN assertNotNull( path ); assertEquals( start, path.startNode() ); assertEquals( start, path.endNode() ); assertEquals( 0, path.length() ); }
@Test public void allPathsToSelfReturnsZero() { // GIVEN Node start = graph.makeNode( "A" ); // WHEN PathFinder<WeightedPath> finder = factory.dijkstra( PathExpanders.allTypesAndDirections() ); Iterable<WeightedPath> paths = finder.findAllPaths( start, start ); // THEN for ( WeightedPath path : paths ) { assertNotNull( path ); assertEquals( start, path.startNode() ); assertEquals( start, path.endNode() ); assertEquals( 0, path.length() ); } }