Skip to content

Instantly share code, notes, and snippets.

@rahilb
Last active April 5, 2016 21:55
Show Gist options
  • Save rahilb/24bcb9ea77b47eaeca56269337f89d27 to your computer and use it in GitHub Desktop.
Save rahilb/24bcb9ea77b47eaeca56269337f89d27 to your computer and use it in GitHub Desktop.
import eu.fakod.neo4jscala.{TypedTraverser, SingletonEmbeddedGraphDatabaseServiceProvider, Neo4jWrapper}
import sys.ShutdownHookThread
trait ProgramBase {
val name: String
}
case class Series(name: String) extends ProgramBase
case class Episode(name: String) extends ProgramBase
case class Showing(name: String) extends ProgramBase
/**
* The Matrix Example
* http://wiki.neo4j.org/content/The_Matrix
*/
object TheMatrix extends App with Neo4jWrapper with SingletonEmbeddedGraphDatabaseServiceProvider with TypedTraverser {
ShutdownHookThread {
shutdown(ds)
}
def neo4jStoreDir = "/tmp/temp-neo-TheMatrix"
/**
* defining nodes
*/
final val nodes: List[ProgramBase] = List(
Episode("GoTs01e01"),
Episode("GoTs01e02"),
Episode("SopranosS01e01"),
Showing("GoTs01e01nowtv"),
Series("GoT"),
Series("Sopranos")
)
val nodeMap = withTx {
implicit neo =>
val nodeMap = (for (node <- nodes) yield (node.name, createNode(node))).toMap
nodeMap("GoTs01e01") --> "EPISODE_OF" --> nodeMap("GoT")
nodeMap("GoTs01e02") --> "EPISODE_OF" --> nodeMap("GoT")
nodeMap("SopranosS01e01") --> "EPISODE_OF" --> nodeMap("Sopranos")
nodeMap("GoTs01e01nowtv") --> "SHOWING_OF" --> nodeMap("GoTs01e01")
nodeMap
}
val startNode = nodeMap("GoT")
def query(rb: TheMatrix.RelationBuffer)(f: PartialFunction[ProgramBase, Boolean]) = withTx { implicit neo =>
startNode.doTraverse[ProgramBase](rb) {
case _ => false
}{
case (v, _) if f.isDefinedAt(v) => f(v)
case _ => false
}.toList.sortWith(_.name < _.name)
}
println("Episodes of Game of Thrones " + query(follow -<- "EPISODE_OF"){case _: Episode => true})
println("All airings " + query(follow -- "EPISODE_OF" -- "SHOWING_OF"){case s: Showing => true })
val m = withTx{ implicit n =>
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment