Search in sources :

Example 31 with BasicEvaluationContext

use of org.neo4j.graphalgo.BasicEvaluationContext in project neo4j by neo4j.

the class TestShortestPath method shouldAbortAsSoonAsPossible.

// Attempt at recreating this issue without cypher
// https://github.com/neo4j/neo4j/issues/4160
@Test
void shouldAbortAsSoonAsPossible() {
    try (Transaction transaction = graphDb.beginTx()) {
        final Label A = Label.label("A");
        final Label B = Label.label("B");
        final Label C = Label.label("C");
        final Label D = Label.label("D");
        final Label E = Label.label("E");
        final Label F = Label.label("F");
        final RelationshipType relType = RelationshipType.withName("TO");
        recursiveSnowFlake(transaction, null, 0, 4, 5, new Label[] { A, B, C, D, E }, relType);
        Node a = getNodeByLabel(transaction, A);
        try (ResourceIterator<Node> allE = transaction.findNodes(E)) {
            while (allE.hasNext()) {
                final Node e = allE.next();
                final Node f = transaction.createNode(F);
                f.createRelationshipTo(e, relType);
            }
        }
        final CountingPathExpander countingPathExpander = new CountingPathExpander(forTypeAndDirection(relType, OUTGOING));
        var context = new BasicEvaluationContext(transaction, graphDb);
        final ShortestPath shortestPath = new ShortestPath(context, Integer.MAX_VALUE, countingPathExpander, Integer.MAX_VALUE, EmptyMemoryTracker.INSTANCE);
        try (ResourceIterator<Node> allF = transaction.findNodes(F)) {
            while (allF.hasNext()) {
                final Node f = allF.next();
                shortestPath.findAllPaths(a, f);
            }
        }
        assertEquals(1250, countingPathExpander.nodesVisited.intValue(), "There are 625 different end nodes. The algorithm should start one traversal for each such node. " + "That is 625*2 visited nodes if traversal is interrupted correctly.");
        transaction.commit();
    }
}
Also used : BasicEvaluationContext(org.neo4j.graphalgo.BasicEvaluationContext) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Label(org.neo4j.graphdb.Label) RelationshipType(org.neo4j.graphdb.RelationshipType) Test(org.junit.jupiter.api.Test)

Example 32 with BasicEvaluationContext

use of org.neo4j.graphalgo.BasicEvaluationContext in project neo4j by neo4j.

the class TestShortestPath method makeSureAMaxResultCountIsObeyed.

@Test
void makeSureAMaxResultCountIsObeyed() {
    // 
    try (Transaction transaction = graphDb.beginTx()) {
        graph.makeEdgeChain(transaction, "a,b,c,d,e");
        graph.makeEdgeChain(transaction, "a,f,g,h,e");
        graph.makeEdgeChain(transaction, "f,i,j,e");
        graph.makeEdgeChain(transaction, "i,k,e");
        final Node a = graph.getNode(transaction, "a");
        final Node e = graph.getNode(transaction, "e");
        final PathExpander expander = forTypeAndDirection(R1, OUTGOING);
        var context = new BasicEvaluationContext(transaction, graphDb);
        testShortestPathFinder(context, finder -> assertEquals(4, count(finder.findAllPaths(a, e))), expander, 10, 10);
        for (int i = 4; i >= 1; i--) {
            final int count = i;
            testShortestPathFinder(context, finder -> assertEquals(count, count(finder.findAllPaths(a, e))), expander, 10, count);
        }
        transaction.commit();
    }
}
Also used : BasicEvaluationContext(org.neo4j.graphalgo.BasicEvaluationContext) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) PathExpander(org.neo4j.graphdb.PathExpander) Test(org.junit.jupiter.api.Test)

Example 33 with BasicEvaluationContext

use of org.neo4j.graphalgo.BasicEvaluationContext in project neo4j by neo4j.

the class TestShortestPath method testFinderShouldNotFindAnythingBeyondLimit.

@Test
void testFinderShouldNotFindAnythingBeyondLimit() {
    // 
    try (Transaction transaction = graphDb.beginTx()) {
        graph.makeEdgeChain(transaction, "a,b,c,d,e");
        var context = new BasicEvaluationContext(transaction, graphDb);
        testShortestPathFinder(context, finder -> assertPaths(finder.findAllPaths(graph.getNode(transaction, "a"), graph.getNode(transaction, "b"))), allTypesAndDirections(), 0);
        testShortestPathFinder(context, finder -> {
            assertPaths(finder.findAllPaths(graph.getNode(transaction, "a"), graph.getNode(transaction, "c")));
            assertPaths(finder.findAllPaths(graph.getNode(transaction, "a"), graph.getNode(transaction, "d")));
        }, allTypesAndDirections(), 1);
        testShortestPathFinder(context, finder -> {
            assertPaths(finder.findAllPaths(graph.getNode(transaction, "a"), graph.getNode(transaction, "d")));
            assertPaths(finder.findAllPaths(graph.getNode(transaction, "a"), graph.getNode(transaction, "e")));
        }, allTypesAndDirections(), 2);
        transaction.commit();
    }
}
Also used : BasicEvaluationContext(org.neo4j.graphalgo.BasicEvaluationContext) Transaction(org.neo4j.graphdb.Transaction) Test(org.junit.jupiter.api.Test)

Example 34 with BasicEvaluationContext

use of org.neo4j.graphalgo.BasicEvaluationContext in project neo4j by neo4j.

the class TestShortestPath method filtersTouchesAllIntermediateNodes.

@Test
void filtersTouchesAllIntermediateNodes() {
    // 
    try (Transaction transaction = graphDb.beginTx()) {
        graph.makeEdgeChain(transaction, "a,b,c,d");
        final Node a = graph.getNode(transaction, "a");
        final Node d = graph.getNode(transaction, "d");
        Collection<Node> touchedByFilter = new HashSet<>();
        final Predicate<Node> filter = item -> {
            touchedByFilter.add(item);
            return true;
        };
        final PathExpander expander = PathExpanderBuilder.empty().add(R1, OUTGOING).addNodeFilter(filter).build();
        // final PathExpander expander = ((StandardExpander) PathExpanders.forTypeAndDirection(R1, OUTGOING)).addNodeFilter( filter );
        var context = new BasicEvaluationContext(transaction, graphDb);
        Path path = Iterables.single(shortestPath(context, expander, 10).findAllPaths(a, d));
        assertEquals(3, path.length());
        List<Node> nodes = Iterables.asList(path.nodes());
        List<Node> intermediateNodes = nodes.subList(1, nodes.size() - 1);
        assertTrue(touchedByFilter.containsAll(intermediateNodes), "touchedByFilter: " + touchedByFilter);
        assertFalse(touchedByFilter.contains(a));
        assertFalse(touchedByFilter.contains(d));
        transaction.commit();
    }
}
Also used : ResourceIterator(org.neo4j.graphdb.ResourceIterator) MutableInt(org.apache.commons.lang3.mutable.MutableInt) Label(org.neo4j.graphdb.Label) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) PathFinder(org.neo4j.graphalgo.PathFinder) OUTGOING(org.neo4j.graphdb.Direction.OUTGOING) GraphAlgoFactory.shortestPath(org.neo4j.graphalgo.GraphAlgoFactory.shortestPath) EmptyMemoryTracker(org.neo4j.memory.EmptyMemoryTracker) PathExpanders(org.neo4j.graphdb.PathExpanders) Node(org.neo4j.graphdb.Node) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) BOTH(org.neo4j.graphdb.Direction.BOTH) EvaluationContext(org.neo4j.graphalgo.EvaluationContext) PathExpanderBuilder(org.neo4j.graphdb.PathExpanderBuilder) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) PathExpander(org.neo4j.graphdb.PathExpander) Iterables(org.neo4j.internal.helpers.collection.Iterables) Arrays.asList(java.util.Arrays.asList) R1(common.Neo4jAlgoTestCase.MyRelTypes.R1) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Transaction(org.neo4j.graphdb.Transaction) StandardExpander(org.neo4j.graphdb.impl.StandardExpander) Iterator(java.util.Iterator) Iterables.count(org.neo4j.internal.helpers.collection.Iterables.count) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) Neo4jAlgoTestCase(common.Neo4jAlgoTestCase) INCOMING(org.neo4j.graphdb.Direction.INCOMING) BranchState(org.neo4j.graphdb.traversal.BranchState) Test(org.junit.jupiter.api.Test) Path(org.neo4j.graphdb.Path) PathExpanders.forTypeAndDirection(org.neo4j.graphdb.PathExpanders.forTypeAndDirection) List(java.util.List) Relationship(org.neo4j.graphdb.Relationship) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) PathExpanders.allTypesAndDirections(org.neo4j.graphdb.PathExpanders.allTypesAndDirections) RelationshipType(org.neo4j.graphdb.RelationshipType) BasicEvaluationContext(org.neo4j.graphalgo.BasicEvaluationContext) GraphAlgoFactory.shortestPath(org.neo4j.graphalgo.GraphAlgoFactory.shortestPath) Path(org.neo4j.graphdb.Path) BasicEvaluationContext(org.neo4j.graphalgo.BasicEvaluationContext) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) PathExpander(org.neo4j.graphdb.PathExpander) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 35 with BasicEvaluationContext

use of org.neo4j.graphalgo.BasicEvaluationContext in project neo4j by neo4j.

the class DijkstraTest method pathToSelfReturnsZero.

@ParameterizedTest
@MethodSource("params")
void pathToSelfReturnsZero(DijkstraFactory factory) {
    // GIVEN
    try (Transaction transaction = graphDb.beginTx()) {
        Node start = graph.makeNode(transaction, "A");
        // WHEN
        var context = new BasicEvaluationContext(transaction, graphDb);
        PathFinder<WeightedPath> finder = factory.dijkstra(context, PathExpanders.allTypesAndDirections());
        WeightedPath path = finder.findSinglePath(start, start);
        // THEN
        assertNotNull(path);
        assertEquals(start, path.startNode());
        assertEquals(start, path.endNode());
        assertEquals(0, path.length());
        transaction.commit();
    }
}
Also used : BasicEvaluationContext(org.neo4j.graphalgo.BasicEvaluationContext) WeightedPath(org.neo4j.graphalgo.WeightedPath) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

BasicEvaluationContext (org.neo4j.graphalgo.BasicEvaluationContext)52 Transaction (org.neo4j.graphdb.Transaction)52 Node (org.neo4j.graphdb.Node)40 Test (org.junit.jupiter.api.Test)33 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)20 MethodSource (org.junit.jupiter.params.provider.MethodSource)20 WeightedPath (org.neo4j.graphalgo.WeightedPath)20 Path (org.neo4j.graphdb.Path)18 ExactDepthPathFinder (org.neo4j.graphalgo.impl.path.ExactDepthPathFinder)8 Relationship (org.neo4j.graphdb.Relationship)8 HashSet (java.util.HashSet)5 PathExpander (org.neo4j.graphdb.PathExpander)5 GraphAlgoFactory.shortestPath (org.neo4j.graphalgo.GraphAlgoFactory.shortestPath)4 Neo4jAlgoTestCase (common.Neo4jAlgoTestCase)3 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)3 EvaluationContext (org.neo4j.graphalgo.EvaluationContext)3 PathFinder (org.neo4j.graphalgo.PathFinder)3 OUTGOING (org.neo4j.graphdb.Direction.OUTGOING)3 Label (org.neo4j.graphdb.Label)3 PathExpanders.allTypesAndDirections (org.neo4j.graphdb.PathExpanders.allTypesAndDirections)3