Search in sources :

Example 21 with BasicEvaluationContext

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

the class TestShortestPath method testSimplestGraph.

@Test
void testSimplestGraph() {
    // \__/
    try (Transaction transaction = graphDb.beginTx()) {
        graph.makeEdge(transaction, "s", "t");
        graph.makeEdge(transaction, "s", "t");
        var context = new BasicEvaluationContext(transaction, graphDb);
        testShortestPathFinder(context, finder -> {
            final Iterable<Path> paths = finder.findAllPaths(graph.getNode(transaction, "s"), graph.getNode(transaction, "t"));
            assertPaths(paths, "s,t", "s,t");
            assertPaths(asList(finder.findSinglePath(graph.getNode(transaction, "s"), graph.getNode(transaction, "t"))), "s,t");
        }, forTypeAndDirection(R1, BOTH), 1);
        transaction.commit();
    }
}
Also used : GraphAlgoFactory.shortestPath(org.neo4j.graphalgo.GraphAlgoFactory.shortestPath) Path(org.neo4j.graphdb.Path) BasicEvaluationContext(org.neo4j.graphalgo.BasicEvaluationContext) Transaction(org.neo4j.graphdb.Transaction) Test(org.junit.jupiter.api.Test)

Example 22 with BasicEvaluationContext

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

the class TestShortestPath method makeSureShortestPathsReturnsNoLoops.

@Test
void makeSureShortestPathsReturnsNoLoops() {
    // 
    try (Transaction transaction = graphDb.beginTx()) {
        graph.makeEdgeChain(transaction, "a,b,c,d,b,c,e");
        var context = new BasicEvaluationContext(transaction, graphDb);
        testShortestPathFinder(context, finder -> {
            final Node a = graph.getNode(transaction, "a");
            final Node e = graph.getNode(transaction, "e");
            assertPaths(finder.findAllPaths(a, e), "a,b,c,e", "a,b,c,e");
        }, forTypeAndDirection(R1, BOTH), 6);
        transaction.commit();
    }
}
Also used : BasicEvaluationContext(org.neo4j.graphalgo.BasicEvaluationContext) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Test(org.junit.jupiter.api.Test)

Example 23 with BasicEvaluationContext

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

the class TestShortestPath method makeSureRelationshipNotConnectedIssueNotThere.

@Test
void makeSureRelationshipNotConnectedIssueNotThere() {
    /*
         *                                  (g)
         *                                  / ^
         *                                 v   \
         * (a)<--(b)<--(c)<--(d)<--(e)<--(f)   (i)
         *                                 ^   /
         *                                  \ v
         *                                  (h)
         */
    try (Transaction transaction = graphDb.beginTx()) {
        graph.makeEdgeChain(transaction, "i,g,f,e,d,c,b,a");
        graph.makeEdgeChain(transaction, "i,h,f");
        var context = new BasicEvaluationContext(transaction, graphDb);
        testShortestPathFinder(context, finder -> {
            final Node start = graph.getNode(transaction, "a");
            final Node end = graph.getNode(transaction, "i");
            assertPaths(finder.findAllPaths(start, end), "a,b,c,d,e,f,g,i", "a,b,c,d,e,f,h,i");
        }, forTypeAndDirection(R1, INCOMING), 10);
        transaction.commit();
    }
}
Also used : BasicEvaluationContext(org.neo4j.graphalgo.BasicEvaluationContext) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Test(org.junit.jupiter.api.Test)

Example 24 with BasicEvaluationContext

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

the class TestShortestPath method makeSureDescentStopsWhenPathIsFound.

@Test
void makeSureDescentStopsWhenPathIsFound() {
    /*
         * (a)==>(b)==>(c)==>(d)==>(e)
         *   \
         *    v
         *    (f)-->(g)-->(h)-->(i)
         */
    try (Transaction transaction = graphDb.beginTx()) {
        graph.makeEdgeChain(transaction, "a,b,c,d,e");
        graph.makeEdgeChain(transaction, "a,b,c,d,e");
        graph.makeEdgeChain(transaction, "a,f,g,h,i");
        final Node a = graph.getNode(transaction, "a");
        final Node b = graph.getNode(transaction, "b");
        final Node c = graph.getNode(transaction, "c");
        final Set<Node> allowedNodes = new HashSet<>(asList(a, b, c));
        var context = new BasicEvaluationContext(transaction, graphDb);
        final PathFinder<Path> finder = new ShortestPath(context, 100, PathExpanders.forDirection(OUTGOING)) {

            @Override
            protected Node filterNextLevelNodes(Node nextNode) {
                if (!allowedNodes.contains(nextNode)) {
                    return null;
                }
                return nextNode;
            }
        };
        Iterator<Path> paths = finder.findAllPaths(a, c).iterator();
        for (int i = 0; i < 4; i++) {
            Path aToBToC = paths.next();
            assertPath(aToBToC, a, b, c);
        }
        assertFalse(paths.hasNext(), "should only have contained four paths");
        transaction.commit();
    }
}
Also used : 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) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 25 with BasicEvaluationContext

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

the class TestShortestPath method makeSureShortestPathCanBeFetchedEvenIfANodeHasLoops.

@Test
void makeSureShortestPathCanBeFetchedEvenIfANodeHasLoops() {
    // (p)
    try (Transaction transaction = graphDb.beginTx()) {
        graph.makeEdgeChain(transaction, "m,s,n,p");
        graph.makeEdgeChain(transaction, "m,o,n");
        graph.makeEdge(transaction, "o", "o");
        graph.makeEdge(transaction, "n", "n");
        var context = new BasicEvaluationContext(transaction, graphDb);
        testShortestPathFinder(context, finder -> assertPaths(finder.findAllPaths(graph.getNode(transaction, "m"), graph.getNode(transaction, "p")), "m,s,n,p", "m,o,n,p"), forTypeAndDirection(R1, BOTH), 3);
        transaction.commit();
    }
}
Also used : BasicEvaluationContext(org.neo4j.graphalgo.BasicEvaluationContext) Transaction(org.neo4j.graphdb.Transaction) Test(org.junit.jupiter.api.Test)

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