Search in sources :

Example 11 with BasicEvaluationContext

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

the class TestAllPaths method testTripleRelationshipGraph.

@Test
void testTripleRelationshipGraph() {
    /* Layout
         *          ___
         * (a)---(b)===(c)---(d)
         */
    try (Transaction transaction = graphDb.beginTx()) {
        graph.makeEdge(transaction, "a", "b");
        graph.makeEdge(transaction, "b", "c");
        graph.makeEdge(transaction, "b", "c");
        graph.makeEdge(transaction, "b", "c");
        graph.makeEdge(transaction, "c", "d");
        var context = new BasicEvaluationContext(transaction, graphDb);
        PathFinder<Path> finder = instantiatePathFinder(context, 10);
        Iterable<Path> paths = finder.findAllPaths(graph.getNode(transaction, "a"), graph.getNode(transaction, "d"));
        assertPaths(paths, "a,b,c,d", "a,b,c,d", "a,b,c,d", "a,b,c,b,c,d", "a,b,c,b,c,d", "a,b,c,b,c,d", "a,b,c,b,c,d", "a,b,c,b,c,d", "a,b,c,b,c,d");
        transaction.commit();
    }
}
Also used : Path(org.neo4j.graphdb.Path) BasicEvaluationContext(org.neo4j.graphalgo.BasicEvaluationContext) Transaction(org.neo4j.graphdb.Transaction) Test(org.junit.jupiter.api.Test)

Example 12 with BasicEvaluationContext

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

the class TestAllPaths method testCircularGraph.

@Test
void testCircularGraph() {
    /* Layout
         *
         * (a)---(b)===(c)---(e)
         *         \   /
         *          (d)
         */
    try (Transaction transaction = graphDb.beginTx()) {
        graph.makeEdge(transaction, "a", "b");
        graph.makeEdge(transaction, "b", "c");
        graph.makeEdge(transaction, "b", "c");
        graph.makeEdge(transaction, "b", "d");
        graph.makeEdge(transaction, "c", "d");
        graph.makeEdge(transaction, "c", "e");
        var context = new BasicEvaluationContext(transaction, graphDb);
        PathFinder<Path> finder = instantiatePathFinder(context, 10);
        Iterable<Path> paths = finder.findAllPaths(graph.getNode(transaction, "a"), graph.getNode(transaction, "e"));
        assertPaths(paths, "a,b,c,e", "a,b,c,e", "a,b,d,c,e", "a,b,c,d,b,c,e", "a,b,c,d,b,c,e", "a,b,c,b,d,c,e", "a,b,c,b,d,c,e", "a,b,d,c,b,c,e", "a,b,d,c,b,c,e");
        transaction.commit();
    }
}
Also used : Path(org.neo4j.graphdb.Path) BasicEvaluationContext(org.neo4j.graphalgo.BasicEvaluationContext) Transaction(org.neo4j.graphdb.Transaction) Test(org.junit.jupiter.api.Test)

Example 13 with BasicEvaluationContext

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

the class TestExactDepthPathFinder method shouldHandleDirectionalGraph.

@Test
void shouldHandleDirectionalGraph() {
    // (d) - (e) ------------ (f)     length 4
    try (Transaction transaction = graphDb.beginTx()) {
        graph.makeEdgeChain(transaction, "a,b,c,g");
        graph.makeEdgeChain(transaction, "a,d,e,f,g");
        graph.makeEdgeChain(transaction, "a,h,i,j,k,g");
        Node a = graph.getNode(transaction, "a");
        Node g = graph.getNode(transaction, "g");
        var context = new BasicEvaluationContext(transaction, graphDb);
        assertPaths(new ExactDepthPathFinder(context, forDirection(Direction.OUTGOING), 3, Integer.MAX_VALUE, false).findAllPaths(a, g), "a,b,c,g");
        assertPaths(new ExactDepthPathFinder(context, forDirection(Direction.OUTGOING), 4, Integer.MAX_VALUE, false).findAllPaths(a, g), "a,d,e,f,g");
        assertPaths(new ExactDepthPathFinder(context, forDirection(Direction.OUTGOING), 5, Integer.MAX_VALUE, false).findAllPaths(a, g), "a,h,i,j,k,g");
        transaction.commit();
    }
}
Also used : BasicEvaluationContext(org.neo4j.graphalgo.BasicEvaluationContext) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) ExactDepthPathFinder(org.neo4j.graphalgo.impl.path.ExactDepthPathFinder) Test(org.junit.jupiter.api.Test)

Example 14 with BasicEvaluationContext

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

the class TestExactDepthPathFinder method testExactDepthPathsReturnsNoLoops.

@Test
void testExactDepthPathsReturnsNoLoops() {
    // 
    try (Transaction transaction = graphDb.beginTx()) {
        graph.makeEdgeChain(transaction, "a,b,c,d,b,c,e");
        Node a = graph.getNode(transaction, "a");
        Node e = graph.getNode(transaction, "e");
        var context = new BasicEvaluationContext(transaction, graphDb);
        assertPaths(pathsWithLength(context, PathExpanders.forType(MyRelTypes.R1), 3).findAllPaths(a, e), "a,b,c,e", "a,b,c,e");
        assertPaths(pathsWithLength(context, PathExpanders.forType(MyRelTypes.R1), 4).findAllPaths(a, e), "a,b,d,c,e");
        assertPaths(pathsWithLength(context, PathExpanders.forType(MyRelTypes.R1), 6).findAllPaths(a, e));
        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 15 with BasicEvaluationContext

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

the class TestExactDepthPathFinder method testAll.

@Test
void testAll() {
    try (Transaction transaction = graphDb.beginTx()) {
        createGraph(transaction);
        var context = new BasicEvaluationContext(transaction, graphDb);
        PathFinder<Path> finder = newFinder(context);
        assertPaths(finder.findAllPaths(graph.getNode(transaction, "SOURCE"), graph.getNode(transaction, "TARGET")), "SOURCE,z,9,0,TARGET", "SOURCE,SUPER,r,SPIDER,TARGET");
        transaction.commit();
    }
}
Also used : Path(org.neo4j.graphdb.Path) 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