@SuppressWarnings( "unchecked" ) public ListRepresentation getNodeRelationships( long nodeId, RelationshipDirection direction, Collection<String> types ) throws NodeNotFoundException { Node node = node( nodeId ); PathExpander expander; if ( types.isEmpty() ) { expander = PathExpanders.forDirection( direction.internal ); } else { PathExpanderBuilder builder = PathExpanderBuilder.empty(); for ( String type : types ) { builder = builder.add( RelationshipType.withName( type ), direction.internal ); } expander = builder.build(); } return RelationshipRepresentation.list( expander.expand( Paths.singleNodePath( node ), BranchState.NO_STATE ) ); }
final Node c = graph.getNode( "c" ); final Set<Node> allowedNodes = new HashSet<>( Arrays.asList( a, b, c ) ); final PathFinder<Path> finder = new ShortestPath( 100, PathExpanders.forDirection( OUTGOING ) )
@Test public void shouldHandleDirectionalGraph() { // ALL DIRECTED from (a) towards (g) // (b) ----------------- (c) length 3 // / \ // (a) - (h) - (i) - (j) - (k) - (g) length 5 // \ / // (d) - (e) ------------ (f) length 4 graph.makeEdgeChain( "a,b,c,g" ); graph.makeEdgeChain( "a,d,e,f,g" ); graph.makeEdgeChain( "a,h,i,j,k,g" ); Node a = graph.getNode( "a" ); Node g = graph.getNode( "g" ); assertPaths( new ExactDepthPathFinder( PathExpanders.forDirection( Direction.OUTGOING ), 3, Integer.MAX_VALUE, false ).findAllPaths( a, g ), "a,b,c,g" ); assertPaths( new ExactDepthPathFinder( PathExpanders.forDirection( Direction.OUTGOING ), 4, Integer.MAX_VALUE, false ).findAllPaths( a, g ), "a,d,e,f,g" ); assertPaths( new ExactDepthPathFinder( PathExpanders.forDirection( Direction.OUTGOING ), 5, Integer.MAX_VALUE, false ).findAllPaths( a, g ), "a,h,i,j,k,g" ); }
@Test public void testExactDepthPathsLoopsAllowed() { // Layout: // // (a)-->(b)==>(c)-->(e) // ^ / // \ v // (d) // graph.makeEdgeChain( "a,b,c,d,b,c,e" ); Node a = graph.getNode( "a" ); Node e = graph.getNode( "e" ); assertPaths( new ExactDepthPathFinder( PathExpanders.forDirection( Direction.OUTGOING ), 6, Integer.MAX_VALUE, true ).findAllPaths( a, e ), "a,b,c,d,b,c,e" ); } }
PathExpanders.forDirection( Direction.OUTGOING ), PathInterestFactory.all( ), Uniqueness.NODE_PATH,
/** * Get a path expander representing this input. * * @return path expander. */ public PathExpander getExpander() { if (typesAndDirections.isEmpty()) { return PathExpanders.forDirection(direction == null ? Direction.BOTH : direction); } //Maybe there's a less ugly way of doing this using Neo4j APIs, who knows: if (typesAndDirections.size() == 2) { return PathExpanders.forTypeAndDirection( (RelationshipType) typesAndDirections.get(0), (Direction) typesAndDirections.get(1) ); } return PathExpanders.forTypesAndDirections( (RelationshipType) typesAndDirections.get(0), (Direction) typesAndDirections.get(1), (RelationshipType) typesAndDirections.get(2), (Direction) typesAndDirections.get(3), typesAndDirections.size() > 3 ? typesAndDirections.subList(4, typesAndDirections.size()).toArray() : new Object[0] ); }
@SuppressWarnings( "unchecked" ) public ListRepresentation getNodeRelationships( long nodeId, RelationshipDirection direction, Collection<String> types ) throws NodeNotFoundException { Node node = node( nodeId ); PathExpander expander; if ( types.isEmpty() ) { expander = PathExpanders.forDirection( direction.internal ); } else { PathExpanderBuilder builder = PathExpanderBuilder.empty(); for ( String type : types ) { builder = builder.add( RelationshipType.withName( type ), direction.internal ); } expander = builder.build(); } return RelationshipRepresentation.list( expander.expand( Paths.singleNodePath( node ), BranchState.NO_STATE ) ); }