nmkkannan
nmkkannan

Reputation: 1303

Neo4j Traversal Description - Custom Evaluator

I am using Neo4j Traversal Framework(Java). I need to create custom evalutor to include nodes where some condition is true

My Code is :

    @Override
        public Evaluation evaluate(Path path) {
            log.info("Node Id: " + path.endNode().getProperty("DIST_ID"));
            long mCount = 0;
            if ((Long) path.endNode().getProperty("RANK") >= 3) {
                mCount++;
            }
            log.info("mCount " + mCount);
            return Evaluation.INCLUDE_AND_CONTINUE;
        }
    };

    TraversalDescription traversalDescription = db.traversalDescription();
    Traverser traverser = traversalDescription.breadthFirst()
            .relationships(RelationshipTypes.SPONSOR, Direction.OUTGOING).evaluator(e).traverse(DSTS);

DSTS is incoming nodes. it means top node.I want to split downline nodes using rank.If example i need two levels so i want split two levels by using rank.rank is one of the property of node.if rank is 5 i want to collect this nodes and it outgoing nodes until will get rank 5.

If any possibilities please guide me...

Upvotes: 1

Views: 320

Answers (1)

Mattias Finné
Mattias Finné

Reputation: 3054

You could look at branch state, i.e. use the evaluate method which also takes a BranchState argument. You can use that to keep state for each visited traversal branch and augment when moving down traversal branches. E.g.

new PathEvaluator<Long> {        
    public Evaluation evaluate(Path path, BranchState<Long> state) {
        log.info("Node Id: " + path.endNode().getProperty("DIST_ID"));
        long mCount = state.getState();
        if ((Long) path.endNode().getProperty("RANK") >= 3) {
            mCount++;
            state.setState( mCount );
        }
        if ( mCount >= 5 ) {
            // do something
        }
        log.info("mCount " + mCount);
        return Evaluation.INCLUDE_AND_CONTINUE;
    }
}

This is equivalent of summing up the RANKs from the whole path on every evaluate, but the BranchState makes this more performant.

Is this something you were thinking of?

Upvotes: 1

Related Questions