org.neo4j.graphdb.Path.reverseNodes()方法的使用及代码示例

x33g5p2x  于2022-01-26 转载在 其他  
字(9.8k)|赞(0)|评价(0)|浏览(94)

本文整理了Java中org.neo4j.graphdb.Path.reverseNodes()方法的一些代码示例,展示了Path.reverseNodes()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Path.reverseNodes()方法的具体详情如下:
包路径:org.neo4j.graphdb.Path
类名称:Path
方法名:reverseNodes

Path.reverseNodes介绍

[英]Returns all the nodes in this path in reversed order, i.e. starting from the end node going backwards instead of from the start node going forwards. The first node is the same as #endNode() and the last node is the same as #startNode(). In between those nodes there can be an arbitrary number of nodes. The shortest path possible is just one node, where also the the start node is the same as the end node.
[中]以相反的顺序返回此路径中的所有节点,即从结束节点向后开始,而不是从开始节点向前开始。第一个节点与#endNode()相同,最后一个节点与#startNode()相同。在这些节点之间可以有任意数量的节点。可能的最短路径只有一个节点,其中起始节点与结束节点的路径相同。

代码示例

代码示例来源:origin: neo4j/neo4j

@Override
public Iterable<Node> reverseNodes()
{
  return path.reverseNodes();
}

代码示例来源:origin: neo4j/neo4j

@Override
  boolean containsDuplicates( Path source )
  {
    Set<Node> nodes = new HashSet<>();
    for ( Node node : source.reverseNodes() )
    {
      if ( !nodes.add( node ) )
      {
        return true;
      }
    }
    return false;
  }
},

代码示例来源:origin: neo4j/neo4j

public static Path path( Node node, Link... links )
{
  List<Node> nodes = new ArrayList<>( links.length + 1 );
  List<Relationship> relationships = new ArrayList<>( links.length );
  List<PropertyContainer> mixed = new ArrayList<>( links.length * 2 + 1 );
  nodes.add( node );
  mixed.add( node );
  Path path = mock( Path.class );
  when( path.startNode() ).thenReturn( node );
  Relationship last = null;
  for ( Link link : links )
  {
    last = link.relationship;
    relationships.add( last );
    mixed.add( last );
    node = link.checkNode( node );
    nodes.add( node );
    mixed.add( node );
  }
  when( path.endNode() ).thenReturn( node );
  when( path.iterator() ).thenAnswer( withIteratorOf( mixed ) );
  when( path.nodes() ).thenReturn( nodes );
  when( path.relationships() ).thenReturn( relationships );
  when( path.lastRelationship() ).thenReturn( last );
  when( path.length() ).thenReturn( links.length );
  when( path.reverseNodes() ).thenReturn( reverse( nodes ) );
  when( path.reverseRelationships() ).thenReturn( reverse( relationships ) );
  return path;
}

代码示例来源:origin: neo4j/neo4j

@Test
public void shouldIterateThroughNodesInReverse()
{
  // given
  Path path = new PathProxy( proxySPI, new long[] {1, 2, 3}, new long[] {100, 200}, new int[] {0, ~0} );
  Iterator<Node> iterator = path.reverseNodes().iterator();
  Node node;
  // then
  assertTrue( iterator.hasNext() );
  assertThat( node = iterator.next(), instanceOf( Node.class ) );
  assertEquals( 3, node.getId() );
  assertTrue( iterator.hasNext() );
  assertThat( node = iterator.next(), instanceOf( Node.class ) );
  assertEquals( 2, node.getId() );
  assertTrue( iterator.hasNext() );
  assertThat( node = iterator.next(), instanceOf( Node.class ) );
  assertEquals( 1, node.getId() );
  assertFalse( iterator.hasNext() );
}

代码示例来源:origin: neo4j/neo4j

@Test
void singularNodeWorksForwardsAndBackwards()
{
  Node node = createNode( 1337L );
  Path path = PathImpl.singular( node );
  assertEquals( node, path.startNode() );
  assertEquals( node, path.endNode() );
  Iterator<Node> forwardIterator = path.nodes().iterator();
  assertTrue( forwardIterator.hasNext() );
  assertEquals( node, forwardIterator.next() );
  assertFalse( forwardIterator.hasNext() );
  Iterator<Node> reverseIterator = path.reverseNodes().iterator();
  assertTrue( reverseIterator.hasNext() );
  assertEquals( node, reverseIterator.next() );
  assertFalse( reverseIterator.hasNext() );
}

代码示例来源:origin: neo4j/neo4j

@Test
public void shouldHandleSingleRelationshipPath()
{
  // Given
  Node start, end;
  Relationship relationship;
  try ( Transaction tx = db.beginTx() )
  {
    start = db.createNode();
    end = db.createNode();
    relationship = start.createRelationshipTo( end, RelationshipType.withName( "R" ) );
    tx.success();
  }
  // When
  Path mapped = mapper.mapPath( path( asNodeValues( start, end ), asRelationshipsValues( relationship ) ) );
  // Then
  try ( Transaction ignore = db.beginTx() )
  {
    assertThat( mapped.length(), equalTo( 1 ) );
    assertThat( mapped.startNode(), equalTo( start ) );
    assertThat( mapped.endNode(), equalTo( end ) );
    assertThat( Iterables.asList( mapped.relationships() ), equalTo( singletonList( relationship ) ) );
    assertThat( Iterables.asList( mapped.reverseRelationships() ), equalTo( singletonList( relationship ) ) );
    assertThat( Iterables.asList( mapped.nodes() ), equalTo( Arrays.asList( start, end ) ) );
    assertThat( Iterables.asList( mapped.reverseNodes() ), equalTo( Arrays.asList( end, start ) ) );
    assertThat( mapped.lastRelationship(), equalTo( relationship ) );
    assertThat( Iterators.asList( mapped.iterator() ), equalTo( Arrays.asList( start, relationship, end ) ) );
  }
}

代码示例来源:origin: neo4j/neo4j

@Test
public void shouldHandleSingleNodePath()
{
  // Given
  Node node;
  try ( Transaction tx = db.beginTx() )
  {
    node = db.createNode();
    tx.success();
  }
  // When
  Path mapped = mapper.mapPath( path( asNodeValues( node ), asRelationshipsValues() ) );
  // Then
  try ( Transaction ignore = db.beginTx() )
  {
    assertThat( mapped.length(), equalTo( 0 ) );
    assertThat( mapped.startNode(), equalTo( node ) );
    assertThat( mapped.endNode(), equalTo( node ) );
    assertThat( Iterables.asList( mapped.relationships() ), hasSize( 0 ) );
    assertThat( Iterables.asList( mapped.reverseRelationships() ), hasSize( 0 ) );
    assertThat( Iterables.asList( mapped.nodes() ), equalTo( singletonList( node ) ) );
    assertThat( Iterables.asList( mapped.reverseNodes() ), equalTo( singletonList( node ) ) );
    assertThat( mapped.lastRelationship(), nullValue() );
    assertThat( Iterators.asList( mapped.iterator() ), equalTo( singletonList( node ) ) );
  }
}

代码示例来源:origin: neo4j/neo4j

assertThat( Iterables.asList( mapped.reverseRelationships() ), equalTo( Arrays.asList( r4, r3, r2, r1 ) ) );
assertThat( Iterables.asList( mapped.nodes() ), equalTo( Arrays.asList( a, b, c, d, e ) ) );
assertThat( Iterables.asList( mapped.reverseNodes() ), equalTo( Arrays.asList( e, d, c, b, a ) ) );
assertThat( mapped.lastRelationship(), equalTo( r4 ) );
assertThat( Iterators.asList( mapped.iterator() ),

代码示例来源:origin: neo4j/neo4j

@Test
void testPathReverseNodes()
{
  when( spi.newNodeProxy( Mockito.anyLong() ) ).thenAnswer( new NodeProxyAnswer() );
  Path path = new PathImpl.Builder( createNodeProxy( 1 ) )
              .push( createRelationshipProxy( 1, 2 ) )
              .push( createRelationshipProxy( 2, 3 ) )
              .build( new PathImpl.Builder( createNodeProxy( 3 ) ) );
  Iterable<Node> nodes = path.reverseNodes();
  List<Node> nodeList = Iterables.asList( nodes );
  assertEquals( 3, nodeList.size() );
  assertEquals( 3, nodeList.get( 0 ).getId() );
  assertEquals( 2, nodeList.get( 1 ).getId() );
  assertEquals( 1, nodeList.get( 2 ).getId() );
}

代码示例来源:origin: neo4j/neo4j

@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 );
}

代码示例来源:origin: neo4j/neo4j

private void assertPathIsCorrect( Path path )
{
  Node a = node( "A" );
  Relationship to1 = getFistRelationship( a );
  Node b = to1.getEndNode();
  Relationship to2 = getFistRelationship( b );
  Node c = to2.getEndNode();
  Relationship to3 = getFistRelationship( c );
  Node d = to3.getEndNode();
  Relationship to4 = getFistRelationship( d );
  Node e = to4.getEndNode();
  assertEquals( (Integer) 4, (Integer) path.length() );
  assertEquals( a, path.startNode() );
  assertEquals( e, path.endNode() );
  assertEquals( to4, path.lastRelationship() );
  assertContainsInOrder( path, a, to1, b, to2, c, to3, d, to4, e );
  assertContainsInOrder( path.nodes(), a, b, c, d, e );
  assertContainsInOrder( path.relationships(), to1, to2, to3, to4 );
  assertContainsInOrder( path.reverseNodes(), e, d, c, b, a );
  assertContainsInOrder( path.reverseRelationships(), to4, to3, to2, to1 );
}

代码示例来源:origin: neo4j/neo4j

@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() );
}

代码示例来源:origin: neo4j/neo4j

@Test
public void testBidirectionalPath()
{
  TraversalDescription side = getGraphDb().traversalDescription().uniqueness( Uniqueness.NODE_PATH );
  BidirectionalTraversalDescription bidirectional =
      getGraphDb().bidirectionalTraversalDescription().mirroredSides( side );
  Path bidirectionalPath = getFirstPath( bidirectional.traverse( a, e ) );
  assertPathIsCorrect( bidirectionalPath );
  Path path = getFirstPath( bidirectional.traverse( a, e ) );
  Node node = path.startNode();
  assertEquals( a, node );
  // White box testing below: relationships(), nodes(), reverseRelationships(), reverseNodes()
  // does cache the start node if not already cached, so just make sure they to it properly.
  bidirectionalPath = getFirstPath( bidirectional.traverse( a, e ) );
  bidirectionalPath.relationships();
  assertEquals( a, bidirectionalPath.startNode() );
  bidirectionalPath = getFirstPath(bidirectional.traverse(a,e ) );
  bidirectionalPath.nodes();
  assertEquals( a, bidirectionalPath.startNode() );
  bidirectionalPath = getFirstPath( bidirectional.traverse( a, e ) );
  bidirectionalPath.reverseRelationships();
  assertEquals( a, bidirectionalPath.startNode() );
  bidirectionalPath = getFirstPath( bidirectional.traverse( a, e ) );
  bidirectionalPath.reverseNodes();
  assertEquals( a, bidirectionalPath.startNode() );
  bidirectionalPath = getFirstPath( bidirectional.traverse( a, e ) );
  bidirectionalPath.iterator();
  assertEquals( a, bidirectionalPath.startNode() );
}

代码示例来源:origin: org.neo4j/neo4j-graph-algo

@Override
public Iterable<Node> reverseNodes()
{
  return path.reverseNodes();
}

代码示例来源:origin: graphaware/neo4j-algorithms

@Override
public Iterable<Node> reverseNodes() {
  return wrapped.reverseNodes();
}

代码示例来源:origin: org.neo4j/neo4j-graphdb-api

@Override
  boolean containsDuplicates( Path source )
  {
    Set<Node> nodes = new HashSet<>();
    for ( Node node : source.reverseNodes() )
    {
      if ( !nodes.add( node ) )
      {
        return true;
      }
    }
    return false;
  }
},

相关文章