Prev |  | Next

## 4.7. Uniqueness of Paths in traversals

This example is demonstrating the use of node uniqueness. Below an imaginary domain graph with Principals that own pets that are descendant to other pets.

Figure 4.6. Descendants Example Graph

In order to return all descendants of `Pet0` which have the relation `owns` to `Principal1` (`Pet1` and `Pet3`), the Uniqueness of the traversal needs to be set to `NODE_PATH` rather than the default `NODE_GLOBAL` so that nodes can be traversed more that once, and paths that have different nodes but can have some nodes in common (like the start and end node) can be returned.

    final Node target = data.get().get( "Principal1" );
    TraversalDescription td = Traversal.description()
            .uniqueness( Uniqueness.NODE_PATH )
            .evaluator( new Evaluator()
    {
        @Override
        public Evaluation evaluate( Path path )
        {
            if ( path.endNode().equals( target ) )
            {
                return Evaluation.INCLUDE_AND_PRUNE;
            }
            return Evaluation.EXCLUDE_AND_CONTINUE;
        }
    } );

    Traverser results = td.traverse( start );

This will return the following paths:

    (3)--[descendant,0]-->(1)<--[owns,3]--(5)
    (3)--[descendant,2]-->(4)<--[owns,5]--(5)

Let’s create a new `TraversalDescription` from the old one, having `NODE_GLOBAL` uniqueness to see the difference.

| Tip
The `TraversalDescription` object is immutable, so we have to use the new instance returned with the new uniqueness setting.

    TraversalDescription nodeGlobalTd = td.uniqueness( Uniqueness.NODE_GLOBAL );
    results = nodeGlobalTd.traverse( start );

Now only one path is returned:

    (3)--[descendant,0]-->(1)<--[owns,3]--(5)

Full source code: UniquenessOfPathsTest.java

Copyright © 2011 Neo Technology

Prev | Up | Next
| Home |