Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Updated traversal printer to give more hierarchical view

  • Loading branch information...
commit 42594c2fb219c7b110107b109a08d409c28192ce 1 parent f8b717a
@iansrobinson iansrobinson authored
Showing with 75 additions and 15 deletions.
  1. +75 −15 src/main/java/org/neo4j/tutorial/TraversalPrinter.java
View
90 src/main/java/org/neo4j/tutorial/TraversalPrinter.java
@@ -1,5 +1,6 @@
package org.neo4j.tutorial;
+import static java.lang.Math.min;
import static java.util.Arrays.asList;
import java.util.Iterator;
@@ -15,6 +16,8 @@
{
private final Iterable<String> propertyNames;
private final Evaluator innerEvaluator;
+ private String lastPath = "";
+
public TraversalPrinter( Evaluator innerEvaluator, String... propertyNames )
{
@@ -24,19 +27,65 @@ public TraversalPrinter( Evaluator innerEvaluator, String... propertyNames )
private void printPath( Path path )
{
+ StringBuilder builder = new StringBuilder();
+ Node previousNode = null;
for ( PropertyContainer propertyContainer : path )
{
if ( Node.class.isAssignableFrom( propertyContainer.getClass() ) )
{
- printNode( (Node) propertyContainer );
+ previousNode = (Node) propertyContainer;
+ builder.append( formatNode( previousNode ) );
+ }
+ else
+ {
+ builder.append( formatRelationship( (Relationship) propertyContainer, previousNode ) );
+ }
+ }
+ String currentPath = builder.toString();
+
+
+ String overlap = overlappingPath( currentPath, lastPath );
+ if ( !overlap.isEmpty() )
+ {
+ String formattedCurrentPath = repeat( " ", overlap.length() ).concat( currentPath.substring( overlap
+ .length() ) );
+ System.out.print( formattedCurrentPath );
+ }
+ else
+ {
+ System.out.print( currentPath );
+ }
+
+
+ lastPath = currentPath;
+
+ }
+
+ private String repeat( String s, int i )
+ {
+ return String.format( String.format( "%%0%dd", i ), 0 ).replace( "0", s );
+ }
+
+ private String overlappingPath( String s1, String s2 )
+ {
+ StringBuilder builder = new StringBuilder();
+ int maxLength = min( s1.length(), s2.length() );
+ for ( int i = 0; i < maxLength; i++ )
+ {
+ if ( s1.charAt( i ) == s2.charAt( i ) )
+ {
+ builder.append( s1.charAt( i ) );
}
else
{
- printRelationship( (Relationship) propertyContainer, path.endNode() );
+ break;
}
}
+ String overlap = builder.toString();
+ return overlap.substring( 0, overlap.lastIndexOf( ")" ) + 1 );
}
+
private Evaluation printResult( Evaluation evaluation )
{
System.out.print( String.format( " %s\n", evaluation.name() ) );
@@ -44,43 +93,54 @@ private Evaluation printResult( Evaluation evaluation )
}
- private void printRelationship( Relationship relationship, Node currentNode )
+ private String formatRelationship( Relationship relationship, Node previousNode )
{
String prefix = "";
String suffix = "";
- if ( relationship.getStartNode().equals( currentNode ) )
+ if ( relationship.getStartNode().equals( previousNode ) )
{
- prefix = "<";
+ suffix = ">";
}
else
{
- suffix = ">";
+ prefix = "<";
}
- System.out.print( String.format( "%s-[:%s]-%s", prefix, relationship.getType().name(), suffix ) );
+ return String.format( "%s-[:%s]-%s", prefix, relationship.getType().name(), suffix );
}
- private void printNode( Node node )
+ private String formatNode( Node node )
{
- System.out.print( "(" );
+ StringBuilder builder = new StringBuilder();
+ builder.append( "(" );
boolean propertyFound = false;
- Iterator<String> iterator = propertyNames.iterator();
- while ( iterator.hasNext() && !propertyFound )
+ Iterator<String> propertyNameIterator = propertyNames.iterator();
+ while ( propertyNameIterator.hasNext() && !propertyFound )
{
- String propertyName = iterator.next();
+ String propertyName = propertyNameIterator.next();
if ( node.hasProperty( propertyName ) )
{
- System.out.print( String.format( "%s:%s", propertyName, node.getProperty( propertyName ) ) );
+ builder.append( String.format( "%s:%s", propertyName, node.getProperty( propertyName ) ) );
propertyFound = true;
}
}
if ( !propertyFound )
{
- System.out.print( String.format( "id:%s", node.getId() ) );
+ Iterator<String> propertyKeyIterator = node.getPropertyKeys().iterator();
+ if ( propertyKeyIterator.hasNext() )
+ {
+ String propertyName = propertyKeyIterator.next();
+ builder.append( String.format( "%s:%s", propertyName, node.getProperty( propertyName ) ) );
+ }
+ else
+ {
+ builder.append( String.format( "id:%s", node.getId() ) );
+ }
}
- System.out.print( ")" );
+ builder.append( ")" );
+ return builder.toString();
}
public Evaluation evaluate( Path path )
Please sign in to comment.
Something went wrong with that request. Please try again.