Search in sources :

Example 6 with TraversalBranch

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;
}
Also used : Node(org.neo4j.graphdb.Node) LinkedList(java.util.LinkedList) TraversalBranch(org.neo4j.graphdb.traversal.TraversalBranch)

Example 7 with TraversalBranch

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;
}
Also used : Path(org.neo4j.graphdb.Path) ArrayList(java.util.ArrayList) Collection(java.util.Collection) TraversalBranch(org.neo4j.graphdb.traversal.TraversalBranch)

Example 8 with TraversalBranch

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();
            }
        }
    }
}
Also used : Path(org.neo4j.graphdb.Path) TraversalBranch(org.neo4j.graphdb.traversal.TraversalBranch)

Example 9 with TraversalBranch

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;
}
Also used : TraversalBranch(org.neo4j.graphdb.traversal.TraversalBranch)

Example 10 with TraversalBranch

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();
}
Also used : Entity(org.neo4j.graphdb.Entity) LinkedList(java.util.LinkedList) TraversalBranch(org.neo4j.graphdb.traversal.TraversalBranch)

Aggregations

TraversalBranch (org.neo4j.graphdb.traversal.TraversalBranch)28 Node (org.neo4j.graphdb.Node)13 LinkedList (java.util.LinkedList)10 Relationship (org.neo4j.graphdb.Relationship)9 Path (org.neo4j.graphdb.Path)7 Traverser (org.neo4j.graphdb.traversal.Traverser)4 Test (org.junit.jupiter.api.Test)3 Entity (org.neo4j.graphdb.Entity)3 TraversalDescription (org.neo4j.graphdb.traversal.TraversalDescription)3 HashMap (java.util.HashMap)2 LiteDepthFirstSelector (org.neo4j.graphalgo.impl.util.LiteDepthFirstSelector)2 PathExpander (org.neo4j.graphdb.PathExpander)2 Transaction (org.neo4j.graphdb.Transaction)2 BranchOrderingPolicy (org.neo4j.graphdb.traversal.BranchOrderingPolicy)2 BranchSelector (org.neo4j.graphdb.traversal.BranchSelector)2 Predicate (org.neo4j.helpers.Predicate)2 PrefetchingIterator (org.neo4j.helpers.collection.PrefetchingIterator)2 ArrayList (java.util.ArrayList)1 Arrays.asList (java.util.Arrays.asList)1 Collection (java.util.Collection)1