Search in sources :

Example 31 with Entity

use of org.neo4j.graphdb.Entity in project neo4j by neo4j.

the class Dijkstra method getPath.

/**
 * @return One of the shortest paths found or null.
 */
@Override
public List<Entity> getPath() {
    if (startNode == null || endNode == null) {
        throw new RuntimeException("Start or end node undefined.");
    }
    calculate();
    if (foundPathsMiddleNodes == null || foundPathsMiddleNodes.isEmpty()) {
        return null;
    }
    Node middleNode = foundPathsMiddleNodes.iterator().next();
    List<Entity> path = new LinkedList<>();
    path.addAll(Util.constructSinglePathToNode(middleNode, predecessors1, true, false));
    path.addAll(Util.constructSinglePathToNode(middleNode, predecessors2, false, true));
    return path;
}
Also used : Entity(org.neo4j.graphdb.Entity) Node(org.neo4j.graphdb.Node) LinkedList(java.util.LinkedList)

Example 32 with Entity

use of org.neo4j.graphdb.Entity in project neo4j by neo4j.

the class Dijkstra method getPaths.

/**
 * @return All the found paths or null.
 */
@Override
public List<List<Entity>> getPaths() {
    if (startNode == null || endNode == null) {
        throw new RuntimeException("Start or end node undefined.");
    }
    calculateMultiple();
    if (foundPathsMiddleNodes == null || foundPathsMiddleNodes.isEmpty()) {
        return Collections.emptyList();
    }
    List<List<Entity>> paths = new LinkedList<>();
    for (Node middleNode : foundPathsMiddleNodes) {
        List<List<Entity>> paths1 = Util.constructAllPathsToNode(middleNode, predecessors1, true, false);
        List<List<Entity>> paths2 = Util.constructAllPathsToNode(middleNode, predecessors2, false, true);
        // For all combinations...
        for (List<Entity> part1 : paths1) {
            for (List<Entity> part2 : paths2) {
                // Combine them
                List<Entity> path = new LinkedList<>();
                path.addAll(part1);
                path.addAll(part2);
                // Add to collection
                paths.add(path);
            }
        }
    }
    return paths;
}
Also used : Entity(org.neo4j.graphdb.Entity) Node(org.neo4j.graphdb.Node) List(java.util.List) LinkedList(java.util.LinkedList) LinkedList(java.util.LinkedList)

Example 33 with Entity

use of org.neo4j.graphdb.Entity in project neo4j by neo4j.

the class PathProxyTest method shouldIterateAlternatingNodesAndRelationships.

@Test
void shouldIterateAlternatingNodesAndRelationships() {
    // given
    Path path = new PathProxy(transaction, new long[] { 1, 2, 3 }, new long[] { 100, 200 }, new int[] { 0, ~0 });
    Iterator<Entity> iterator = path.iterator();
    Entity entity;
    // then
    assertTrue(iterator.hasNext());
    assertThat(entity = iterator.next()).isInstanceOf(Node.class);
    assertEquals(1, entity.getId());
    assertTrue(iterator.hasNext());
    assertThat(entity = iterator.next()).isInstanceOf(Relationship.class);
    assertEquals(100, entity.getId());
    assertTrue(iterator.hasNext());
    assertThat(entity = iterator.next()).isInstanceOf(Node.class);
    assertEquals(2, entity.getId());
    assertTrue(iterator.hasNext());
    assertThat(entity = iterator.next()).isInstanceOf(Relationship.class);
    assertEquals(200, entity.getId());
    assertTrue(iterator.hasNext());
    assertThat(entity = iterator.next()).isInstanceOf(Node.class);
    assertEquals(3, entity.getId());
    assertFalse(iterator.hasNext());
}
Also used : Path(org.neo4j.graphdb.Path) Entity(org.neo4j.graphdb.Entity) Test(org.junit.jupiter.api.Test)

Aggregations

Entity (org.neo4j.graphdb.Entity)33 Transaction (org.neo4j.graphdb.Transaction)12 Test (org.junit.jupiter.api.Test)10 Node (org.neo4j.graphdb.Node)10 Relationship (org.neo4j.graphdb.Relationship)8 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)7 MethodSource (org.junit.jupiter.params.provider.MethodSource)7 Path (org.neo4j.graphdb.Path)7 LinkedList (java.util.LinkedList)6 Map (java.util.Map)4 List (java.util.List)3 TraversalBranch (org.neo4j.graphdb.traversal.TraversalBranch)3 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 Result (org.neo4j.graphdb.Result)2 Geometry (org.neo4j.graphdb.spatial.Geometry)2 Point (org.neo4j.graphdb.spatial.Point)2 Temporal (java.time.temporal.Temporal)1 TemporalAmount (java.time.temporal.TemporalAmount)1 ArrayList (java.util.ArrayList)1