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());
}
}
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;
}
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");
}
}
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();
}
}
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);
}
}
Aggregations