@Override protected Node map( Relationship rel ) { return rel.getOtherNode( node ); } };
public ExtendedPath( Path start, Relationship lastRelationship ) { this.start = start; this.lastRelationship = lastRelationship; this.endNode = lastRelationship.getOtherNode( start.endNode() ); }
/** * @see SingleSourceShortestPath */ @Override public List<Node> getPredecessorNodes( Node node ) { List<Node> result = new LinkedList<>(); List<Relationship> predecessorRelationShips = predecessors1.get( node ); if ( predecessorRelationShips == null || predecessorRelationShips.size() == 0 ) { return null; } for ( Relationship relationship : predecessorRelationShips ) { result.add( relationship.getOtherNode( node ) ); } return result; }
/** * @see SingleSourceShortestPath */ @Override public List<Node> getPredecessorNodes( Node node ) { List<Node> result = new LinkedList<>(); List<Relationship> predecessorRelationShips = predecessors.get( node ); if ( predecessorRelationShips == null || predecessorRelationShips.size() == 0 ) { return null; } for ( Relationship relationship : predecessorRelationShips ) { result.add( relationship.getOtherNode( node ) ); } return result; }
/** * This can be used to generate the inverse of a structure with * predecessors, i.e. the successors. * @param predecessors * @return */ public static Map<Node,List<Relationship>> reversedPredecessors( Map<Node,List<Relationship>> predecessors ) { Map<Node,List<Relationship>> result = new HashMap<>(); Set<Node> keys = predecessors.keySet(); for ( Node node : keys ) { List<Relationship> preds = predecessors.get( node ); for ( Relationship relationship : preds ) { Node otherNode = relationship.getOtherNode( node ); // We add node as a predecessor to otherNode, instead of the // other way around List<Relationship> otherPreds = result.computeIfAbsent( otherNode, k -> new LinkedList<>() ); otherPreds.add( relationship ); } } return result; } }
public int getNumberOfPathsToNode( Node node ) { Integer i = pathCounts.get( node ); if ( i != null ) { return i; } List<Relationship> preds = predecessors.get( node ); if ( preds == null || preds.size() == 0 ) { return 1; } int result = 0; for ( Relationship relationship : preds ) { result += getNumberOfPathsToNode( relationship .getOtherNode( node ) ); } pathCounts.put( node, result ); return result; } }
private void addRelationshipsBetweenNodes() { Set<Node> newNodes = new HashSet<>(); for ( Node node : nodes.values() ) { for ( Relationship relationship : node.getRelationships() ) { if ( !relationships.containsKey( relationship.getId() ) ) { continue; } final Node other = relationship.getOtherNode( node ); if ( nodes.containsKey( other.getId() ) || newNodes.contains( other ) ) { continue; } newNodes.add( other ); } } for ( Node node : newNodes ) { add( node ); } }
@Override public NodeValue otherNode( VirtualNodeValue node ) { if ( node instanceof NodeProxyWrappingNodeValue ) { Node proxy = ((NodeProxyWrappingNodeValue) node).nodeProxy(); return ValueUtils.fromNodeProxy( relationship.getOtherNode( proxy ) ); } else { return super.otherNode( node ); } }
/** * 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(); }
@Name( GET_CONNECTED_NODES ) @PluginTarget( Node.class ) public Iterable<Node> getAllConnectedNodes( @Source Node start ) { ArrayList<Node> nodes = new ArrayList<>(); for ( Relationship rel : start.getRelationships() ) { nodes.add( rel.getOtherNode( start ) ); } return nodes; }
private Runnable getNodesFromRelationship() { return () -> { relationship.getEndNode(); relationship.getStartNode(); relationship.getNodes(); relationship.getOtherNode( node ); }; }
@PluginTarget( Node.class ) public Iterable<Relationship> getRelationshipsBetween( @Source final Node start, @Parameter( name = "other" ) final Node end ) { return new FilteringIterable<>( start.getRelationships(), item -> item.getOtherNode( start ).equals( end ) ); }
@PluginTarget( Node.class ) public Iterable<Relationship> getRelationshipsBetween( @Source final Node start, @Parameter( name = "other" ) final Node end ) { List<Relationship> result = new ArrayList<>(); try ( Transaction tx = start.getGraphDatabase().beginTx() ) { for ( Relationship relationship : start.getRelationships() ) { if ( relationship.getOtherNode( start ).equals( end ) ) { result.add( relationship ); } } tx.success(); } return result; }
@Name( GET_CONNECTED_NODES ) @PluginTarget( Node.class ) public Iterable<Node> getAllConnectedNodes( @Source Node start ) { ArrayList<Node> nodes = new ArrayList<>(); try ( Transaction tx = start.getGraphDatabase().beginTx() ) { for ( Relationship rel : start.getRelationships() ) { nodes.add( rel.getOtherNode( start ) ); } tx.success(); } return nodes; }
@Test public void getOtherNodeFunctionsCorrectly() { Node node = getGraphDb().createNode(); Relationship relationship = node.createRelationshipTo( node, TEST ); // This loop messes up the readability of the test case, but avoids duplicated // assertion code. Same assertions withing the transaction as after it has committed. for ( int i = 0; i < 2; i++ ) { assertEquals( node, relationship.getOtherNode( node ) ); assertEquals( asList( node, node ), asList( relationship.getNodes() ) ); try { relationship.getOtherNode( getGraphDb().createNode() ); fail( "Should throw exception if another node is passed into loop.getOtherNode" ); } catch ( NotFoundException e ) { // Good } newTransaction(); } }
private void setWeight( String start, String end, double weight ) { Node startNode = graph.getNode( start ); Node endNode = graph.getNode( end ); ResourceIterable<Relationship> relationships = Iterables.asResourceIterable( startNode.getRelationships() ); try ( ResourceIterator<Relationship> resourceIterator = relationships.iterator() ) { while ( resourceIterator.hasNext() ) { Relationship rel = resourceIterator.next(); if ( rel.getOtherNode( startNode ).equals( endNode ) ) { rel.setProperty( "weight", weight ); return; } } } throw new RuntimeException( "No relationship between nodes " + start + " and " + end ); } }
@Graph( value = { "I know you" }, nodes = { @NODE( name = "I", properties = { @PROP( key = "name", value = "me" ) } ) } ) private void verifyIKnowYou( String type, String myName ) { try ( Transaction ignored = graphdb.beginTx() ) { Map<String, Node> graph = data.get(); assertEquals( "Wrong graph size.", 2, graph.size() ); Node iNode = graph.get( "I" ); assertNotNull( "The node 'I' was not defined", iNode ); Node you = graph.get( "you" ); assertNotNull( "The node 'you' was not defined", you ); assertEquals( "'I' has wrong 'name'.", myName, iNode.getProperty( "name" ) ); assertEquals( "'you' has wrong 'name'.", "you", you.getProperty( "name" ) ); Iterator<Relationship> rels = iNode.getRelationships().iterator(); assertTrue( "'I' has too few relationships", rels.hasNext() ); Relationship rel = rels.next(); assertEquals( "'I' is not related to 'you'", you, rel.getOtherNode( iNode ) ); assertEquals( "Wrong relationship type.", type, rel.getType().name() ); assertFalse( "'I' has too many relationships", rels.hasNext() ); rels = you.getRelationships().iterator(); assertTrue( "'you' has too few relationships", rels.hasNext() ); rel = rels.next(); assertEquals( "'you' is not related to 'i'", iNode, rel.getOtherNode( you ) ); assertEquals( "Wrong relationship type.", type, rel.getType().name() ); assertFalse( "'you' has too many relationships", rels.hasNext() ); assertEquals( "wrong direction", iNode, rel.getStartNode() ); } }
/** * @param node1Id * @param node2Id * @return One relationship between two given nodes, if there exists one, * otherwise null. */ public Relationship getRelationship( String node1Id, String node2Id ) { Node node1 = getNode( node1Id ); Node node2 = getNode( node2Id ); if ( node1 == null || node2 == null ) { return null; } ResourceIterable<Relationship> relationships = Iterables.asResourceIterable( node1.getRelationships() ); try ( ResourceIterator<Relationship> resourceIterator = relationships.iterator() ) { while ( resourceIterator.hasNext() ) { Relationship relationship = resourceIterator.next(); if ( relationship.getOtherNode( node1 ).equals( node2 ) ) { return relationship; } } } return null; }
@Test public void canAddLoopRelationship() { Node node = getGraphDb().createNode(); node.createRelationshipTo( node, TEST ); newTransaction(); for ( Direction dir : Direction.values() ) { int count = 0; for ( Relationship rel : node.getRelationships( dir ) ) { count++; assertEquals( "start node", node, rel.getStartNode() ); assertEquals( "end node", node, rel.getEndNode() ); assertEquals( "other node", node, rel.getOtherNode( node ) ); } assertEquals( dir.name() + " relationship count", 1, count ); } }
@PluginTarget( Node.class ) public Path pathToReference( @Source Node me ) { PathFinder<Path> finder = GraphAlgoFactory.shortestPath( PathExpanders.allTypesAndDirections(), 6 ); try ( Transaction tx = me.getGraphDatabase().beginTx() ) { Node other; if ( me.hasRelationship( RelationshipType.withName( "friend" ) ) ) { ResourceIterable<Relationship> relationships = (ResourceIterable<Relationship>) me.getRelationships( RelationshipType.withName( "friend" ) ); try ( ResourceIterator<Relationship> resourceIterator = relationships.iterator() ) { other = resourceIterator.next().getOtherNode( me ); } } else { other = me.getGraphDatabase().createNode(); } Path path = finder.findSinglePath( other, me ); tx.success(); return path; } }