Permalink
Browse files

Improved printing of paths, changed to use ImpermanentGDb when execut…

…ed as test.
  • Loading branch information...
nawroth committed Mar 13, 2012
1 parent df1dfc0 commit 5e50ca206f904188ca817da7fc9f66473cacdd0a
@@ -25,7 +25,6 @@
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Path;
-import org.neo4j.graphdb.PropertyContainer;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;
@@ -43,9 +42,15 @@
static final String DB_PATH = "target/neo4j-orderedpath-db";
GraphDatabaseService db;
+ public OrderedPath( GraphDatabaseService db )
+ {
+ this.db = db;
+ }
+
public static void main( String[] args )
{
- OrderedPath op = new OrderedPath();
+ GraphDatabaseService db = new EmbeddedGraphDatabase( DB_PATH );
+ OrderedPath op = new OrderedPath( db );
Node A = op.createTheGraph();
TraversalDescription traversalDescription = op.findPaths();
System.out.println( op.printPaths( traversalDescription, A ) );
@@ -54,7 +59,6 @@ public static void main( String[] args )
public Node createTheGraph()
{
- db = new EmbeddedGraphDatabase( DB_PATH );
Transaction tx = db.beginTx();
// START SNIPPET: createGraph
Node A = db.createNode();
@@ -123,25 +127,47 @@ String printPaths( TraversalDescription td, Node A )
String output = "";
// START SNIPPET: printPath
Traverser traverser = td.traverse( A );
+ PathPrinter pathPrinter = new PathPrinter( "name" );
for ( Path path : traverser )
{
- for ( PropertyContainer entity : path )
- {
- if ( entity instanceof Relationship )
- {
- Relationship relationship = (Relationship) entity;
- output += "--[" + relationship.getType()
- .name() + "]-->";
- }
- else if ( entity instanceof Node )
- {
- Node node = (Node) entity;
- output += "(" + node.getProperty( "name" ) + ")";
- }
- }
+ output += Traversal.pathToString( path, pathPrinter );
}
// END SNIPPET: printPath
output += "\n";
return output;
}
+
+ // START SNIPPET: pathPrinter
+ static class PathPrinter implements Traversal.PathDescriptor<Path>
+ {
+ private final String nodePropertyKey;
+
+ public PathPrinter( String nodePropertyKey )
+ {
+ this.nodePropertyKey = nodePropertyKey;
+ }
+
+ @Override
+ public String nodeRepresentation( Path path, Node node )
+ {
+ return "(" + node.getProperty( nodePropertyKey, "" ) + ")";
+ }
+
+ @Override
+ public String relationshipRepresentation( Path path, Node from,
+ Relationship relationship )
+ {
+ String prefix = "--", suffix = "--";
+ if ( from.equals( relationship.getEndNode() ) )
+ {
+ prefix = "<--";
+ }
+ else
+ {
+ suffix = "-->";
+ }
+ return prefix + "[" + relationship.getType().name() + "]" + suffix;
+ }
+ }
+ // END SNIPPET: pathPrinter
}
@@ -20,6 +20,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
import static org.neo4j.helpers.collection.IteratorUtil.count;
import static org.neo4j.visualization.asciidoc.AsciidocHelper.createOutputSnippet;
@@ -32,6 +33,7 @@
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.traversal.TraversalDescription;
import org.neo4j.kernel.impl.util.FileUtils;
+import org.neo4j.test.ImpermanentGraphDatabase;
import org.neo4j.test.JavaDocsGenerator;
import org.neo4j.visualization.asciidoc.AsciidocHelper;
@@ -48,7 +50,7 @@ public static void setUp() throws IOException
{
FileUtils.deleteRecursively( dir );
}
- orderedPath = new OrderedPath();
+ orderedPath = new OrderedPath( new ImpermanentGraphDatabase() );
gen = new JavaDocsGenerator( "ordered-path-java", "dev" );
}
@@ -66,6 +68,7 @@ public void testPath()
assertEquals( 1, count( traversalDescription.traverse( A ) ) );
String output = orderedPath.printPaths( traversalDescription, A );
System.out.println( output );
+ assertTrue( output.contains( "(A)--[REL1]-->(B)--[REL2]-->(C)--[REL3]-->(D)" ) );
String graph = AsciidocHelper.createGraphVizDeletingReferenceNode(
"Ordered Path Graph", orderedPath.db, "java" );
assertFalse( graph.isEmpty() );

0 comments on commit 5e50ca2

Please sign in to comment.