Skip to content

Commit

Permalink
Refactored the new traverser matrix example to be normal Java code.
Browse files Browse the repository at this point in the history
  • Loading branch information
nawroth authored and Peter Neubauer committed Mar 8, 2012
1 parent 9738191 commit fc0998c
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 57 deletions.
22 changes: 17 additions & 5 deletions embedded-examples/src/docs/dev/traversal.txt
Expand Up @@ -76,13 +76,14 @@ The traversals from the Matrix example above, this time using the new traversal

[TIP]
The source code of the examples is found here:
https://github.com/neo4j/community/blob/{neo4j-git-tag}/embedded-examples/src/test/java/org/neo4j/examples/MatrixNewTravTest.java[MatrixNewTravTest.java]
https://github.com/neo4j/community/blob/{neo4j-git-tag}/embedded-examples/src/main/java/org/neo4j/examples/NewMatrix.java[NewMatrix.java]

.Friends and friends of friends
[snippet,java]
----
component=neo4j-examples
source=org/neo4j/examples/MatrixNewTravTest.java
classifier=sources
source=org/neo4j/examples/NewMatrix.java
tag=get-friends
----

Expand All @@ -91,15 +92,21 @@ Let's perform the actual traversal and print the results:
[snippet,java]
----
component=neo4j-examples
source=org/neo4j/examples/MatrixNewTravTest.java
classifier=sources
source=org/neo4j/examples/NewMatrix.java
tag=friends-usage
----

Which will give us the following output:

include::matrix-traversal-java-new-friends.txt[]

.Who coded the Matrix?
[snippet,java]
----
component=neo4j-examples
source=org/neo4j/examples/MatrixNewTravTest.java
classifier=sources
source=org/neo4j/examples/NewMatrix.java
tag=find-hackers
----

Expand All @@ -108,10 +115,15 @@ Print out the result:
[snippet,java]
----
component=neo4j-examples
source=org/neo4j/examples/MatrixNewTravTest.java
classifier=sources
source=org/neo4j/examples/NewMatrix.java
tag=find--hackers-usage
----

Now we know who coded the Matrix:

include::matrix-traversal-java-new-hackers.txt[]


=== Walking an ordered path ===

Expand Down
Expand Up @@ -18,13 +18,8 @@
*/
package org.neo4j.examples;

import static org.junit.Assert.assertEquals;

import java.io.File;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
Expand All @@ -38,7 +33,7 @@
import org.neo4j.kernel.EmbeddedGraphDatabase;
import org.neo4j.kernel.Traversal;

public class MatrixNewTravTest
public class NewMatrix
{
public enum RelTypes implements RelationshipType
{
Expand All @@ -47,25 +42,32 @@ public enum RelTypes implements RelationshipType
CODED_BY
}

private static final String MATRIX_DB = "target/matrix-db";
private static GraphDatabaseService graphDb;
private static final String MATRIX_DB = "target/matrix-new-db";
private GraphDatabaseService graphDb;

public static void main( String[] args )
{
NewMatrix matrix = new NewMatrix();
matrix.setUp();
System.out.println( matrix.printNeoFriends() );
System.out.println( matrix.printMatrixHackers() );
matrix.shutdown();
}

@BeforeClass
public static void setUp()
public void setUp()
{
deleteFileOrDirectory( new File( MATRIX_DB ) );
graphDb = new EmbeddedGraphDatabase( MATRIX_DB );
registerShutdownHook();
createNodespace();
}

@AfterClass
public static void tearDown()
public void shutdown()
{
graphDb.shutdown();
}

private static void createNodespace()
public void createNodespace()
{
Transaction tx = graphDb.beginTx();
try
Expand Down Expand Up @@ -117,38 +119,38 @@ private static void createNodespace()

/**
* Get the Neo node. (a.k.a. Thomas Anderson node)
*
*
* @return the Neo node
*/
private static Node getNeoNode()
private Node getNeoNode()
{
return graphDb.getReferenceNode().getSingleRelationship(
RelTypes.NEO_NODE, Direction.OUTGOING ).getEndNode();
return graphDb.getReferenceNode()
.getSingleRelationship( RelTypes.NEO_NODE, Direction.OUTGOING )
.getEndNode();
}

@Test
public void printNeoFriends() throws Exception
public String printNeoFriends()
{
Node neoNode = getNeoNode();
System.out.println( neoNode.getProperty( "name" ) + "'s friends:" );
int numberOfFriends = 0;
// START SNIPPET: friends-usage
int numberOfFriends = 0;
String output = neoNode.getProperty( "name" ) + "'s friends:\n";
Traverser friendsTraverser = getFriends( neoNode );
for ( Path friendPath : friendsTraverser )
{
System.out.println( "At depth " + friendPath.length() + " => "
+ friendPath.endNode()
.getProperty( "name" ) );
// END SNIPPET: friends-usage
output += "At depth " + friendPath.length() + " => "
+ friendPath.endNode()
.getProperty( "name" ) + "\n";
numberOfFriends++;
// START SNIPPET: friends-usage
}
output += "Number of friends found: " + numberOfFriends + "\n";
// END SNIPPET: friends-usage
assertEquals( 4, numberOfFriends );
return output;
}

// START SNIPPET: get-friends
private static Traverser getFriends( final Node person )
private static Traverser getFriends(
final Node person )
{
TraversalDescription td = Traversal.description()
.breadthFirst()
Expand All @@ -158,24 +160,22 @@ private static Traverser getFriends( final Node person )
}
// END SNIPPET: get-friends

@Test
public void printMatrixHackers() throws Exception
public String printMatrixHackers()
{
System.out.println( "Hackers:" );
int numberOfHackers = 0;
// START SNIPPET: find--hackers-usage
String output = "Hackers:\n";
int numberOfHackers = 0;
Traverser traverser = findHackers( getNeoNode() );
for ( Path hackerPath : traverser )
{
System.out.println( "At depth " + hackerPath.length() + " => "
+ hackerPath.endNode()
.getProperty( "name" ) );
// END SNIPPET: find--hackers-usage
output += "At depth " + hackerPath.length() + " => "
+ hackerPath.endNode()
.getProperty( "name" ) + "\n";
numberOfHackers++;
// START SNIPPET: find--hackers-usage
}
output += "Number of hackers found: " + numberOfHackers + "\n";
// END SNIPPET: find--hackers-usage
assertEquals( 1, numberOfHackers );
return output;
}

// START SNIPPET: find-hackers
Expand All @@ -191,19 +191,20 @@ private static Traverser findHackers( final Node startNode )
}
// END SNIPPET: find-hackers

private static void registerShutdownHook()
private void registerShutdownHook()
{
// Registers a shutdown hook for the Neo4j instance so that it
// shuts down nicely when the VM exits (even if you "Ctrl-C" the
// running example before it's completed)
Runtime.getRuntime().addShutdownHook( new Thread()
{
@Override
public void run()
{
graphDb.shutdown();
}
} );
Runtime.getRuntime()
.addShutdownHook( new Thread()
{
@Override
public void run()
{
graphDb.shutdown();
}
} );
}

private static void deleteFileOrDirectory( final File file )
Expand All @@ -219,7 +220,8 @@ private static void deleteFileOrDirectory( final File file )
{
deleteFileOrDirectory( child );
}
} else
}
else
{
file.delete();
}
Expand Down
25 changes: 21 additions & 4 deletions embedded-examples/src/test/java/org/neo4j/examples/MatrixTest.java
Expand Up @@ -27,31 +27,48 @@

public class MatrixTest
{
private static Matrix matrix;
private static JavaDocsGenerator gen;

@BeforeClass
public static void setUpBeforeClass() throws Exception
{
matrix = new Matrix();
gen = new JavaDocsGenerator( "matrix-traversal-java", "dev" );
}

@Test
public void matrix() throws Exception
{
Matrix matrix = new Matrix();
matrix.setUp();
String friends = matrix.printNeoFriends();
String hackers = matrix.printMatrixHackers();
matrix.shutdown();
check( friends, hackers );
gen.saveToFile( "friends", createOutputSnippet( friends ) );
gen.saveToFile( "hackers", createOutputSnippet( hackers ) );
}

@Test
public void newMatrix() throws Exception
{
NewMatrix newMatrix = new NewMatrix();
newMatrix.setUp();
String friends = newMatrix.printNeoFriends();
String hackers = newMatrix.printMatrixHackers();
newMatrix.shutdown();
check( friends, hackers );
gen.saveToFile( "new-friends", createOutputSnippet( friends ) );
gen.saveToFile( "new-hackers", createOutputSnippet( hackers ) );
}

private void check( String friends, String hackers )
{
assertTrue( friends.contains( "friends found: 4" ) );
assertTrue( friends.contains( "Trinity" ) );
assertTrue( friends.contains( "Morpheus" ) );
assertTrue( friends.contains( "Cypher" ) );
assertTrue( friends.contains( "Agent Smith" ) );
assertTrue( hackers.contains( "hackers found: 1" ) );
assertTrue( hackers.contains( "The Architect" ) );
gen.saveToFile( "friends", createOutputSnippet( friends ) );
gen.saveToFile( "hackers", createOutputSnippet( hackers ) );
}
}

0 comments on commit fc0998c

Please sign in to comment.