Search in sources :

Example 1 with BranchSelector

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

the class ExactDepthPathFinder method instantiateTraverser.

@Override
protected Traverser instantiateTraverser(Node start, Node end) {
    GraphDatabaseService db = start.getGraphDatabase();
    TraversalDescription side = db.traversalDescription().breadthFirst().uniqueness(uniqueness).order(new BranchOrderingPolicy() {

        @Override
        public BranchSelector create(TraversalBranch startSource, PathExpander expander) {
            return new LiteDepthFirstSelector(startSource, startThreshold, expander);
        }
    });
    return db.bidirectionalTraversalDescription().startSide(side.expand(expander).evaluator(toDepth(onDepth / 2))).endSide(side.expand(expander.reverse()).evaluator(toDepth(onDepth - onDepth / 2))).collisionEvaluator(atDepth(onDepth)).traverse(start, end);
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) BranchOrderingPolicy(org.neo4j.graphdb.traversal.BranchOrderingPolicy) TraversalDescription(org.neo4j.graphdb.traversal.TraversalDescription) LiteDepthFirstSelector(org.neo4j.graphalgo.impl.util.LiteDepthFirstSelector) PathExpander(org.neo4j.graphdb.PathExpander) TraversalBranch(org.neo4j.graphdb.traversal.TraversalBranch) BranchSelector(org.neo4j.graphdb.traversal.BranchSelector)

Example 2 with BranchSelector

use of org.neo4j.graphdb.traversal.BranchSelector 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 3 with BranchSelector

use of org.neo4j.graphdb.traversal.BranchSelector 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)

Aggregations

LiteDepthFirstSelector (org.neo4j.graphalgo.impl.util.LiteDepthFirstSelector)3 BranchOrderingPolicy (org.neo4j.graphdb.traversal.BranchOrderingPolicy)3 BranchSelector (org.neo4j.graphdb.traversal.BranchSelector)3 TraversalBranch (org.neo4j.graphdb.traversal.TraversalBranch)3 TraversalDescription (org.neo4j.graphdb.traversal.TraversalDescription)3 HashMap (java.util.HashMap)2 Node (org.neo4j.graphdb.Node)2 Path (org.neo4j.graphdb.Path)2 Traverser (org.neo4j.graphdb.traversal.Traverser)2 Predicate (org.neo4j.helpers.Predicate)2 PrefetchingIterator (org.neo4j.helpers.collection.PrefetchingIterator)2 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)1 PathExpander (org.neo4j.graphdb.PathExpander)1