    1   /**
    2    * Licensed to Neo Technology under one or more contributor
    3    * license agreements. See the NOTICE file distributed with
    4    * this work for additional information regarding copyright
    5    * ownership. Neo Technology licenses this file to you under
    6    * the Apache License, Version 2.0 (the "License"); you may
    7    * not use this file except in compliance with the License.
    8    * You may obtain a copy of the License at
    9    *
    10   * http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   * Unless required by applicable law or agreed to in writing,
    13   * software distributed under the License is distributed on an
    14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    15   * KIND, either express or implied. See the License for the
    16   * specific language governing permissions and limitations
    17   * under the License.
    18   */
    19  package org.neo4j.examples.server.plugins;
    20
    21  import org.neo4j.graphdb.Path;
    22  import org.neo4j.graphdb.Relationship;
    23  import org.neo4j.graphdb.Node;
    24  import org.neo4j.graphdb.traversal.PruneEvaluator;
    25  import org.neo4j.graphdb.traversal.TraversalDescription;
    26  import org.neo4j.helpers.Predicate;
    27  import org.neo4j.kernel.Traversal;
    28  import org.neo4j.kernel.Uniqueness;
    29  import org.neo4j.server.plugins.Description;
    30  import org.neo4j.server.plugins.PluginTarget;
    31  import org.neo4j.server.plugins.ServerPlugin;
    32  import org.neo4j.server.plugins.Source;
    33
    34
    35
    36  /**
    37  * An extension performaing a predefined graph traversal
    38  */
    39  @Description( "Performs a depth two traversal along all relationship types." )
    40  public class
    41          DepthTwo extends ServerPlugin
    42  {
    43      @Description( "Traverse depth two and return the end nodes" )
    44      @PluginTarget( Node.class )
    45      public Iterable<Node> nodesOnDepthTwo( @Source Node node )
    46      {
    47          return traversal.traverse( node ).nodes();
    48      }
    49
    50      @Description( "Traverse depth two and return the last relationships" )
    51      @PluginTarget( Node.class )
    52      public Iterable<Relationship> relationshipsOnDepthTwo( @Source Node node )
    53      {
    54          return traversal.traverse( node ).relationships();
    55      }
    56
    57      @Description( "Traverse depth two and return the paths" )
    58      @PluginTarget( Node.class )
    59      public Iterable<Path> pathsOnDepthTwo( @Source Node node )
    60      {
    61          return traversal.traverse( node );
    62      }
    63
    64      private static final TraversalDescription traversal = Traversal.description().uniqueness(
    65              Uniqueness.RELATIONSHIP_PATH ).prune( new PruneEvaluator()
    66      {
    67          public boolean pruneAfter( Path position )
    68          {
    69              return position.length() >= 2;
    70          }
    71      } ).filter( new Predicate<Path>()
    72      {
    73          public boolean accept( Path item )
    74          {
    75              return item.length() == 2;
    76          }
    77      } );
    78  }