Walking an ordered graph with the Traversal Framework

This example shows how to use a path context holding a represenation of a path.

First, we create a toy graph:

        Node A = db.createNode();
        Node B = db.createNode();
        Node C = db.createNode();
        Node D = db.createNode();
        A.createRelationshipTo( B, REL1 );
        B.createRelationshipTo( C, REL2 );
        C.createRelationshipTo( D, REL3 );
        A.createRelationshipTo( C, REL2 );

Now, the order of relationships (REL1 -> REL2 -> REL3) is stored in an ArrayList. Upon traversal, the Evaluator can check against it to for instance ensure that only paths are included and returned that have the predefined order of relationships:

        final ArrayList<RelationshipType> orderedPathContext = new ArrayList<RelationshipType>();
        orderedPathContext.add( REL1 );
        orderedPathContext.add( withName( "REL2" ) );
        orderedPathContext.add( withName( "REL3" ) );
        TraversalDescription td = Traversal.description().evaluator(
                new Evaluator()
                {

                    public Evaluation evaluate( Path path )
                    {
                        if ( path.length() == 0 )
                        {
                            return Evaluation.EXCLUDE_AND_CONTINUE;
                        }
                        String currentName = path.lastRelationship().getType().name();
                        String relationshipType = orderedPathContext.get(
                                path.length() - 1 ).name();
                        if ( path.length() == orderedPathContext.size() )
                        {
                            if ( currentName.equals( relationshipType ) )
                            {
                                return Evaluation.INCLUDE_AND_PRUNE;
                            }
                            else
                            {
                                return Evaluation.EXCLUDE_AND_PRUNE;
                            }
                        }
                        else
                        {
                            if ( currentName.equals( relationshipType ) )
                            {
                                return Evaluation.EXCLUDE_AND_CONTINUE;
                            }
                            else
                            {
                                return Evaluation.EXCLUDE_AND_PRUNE;
                            }
                        }
                    }
                } );
        Traverser t = td.traverse( db.getNodeById( 1 ) );
        for ( Path path : t )
        {
            System.out.println( path );
        }

Full source code: OrderedPathTest