use of org.neo4j.graphdb.PropertyContainer in project graphdb by neo4j-attic.
the class Dijkstra method getPaths.
/**
* @return All the found paths or null.
*/
public List<List<PropertyContainer>> getPaths() {
if (startNode == null || endNode == null) {
throw new RuntimeException("Start or end node undefined.");
}
calculateMultiple();
if (foundPathsMiddleNodes == null || foundPathsMiddleNodes.size() == 0) {
return Collections.emptyList();
}
// Currently we use a set to avoid duplicate paths
// TODO: can this be done smarter?
Set<List<PropertyContainer>> paths = new HashSet<List<PropertyContainer>>();
for (Node middleNode : foundPathsMiddleNodes) {
List<List<PropertyContainer>> paths1 = Util.constructAllPathsToNode(middleNode, predecessors1, true, false);
List<List<PropertyContainer>> paths2 = Util.constructAllPathsToNode(middleNode, predecessors2, false, true);
// For all combinations...
for (List<PropertyContainer> part1 : paths1) {
for (List<PropertyContainer> part2 : paths2) {
// Combine them
LinkedList<PropertyContainer> path = new LinkedList<PropertyContainer>();
path.addAll(part1);
path.addAll(part2);
// Add to collection
paths.add(path);
}
}
}
return new LinkedList<List<PropertyContainer>>(paths);
}
use of org.neo4j.graphdb.PropertyContainer in project graphdb by neo4j-attic.
the class Util method constructSinglePathToNode.
/**
* Constructs a path to a given node, for a given set of predecessors. The
* result is a list of alternating Node/Relationship.
* @param node
* The start node
* @param predecessors
* The predecessors set
* @param includeNode
* Boolean which determines if the start node should be included
* in the paths
* @param backwards
* Boolean, if true the order of the nodes in the paths will be
* reversed
* @return A path as a list of alternating Node/Relationship.
*/
public static List<PropertyContainer> constructSinglePathToNode(Node node, Map<Node, List<Relationship>> predecessors, boolean includeNode, boolean backwards) {
LinkedList<PropertyContainer> path = new LinkedList<PropertyContainer>();
if (includeNode) {
if (backwards) {
path.addLast(node);
} else {
path.addFirst(node);
}
}
Node currentNode = node;
List<Relationship> currentPreds = predecessors.get(currentNode);
// Traverse predecessors until we have added a node without predecessors
while (currentPreds != null && currentPreds.size() != 0) {
// Get next node
Relationship currentRelationship = currentPreds.get(0);
currentNode = currentRelationship.getOtherNode(currentNode);
// Add current
if (backwards) {
path.addLast(currentRelationship);
path.addLast(currentNode);
} else {
path.addFirst(currentRelationship);
path.addFirst(currentNode);
}
// Continue with the next node
currentPreds = predecessors.get(currentNode);
}
return path;
}
use of org.neo4j.graphdb.PropertyContainer in project graphdb by neo4j-attic.
the class Util method constructAllPathsToNodeAsLinkedLists.
/**
* Same as constructAllPathsToNode, but different return type
*/
protected static List<LinkedList<PropertyContainer>> constructAllPathsToNodeAsLinkedLists(Node node, Map<Node, List<Relationship>> predecessors, boolean includeNode, boolean backwards) {
List<LinkedList<PropertyContainer>> paths = new LinkedList<LinkedList<PropertyContainer>>();
List<Relationship> current = predecessors.get(node);
// First build all paths to this node's predecessors
if (current != null) {
for (Relationship r : current) {
Node n = r.getOtherNode(node);
List<LinkedList<PropertyContainer>> newPaths = constructAllPathsToNodeAsLinkedLists(n, predecessors, true, backwards);
paths.addAll(newPaths);
// Add the relationship
for (LinkedList<PropertyContainer> path : newPaths) {
if (backwards) {
path.addFirst(r);
} else {
path.addLast(r);
}
}
}
}
// have this node added to it)
if (paths.isEmpty()) {
paths.add(new LinkedList<PropertyContainer>());
}
// Then add this node to all those paths
if (includeNode) {
for (LinkedList<PropertyContainer> path : paths) {
if (backwards) {
path.addFirst(node);
} else {
path.addLast(node);
}
}
}
return paths;
}
use of org.neo4j.graphdb.PropertyContainer in project graphdb by neo4j-attic.
the class TestTimeline method createTimestampedEntity.
private Pair<PropertyContainer, Long> createTimestampedEntity(EntityCreator<PropertyContainer> creator, TimelineIndex<PropertyContainer> timeline, long timestamp) {
PropertyContainer entity = creator.create();
timeline.add(entity, timestamp);
return Pair.of(entity, timestamp);
}
use of org.neo4j.graphdb.PropertyContainer in project graphdb by neo4j-attic.
the class TestPath method assertPathIsCorrect.
private void assertPathIsCorrect(Path path) {
Node a = referenceNode();
Relationship to1 = a.getRelationships(Direction.OUTGOING).iterator().next();
Node b = to1.getEndNode();
Relationship to2 = b.getRelationships(Direction.OUTGOING).iterator().next();
Node c = to2.getEndNode();
Relationship to3 = c.getRelationships(Direction.OUTGOING).iterator().next();
Node d = to3.getEndNode();
Relationship to4 = d.getRelationships(Direction.OUTGOING).iterator().next();
Node e = to4.getEndNode();
assertEquals((Integer) 4, (Integer) path.length());
assertEquals(a, path.startNode());
assertEquals(e, path.endNode());
assertEquals(to4, path.lastRelationship());
Iterator<PropertyContainer> pathEntities = path.iterator();
assertEquals(a, pathEntities.next());
assertEquals(to1, pathEntities.next());
assertEquals(b, pathEntities.next());
assertEquals(to2, pathEntities.next());
assertEquals(c, pathEntities.next());
assertEquals(to3, pathEntities.next());
assertEquals(d, pathEntities.next());
assertEquals(to4, pathEntities.next());
assertEquals(e, pathEntities.next());
assertFalse(pathEntities.hasNext());
Iterator<Node> nodes = path.nodes().iterator();
assertEquals(a, nodes.next());
assertEquals(b, nodes.next());
assertEquals(c, nodes.next());
assertEquals(d, nodes.next());
assertEquals(e, nodes.next());
assertFalse(nodes.hasNext());
Iterator<Relationship> relationships = path.relationships().iterator();
assertEquals(to1, relationships.next());
assertEquals(to2, relationships.next());
assertEquals(to3, relationships.next());
assertEquals(to4, relationships.next());
assertFalse(relationships.hasNext());
}
Aggregations