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