Skip to content

Commit

Permalink
Updated traversal printer to give more hierarchical view
Browse files Browse the repository at this point in the history
  • Loading branch information
iansrobinson committed Oct 11, 2012
1 parent f8b717a commit 42594c2
Showing 1 changed file with 75 additions and 15 deletions.
90 changes: 75 additions & 15 deletions 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;
Expand All @@ -15,6 +16,8 @@ public class TraversalPrinter implements Evaluator
{
private final Iterable<String> propertyNames;
private final Evaluator innerEvaluator;
private String lastPath = "";


public TraversalPrinter( Evaluator innerEvaluator, String... propertyNames )
{
Expand All @@ -24,63 +27,120 @@ 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() ) );
return 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 )
Expand Down

0 comments on commit 42594c2

Please sign in to comment.