Search in sources :

Example 26 with Path

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

the class TestPath method testBidirectionalPath.

@Test
public void testBidirectionalPath() {
    var graphDb = getGraphDb();
    BidirectionalTraversalDescription bidirectional;
    try (var transaction = graphDb.beginTx()) {
        Node a = transaction.getNodeById(this.a.getId());
        Node e = transaction.getNodeById(this.e.getId());
        TraversalDescription side = transaction.traversalDescription().uniqueness(Uniqueness.NODE_PATH);
        bidirectional = transaction.bidirectionalTraversalDescription().mirroredSides(side);
        Path bidirectionalPath = getFirstPath(bidirectional.traverse(a, e));
        assertPathIsCorrect(transaction, bidirectionalPath);
        Path path = getFirstPath(bidirectional.traverse(a, e));
        Node node = path.startNode();
        assertEquals(a, node);
        // White box testing below: relationships(), nodes(), reverseRelationships(), reverseNodes()
        // does cache the start node if not already cached, so just make sure they to it properly.
        bidirectionalPath = getFirstPath(bidirectional.traverse(a, e));
        bidirectionalPath.relationships();
        assertEquals(a, bidirectionalPath.startNode());
        bidirectionalPath = getFirstPath(bidirectional.traverse(a, e));
        bidirectionalPath.nodes();
        assertEquals(a, bidirectionalPath.startNode());
        bidirectionalPath = getFirstPath(bidirectional.traverse(a, e));
        bidirectionalPath.reverseRelationships();
        assertEquals(a, bidirectionalPath.startNode());
        bidirectionalPath = getFirstPath(bidirectional.traverse(a, e));
        bidirectionalPath.reverseNodes();
        assertEquals(a, bidirectionalPath.startNode());
        bidirectionalPath = getFirstPath(bidirectional.traverse(a, e));
        bidirectionalPath.iterator();
        assertEquals(a, bidirectionalPath.startNode());
    }
}
Also used : Path(org.neo4j.graphdb.Path) Node(org.neo4j.graphdb.Node) BidirectionalTraversalDescription(org.neo4j.graphdb.traversal.BidirectionalTraversalDescription) TraversalDescription(org.neo4j.graphdb.traversal.TraversalDescription) BidirectionalTraversalDescription(org.neo4j.graphdb.traversal.BidirectionalTraversalDescription) Test(org.junit.jupiter.api.Test)

Example 27 with Path

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

the class TestUniqueness method splitPathsOnePerLevel.

private static Path[] splitPathsOnePerLevel(Traverser traverser) {
    Path[] paths = new Path[10];
    for (Path path : traverser) {
        int depth = path.length();
        if (paths[depth] != null) {
            fail("More than one path one depth " + depth);
        }
        paths[depth] = path;
    }
    return paths;
}
Also used : Path(org.neo4j.graphdb.Path)

Example 28 with Path

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

the class TestUniqueness method relationshipLevelAndGlobalUniqueness.

@Test
void relationshipLevelAndGlobalUniqueness() {
    /*
         *    (a)=TO=>(b)=TO=>(c)-TO->(d)
         *       \====TO====>/
         */
    createGraph("a TO b", "b TO c", "a TO b", "b TO c", "a TO c", "a TO c", "c TO d");
    RelationshipType to = withName("TO");
    try (Transaction tx = beginTx()) {
        Node a = getNodeWithName(tx, "a");
        Node d = getNodeWithName(tx, "d");
        Iterator<Path> paths = tx.traversalDescription().relationships(to, OUTGOING).uniqueness(Uniqueness.NONE).evaluator(includeWhereEndNodeIs(d)).traverse(a).iterator();
        int count = 0;
        while (paths.hasNext()) {
            count++;
            paths.next();
        }
        assertEquals(6, count, "wrong number of paths calculated, the test assumption is wrong");
        // Now do the same traversal but with unique per level relationships
        paths = tx.traversalDescription().relationships(to, OUTGOING).uniqueness(RELATIONSHIP_LEVEL).evaluator(includeWhereEndNodeIs(d)).traverse(a).iterator();
        count = 0;
        while (paths.hasNext()) {
            count++;
            paths.next();
        }
        assertEquals(2, count, "wrong number of paths calculated with relationship level uniqueness");
        /*
            *  And yet again, but this time with global uniqueness, it should present only one path, since
            *  c TO d is contained on all paths.
            */
        paths = tx.traversalDescription().relationships(to, OUTGOING).uniqueness(RELATIONSHIP_GLOBAL).evaluator(includeWhereEndNodeIs(d)).traverse(a).iterator();
        count = 0;
        while (paths.hasNext()) {
            count++;
            paths.next();
        }
        assertEquals(1, count, "wrong number of paths calculated with relationship global uniqueness");
    }
}
Also used : Path(org.neo4j.graphdb.Path) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) RelationshipType(org.neo4j.graphdb.RelationshipType) Test(org.junit.jupiter.api.Test)

Example 29 with Path

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

the class TestUniqueness method nodeLevelUniqueness.

@Test
void nodeLevelUniqueness() {
    /*
         *         (b)
         *       /  |  \
         *    (e)==(a)--(c)
         *       \  |
         *         (d)
         */
    createGraph("a TO b", "a TO c", "a TO d", "a TO e", "a TO e", "b TO e", "d TO e", "c TO b");
    RelationshipType to = withName("TO");
    try (Transaction tx = beginTx()) {
        Node a = getNodeWithName(tx, "a");
        Node e = getNodeWithName(tx, "e");
        Path[] paths = splitPathsOnePerLevel(tx.traversalDescription().relationships(to, OUTGOING).uniqueness(NODE_LEVEL).evaluator(includeWhereEndNodeIs(e)).traverse(a));
        NodePathRepresentation pathRepresentation = new NodePathRepresentation(NAME_PROPERTY_REPRESENTATION);
        assertEquals("a,e", pathRepresentation.represent(paths[1]));
        String levelTwoPathRepresentation = pathRepresentation.represent(paths[2]);
        assertTrue(levelTwoPathRepresentation.equals("a,b,e") || levelTwoPathRepresentation.equals("a,d,e"));
        assertEquals("a,c,b,e", pathRepresentation.represent(paths[3]));
        tx.commit();
    }
}
Also used : Path(org.neo4j.graphdb.Path) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) RelationshipType(org.neo4j.graphdb.RelationshipType) Test(org.junit.jupiter.api.Test)

Example 30 with Path

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

the class TreeGraphTest method pathsIteratorReturnAllNodes.

@Test
void pathsIteratorReturnAllNodes() {
    try (Transaction transaction = beginTx()) {
        Traverser traverser = transaction.traversalDescription().traverse(transaction.getNodeById(node("1").getId()));
        int count = 0;
        for (Path path : traverser) {
            assertNotNull(path, "returned paths should not be null. path #" + count);
            count++;
        }
        assertEquals(13, count);
    }
}
Also used : Path(org.neo4j.graphdb.Path) Transaction(org.neo4j.graphdb.Transaction) Traverser(org.neo4j.graphdb.traversal.Traverser) 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