use of org.neo4j.graphdb.traversal.TraversalBranch in project neo4j by neo4j.
the class BidirectionalTraversalBranchPath method gatherNodes.
private Iterable<Node> gatherNodes(TraversalBranch first, TraversalBranch then) {
// TODO Don't loop through them all up front
LinkedList<Node> nodes = new LinkedList<>();
TraversalBranch branch = first;
while (branch.length() > 0) {
nodes.addFirst(branch.endNode());
branch = branch.parent();
}
if (cachedStartNode == null && first == start && branch.length() >= 0) {
cachedStartNode = branch.endNode();
}
nodes.addFirst(branch.endNode());
branch = then.parent();
if (branch != null) {
while (branch.length() > 0) {
nodes.add(branch.endNode());
branch = branch.parent();
}
if (branch.length() >= 0) {
nodes.add(branch.endNode());
}
}
if (cachedStartNode == null && then == start && branch != null && branch.length() >= 0) {
cachedStartNode = branch.endNode();
}
return nodes;
}
use of org.neo4j.graphdb.traversal.TraversalBranch in project neo4j by neo4j.
the class StandardBranchCollisionDetector method evaluate.
@Override
@SuppressWarnings("unchecked")
public Collection<Path> evaluate(TraversalBranch branch, Direction direction) {
// [0] for paths from start, [1] for paths from end
Collection<TraversalBranch>[] pathsHere = paths.get(branch.endNode());
int index = direction.ordinal();
if (pathsHere == null) {
pathsHere = new Collection[] { new ArrayList<>(), new ArrayList<>() };
paths.put(branch.endNode(), pathsHere);
}
pathsHere[index].add(branch);
// If there are paths from the other side then include all the
// combined paths
Collection<TraversalBranch> otherCollections = pathsHere[index == 0 ? 1 : 0];
if (!otherCollections.isEmpty()) {
Collection<Path> foundPaths = new ArrayList<>();
for (TraversalBranch otherBranch : otherCollections) {
TraversalBranch startPath = index == 0 ? branch : otherBranch;
TraversalBranch endPath = index == 0 ? otherBranch : branch;
BidirectionalTraversalBranchPath path = new BidirectionalTraversalBranchPath(startPath, endPath);
if (isAcceptablePath(path)) {
if (returnedPaths.add(path) && includePath(path, startPath, endPath)) {
foundPaths.add(path);
}
}
}
if (!foundPaths.isEmpty()) {
return foundPaths;
}
}
return null;
}
use of org.neo4j.graphdb.traversal.TraversalBranch in project neo4j by neo4j.
the class BidirectionalTraverserIterator method fetchNextOrNull.
@Override
protected Path fetchNextOrNull() {
if (foundPaths != null) {
if (foundPaths.hasNext()) {
numberOfPathsReturned++;
return foundPaths.next();
}
foundPaths = null;
}
TraversalBranch result;
while (true) {
result = selector.next(this);
if (result == null) {
return null;
}
Iterable<Path> pathCollisions = collisionDetector.evaluate(result, selector.currentSide());
if (pathCollisions != null) {
foundPaths = pathCollisions.iterator();
if (foundPaths.hasNext()) {
numberOfPathsReturned++;
return foundPaths.next();
}
}
}
}
use of org.neo4j.graphdb.traversal.TraversalBranch in project neo4j by neo4j.
the class TraversalBranchImpl method equals.
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof TraversalBranch)) {
return false;
}
TraversalBranch branch = this;
TraversalBranch other = (TraversalBranch) obj;
if (branch.length() != other.length()) {
return false;
}
while (branch.length() > 0) {
if (!branch.lastRelationship().equals(other.lastRelationship())) {
return false;
}
branch = branch.parent();
other = other.parent();
}
return true;
}
use of org.neo4j.graphdb.traversal.TraversalBranch in project neo4j by neo4j.
the class TraversalBranchImpl method iterator.
@Override
public Iterator<Entity> iterator() {
LinkedList<Entity> entities = new LinkedList<>();
TraversalBranch branch = this;
while (branch.length() > 0) {
entities.addFirst(branch.endNode());
entities.addFirst(branch.lastRelationship());
branch = branch.parent();
}
entities.addFirst(branch.endNode());
return entities.iterator();
}
Aggregations