Search in sources :

Example 26 with TraversalBranch

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

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)

Example 27 with TraversalBranch

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

the class StandardBranchCollisionDetectorTest method testFilteredPathEvaluation.

@Test
void testFilteredPathEvaluation() {
    final Entity endNode = mock(Node.class);
    final Entity alternativeEndNode = mock(Node.class);
    final Node startNode = mock(Node.class);
    Evaluator evaluator = mock(Evaluator.class);
    TraversalBranch branch = mock(TraversalBranch.class);
    TraversalBranch alternativeBranch = mock(TraversalBranch.class);
    when(branch.iterator()).thenAnswer(new IteratorAnswer(endNode));
    when(alternativeBranch.iterator()).thenAnswer(new IteratorAnswer(alternativeEndNode));
    when(alternativeBranch.startNode()).thenReturn(startNode);
    when(evaluator.evaluate(Mockito.any(Path.class))).thenReturn(Evaluation.INCLUDE_AND_CONTINUE);
    StandardBranchCollisionDetector collisionDetector = new StandardBranchCollisionDetector(evaluator, path -> alternativeEndNode.equals(path.endNode()) && startNode.equals(path.startNode()));
    Collection<Path> incoming = collisionDetector.evaluate(branch, Direction.INCOMING);
    Collection<Path> outgoing = collisionDetector.evaluate(branch, Direction.OUTGOING);
    Collection<Path> alternativeIncoming = collisionDetector.evaluate(alternativeBranch, Direction.INCOMING);
    Collection<Path> alternativeOutgoing = collisionDetector.evaluate(alternativeBranch, Direction.OUTGOING);
    assertNull(incoming);
    assertNull(outgoing);
    assertNull(alternativeIncoming);
    assertEquals(1, alternativeOutgoing.size());
}
Also used : Path(org.neo4j.graphdb.Path) Entity(org.neo4j.graphdb.Entity) Node(org.neo4j.graphdb.Node) Evaluator(org.neo4j.graphdb.traversal.Evaluator) TraversalBranch(org.neo4j.graphdb.traversal.TraversalBranch) Test(org.junit.jupiter.api.Test)

Example 28 with TraversalBranch

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

the class TraversalBranchImplTest method shouldExpandOnFirstAccess.

@SuppressWarnings("unchecked")
@Test
void shouldExpandOnFirstAccess() {
    // GIVEN
    TraversalBranch parent = mock(TraversalBranch.class);
    Node source = mock(Node.class);
    TraversalBranchImpl branch = new TraversalBranchImpl(parent, source);
    @SuppressWarnings("rawtypes") PathExpander expander = mock(PathExpander.class);
    when(expander.expand(eq(branch), any(BranchState.class))).thenReturn(Iterables.emptyResourceIterable());
    TraversalContext context = mock(TraversalContext.class);
    when(context.evaluate(eq(branch), isNull())).thenReturn(INCLUDE_AND_CONTINUE);
    // WHEN initializing
    branch.initialize(expander, context);
    // THEN the branch should not be expanded
    verifyNoInteractions(source);
    // and WHEN actually traversing from it
    branch.next(expander, context);
    // THEN we should expand it
    verify(expander).expand(any(Path.class), any(BranchState.class));
}
Also used : Path(org.neo4j.graphdb.Path) Node(org.neo4j.graphdb.Node) PathExpander(org.neo4j.graphdb.PathExpander) TraversalContext(org.neo4j.graphdb.traversal.TraversalContext) BranchState(org.neo4j.graphdb.traversal.BranchState) TraversalBranch(org.neo4j.graphdb.traversal.TraversalBranch) Test(org.junit.jupiter.api.Test)

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