use of org.neo4j.graphdb.traversal.Evaluator in project neo4j by neo4j.
the class DijkstraBidirectional method traverser.
private Traverser traverser(Node start, final Node end, PathInterest interest) {
final MutableDouble shortestSoFar = new MutableDouble(Double.MAX_VALUE);
final MutableDouble startSideShortest = new MutableDouble(0);
final MutableDouble endSideShortest = new MutableDouble(0);
PathExpander dijkstraExpander = new DijkstraBidirectionalPathExpander(expander, shortestSoFar, true, startSideShortest, endSideShortest, epsilon);
GraphDatabaseService db = start.getGraphDatabase();
TraversalDescription side = db.traversalDescription().expand(dijkstraExpander, stateFactory).order(new DijkstraSelectorFactory(interest, costEvaluator)).evaluator(new DijkstraBidirectionalEvaluator(costEvaluator)).uniqueness(Uniqueness.NODE_PATH);
TraversalDescription startSide = side;
TraversalDescription endSide = side.reverse();
BidirectionalTraversalDescription traversal = db.bidirectionalTraversalDescription().startSide(startSide).endSide(endSide).collisionEvaluator(Evaluators.all()).collisionPolicy(new BranchCollisionPolicy() {
@Override
public BranchCollisionDetector create(Evaluator evaluator, Predicate<Path> pathPredicate) {
return new DijkstraBranchCollisionDetector(evaluator, costEvaluator, shortestSoFar, epsilon, pathPredicate);
}
});
lastTraverser = traversal.traverse(start, end);
return lastTraverser;
}
use of org.neo4j.graphdb.traversal.Evaluator in project neo4j by neo4j.
the class TestTraversalWithLoops method traverseThroughNodeWithLoop.
@Test
public void traverseThroughNodeWithLoop() throws Exception {
/*
* (a)-->(b)-->(c)-->(d)-->(e)
* / \ / \
* \__/ \__/
*/
createGraph("a TO b", "b TO c", "c TO c", "c TO d", "d TO d", "d TO e");
try (Transaction tx = beginTx()) {
Node a = getNodeWithName("a");
final Node e = getNodeWithName("e");
Evaluator onlyEndNode = new Evaluator() {
@Override
public Evaluation evaluate(Path path) {
return Evaluation.ofIncludes(path.endNode().equals(e));
}
};
TraversalDescription basicTraverser = getGraphDb().traversalDescription().evaluator(onlyEndNode);
expectPaths(basicTraverser.traverse(a), "a,b,c,d,e");
expectPaths(basicTraverser.uniqueness(Uniqueness.RELATIONSHIP_PATH).traverse(a), "a,b,c,d,e", "a,b,c,c,d,e", "a,b,c,d,d,e", "a,b,c,c,d,d,e");
tx.success();
}
}
use of org.neo4j.graphdb.traversal.Evaluator in project neo4j by neo4j.
the class TestBidirectionalTraversal method mirroredTraversalReversesInitialState.
@Test
public void mirroredTraversalReversesInitialState() throws Exception {
/*
* (a)-->(b)-->(c)-->(d)
*/
createGraph("a TO b", "b TO c", "c TO d");
BranchCollisionPolicy collisionPolicy = new BranchCollisionPolicy() {
@Override
public BranchCollisionDetector create(Evaluator evaluator, Predicate<Path> pathPredicate) {
return 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(getGraphDb().bidirectionalTraversalDescription().mirroredSides(getGraphDb().traversalDescription().uniqueness(NODE_PATH).expand(PathExpanders.<Integer>forType(to), new InitialBranchState.State<>(0, 10))).collisionPolicy(collisionPolicy).traverse(getNodeWithName("a"), getNodeWithName("d")));
}
use of org.neo4j.graphdb.traversal.Evaluator in project neo4j by neo4j.
the class TestMultipleFilters method testNarrowingFilters.
@Test
public void testNarrowingFilters() {
Evaluator mustBeConnectedToK = new MustBeConnectedToNodeFilter(getNodeWithName("k"));
Evaluator mustNotHaveMoreThanTwoOutRels = path -> Evaluation.ofIncludes(Iterables.count(path.endNode().getRelationships(Direction.OUTGOING)) <= 2);
TraversalDescription description = getGraphDb().traversalDescription().evaluator(mustBeConnectedToK);
expectNodes(description.traverse(node("a")), "b", "c");
expectNodes(description.evaluator(mustNotHaveMoreThanTwoOutRels).traverse(node("a")), "c");
}
use of org.neo4j.graphdb.traversal.Evaluator in project graphdb by neo4j-attic.
the class TestMultiPruneEvaluators method testMaxDepthAndCustomPruneEvaluatorCombined.
@Test
public void testMaxDepthAndCustomPruneEvaluatorCombined() {
Evaluator lessThanThreeRels = new Evaluator() {
public Evaluation evaluate(Path path) {
return IteratorUtil.count(path.endNode().getRelationships(Direction.OUTGOING).iterator()) < 3 ? Evaluation.INCLUDE_AND_PRUNE : Evaluation.INCLUDE_AND_CONTINUE;
}
};
TraversalDescription description = Traversal.description().evaluator(Evaluators.all()).evaluator(Evaluators.toDepth(1)).evaluator(lessThanThreeRels);
Set<String> expectedNodes = new HashSet<String>(Arrays.asList("a", "b", "c", "d", "e"));
for (Path position : description.traverse(referenceNode())) {
String name = (String) position.endNode().getProperty("name");
assertTrue(name + " shouldn't have been returned", expectedNodes.remove(name));
}
assertTrue(expectedNodes.isEmpty());
}
Aggregations