use of org.neo4j.graphdb.traversal.TraversalBranch in project neo4j by neo4j.
the class TestBidirectionalTraversal method mirroredTraversalReversesInitialState.
@Test
void mirroredTraversalReversesInitialState() {
/*
* (a)-->(b)-->(c)-->(d)
*/
createGraph("a TO b", "b TO c", "c TO d");
try (Transaction transaction = getGraphDb().beginTx()) {
BranchCollisionPolicy collisionPolicy = (evaluator, pathPredicate) -> new StandardBranchCollisionDetector(null, null) {
@Override
protected boolean includePath(Path path, TraversalBranch startPath, TraversalBranch endPath) {
assertEquals(0, startPath.state());
assertEquals(10, endPath.state());
return true;
}
};
Iterables.count(transaction.bidirectionalTraversalDescription().mirroredSides(transaction.traversalDescription().uniqueness(NODE_PATH).expand(PathExpanders.forType(to), new InitialBranchState.State<>(0, 10))).collisionPolicy(collisionPolicy).traverse(getNodeWithName(transaction, "a"), getNodeWithName(transaction, "d")));
}
}
use of org.neo4j.graphdb.traversal.TraversalBranch in project neo4j by neo4j.
the class TestBestFirstSelectorFactory method shouldDoWholeTraversalInCorrectOrder.
@ParameterizedTest
@MethodSource("params")
void shouldDoWholeTraversalInCorrectOrder(PathExpander expander, PathInterest<Integer> interest, Uniqueness uniqueness, String[] expectedResult) {
try (Transaction transaction = graphDb.beginTx()) {
var factory = new BestFirstSelectorFactory<Integer, Integer>(interest) {
private final CostEvaluator<Integer> evaluator = CommonEvaluators.intCostEvaluator(LENGTH);
@Override
protected Integer getStartData() {
return 0;
}
@Override
protected Integer addPriority(TraversalBranch source, Integer currentAggregatedValue, Integer value) {
return value + currentAggregatedValue;
}
@Override
protected Integer calculateValue(TraversalBranch next) {
return next.length() == 0 ? 0 : evaluator.getCost(next.lastRelationship(), Direction.BOTH);
}
};
Node a = transaction.getNodeById(graph.getNode(transaction, "a").getId());
Traverser traverser = new MonoDirectionalTraversalDescription().expand(expander).order(factory).uniqueness(uniqueness).traverse(a);
var iterator = traverser.iterator();
int i = 0;
while (iterator.hasNext()) {
assertPath(transaction, iterator.next(), expectedResult[i]);
i++;
}
assertEquals(expectedResult.length, i, String.format("Not all expected paths where traversed. Missing paths are %s\n", Arrays.toString(Arrays.copyOfRange(expectedResult, i, expectedResult.length))));
transaction.commit();
}
}
use of org.neo4j.graphdb.traversal.TraversalBranch in project neo4j by neo4j.
the class LiteDepthFirstSelector method next.
@Override
public TraversalBranch next(TraversalContext metadata) {
TraversalBranch result = null;
while (result == null) {
if (current == null) {
current = superNodes.poll();
if (current == null) {
return null;
}
} else if (current.expanded() > 0 && current.expanded() % threshold == 0) {
superNodes.add(current);
current = current.parent();
continue;
}
TraversalBranch next = current.next(expander, metadata);
if (next == null) {
current = current.parent();
continue;
}
current = next;
result = current;
}
return result;
}
use of org.neo4j.graphdb.traversal.TraversalBranch in project neo4j by neo4j.
the class BidirectionalTraversalBranchPath method iterator.
@Override
public Iterator<Entity> iterator() {
// TODO Don't loop through them all up front
LinkedList<Entity> entities = new LinkedList<>();
TraversalBranch branch = start;
while (branch.length() > 0) {
entities.addFirst(branch.endNode());
entities.addFirst(branch.lastRelationship());
branch = branch.parent();
}
entities.addFirst(branch.endNode());
if (cachedStartNode == null) {
cachedStartNode = branch.endNode();
}
if (end.length() > 0) {
entities.add(end.lastRelationship());
branch = end.parent();
while (branch.length() > 0) {
entities.add(branch.endNode());
entities.add(branch.lastRelationship());
branch = branch.parent();
}
entities.add(branch.endNode());
}
return entities.iterator();
}
use of org.neo4j.graphdb.traversal.TraversalBranch in project neo4j by neo4j.
the class BidirectionalTraversalBranchPath method gatherRelationships.
private List<Relationship> gatherRelationships(TraversalBranch first, TraversalBranch then) {
// TODO Don't loop through them all up front
LinkedList<Relationship> relationships = new LinkedList<>();
TraversalBranch branch = first;
while (branch.length() > 0) {
relationships.addFirst(branch.lastRelationship());
branch = branch.parent();
}
// We can might as well cache start node since we're right now there anyway
if (cachedStartNode == null && first == start && branch.length() >= 0) {
cachedStartNode = branch.endNode();
}
branch = then;
while (branch.length() > 0) {
relationships.add(branch.lastRelationship());
branch = branch.parent();
}
if (cachedStartNode == null && then == start && branch.length() >= 0) {
cachedStartNode = branch.endNode();
}
return relationships;
}
Aggregations