@Override protected Traverser instantiateTraverser( Node start, Node end ) { // Bidirectional traversal GraphDatabaseService db = start.getGraphDatabase(); TraversalDescription base = db.traversalDescription().depthFirst().uniqueness( uniqueness() ); return db.bidirectionalTraversalDescription() .startSide( base.expand( expander ).evaluator( toDepth( maxDepth / 2 ) ) ) .endSide( base.expand( expander.reverse() ).evaluator( toDepth( maxDepth - maxDepth / 2 ) ) ) .traverse( start, end ); } }
private void shouldGetBothNodesOnDepthOne( TraversalDescription description ) { description = description.evaluator( atDepth( 1 ) ); expectNodes( description.traverse( getNodeWithName( "3" ) ), "1", "2" ); }
@Override protected Traverser instantiateTraverser( Node start, Node end ) { GraphDatabaseService db = start.getGraphDatabase(); TraversalDescription side = db.traversalDescription().breadthFirst().uniqueness( uniqueness ).order( ( startSource, expander ) -> new LiteDepthFirstSelector( startSource, startThreshold, expander ) ); return db.bidirectionalTraversalDescription().startSide( side.expand( expander ).evaluator( toDepth( onDepth / 2 ) ) ) .endSide( side.expand( expander.reverse() ).evaluator( toDepth( onDepth - onDepth / 2 ) ) ) .collisionEvaluator( atDepth( onDepth ) ) // TODO Level side selector will make the traversal return wrong result, why? // .sideSelector( SideSelectorPolicies.LEVEL, onDepth ) .traverse( start, end ); } }
private void canPruneTraversalAtSpecificDepth( TraversalDescription description ) { Traverser traverser = description.uniqueness( Uniqueness.NONE ).evaluator( toDepth( 1 ) ).traverse( node( "1" ) ); expectNodes( traverser, "1", "2", "3", "4", "5" ); }
private void canPreFilterNodes( TraversalDescription description ) { Traverser traverser = description.uniqueness( Uniqueness.NONE ).evaluator( atDepth( 2 ) ).traverse( node( "1" ) ); expectPaths( traverser, "1,2,6", "1,3,5", "1,4,5", "1,5,3", "1,5,4", "1,5,6" ); } }
@Test public void depths() { Node a = getNodeWithName( "a" ); expectPaths( getGraphDb().traversalDescription().evaluator( Evaluators.atDepth( 1 ) ).traverse( a ), "a,b", "a,f" ); expectPaths( getGraphDb().traversalDescription().evaluator( Evaluators.fromDepth( 2 ) ).traverse( a ), "a,f,g", "a,b,h", "a,b,h,i", "a,b,h,i,k", "a,b,c", "a,b,c,d", "a,b,c,d,e", "a,b,c,d,e,j" ); expectPaths( getGraphDb().traversalDescription().evaluator( Evaluators.toDepth( 2 ) ).traverse( a ), "a", "a,b", "a,b,c", "a,b,h", "a,f", "a,f,g" ); } }
@Test public void lastRelationshipTypeEvaluator() { Node a = getNodeWithName( "a" ); expectPaths( getGraphDb().traversalDescription().evaluator( lastRelationshipTypeIs( INCLUDE_AND_PRUNE, EXCLUDE_AND_CONTINUE, Types.C ) ).traverse( a ), "a,b,c,d,e", "a,f,g", "a,b,h" ); expectPaths( getGraphDb().traversalDescription().evaluator( lastRelationshipTypeIs( INCLUDE_AND_CONTINUE, EXCLUDE_AND_CONTINUE, Types.C ) ).traverse( a ), "a,b,c,d,e", "a,f,g", "a,b,h", "a,b,h,i,k" ); }
@Test public void shouldGetStartNodeOnDepthZero() { TraversalDescription description = getGraphDb().traversalDescription().evaluator( Evaluators.atDepth( 0 ) ); expectNodes( description.traverse( getNodeWithName( "6" ) ), "6" ); }
@Test public void shouldGetCorrectNodeAtDepthOne() { TraversalDescription description = getGraphDb().traversalDescription().evaluator( Evaluators.atDepth( 1 ) ); expectNodes( description.traverse( getNodeWithName( "6" ) ), "5" ); }
@Test public void endNodeIs() { Node a = getNodeWithName( "a" ); Node c = getNodeWithName( "c" ); Node h = getNodeWithName( "h" ); Node g = getNodeWithName( "g" ); expectPaths( getGraphDb().traversalDescription().evaluator( includeWhereEndNodeIs( c, h, g ) ).traverse( a ), "a,b,c", "a,b,h", "a,f,g" ); expectPaths( getGraphDb().traversalDescription().evaluator( includeWhereEndNodeIs( g ) ).traverse( a ), "a,f,g" ); }
@Test public void reverseNodes() { Traverser traverse = getGraphDb().traversalDescription().evaluator( atDepth( 0 ) ).traverse( a ); Path path = getFirstPath( traverse ); assertContains( path.reverseNodes(), a ); Traverser traverse2 = getGraphDb().traversalDescription().evaluator( atDepth( 4 ) ).traverse( a ); Path path2 = getFirstPath( traverse2 ); assertContainsInOrder( path2.reverseNodes(), e, d, c, b, a ); }
@Test public void testNarrowingFilters() { Evaluator mustBeConnectedToK = new MustBeConnectedToNodeFilter( getNodeWithName( "k" ) ); Evaluator mustNotHaveMoreThanTwoOutRels = path -> Evaluation.ofIncludes( Iterables .count( path.endNode().getRelationships( Direction.OUTGOING ) ) <= 2 ); TraversalDescription description = getGraphDb().traversalDescription().evaluator( mustBeConnectedToK ); expectNodes( description.traverse( node( "a" ) ), "b", "c" ); expectNodes( description.evaluator( mustNotHaveMoreThanTwoOutRels ).traverse( node( "a" ) ), "c" ); }
@Test public void shouldGetStartNodeWhenAtIsZeroBreadthFirst() { TraversalDescription description = getGraphDb().traversalDescription().breadthFirst() .evaluator(Evaluators.atDepth(0)); expectNodes( description.traverse( getNodeWithName( "2" ) ), "2" ); }
@Test public void shouldGetSecondNodeWhenAtIsTwoBreadthFirst() { TraversalDescription description = getGraphDb().traversalDescription().breadthFirst() .evaluator( Evaluators.atDepth( 2 ) ); expectNodes( description.traverse( getNodeWithName( "6" ) ), "4" ); } }
@Test public void testPathIterator() { Traverser traverse = getGraphDb().traversalDescription().evaluator( atDepth( 4 ) ).traverse( node( "A" ) ); try ( ResourceIterator<Path> resourceIterator = traverse.iterator() ) { Path path = resourceIterator.next(); assertPathIsCorrect( path ); } }
@Test public void testTraverseRelationshipsWithStartNodeNotIncluded() { try ( Transaction transaction = beginTx() ) { TraversalDescription traversal = getGraphDb().traversalDescription().evaluator( excludeStartPosition() ); assertEquals( 1, Iterables.count( traversal.traverse( node( "1" ) ).relationships() ) ); } } }