Skip to content

Instantly share code, notes, and snippets.

@drexin
Created March 11, 2011 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drexin/865965 to your computer and use it in GitHub Desktop.
Save drexin/865965 to your computer and use it in GitHub Desktop.
A Neo4j REST Server Plugin to request contact paths
import org.neo4j.graphdb.GraphDatabaseService
import org.neo4j.graphdb.Node
import org.neo4j.server.plugins._
import org.neo4j.graphalgo.GraphAlgoFactory
import org.neo4j.kernel.OrderedByTypeExpander
import org.neo4j.graphdb.RelationshipType
import org.neo4j.graphdb.Path
import org.neo4j.helpers.collection.NestingIterable
import org.neo4j.helpers.collection.NestingIterator
import collection.JavaConversions.{asJavaIterable, asScalaIterable}
@Description( "An extension to the Neo4j Server for getting all nodes or relationships" )
class ContactPaths extends ServerPlugin {
@Name("get_shortest_path")
@Description( "Get shortest contact path from one user to another" )
@PluginTarget( classOf[GraphDatabaseService] )
def getShortestPath( @Source graphDb: GraphDatabaseService, @Parameter(name="from")
from: Node, @Parameter(name="to") to: Node): java.lang.Iterable[Node] = {
val algo = GraphAlgoFactory.shortestPath(new OrderedByTypeExpander().add(Relationship.contact), 5)
algo.findSinglePath(from, to) match {
case null => new java.util.ArrayList[Node]()
case x => x.nodes()
}
}
implicit object PathCollectionOrdering extends Ordering[Path] {
def compare(first: Path, second: Path) =
first.length - second.length
}
case class PathCollection(paths: Iterable[Path]) extends NestingIterable[Node, Path](paths) {
def createNestedIterator(path: Path): java.util.Iterator[Node] = path.nodes.iterator
}
implicit def iterable2PathCollection(paths: java.lang.Iterable[Path]) = PathCollection(paths)
implicit def iterable2PathCollection(paths: Iterable[Path]) = PathCollection(paths)
@Name("get_all_paths")
@Description( "Get all contact paths from one user to another" )
@PluginTarget( classOf[GraphDatabaseService] )
def getAllPaths( @Source graphDb: GraphDatabaseService, @Parameter(name="from")
from: Node, @Parameter(name="to") to: Node): NestingIterable[Node, Path] = {
val algo = GraphAlgoFactory.allPaths(new OrderedByTypeExpander().add(Relationship.contact), 5)
algo.findAllPaths(from, to) match {
case null => List[Path]()
case x => x
}
}
}
import org.neo4j.graphdb.RelationshipType;
enum Relationship implements RelationshipType {
contact
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment