Search in sources :

Example 1 with Predicate

use of org.neo4j.helpers.Predicate in project neo4j-mobile-android by neo4j-contrib.

the class ExperimentalAStar method findAllPaths.

public Iterable<WeightedPath> findAllPaths(Node start, final Node end) {
    Predicate<Path> filter = new Predicate<Path>() {

        public boolean accept(Path position) {
            return position.endNode().equals(end);
        }
    };
    final Traverser traverser = traversalDescription.order(new SelectorFactory(end)).filter(filter).traverse(start);
    return new Iterable<WeightedPath>() {

        public Iterator<WeightedPath> iterator() {
            return new StopAfterWeightIterator(traverser.iterator(), costEvaluator);
        }
    };
}
Also used : Path(org.neo4j.graphdb.Path) WeightedPath(org.neo4j.graphalgo.WeightedPath) WeightedPath(org.neo4j.graphalgo.WeightedPath) BestFirstSelectorFactory(org.neo4j.graphalgo.impl.util.BestFirstSelectorFactory) Traverser(org.neo4j.graphdb.traversal.Traverser) StopAfterWeightIterator(org.neo4j.graphalgo.impl.util.StopAfterWeightIterator) Predicate(org.neo4j.helpers.Predicate)

Example 2 with Predicate

use of org.neo4j.helpers.Predicate in project neo4j-mobile-android by neo4j-contrib.

the class Dijkstra method findAllPaths.

public Iterable<WeightedPath> findAllPaths(Node start, final Node end) {
    Predicate<Path> filter = new Predicate<Path>() {

        public boolean accept(Path position) {
            return position.endNode().equals(end);
        }
    };
    final Traverser traverser = TRAVERSAL.expand(expander).order(new SelectorFactory(costEvaluator)).filter(filter).traverse(start);
    return new Iterable<WeightedPath>() {

        public Iterator<WeightedPath> iterator() {
            return new StopAfterWeightIterator(traverser.iterator(), costEvaluator);
        }
    };
}
Also used : Path(org.neo4j.graphdb.Path) WeightedPath(org.neo4j.graphalgo.WeightedPath) WeightedPath(org.neo4j.graphalgo.WeightedPath) BestFirstSelectorFactory(org.neo4j.graphalgo.impl.util.BestFirstSelectorFactory) Traverser(org.neo4j.graphdb.traversal.Traverser) StopAfterWeightIterator(org.neo4j.graphalgo.impl.util.StopAfterWeightIterator) Predicate(org.neo4j.helpers.Predicate)

Example 3 with Predicate

use of org.neo4j.helpers.Predicate in project graphdb by neo4j-attic.

the class Dijkstra method findAllPaths.

public Iterable<WeightedPath> findAllPaths(Node start, final Node end) {
    Predicate<Path> filter = new Predicate<Path>() {

        public boolean accept(Path position) {
            return position.endNode().equals(end);
        }
    };
    final Traverser traverser = TRAVERSAL.expand(expander).order(new SelectorFactory(costEvaluator)).filter(filter).traverse(start);
    return new Iterable<WeightedPath>() {

        public Iterator<WeightedPath> iterator() {
            return new StopAfterWeightIterator(traverser.iterator(), costEvaluator);
        }
    };
}
Also used : Path(org.neo4j.graphdb.Path) WeightedPath(org.neo4j.graphalgo.WeightedPath) WeightedPath(org.neo4j.graphalgo.WeightedPath) BestFirstSelectorFactory(org.neo4j.graphalgo.impl.util.BestFirstSelectorFactory) Traverser(org.neo4j.graphdb.traversal.Traverser) StopAfterWeightIterator(org.neo4j.graphalgo.impl.util.StopAfterWeightIterator) Predicate(org.neo4j.helpers.Predicate)

Example 4 with Predicate

use of org.neo4j.helpers.Predicate 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 5 with Predicate

use of org.neo4j.helpers.Predicate in project graphdb by neo4j-attic.

the class TestShortestPath method withFilters.

@Test
public void withFilters() throws Exception {
    graph.makeEdgeChain("a,b,c,d");
    graph.makeEdgeChain("a,g,h,d");
    Node a = graph.getNode("a");
    Node d = graph.getNode("d");
    Node b = graph.getNode("b");
    b.setProperty("skip", true);
    Predicate<Node> filter = new Predicate<Node>() {

        @Override
        public boolean accept(Node item) {
            boolean skip = (Boolean) item.getProperty("skip", false);
            return !skip;
        }
    };
    assertPaths(GraphAlgoFactory.shortestPath(Traversal.expanderForAllTypes().addNodeFilter(filter), 10).findAllPaths(a, d), "a,g,h,d");
}
Also used : Node(org.neo4j.graphdb.Node) Predicate(org.neo4j.helpers.Predicate) Test(org.junit.Test)

Aggregations

Predicate (org.neo4j.helpers.Predicate)8 Path (org.neo4j.graphdb.Path)6 Traverser (org.neo4j.graphdb.traversal.Traverser)6 WeightedPath (org.neo4j.graphalgo.WeightedPath)4 BestFirstSelectorFactory (org.neo4j.graphalgo.impl.util.BestFirstSelectorFactory)4 StopAfterWeightIterator (org.neo4j.graphalgo.impl.util.StopAfterWeightIterator)4 Node (org.neo4j.graphdb.Node)3 HashMap (java.util.HashMap)2 LiteDepthFirstSelector (org.neo4j.graphalgo.impl.util.LiteDepthFirstSelector)2 BranchOrderingPolicy (org.neo4j.graphdb.traversal.BranchOrderingPolicy)2 BranchSelector (org.neo4j.graphdb.traversal.BranchSelector)2 TraversalBranch (org.neo4j.graphdb.traversal.TraversalBranch)2 TraversalDescription (org.neo4j.graphdb.traversal.TraversalDescription)2 PrefetchingIterator (org.neo4j.helpers.collection.PrefetchingIterator)2 IOException (java.io.IOException)1 Transaction (javax.transaction.Transaction)1 Test (org.junit.Test)1 NeoStoreXaDataSource (org.neo4j.kernel.impl.nioneo.xa.NeoStoreXaDataSource)1 XaDataSource (org.neo4j.kernel.impl.transaction.xaframework.XaDataSource)1