Search in sources :

Example 6 with TraversalDescription

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

the class TestPath method testBidirectionalPath.

@Test
public void testBidirectionalPath() throws Exception {
    TraversalDescription side = getGraphDb().traversalDescription().uniqueness(Uniqueness.NODE_PATH);
    BidirectionalTraversalDescription bidirectional = getGraphDb().bidirectionalTraversalDescription().mirroredSides(side);
    Path bidirectionalPath = Iterables.first(bidirectional.traverse(a, e));
    assertPathIsCorrect(bidirectionalPath);
    assertEquals(a, Iterables.first(bidirectional.traverse(a, e)).startNode());
    // White box testing below: relationships(), nodes(), reverseRelationships(), reverseNodes()
    // does cache the start node if not already cached, so just make sure they to it properly.
    bidirectionalPath = Iterables.first(bidirectional.traverse(a, e));
    bidirectionalPath.relationships();
    assertEquals(a, bidirectionalPath.startNode());
    bidirectionalPath = Iterables.first(bidirectional.traverse(a, e));
    bidirectionalPath.nodes();
    assertEquals(a, bidirectionalPath.startNode());
    bidirectionalPath = Iterables.first(bidirectional.traverse(a, e));
    bidirectionalPath.reverseRelationships();
    assertEquals(a, bidirectionalPath.startNode());
    bidirectionalPath = Iterables.first(bidirectional.traverse(a, e));
    bidirectionalPath.reverseNodes();
    assertEquals(a, bidirectionalPath.startNode());
    bidirectionalPath = Iterables.first(bidirectional.traverse(a, e));
    bidirectionalPath.iterator();
    assertEquals(a, bidirectionalPath.startNode());
}
Also used : Path(org.neo4j.graphdb.Path) BidirectionalTraversalDescription(org.neo4j.graphdb.traversal.BidirectionalTraversalDescription) TraversalDescription(org.neo4j.graphdb.traversal.TraversalDescription) BidirectionalTraversalDescription(org.neo4j.graphdb.traversal.BidirectionalTraversalDescription) Test(org.junit.Test)

Example 7 with TraversalDescription

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

the class TestTraversalWithIterable method traverseWithIterableForStartNodes.

@Test
public void traverseWithIterableForStartNodes() throws Exception {
    /*
         * (a)-->(b)-->(c)
         * (d)-->(e)-->(f)
         *
         */
    createGraph("a TO b", "b TO c", "d TO e", "e TO f");
    try (Transaction tx = beginTx()) {
        TraversalDescription basicTraverser = getGraphDb().traversalDescription().evaluator(Evaluators.atDepth(2));
        Collection<Node> startNodes = new ArrayList<>();
        startNodes.add(getNodeWithName("a"));
        startNodes.add(getNodeWithName("d"));
        Iterable<Node> iterableStartNodes = startNodes;
        expectPaths(basicTraverser.traverse(iterableStartNodes), "a,b,c", "d,e,f");
        tx.success();
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) TraversalDescription(org.neo4j.graphdb.traversal.TraversalDescription) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 8 with TraversalDescription

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

the class TestTraversalWithLoops method traverseThroughNodeWithLoop.

@Test
public void traverseThroughNodeWithLoop() throws Exception {
    /*
         * (a)-->(b)-->(c)-->(d)-->(e)
         *             /  \ /  \
         *             \__/ \__/
         */
    createGraph("a TO b", "b TO c", "c TO c", "c TO d", "d TO d", "d TO e");
    try (Transaction tx = beginTx()) {
        Node a = getNodeWithName("a");
        final Node e = getNodeWithName("e");
        Evaluator onlyEndNode = new Evaluator() {

            @Override
            public Evaluation evaluate(Path path) {
                return Evaluation.ofIncludes(path.endNode().equals(e));
            }
        };
        TraversalDescription basicTraverser = getGraphDb().traversalDescription().evaluator(onlyEndNode);
        expectPaths(basicTraverser.traverse(a), "a,b,c,d,e");
        expectPaths(basicTraverser.uniqueness(Uniqueness.RELATIONSHIP_PATH).traverse(a), "a,b,c,d,e", "a,b,c,c,d,e", "a,b,c,d,d,e", "a,b,c,c,d,d,e");
        tx.success();
    }
}
Also used : Path(org.neo4j.graphdb.Path) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) TraversalDescription(org.neo4j.graphdb.traversal.TraversalDescription) Evaluator(org.neo4j.graphdb.traversal.Evaluator) Test(org.junit.Test)

Example 9 with TraversalDescription

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

the class SpecificDepthTraversalTest method shouldGetStartNodeWhenFromToIsZeroBreadthFirst.

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

Example 10 with TraversalDescription

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

the class SpecificDepthTraversalTest method shouldGetCorrectNodesFromToDepthOne.

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

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