Search in sources :

Example 1 with BasicEvaluationContext

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

the class TestShortestPath method testCrossedCircle.

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

Example 2 with BasicEvaluationContext

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

the class TestShortestPath method withFilters.

@Test
void withFilters() {
    // 
    try (Transaction transaction = graphDb.beginTx()) {
        graph.makeEdgeChain(transaction, "a,b,c,d");
        graph.makeEdgeChain(transaction, "a,g,h,d");
        final Node a = graph.getNode(transaction, "a");
        final Node d = graph.getNode(transaction, "d");
        final Node b = graph.getNode(transaction, "b");
        b.setProperty("skip", true);
        var context = new BasicEvaluationContext(transaction, graphDb);
        final Predicate<Node> filter = item -> {
            final boolean skip = (Boolean) item.getProperty("skip", false);
            return !skip;
        };
        testShortestPathFinder(context, finder -> assertPaths(finder.findAllPaths(a, d), "a,g,h,d"), ((StandardExpander) allTypesAndDirections()).addNodeFilter(filter), 10);
        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) BasicEvaluationContext(org.neo4j.graphalgo.BasicEvaluationContext) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Test(org.junit.jupiter.api.Test)

Example 3 with BasicEvaluationContext

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

the class TestAStar method testSimplest.

/**
 * <pre>
 *   01234567
 *  +-------->x  A - C: 10
 * 0|A      C    A - B:  2 (x2)
 * 1|  B         B - C:  3
 *  V
 *  y
 * </pre>
 */
@ParameterizedTest
@MethodSource("params")
void testSimplest(Function<EvaluationContext, PathFinder<WeightedPath>> finderFactory) {
    try (Transaction transaction = graphDb.beginTx()) {
        Node nodeA = graph.makeNode(transaction, "A", "x", 0d, "y", 0d);
        Node nodeB = graph.makeNode(transaction, "B", "x", 2d, "y", 1d);
        Node nodeC = graph.makeNode(transaction, "C", "x", 7d, "y", 0d);
        Relationship relAB = graph.makeEdge(transaction, "A", "B", "length", 2d);
        Relationship relAB2 = graph.makeEdge(transaction, "A", "B", "length", 2);
        Relationship relBC = graph.makeEdge(transaction, "B", "C", "length", 3f);
        Relationship relAC = graph.makeEdge(transaction, "A", "C", "length", (short) 10);
        int counter = 0;
        var context = new BasicEvaluationContext(transaction, graphDb);
        Iterable<WeightedPath> allPaths = finderFactory.apply(context).findAllPaths(nodeA, nodeC);
        for (WeightedPath path : allPaths) {
            assertEquals((Double) 5d, (Double) path.weight());
            assertPath(path, nodeA, nodeB, nodeC);
            counter++;
        }
        assertEquals(1, counter);
        transaction.commit();
    }
}
Also used : BasicEvaluationContext(org.neo4j.graphalgo.BasicEvaluationContext) WeightedPath(org.neo4j.graphalgo.WeightedPath) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 4 with BasicEvaluationContext

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

the class TestAStar method pathToSelfReturnsZero.

@ParameterizedTest
@MethodSource("params")
void pathToSelfReturnsZero(Function<EvaluationContext, PathFinder<WeightedPath>> finderFactory) {
    // GIVEN
    try (Transaction transaction = graphDb.beginTx()) {
        Node start = graph.makeNode(transaction, "start", "x", 0d, "y", 0d);
        // WHEN
        var context = new BasicEvaluationContext(transaction, graphDb);
        WeightedPath path = finderFactory.apply(context).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)

Example 5 with BasicEvaluationContext

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

the class TestAStar method wikipediaExample.

@ParameterizedTest
@MethodSource("params")
void wikipediaExample(Function<EvaluationContext, PathFinder<WeightedPath>> finderFactory) {
    /* GIVEN
         *
         * (start)---2--->(d)
         *    \             \
         *    1.5            .\
         *     v               3
         *    (a)-\             v
         *         -2-\         (e)
         *             ->(b)     \
         *               /        \
         *           /--           2
         *      /-3-                v
         *     v        --4------->(end)
         *    (c)------/
         */
    try (Transaction transaction = graphDb.beginTx()) {
        Node start = graph.makeNode(transaction, "start", "x", 0d, "y", 0d);
        graph.makeNode(transaction, "a", "x", 0.3d, "y", 1d);
        graph.makeNode(transaction, "b", "x", 2d, "y", 2d);
        graph.makeNode(transaction, "c", "x", 0d, "y", 3d);
        graph.makeNode(transaction, "d", "x", 2d, "y", 0d);
        graph.makeNode(transaction, "e", "x", 3d, "y", 1.5d);
        Node end = graph.makeNode(transaction, "end", "x", 3.3d, "y", 2.8d);
        graph.makeEdge(transaction, "start", "a", "length", 1.5d);
        graph.makeEdge(transaction, "a", "b", "length", 2f);
        graph.makeEdge(transaction, "b", "c", "length", 3);
        graph.makeEdge(transaction, "c", "end", "length", 4L);
        graph.makeEdge(transaction, "start", "d", "length", (short) 2);
        graph.makeEdge(transaction, "d", "e", "length", (byte) 3);
        graph.makeEdge(transaction, "e", "end", "length", 2);
        // WHEN
        var context = new BasicEvaluationContext(transaction, graphDb);
        WeightedPath path = finderFactory.apply(context).findSinglePath(start, end);
        // THEN
        assertPathDef(path, "start", "d", "e", "end");
        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