getGraphDb().traversalDescription().relationships( to, OUTGOING ).uniqueness( Uniqueness.NONE ).evaluator( includeWhereEndNodeIs( d ) ).traverse( a ).iterator(); int count = 0; paths = getGraphDb().traversalDescription().relationships( to, OUTGOING ).uniqueness( RELATIONSHIP_LEVEL ).evaluator( includeWhereEndNodeIs( d ) ).traverse( a ).iterator(); count = 0; paths = getGraphDb().traversalDescription().relationships( to, OUTGOING ).uniqueness( RELATIONSHIP_GLOBAL ).evaluator( includeWhereEndNodeIs( d ) ).traverse( a ).iterator(); count = 0;
@Test public void nodeGlobalUniqueness() { /* * (a)-TO->(b)-TO->(c) * \----TO---->/ */ createGraph( "a TO b", "a TO c", "b TO c" ); RelationshipType to = withName( "TO" ); try ( Transaction tx = beginTx() ) { Node a = getNodeWithName( "a" ); Node c = getNodeWithName( "c" ); Iterator<Path> path = getGraphDb().traversalDescription().relationships( to, OUTGOING ).uniqueness( NODE_GLOBAL ).evaluator( includeWhereEndNodeIs( c ) ).traverse( a ).iterator(); Path thePath = path.next(); assertFalse( path.hasNext() ); NodePathRepresentation pathRepresentation = new NodePathRepresentation( NAME_PROPERTY_REPRESENTATION ); assertEquals( "a,b,c", pathRepresentation.represent( thePath ) ); tx.success(); } }
@Test public void nodeLevelUniqueness() { /* * (b) * / | \ * (e)==(a)--(c) * \ | * (d) */ createGraph( "a TO b", "a TO c", "a TO d", "a TO e", "a TO e", "b TO e", "d TO e", "c TO b" ); RelationshipType to = withName( "TO" ); try ( Transaction tx = beginTx() ) { Node a = getNodeWithName( "a" ); Node e = getNodeWithName( "e" ); Path[] paths = splitPathsOnePerLevel( getGraphDb().traversalDescription().relationships( to, OUTGOING ) .uniqueness( NODE_LEVEL ).evaluator( includeWhereEndNodeIs( e ) ).traverse( a ) ); NodePathRepresentation pathRepresentation = new NodePathRepresentation( NAME_PROPERTY_REPRESENTATION ); assertEquals( "a,e", pathRepresentation.represent( paths[1] ) ); String levelTwoPathRepresentation = pathRepresentation.represent( paths[2] ); assertTrue( levelTwoPathRepresentation.equals( "a,b,e" ) || levelTwoPathRepresentation.equals( "a,d,e" ) ); assertEquals( "a,c,b,e", pathRepresentation.represent( paths[3] ) ); tx.success(); } }
Iterator<Node> nodes = getGraphDb().traversalDescription() .depthFirst() .relationships( type, Direction.OUTGOING ) .evaluator( path ->
TraversalDescription levelOneTraversal = getGraphDb().traversalDescription().relationships( knowRelType ).evaluator( atDepth( 1 ) ); expectNodes( levelOneTraversal.depthFirst().traverse( you, me ), levelOneFriends ); expectNodes( levelOneTraversal.breadthFirst().traverse( you, me ), levelOneFriends ); TraversalDescription levelTwoTraversal = getGraphDb().traversalDescription().relationships( knowRelType ).evaluator( atDepth( 2 ) ); expectNodes( levelTwoTraversal.depthFirst().traverse( you, me ), levelTwoFriends ); expectNodes( levelTwoTraversal.breadthFirst().traverse( you, me ), levelTwoFriends );
@Test public void useTraverserInsideTraverser() { /* * (a)-->(b)-->(c) * | * \/ * (d)-->(e)-->(f) * */ createGraph( "a FIRST d", "a TO b", "b TO c", "d TO e", "e TO f" ); try ( Transaction tx = beginTx() ) { TraversalDescription firstTraverser = getGraphDb().traversalDescription() .relationships( RelationshipType.withName( "FIRST" ) ) .evaluator( Evaluators.toDepth( 1 ) ); final Iterable<Path> firstResult = firstTraverser.traverse( getNodeWithName( "a" ) ); Iterable<Node> startNodesForNestedTraversal = new IterableWrapper<Node,Path>( firstResult ) { @Override protected Node underlyingObjectToObject( Path path ) { return path.endNode(); } }; TraversalDescription nestedTraversal = getGraphDb().traversalDescription().evaluator( Evaluators.atDepth( 2 ) ); expectPaths( nestedTraversal.traverse( startNodesForNestedTraversal ), "a,b,c", "d,e,f"); tx.success(); } }
@Test public void ensureCorrectPathEntitiesInShortPath() { /* * (a)-->(b) */ createGraph( "a TO b" ); Node a = getNodeWithName( "a" ); Node b = getNodeWithName( "b" ); Relationship r = a.getSingleRelationship( to, OUTGOING ); Path path = Iterables.single( getGraphDb().bidirectionalTraversalDescription() .mirroredSides( getGraphDb().traversalDescription().relationships( to, OUTGOING ).uniqueness( NODE_PATH ) ) .collisionEvaluator( Evaluators.atDepth( 1 ) ) .sideSelector( SideSelectorPolicies.LEVEL, 1 ) .traverse( a, b ) ); assertContainsInOrder( path.nodes(), a, b ); assertContainsInOrder( path.reverseNodes(), b, a ); assertContainsInOrder( path.relationships(), r ); assertContainsInOrder( path.reverseRelationships(), r ); assertContainsInOrder( path, a, r, b ); assertEquals( a, path.startNode() ); assertEquals( b, path.endNode() ); assertEquals( r, path.lastRelationship() ); }
private Traverser findHackers( final Node startNode ) { TraversalDescription td = graphDb.traversalDescription() .breadthFirst() .relationships( RelTypes.CODED_BY, Direction.OUTGOING ) .relationships( RelTypes.KNOWS, Direction.OUTGOING ) .evaluator( Evaluators.includeWhereLastRelationshipTypeIs( RelTypes.CODED_BY ) ); return td.traverse( startNode ); } // END SNIPPET: find-hackers
public String knowsLikesTraverser( Node node ) { String output = ""; // START SNIPPET: knowslikestraverser for ( Path position : db.traversalDescription() .depthFirst() .relationships( Rels.KNOWS ) .relationships( Rels.LIKES, Direction.INCOMING ) .evaluator( Evaluators.toDepth( 5 ) ) .traverse( node ) ) { output += position + "\n"; } // END SNIPPET: knowslikestraverser return output; }
public TraversalExample( GraphDatabaseService db ) { this.db = db; // START SNIPPET: basetraverser friendsTraversal = db.traversalDescription() .depthFirst() .relationships( Rels.KNOWS ) .uniqueness( Uniqueness.RELATIONSHIP_GLOBAL ); // END SNIPPET: basetraverser }
public void breadthFirstFindAll(Node startNode) { try (Transaction tx = graphDb.beginTx()) { TraversalDescription traversalDescription = Traversal.description() .relationships(CHILD) .breadthFirst(); Iterable<Node> nodes = traversalDescription.traverse(startNode).nodes(); int cnt = 0; long startTime = System.currentTimeMillis(); for (Node n : nodes) { cnt++; } // logger.info("BREADTH FIRST find all at depth returned " + cnt + " nodes, took " + (System.currentTimeMillis() - startTime) + " millis."); } }
public void depthFirstFindAll(Node startNode) { try (Transaction tx = graphDb.beginTx()) { TraversalDescription traversalDescription = Traversal.description() .relationships(CHILD) .depthFirst(); Iterable<Node> nodes = traversalDescription.traverse(startNode).nodes(); int cnt = 0; long startTime = System.currentTimeMillis(); for (Node n : nodes) { cnt++; } // logger.info("DEPTH FIRST find all at depth returned " + cnt + " nodes, took " + (System.currentTimeMillis() - startTime) + " millis."); } }
private Node getPersonNode() { TraversalDescription traversalDescription = underlyingNode.getGraphDatabase() .traversalDescription() .depthFirst() .relationships( NEXT, Direction.INCOMING ) .relationships( STATUS, Direction.INCOMING ) .evaluator( Evaluators.includeWhereLastRelationshipTypeIs( STATUS ) ); Traverser traverser = traversalDescription.traverse( getUnderlyingNode() ); return singleOrNull( traverser.iterator() ).endNode(); }
private Traverser getFriends( final Node person ) { TraversalDescription td = graphDb.traversalDescription() .breadthFirst() .relationships( RelTypes.KNOWS, Direction.OUTGOING ) .evaluator( Evaluators.excludeStartPosition() ); return td.traverse( person ); } // END SNIPPET: get-friends
public Long countFriendsOfFriends(Long userId) { Node nodeById = graphDb.getNodeById(userId); StopWatch stopWatch = new StopWatch(); stopWatch.start(); TraversalDescription traversalDescription = Traversal.description().relationships(Constants.IS_FRIEND_OF, Direction.OUTGOING).evaluator(Evaluators.atDepth(2)).uniqueness(Uniqueness.NODE_GLOBAL); Iterable<Node> nodes = traversalDescription.traverse(nodeById).nodes(); Long result = 0L; for (Node n : nodes) { result++; } stopWatch.stop(); logger.info("NEO4J: Found {} friends of friends for user {}, took " + stopWatch.getTotalTimeMillis() + " millis.", result, userId); return result; }
public Long countFriendsOfFriendsDepth3(Long userId) { Node nodeById = graphDb.getNodeById(userId); StopWatch stopWatch = new StopWatch(); stopWatch.start(); TraversalDescription traversalDescription = Traversal.description().relationships(Constants.IS_FRIEND_OF, Direction.OUTGOING).evaluator(Evaluators.atDepth(3)).uniqueness(Uniqueness.NODE_GLOBAL); Iterable<Node> nodes = traversalDescription.traverse(nodeById).nodes(); Long result = 0L; for (Node n : nodes) { result++; } stopWatch.stop(); logger.info("NEO4J: Found {} friends of friends depth 3 for user {}, took " + stopWatch.getTotalTimeMillis() + " millis.", result, userId); return result; }
public Long countFriendsOfFriendsDepth4(Long userId) { Node nodeById = graphDb.getNodeById(userId); StopWatch stopWatch = new StopWatch(); stopWatch.start(); TraversalDescription traversalDescription = Traversal.description().relationships(Constants.IS_FRIEND_OF, Direction.OUTGOING).evaluator(Evaluators.atDepth(4)).uniqueness(Uniqueness.NODE_GLOBAL); Iterable<Node> nodes = traversalDescription.traverse(nodeById).nodes(); Long result = 0L; for (Node n : nodes) { result++; } stopWatch.stop(); logger.info("NEO4J: Found {} friends of friends depth 4 for user {}, took " + stopWatch.getTotalTimeMillis() + " millis.", result, userId); return result; }
public Long countFriendsOfFriendsDepth5(Long userId) { Node nodeById = graphDb.getNodeById(userId); StopWatch stopWatch = new StopWatch(); stopWatch.start(); TraversalDescription traversalDescription = Traversal.description().relationships(Constants.IS_FRIEND_OF, Direction.OUTGOING).evaluator(Evaluators.atDepth(5)).uniqueness(Uniqueness.NODE_GLOBAL); Iterable<Node> nodes = traversalDescription.traverse(nodeById).nodes(); Long result = 0L; for (Node n : nodes) { result++; } stopWatch.stop(); logger.info("NEO4J: Found {} friends of friends depth 5 for user {}, took " + stopWatch.getTotalTimeMillis() + " millis.", result, userId); return result; }
private Iterable<Person> getFriendsByDepth( int depth ) { // return all my friends and their friends using new traversal API TraversalDescription travDesc = graphDb().traversalDescription() .breadthFirst() .relationships( FRIEND ) .uniqueness( Uniqueness.NODE_GLOBAL ) .evaluator( Evaluators.toDepth( depth ) ) .evaluator( Evaluators.excludeStartPosition() ); return createPersonsFromPath( travDesc.traverse( underlyingNode ) ); }
private static Iterable<Node> traverse( final Node startNode ) { return graphdb.traversalDescription() .relationships( withName( "FOLLOWS" ), Direction.OUTGOING ) .relationships( withName( "CREATED" ), Direction.OUTGOING ) .breadthFirst() .evaluator( toDepth( 2 ) ) .evaluator( hasProperty( "text" ) ) .evaluator( isVisibleTo( startNode ) ) .traverse( startNode ) .nodes(); }