Created
November 1, 2011 19:05
-
-
Save FaKod/1331556 to your computer and use it in GitHub Desktop.
The Neo4j Matrix Example with Neo4j-Scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* The Matrix Example | |
* http://wiki.neo4j.org/content/The_Matrix | |
*/ | |
case class Matrix(name: String, profession: String) | |
object TheMatrix extends App with Neo4jWrapper with EmbeddedGraphDatabaseServiceProvider { | |
ShutdownHookThread { | |
shutdown(ds) | |
} | |
def neo4jStoreDir = "/tmp/temp-neo-TheMatrix" | |
final val nodes = Map("Neo" -> "Hacker", | |
"Morpheus" -> "Hacker", | |
"Trinity" -> "Hacker", | |
"Cypher" -> "Hacker", | |
"Agent Smith" -> "Program", | |
"The Architect" -> "Whatever") | |
withTx { | |
implicit neo => | |
val nodeMap = for ((name, prof) <- nodes) yield (name, createNode(Matrix(name, prof))) | |
getReferenceNode --> "ROOT" --> nodeMap("Neo") | |
nodeMap("Neo") --> "KNOWS" --> nodeMap("Trinity") | |
nodeMap("Neo") --> "KNOWS" --> nodeMap("Morpheus") --> "KNOWS" --> nodeMap("Trinity") | |
nodeMap("Morpheus") --> "KNOWS" --> nodeMap("Cypher") --> "KNOWS" --> nodeMap("Agent Smith") | |
nodeMap("Agent Smith") --> "CODED_BY" --> nodeMap("The Architect") | |
/** | |
* Find the friends | |
*/ | |
println("\n***** Find the friends") | |
nodeMap("Neo").traverse(Order.BREADTH_FIRST, | |
StopEvaluator.END_OF_GRAPH, | |
ReturnableEvaluator.ALL_BUT_START_NODE, | |
DynamicRelationshipType.withName("KNOWS"), | |
Direction.OUTGOING).foreach { | |
n => | |
n.toCC[Matrix] match { | |
case None => println("not a Matrix Case Class") | |
case Some(Matrix(name, prof)) => println("Name: " + name + " Profession: " + prof) | |
} | |
} | |
/** | |
* Find the hackers | |
*/ | |
println("\n***** Find the hackers") | |
def isReturnableNode(currentPosition: TraversalPosition) = | |
currentPosition.lastRelationshipTraversed match { | |
case null => false | |
case rel => rel.isType(DynamicRelationshipType.withName("CODED_BY")) | |
} | |
val traverser = nodeMap("Neo").traverse(Order.BREADTH_FIRST, | |
StopEvaluator.END_OF_GRAPH, | |
isReturnableNode _, | |
DynamicRelationshipType.withName("CODED_BY"), | |
Direction.OUTGOING, | |
DynamicRelationshipType.withName("KNOWS"), | |
Direction.OUTGOING) | |
traverser.foreach { | |
n => | |
n.toCC[Matrix] match { | |
case None => println("not a Matrix Case Class") | |
case Some(Matrix(name, prof)) => | |
println("At depth " +traverser.currentPosition.depth + " Name: " + name + " Profession: " + prof) | |
} | |
} | |
} | |
} |
Would be nice to see the imports in there. I've included them in my fork of this gist.
I just updated this example with new package name: eu.fakod.neo4jscala
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Prints out:
***** Find the friends
Name: Trinity Profession: Hacker
Name: Morpheus Profession: Hacker
Name: Cypher Profession: Hacker
Name: Agent Smith Profession: Program
***** Find the hackers
At depth 4 Name: The Architect Profession: Whatever