    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.graphalgo.GraphAlgoFactory;
    22  import org.neo4j.graphalgo.PathFinder;
    23  import org.neo4j.graphdb.DynamicRelationshipType;
    24  import org.neo4j.graphdb.Expander;
    25  import org.neo4j.graphdb.Node;
    26  import org.neo4j.graphdb.Path;
    27  import org.neo4j.kernel.Traversal;
    28  import org.neo4j.server.plugins.Description;
    29  import org.neo4j.server.plugins.Parameter;
    30  import org.neo4j.server.plugins.PluginTarget;
    31  import org.neo4j.server.plugins.ServerPlugin;
    32  import org.neo4j.server.plugins.Source;
    33
    34  public class ShortestPath extends ServerPlugin
    35  {
    36      @Description( "Find the shortest path between two nodes." )
    37      @PluginTarget( Node.class )
    38      public Iterable<Path> shortestPath(
    39              @Source Node source,
    40              @Description( "The node to find the shortest path to." ) @Parameter( name = "target" ) Node target,
    41              @Description( "The relationship types to follow when searching for the shortest path(s). Order is insignificant, if omitted all types are followed." ) @Parameter( name = "types", optional = true ) String[] types,
    42              @Description( "The maximum path length to search for, default value (if omitted) is 4." ) @Parameter( name = "depth", optional = true ) Integer depth )
    43      {
    44          Expander expander;
    45          if ( types == null )
    46          {
    47              expander = Traversal.expanderForAllTypes();
    48          }
    49          else
    50          {
    51              expander = Traversal.emptyExpander();
    52              for ( int i = 0; i < types.length; i++ )
    53              {
    54                  expander = expander.add( DynamicRelationshipType.withName( types[i] ) );
    55              }
    56          }
    57          PathFinder<Path> shortestPath = GraphAlgoFactory.shortestPath( expander, depth == null ? 4
    58                  : depth.intValue() );
    59          return shortestPath.findAllPaths( source, target );
    60      }
    61  }