Search in sources :

Example 11 with TraversalBranch

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

the class TraversalBranchImpl method next.

@Override
public TraversalBranch next(PathExpander expander, TraversalContext context) {
    if (relationships == null) {
        expandRelationships(expander);
    }
    while (relationships.hasNext()) {
        Relationship relationship = relationships.next();
        if (relationship.equals(howIGotHere)) {
            context.unnecessaryRelationshipTraversed();
            continue;
        }
        expandedCount++;
        Node node = relationship.getOtherNode(source);
        // TODO maybe an unnecessary instantiation. Instead pass in this+node+relationship to uniqueness check
        TraversalBranch next = newNextBranch(node, relationship);
        if (context.isUnique(next)) {
            context.relationshipTraversed();
            next.initialize(expander, context);
            return next;
        } else {
            context.unnecessaryRelationshipTraversed();
        }
    }
    resetRelationships();
    return null;
}
Also used : Relationship(org.neo4j.graphdb.Relationship) Node(org.neo4j.graphdb.Node) TraversalBranch(org.neo4j.graphdb.traversal.TraversalBranch)

Example 12 with TraversalBranch

use of org.neo4j.graphdb.traversal.TraversalBranch in project neo4j-mobile-android by neo4j-contrib.

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)

Example 13 with TraversalBranch

use of org.neo4j.graphdb.traversal.TraversalBranch in project neo4j-mobile-android by neo4j-contrib.

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 14 with TraversalBranch

use of org.neo4j.graphdb.traversal.TraversalBranch in project neo4j-mobile-android by neo4j-contrib.

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 15 with TraversalBranch

use of org.neo4j.graphdb.traversal.TraversalBranch in project neo4j-mobile-android by neo4j-contrib.

the class TraversalPath method ensureEntitiesAreGathered.

private void ensureEntitiesAreGathered() {
    if (nodes == null) {
        // We don't synchronize on nodes/relationship... and that's fine
        // because even if there would be a situation where two (or more)
        // threads comes here at the same time everything would still
        // work as expected (in here as well as outside).
        LinkedList<Node> nodesList = new LinkedList<Node>();
        LinkedList<Relationship> relationshipsList = new LinkedList<Relationship>();
        TraversalBranch stepper = branch;
        while (stepper != null) {
            nodesList.addFirst(stepper.node());
            Relationship relationship = stepper.relationship();
            if (relationship != null) {
                relationshipsList.addFirst(relationship);
            }
            stepper = stepper.parent();
        }
        nodes = nodesList;
        relationships = relationshipsList;
    }
}
Also used : Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) 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