Search in sources :

Example 1 with TraversalBranch

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

the class TestBidirectionalTraversal method mirroredTraversalReversesInitialState.

@Test
void mirroredTraversalReversesInitialState() {
    /*
         * (a)-->(b)-->(c)-->(d)
         */
    createGraph("a TO b", "b TO c", "c TO d");
    try (Transaction transaction = getGraphDb().beginTx()) {
        BranchCollisionPolicy collisionPolicy = (evaluator, pathPredicate) -> new StandardBranchCollisionDetector(null, null) {

            @Override
            protected boolean includePath(Path path, TraversalBranch startPath, TraversalBranch endPath) {
                assertEquals(0, startPath.state());
                assertEquals(10, endPath.state());
                return true;
            }
        };
        Iterables.count(transaction.bidirectionalTraversalDescription().mirroredSides(transaction.traversalDescription().uniqueness(NODE_PATH).expand(PathExpanders.forType(to), new InitialBranchState.State<>(0, 10))).collisionPolicy(collisionPolicy).traverse(getNodeWithName(transaction, "a"), getNodeWithName(transaction, "d")));
    }
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) BranchCollisionPolicy(org.neo4j.graphdb.traversal.BranchCollisionPolicy) Traverser(org.neo4j.graphdb.traversal.Traverser) OUTGOING(org.neo4j.graphdb.Direction.OUTGOING) Iterators.count(org.neo4j.internal.helpers.collection.Iterators.count) PathExpanders(org.neo4j.graphdb.PathExpanders) Node(org.neo4j.graphdb.Node) TraversalBranch(org.neo4j.graphdb.traversal.TraversalBranch) PathExpanderBuilder(org.neo4j.graphdb.PathExpanderBuilder) PathExpander(org.neo4j.graphdb.PathExpander) SideSelectorPolicies(org.neo4j.graphdb.traversal.SideSelectorPolicies) Evaluators.includeIfContainsAll(org.neo4j.graphdb.traversal.Evaluators.includeIfContainsAll) Iterables(org.neo4j.internal.helpers.collection.Iterables) Arrays.asList(java.util.Arrays.asList) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Transaction(org.neo4j.graphdb.Transaction) NODE_PATH(org.neo4j.graphdb.traversal.Uniqueness.NODE_PATH) StandardBranchCollisionDetector(org.neo4j.graphdb.impl.traversal.StandardBranchCollisionDetector) RelationshipType.withName(org.neo4j.graphdb.RelationshipType.withName) BidirectionalTraversalDescription(org.neo4j.graphdb.traversal.BidirectionalTraversalDescription) RELATIONSHIP_PATH(org.neo4j.graphdb.traversal.Uniqueness.RELATIONSHIP_PATH) Uniqueness(org.neo4j.graphdb.traversal.Uniqueness) Test(org.junit.jupiter.api.Test) Path(org.neo4j.graphdb.Path) InitialBranchState(org.neo4j.graphdb.traversal.InitialBranchState) TraversalDescription(org.neo4j.graphdb.traversal.TraversalDescription) Relationship(org.neo4j.graphdb.Relationship) Evaluators(org.neo4j.graphdb.traversal.Evaluators) RelationshipType(org.neo4j.graphdb.RelationshipType) Path(org.neo4j.graphdb.Path) BranchCollisionPolicy(org.neo4j.graphdb.traversal.BranchCollisionPolicy) InitialBranchState(org.neo4j.graphdb.traversal.InitialBranchState) Transaction(org.neo4j.graphdb.Transaction) StandardBranchCollisionDetector(org.neo4j.graphdb.impl.traversal.StandardBranchCollisionDetector) TraversalBranch(org.neo4j.graphdb.traversal.TraversalBranch) Test(org.junit.jupiter.api.Test)

Example 2 with TraversalBranch

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

the class TestBestFirstSelectorFactory method shouldDoWholeTraversalInCorrectOrder.

@ParameterizedTest
@MethodSource("params")
void shouldDoWholeTraversalInCorrectOrder(PathExpander expander, PathInterest<Integer> interest, Uniqueness uniqueness, String[] expectedResult) {
    try (Transaction transaction = graphDb.beginTx()) {
        var factory = new BestFirstSelectorFactory<Integer, Integer>(interest) {

            private final CostEvaluator<Integer> evaluator = CommonEvaluators.intCostEvaluator(LENGTH);

            @Override
            protected Integer getStartData() {
                return 0;
            }

            @Override
            protected Integer addPriority(TraversalBranch source, Integer currentAggregatedValue, Integer value) {
                return value + currentAggregatedValue;
            }

            @Override
            protected Integer calculateValue(TraversalBranch next) {
                return next.length() == 0 ? 0 : evaluator.getCost(next.lastRelationship(), Direction.BOTH);
            }
        };
        Node a = transaction.getNodeById(graph.getNode(transaction, "a").getId());
        Traverser traverser = new MonoDirectionalTraversalDescription().expand(expander).order(factory).uniqueness(uniqueness).traverse(a);
        var iterator = traverser.iterator();
        int i = 0;
        while (iterator.hasNext()) {
            assertPath(transaction, iterator.next(), expectedResult[i]);
            i++;
        }
        assertEquals(expectedResult.length, i, String.format("Not all expected paths where traversed. Missing paths are %s\n", Arrays.toString(Arrays.copyOfRange(expectedResult, i, expectedResult.length))));
        transaction.commit();
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) CostEvaluator(org.neo4j.graphalgo.CostEvaluator) Node(org.neo4j.graphdb.Node) Traverser(org.neo4j.graphdb.traversal.Traverser) MonoDirectionalTraversalDescription(org.neo4j.kernel.impl.traversal.MonoDirectionalTraversalDescription) TraversalBranch(org.neo4j.graphdb.traversal.TraversalBranch) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 3 with TraversalBranch

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

the class LiteDepthFirstSelector method next.

@Override
public TraversalBranch next(TraversalContext metadata) {
    TraversalBranch result = null;
    while (result == null) {
        if (current == null) {
            current = superNodes.poll();
            if (current == null) {
                return null;
            }
        } else if (current.expanded() > 0 && current.expanded() % threshold == 0) {
            superNodes.add(current);
            current = current.parent();
            continue;
        }
        TraversalBranch next = current.next(expander, metadata);
        if (next == null) {
            current = current.parent();
            continue;
        }
        current = next;
        result = current;
    }
    return result;
}
Also used : TraversalBranch(org.neo4j.graphdb.traversal.TraversalBranch)

Example 4 with TraversalBranch

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

the class BidirectionalTraversalBranchPath method iterator.

@Override
public Iterator<Entity> iterator() {
    // TODO Don't loop through them all up front
    LinkedList<Entity> entities = new LinkedList<>();
    TraversalBranch branch = start;
    while (branch.length() > 0) {
        entities.addFirst(branch.endNode());
        entities.addFirst(branch.lastRelationship());
        branch = branch.parent();
    }
    entities.addFirst(branch.endNode());
    if (cachedStartNode == null) {
        cachedStartNode = branch.endNode();
    }
    if (end.length() > 0) {
        entities.add(end.lastRelationship());
        branch = end.parent();
        while (branch.length() > 0) {
            entities.add(branch.endNode());
            entities.add(branch.lastRelationship());
            branch = branch.parent();
        }
        entities.add(branch.endNode());
    }
    return entities.iterator();
}
Also used : Entity(org.neo4j.graphdb.Entity) LinkedList(java.util.LinkedList) TraversalBranch(org.neo4j.graphdb.traversal.TraversalBranch)

Example 5 with TraversalBranch

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

the class BidirectionalTraversalBranchPath method gatherRelationships.

private List<Relationship> gatherRelationships(TraversalBranch first, TraversalBranch then) {
    // TODO Don't loop through them all up front
    LinkedList<Relationship> relationships = new LinkedList<>();
    TraversalBranch branch = first;
    while (branch.length() > 0) {
        relationships.addFirst(branch.lastRelationship());
        branch = branch.parent();
    }
    // We can might as well cache start node since we're right now there anyway
    if (cachedStartNode == null && first == start && branch.length() >= 0) {
        cachedStartNode = branch.endNode();
    }
    branch = then;
    while (branch.length() > 0) {
        relationships.add(branch.lastRelationship());
        branch = branch.parent();
    }
    if (cachedStartNode == null && then == start && branch.length() >= 0) {
        cachedStartNode = branch.endNode();
    }
    return relationships;
}
Also used : Relationship(org.neo4j.graphdb.Relationship) LinkedList(java.util.LinkedList) TraversalBranch(org.neo4j.graphdb.traversal.TraversalBranch)

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