@Override public Iterable<Relationship> relationships() { return path.relationships(); }
public WeightedPathImpl( CostEvaluator<Double> costEvaluator, Path path ) { this.path = path; double cost = 0; for ( Relationship relationship : path.relationships() ) { cost += costEvaluator.getCost( relationship, Direction.OUTGOING ); } this.weight = cost; }
@Mapping( "relationships" ) public ListRepresentation relationships() { return new ListRepresentation( RepresentationType.RELATIONSHIP, new IterableWrapper<Representation, Relationship>( path.relationships() ) { @Override protected Representation underlyingObjectToObject( Relationship node ) { return ValueRepresentation.uri( RelationshipRepresentation.path( node ) ); } } ); }
@Override public boolean equals( Object obj ) { if ( obj == this ) { return true; } if ( !(obj instanceof Path) ) { return false; } Path other = (Path) obj; return relationships().equals( other.relationships() ) && other.startNode().equals( cachedStartNode ); }
@Override public RelationshipValue[] relationships() { int length = path.length(); RelationshipValue[] values = new RelationshipValue[length]; int i = 0; for ( Relationship relationship : path.relationships() ) { values[i++] = ValueUtils.fromRelationshipProxy( relationship ); } return values; }
public static String defaultPathToStringWithNotInTransactionFallback( Path path ) { try { return Paths.defaultPathToString( path ); } catch ( NotInTransactionException | DatabaseShutdownException e ) { // We don't keep the rel-name lookup if the database is shut down. Source ID and target ID also requires // database access in a transaction. However, failing on toString would be uncomfortably evil, so we fall // back to noting the relationship type id. } StringBuilder sb = new StringBuilder(); for ( Relationship rel : path.relationships() ) { if ( sb.length() == 0 ) { sb.append( "(?)" ); } sb.append( "-[?," ).append( rel.getId() ).append( "]-(?)" ); } return sb.toString(); } }
@Override public boolean equals( Object obj ) { if ( this == obj ) { return true; } else if ( obj instanceof Path ) { Path other = (Path) obj; return start.equals( other.startNode() ) && iteratorsEqual( this.relationships().iterator(), other.relationships().iterator() ); } else { return false; } }
@Override public boolean equals( Object obj ) { if ( this == obj ) { return true; } else if ( obj instanceof Path ) { Path other = (Path) obj; return startNode().equals( other.startNode() ) && iteratorsEqual( this.relationships().iterator(), other.relationships().iterator() ); } else { return false; } }
@Override public boolean equals( Object obj ) { if ( this == obj ) { return true; } else if ( obj instanceof Path ) { Path other = (Path) obj; return startNode().equals( other.startNode() ) && iteratorsEqual( this.relationships().iterator(), other.relationships().iterator() ); } else { return false; } }
@Override public boolean equals( Object obj ) { if ( this == obj ) { return true; } if ( obj instanceof PathProxy ) { PathProxy that = (PathProxy) obj; return Arrays.equals( this.nodes, that.nodes ) && Arrays.equals( this.relationships, that.relationships ); } else if ( obj instanceof Path ) { Path other = (Path) obj; if ( nodes[0] != other.startNode().getId() ) { return false; } return iteratorsEqual( this.relationships().iterator(), other.relationships().iterator() ); } else { return false; } }
/** * Method for building a string representation of a {@link Path}, using * the given {@code builder}. * @param <T> the type of {@link Path}. * @param path the {@link Path} to build a string representation of. * @param builder the {@link PathDescriptor} to get * {@link Node} and {@link Relationship} representations from. * @return a string representation of a {@link Path}. */ public static <T extends Path> String pathToString( T path, PathDescriptor<T> builder ) { Node current = path.startNode(); StringBuilder result = new StringBuilder(); for ( Relationship rel : path.relationships() ) { result.append( builder.nodeRepresentation( path, current ) ); result.append( builder.relationshipRepresentation( path, current, rel ) ); current = rel.getOtherNode( current ); } if ( null != current ) { result.append( builder.nodeRepresentation( path, current ) ); } return result.toString(); }
@Override public boolean equals( Object obj ) { if ( this == obj ) { return true; } if ( obj instanceof CoreAPIPath ) { return value.equals( ((CoreAPIPath) obj).value ); } else if ( obj instanceof Path ) { Path other = (Path) obj; if ( value.nodes()[0].id() != other.startNode().getId() ) { return false; } return iteratorsEqual( this.relationships().iterator(), other.relationships().iterator() ); } else { return false; } }
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; }
@Test public void shouldHandlePaths() { // Given NodeValue n1 = nodeValue( 42L, stringArray( "L" ), EMPTY_MAP ); NodeValue n2 = nodeValue( 43L, stringArray( "L" ), EMPTY_MAP ); PathValue p = path( new NodeValue[]{n1, n2}, new RelationshipValue[]{relationshipValue( 1L, n1, n2, stringValue( "T" ), EMPTY_MAP )} ); // When p.writeTo( converter ); // Then Object value = converter.value(); assertThat( value, instanceOf( Path.class ) ); Path path = (Path) value; assertThat( path.length(), equalTo( 1 ) ); assertThat( path.startNode().getId(), equalTo( 42L ) ); assertThat( path.endNode().getId(), equalTo( 43L ) ); assertThat( path.relationships().iterator().next().getId(), equalTo( 1L ) ); }
@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 ) ) ); } }
@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 ) ) ); } }
@Test public void shouldIterateThroughRelationships() { // given Path path = new PathProxy( proxySPI, new long[] {1, 2, 3}, new long[] {100, 200}, new int[] {0, ~0} ); Iterator<Relationship> iterator = path.relationships().iterator(); Relationship relationship; // then assertTrue( iterator.hasNext() ); assertThat( relationship = iterator.next(), instanceOf( Relationship.class ) ); assertEquals( 100, relationship.getId() ); assertEquals( 1, relationship.getStartNodeId() ); assertEquals( 2, relationship.getEndNodeId() ); assertTrue( iterator.hasNext() ); assertThat( relationship = iterator.next(), instanceOf( Relationship.class ) ); assertEquals( 200, relationship.getId() ); assertEquals( 3, relationship.getStartNodeId() ); assertEquals( 2, relationship.getEndNodeId() ); assertFalse( iterator.hasNext() ); }
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 ); }
@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() ); }
@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() ); }