Search in sources :

Example 36 with Path

use of org.neo4j.graphdb.Path in project neo4j by neo4j.

the class DepthPitfallGraphTest method testSmallestPossibleInit.

@Test
void testSmallestPossibleInit() {
    int count = 0;
    try (Transaction transaction = beginTx()) {
        Traverser traversal = transaction.traversalDescription().traverse(transaction.getNodeById(node("1").getId()));
        for (Path position : traversal) {
            count++;
            assertNotNull(position);
            assertNotNull(position.endNode());
            if (position.length() > 0) {
                assertNotNull(position.lastRelationship());
            }
        }
        assertNotEquals(0, count, "empty traversal");
        transaction.commit();
    }
}
Also used : Path(org.neo4j.graphdb.Path) Transaction(org.neo4j.graphdb.Transaction) Traverser(org.neo4j.graphdb.traversal.Traverser) Test(org.junit.jupiter.api.Test)

Example 37 with Path

use of org.neo4j.graphdb.Path in project neo4j by neo4j.

the class TestShortestPath method testAnotherSimpleGraph.

@Test
void testAnotherSimpleGraph() {
    // (n)---(p)---(q)
    try (Transaction transaction = graphDb.beginTx()) {
        graph.makeEdge(transaction, "s", "m");
        graph.makeEdge(transaction, "m", "o");
        graph.makeEdge(transaction, "s", "n");
        graph.makeEdge(transaction, "n", "p");
        graph.makeEdge(transaction, "p", "q");
        graph.makeEdge(transaction, "q", "t");
        graph.makeEdge(transaction, "n", "o");
        graph.makeEdge(transaction, "o", "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,m,o,t", "s,n,o,t");
        }, forTypeAndDirection(R1, BOTH), 6);
        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 38 with Path

use of org.neo4j.graphdb.Path 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 39 with Path

use of org.neo4j.graphdb.Path 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 40 with Path

use of org.neo4j.graphdb.Path in project neo4j by neo4j.

the class Neo4jJsonCodecTest method testPathWriting.

@Test
void testPathWriting() throws IOException {
    // Given
    Path path = mock(Path.class);
    Entity entity = mock(Entity.class);
    when(entity.getAllProperties()).thenThrow(RuntimeException.class);
    when(path.iterator()).thenReturn(Arrays.asList(entity).listIterator());
    // When
    assertThrows(Exception.class, () -> jsonCodec.writeValue(jsonGenerator, path));
    // Then
    verify(jsonGenerator).writeEndArray();
}
Also used : Path(org.neo4j.graphdb.Path) Entity(org.neo4j.graphdb.Entity) Test(org.junit.jupiter.api.Test)

Aggregations

Path (org.neo4j.graphdb.Path)170 Node (org.neo4j.graphdb.Node)73 Transaction (org.neo4j.graphdb.Transaction)51 Test (org.junit.jupiter.api.Test)49 Relationship (org.neo4j.graphdb.Relationship)47 Test (org.junit.Test)37 WeightedPath (org.neo4j.graphalgo.WeightedPath)25 Traverser (org.neo4j.graphdb.traversal.Traverser)24 BasicEvaluationContext (org.neo4j.graphalgo.BasicEvaluationContext)21 ArrayList (java.util.ArrayList)19 Map (java.util.Map)18 TraversalDescription (org.neo4j.graphdb.traversal.TraversalDescription)18 HashSet (java.util.HashSet)12 RelationshipType (org.neo4j.graphdb.RelationshipType)10 Entity (org.neo4j.graphdb.Entity)9 Evaluator (org.neo4j.graphdb.traversal.Evaluator)9 HashMap (java.util.HashMap)7 LinkedList (java.util.LinkedList)6 PathFinder (org.neo4j.graphalgo.PathFinder)6 PathExpander (org.neo4j.graphdb.PathExpander)6