Search in sources :

Example 21 with TraversalBranch

use of org.neo4j.graphdb.traversal.TraversalBranch in project graphdb by neo4j-attic.

the class ExactDepthPathFinder method paths.

private Iterator<Path> paths(final Node start, final Node end) {
    TraversalDescription base = Traversal.description().uniqueness(Uniqueness.RELATIONSHIP_PATH).order(new BranchOrderingPolicy() {

        public BranchSelector create(TraversalBranch startSource) {
            return new LiteDepthFirstSelector(startSource, startThreshold);
        }
    });
    final int firstHalf = onDepth / 2;
    Traverser startTraverser = base.prune(Traversal.pruneAfterDepth(firstHalf)).expand(expander).filter(new Predicate<Path>() {

        public boolean accept(Path item) {
            return item.length() == firstHalf;
        }
    }).traverse(start);
    final int secondHalf = onDepth - firstHalf;
    Traverser endTraverser = base.prune(Traversal.pruneAfterDepth(secondHalf)).expand(expander.reversed()).filter(new Predicate<Path>() {

        public boolean accept(Path item) {
            return item.length() == secondHalf;
        }
    }).traverse(end);
    final Iterator<Path> startIterator = startTraverser.iterator();
    final Iterator<Path> endIterator = endTraverser.iterator();
    final Map<Node, Visit> visits = new HashMap<Node, Visit>();
    return new PrefetchingIterator<Path>() {

        @Override
        protected Path fetchNextOrNull() {
            Path[] found = null;
            while (found == null && (startIterator.hasNext() || endIterator.hasNext())) {
                found = goOneStep(start, startIterator, visits);
                if (found == null) {
                    found = goOneStep(end, endIterator, visits);
                }
            }
            return found != null ? toPath(found, start) : null;
        }
    };
}
Also used : Path(org.neo4j.graphdb.Path) PrefetchingIterator(org.neo4j.helpers.collection.PrefetchingIterator) BranchOrderingPolicy(org.neo4j.graphdb.traversal.BranchOrderingPolicy) HashMap(java.util.HashMap) Node(org.neo4j.graphdb.Node) BranchSelector(org.neo4j.graphdb.traversal.BranchSelector) Predicate(org.neo4j.helpers.Predicate) Traverser(org.neo4j.graphdb.traversal.Traverser) TraversalDescription(org.neo4j.graphdb.traversal.TraversalDescription) LiteDepthFirstSelector(org.neo4j.graphalgo.impl.util.LiteDepthFirstSelector) TraversalBranch(org.neo4j.graphdb.traversal.TraversalBranch)

Example 22 with TraversalBranch

use of org.neo4j.graphdb.traversal.TraversalBranch in project graphdb by neo4j-attic.

the class PostorderBreadthFirstSelector method gatherOneLevel.

private List<TraversalBranch> gatherOneLevel(List<TraversalBranch> queue) {
    List<TraversalBranch> level = new LinkedList<TraversalBranch>();
    Integer depth = null;
    for (TraversalBranch source : queue) {
        if (depth == null) {
            depth = source.depth();
        } else if (source.depth() != depth) {
            break;
        }
        while (true) {
            TraversalBranch next = source.next();
            if (next == null) {
                break;
            }
            level.add(next);
        }
    }
    return level;
}
Also used : TraversalBranch(org.neo4j.graphdb.traversal.TraversalBranch) LinkedList(java.util.LinkedList)

Example 23 with TraversalBranch

use of org.neo4j.graphdb.traversal.TraversalBranch in project graphdb by neo4j-attic.

the class PreorderBreadthFirstSelector method next.

public TraversalBranch next() {
    TraversalBranch result = null;
    while (result == null) {
        TraversalBranch next = current.next();
        if (next != null) {
            queue.add(next);
            result = next;
        } else {
            current = queue.poll();
            if (current == null) {
                return null;
            }
        }
    }
    return result;
}
Also used : TraversalBranch(org.neo4j.graphdb.traversal.TraversalBranch)

Example 24 with TraversalBranch

use of org.neo4j.graphdb.traversal.TraversalBranch in project graphdb by neo4j-attic.

the class PostorderDepthFirstSelector method next.

public TraversalBranch next() {
    TraversalBranch result = null;
    while (result == null) {
        if (current == null) {
            return null;
        }
        TraversalBranch next = current.next();
        if (next != null) {
            current = next;
        } else {
            result = current;
            current = current.parent();
        }
    }
    return result;
}
Also used : TraversalBranch(org.neo4j.graphdb.traversal.TraversalBranch)

Example 25 with TraversalBranch

use of org.neo4j.graphdb.traversal.TraversalBranch in project graphdb by neo4j-attic.

the class TraversalBranchImpl method next.

public TraversalBranch next() {
    while (relationships.hasNext()) {
        Relationship relationship = relationships.next();
        if (relationship.equals(howIGotHere)) {
            continue;
        }
        expandedCount++;
        Node node = relationship.getOtherNode(source);
        TraversalBranch next = new TraversalBranchImpl(traverser, this, depth + 1, node, traverser.description.expander, relationship);
        if (traverser.okToProceed(next)) {
            next.initialize();
            return next;
        }
    }
    return null;
}
Also used : Relationship(org.neo4j.graphdb.Relationship) Node(org.neo4j.graphdb.Node) 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