Search in sources :

Example 11 with TraversalDescription

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

the class SpecificDepthTraversalTest method shouldGetSecondNodeWhenFromToIsTwoBreadthFirst.

@Test
public void shouldGetSecondNodeWhenFromToIsTwoBreadthFirst() {
    TraversalDescription description = getGraphDb().traversalDescription().breadthFirst().evaluator(Evaluators.fromDepth(2)).evaluator(Evaluators.toDepth(2));
    expectNodes(description.traverse(getNodeWithName("5")), "2");
}
Also used : TraversalDescription(org.neo4j.graphdb.traversal.TraversalDescription) Test(org.junit.Test)

Example 12 with TraversalDescription

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

the class TestBidirectionalTraversal method multipleStartAndEndNodes.

@Test
public void multipleStartAndEndNodes() throws Exception {
    /*
         * (a)--\         -->(f)
         *       v       /
         * (b)-->(d)<--(e)-->(g)
         *       ^
         * (c)--/
         */
    createGraph("a TO d", "b TO d", "c TO d", "e TO d", "e TO f", "e TO g");
    PathExpander<Void> expander = PathExpanderBuilder.<Void>empty().add(to).build();
    TraversalDescription side = getGraphDb().traversalDescription().uniqueness(NODE_PATH).expand(expander);
    expectPaths(getGraphDb().bidirectionalTraversalDescription().mirroredSides(side).traverse(asList(getNodeWithName("a"), getNodeWithName("b"), getNodeWithName("c")), asList(getNodeWithName("f"), getNodeWithName("g"))), "a,d,e,f", "a,d,e,g", "b,d,e,f", "b,d,e,g", "c,d,e,f", "c,d,e,g");
}
Also used : BidirectionalTraversalDescription(org.neo4j.graphdb.traversal.BidirectionalTraversalDescription) TraversalDescription(org.neo4j.graphdb.traversal.TraversalDescription) Test(org.junit.Test)

Example 13 with TraversalDescription

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

the class TestMultipleFilters method testNarrowingFilters.

@Test
public void testNarrowingFilters() {
    Evaluator mustBeConnectedToK = new MustBeConnectedToNodeFilter(getNodeWithName("k"));
    Evaluator mustNotHaveMoreThanTwoOutRels = path -> Evaluation.ofIncludes(Iterables.count(path.endNode().getRelationships(Direction.OUTGOING)) <= 2);
    TraversalDescription description = getGraphDb().traversalDescription().evaluator(mustBeConnectedToK);
    expectNodes(description.traverse(node("a")), "b", "c");
    expectNodes(description.evaluator(mustNotHaveMoreThanTwoOutRels).traverse(node("a")), "c");
}
Also used : Evaluator(org.neo4j.graphdb.traversal.Evaluator) Predicate(java.util.function.Predicate) Direction(org.neo4j.graphdb.Direction) Test(org.junit.Test) Evaluators.includeIfAcceptedByAny(org.neo4j.graphdb.traversal.Evaluators.includeIfAcceptedByAny) Node(org.neo4j.graphdb.Node) Evaluation(org.neo4j.graphdb.traversal.Evaluation) Path(org.neo4j.graphdb.Path) TraversalDescription(org.neo4j.graphdb.traversal.TraversalDescription) Relationship(org.neo4j.graphdb.Relationship) Iterables(org.neo4j.helpers.collection.Iterables) After(org.junit.After) Transaction(org.neo4j.graphdb.Transaction) Before(org.junit.Before) TraversalDescription(org.neo4j.graphdb.traversal.TraversalDescription) Evaluator(org.neo4j.graphdb.traversal.Evaluator) Test(org.junit.Test)

Example 14 with TraversalDescription

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

the class TraversalAStar method findPaths.

private Iterable<WeightedPath> findPaths(Node start, Node end, boolean multiplePaths) {
    PathInterest interest;
    if (multiplePaths) {
        interest = stopAfterLowestWeight ? PathInterestFactory.allShortest() : PathInterestFactory.all();
    } else {
        interest = PathInterestFactory.single();
    }
    GraphDatabaseService db = start.getGraphDatabase();
    TraversalDescription traversalDescription = db.traversalDescription().uniqueness(Uniqueness.NONE).expand(expander, initialState);
    lastTraverser = traversalDescription.order(new SelectorFactory(end, interest)).evaluator(includeWhereEndNodeIs(end)).traverse(start);
    return new Iterable<WeightedPath>() {

        @Override
        public Iterator<WeightedPath> iterator() {
            return new WeightedPathIterator(lastTraverser.iterator(), costEvaluator, stopAfterLowestWeight);
        }
    };
}
Also used : WeightedPathIterator(org.neo4j.graphalgo.impl.util.WeightedPathIterator) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) WeightedPath(org.neo4j.graphalgo.WeightedPath) BestFirstSelectorFactory(org.neo4j.graphalgo.impl.util.BestFirstSelectorFactory) PathInterest(org.neo4j.graphalgo.impl.util.PathInterest) TraversalDescription(org.neo4j.graphdb.traversal.TraversalDescription)

Example 15 with TraversalDescription

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

the class TraversalShortestPath method instantiateTraverser.

@Override
protected Traverser instantiateTraverser(Node start, Node end) {
    GraphDatabaseService db = start.getGraphDatabase();
    TraversalDescription sideBase = db.traversalDescription().breadthFirst().uniqueness(NODE_PATH);
    return db.bidirectionalTraversalDescription().mirroredSides(sideBase.expand(expander)).sideSelector(LEVEL_STOP_DESCENT_ON_RESULT, maxDepth).collisionEvaluator(toDepth(maxDepth)).traverse(start, end);
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) TraversalDescription(org.neo4j.graphdb.traversal.TraversalDescription)

Aggregations

TraversalDescription (org.neo4j.graphdb.traversal.TraversalDescription)37 Test (org.junit.Test)20 Node (org.neo4j.graphdb.Node)12 Path (org.neo4j.graphdb.Path)12 Transaction (org.neo4j.graphdb.Transaction)7 Evaluator (org.neo4j.graphdb.traversal.Evaluator)6 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)5 HashMap (java.util.HashMap)3 WeightedPath (org.neo4j.graphalgo.WeightedPath)3 LiteDepthFirstSelector (org.neo4j.graphalgo.impl.util.LiteDepthFirstSelector)3 BidirectionalTraversalDescription (org.neo4j.graphdb.traversal.BidirectionalTraversalDescription)3 BranchOrderingPolicy (org.neo4j.graphdb.traversal.BranchOrderingPolicy)3 BranchSelector (org.neo4j.graphdb.traversal.BranchSelector)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Direction (org.neo4j.graphdb.Direction)2 PathExpander (org.neo4j.graphdb.PathExpander)2 Relationship (org.neo4j.graphdb.Relationship)2 RelationshipType (org.neo4j.graphdb.RelationshipType)2 TraversalBranch (org.neo4j.graphdb.traversal.TraversalBranch)2