Skip to content

Commit

Permalink
Cleaned up PathEvaluator and InitialBranchState
Browse files Browse the repository at this point in the history
  • Loading branch information
tinwelint committed Sep 27, 2012
1 parent 64085e1 commit fb70f96
Show file tree
Hide file tree
Showing 23 changed files with 222 additions and 255 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
*/
package org.neo4j.graphalgo;

import static org.neo4j.kernel.Traversal.wrapInitialStateFactory;

import org.neo4j.graphalgo.impl.path.AStar;
import org.neo4j.graphalgo.impl.path.AllPaths;
import org.neo4j.graphalgo.impl.path.AllSimplePaths;
Expand Down Expand Up @@ -157,7 +155,7 @@ public static PathFinder<Path> shortestPath( PathExpander expander, int maxDepth
* {@link Relationship}s for each {@link Node}.
* @param maxDepth the max {@link Path#length()} returned paths are allowed
* to have.
* @param maxResultCount the maximum number of {@link Path}s to return.
* @param maxHitCount the maximum number of {@link Path}s to return.
* If this number of found paths are encountered the traversal will stop.
* @return an algorithm which finds shortest paths between two nodes.
*/
Expand All @@ -177,7 +175,7 @@ public static PathFinder<Path> shortestPath( RelationshipExpander expander, int
* {@link Relationship}s for each {@link Path}.
* @param maxDepth the max {@link Path#length()} returned paths are allowed
* to have.
* @param maxResultCount the maximum number of {@link Path}s to return.
* @param maxHitCount the maximum number of {@link Path}s to return.
* If this number of found paths are encountered the traversal will stop.
* @return an algorithm which finds shortest paths between two nodes.
*/
Expand Down Expand Up @@ -373,15 +371,14 @@ public static PathFinder<WeightedPath> dijkstra( PathExpander expander,
* @param expander the {@link PathExpander} to use for expanding
* {@link Relationship}s for each {@link Path}.
* @param stateFactory initial state for the traversal branches.
* @param relationshipPropertyRepresentingCost the property to represent cost
* on each relationship the algorithm traverses.
* @param costEvaluator the cost evaluator for each relationship the algorithm traverses.
* @return an algorithm which finds the cheapest path between two nodes
* using the Dijkstra algorithm.
*/
public static PathFinder<WeightedPath> dijkstra( PathExpander expander,
InitialStateFactory stateFactory, CostEvaluator<Double> costEvaluator )
{
return new Dijkstra( expander, wrapInitialStateFactory( stateFactory ), costEvaluator );
return new Dijkstra( expander, new InitialStateFactory.AsInitialBranchState( stateFactory ), costEvaluator );
}

/**
Expand All @@ -393,8 +390,7 @@ public static PathFinder<WeightedPath> dijkstra( PathExpander expander,
* @param expander the {@link PathExpander} to use for expanding
* {@link Relationship}s for each {@link Path}.
* @param stateFactory initial state for the traversal branches.
* @param relationshipPropertyRepresentingCost the property to represent cost
* on each relationship the algorithm traverses.
* @param costEvaluator the cost evaluator for each relationship the algorithm traverses.
* @return an algorithm which finds the cheapest path between two nodes
* using the Dijkstra algorithm.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
*/
package org.neo4j.graphalgo.impl.path;

import static org.neo4j.kernel.StandardExpander.toPathExpander;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -46,9 +44,11 @@
import org.neo4j.graphdb.PropertyContainer;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.RelationshipExpander;
import org.neo4j.graphdb.traversal.BranchState;
import org.neo4j.graphdb.traversal.TraversalMetadata;
import org.neo4j.helpers.collection.PrefetchingIterator;
import org.neo4j.kernel.Traversal;

import static org.neo4j.kernel.StandardExpander.toPathExpander;

public class AStar implements PathFinder<WeightedPath>
{
Expand Down Expand Up @@ -220,7 +220,7 @@ protected Node fetchNextOrNull()
@SuppressWarnings( "unchecked" )
private void expand()
{
for ( Relationship rel : expander.expand( this, Traversal.NO_BRANCH_STATE ) )
for ( Relationship rel : expander.expand( this, BranchState.NO_STATE ) )
{
lastMetadata.rels++;
Node node = rel.getOtherNode( this.lastNode );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
*/
package org.neo4j.graphalgo.impl.path;

import static org.neo4j.kernel.StandardExpander.toPathExpander;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand All @@ -42,11 +40,13 @@
import org.neo4j.graphdb.PropertyContainer;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.RelationshipExpander;
import org.neo4j.graphdb.traversal.BranchState;
import org.neo4j.graphdb.traversal.TraversalMetadata;
import org.neo4j.helpers.collection.IterableWrapper;
import org.neo4j.helpers.collection.NestingIterator;
import org.neo4j.helpers.collection.PrefetchingIterator;
import org.neo4j.kernel.Traversal;

import static org.neo4j.kernel.StandardExpander.toPathExpander;

/**
* Find (all or one) simple shortest path(s) between two nodes. It starts
Expand Down Expand Up @@ -311,7 +311,7 @@ private void prepareNextLevel()
protected Iterator<Relationship> createNestedIterator( Node node )
{
lastParentTraverserNode = node;
return expander.expand( DirectionData.this, Traversal.NO_BRANCH_STATE ).iterator();
return expander.expand( DirectionData.this, BranchState.NO_STATE ).iterator();
}
};
this.currentDepth++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public List<List<Relationship>> getPathsAsRelationships( Node targetNode )

/**
* Iterator-style "next" method.
* @return True if advance was made. False if no more computation could be
* @return True if evaluate was made. False if no more computation could be
* done.
*/
public boolean processNextNode()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,20 +210,7 @@ public void withState() throws Exception
setWeight( "b", "c", 2 );
setWeight( "c", "d", 5 );

InitialBranchState<Integer> state = new InitialBranchState<Integer>()
{
@Override
public Integer initialState( Path path )
{
return 0;
}

@Override
public InitialBranchState<Integer> reverse()
{
return this;
}
};
InitialBranchState<Integer> state = new InitialBranchState.State<Integer>( 0, 0 );
final Map<Node, Integer> encounteredState = new HashMap<Node, Integer>();
PathExpander<Integer> expander = new PathExpander<Integer>()
{
Expand Down

This file was deleted.

19 changes: 19 additions & 0 deletions kernel/src/main/java/org/neo4j/graphdb/traversal/BranchState.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,23 @@ public interface BranchState<STATE>
* children.
*/
void setState( STATE state );

/**
* Instance representing no state, usage resulting in
* {@link IllegalStateException} being thrown.
*/
public static final BranchState NO_STATE = new BranchState()
{
@Override
public Object getState()
{
throw new IllegalStateException( "Branch state disabled, pass in an initial state to enable it" );
}

@Override
public void setState( Object state )
{
throw new IllegalStateException( "Branch state disabled, pass in an initial state to enable it" );
}
};
}
27 changes: 26 additions & 1 deletion kernel/src/main/java/org/neo4j/graphdb/traversal/Evaluator.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
* @see Evaluation
* @see Evaluators
* @see TraversalDescription#evaluator(Evaluator)
* @deprecated replaced by {@link PathEvaluator}
*/
public interface Evaluator
{
Expand All @@ -50,4 +49,30 @@ public interface Evaluator
* down that path.
*/
Evaluation evaluate( Path path );

/**
* Exposes an {@link Evaluator} as a {@link PathEvaluator}.
* @param <STATE> the type of state passed into the evaluator.
*/
public static class AsPathEvaluator<STATE> implements PathEvaluator<STATE>
{
private final Evaluator evaluator;

public AsPathEvaluator( Evaluator evaluator )
{
this.evaluator = evaluator;
}

@Override
public Evaluation evaluate( Path path, BranchState<STATE> state )
{
return evaluator.evaluate( path );
}

@Override
public Evaluation evaluate( Path path )
{
return evaluator.evaluate( path );
}
}
}
26 changes: 13 additions & 13 deletions kernel/src/main/java/org/neo4j/graphdb/traversal/Evaluators.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
public abstract class Evaluators
{
@SuppressWarnings( "rawtypes" )
private static final PathEvaluator ALL = new AbstractPathEvaluator()
private static final PathEvaluator ALL = new PathEvaluator.Adapter()
{
public Evaluation evaluate( Path path, BranchState state )
{
Expand Down Expand Up @@ -83,7 +83,7 @@ public static PathEvaluator excludeStartPosition()
@SuppressWarnings( "rawtypes" )
public static PathEvaluator toDepth( final int depth )
{
return new AbstractPathEvaluator()
return new PathEvaluator.Adapter()
{
public Evaluation evaluate( Path path, BranchState state )
{
Expand All @@ -104,7 +104,7 @@ public Evaluation evaluate( Path path, BranchState state )
@SuppressWarnings( "rawtypes" )
public static PathEvaluator fromDepth( final int depth )
{
return new AbstractPathEvaluator()
return new PathEvaluator.Adapter()
{
public Evaluation evaluate( Path path, BranchState state )
{
Expand All @@ -124,7 +124,7 @@ public Evaluation evaluate( Path path, BranchState state )
@SuppressWarnings( "rawtypes" )
public static PathEvaluator atDepth( final int depth )
{
return new AbstractPathEvaluator()
return new PathEvaluator.Adapter()
{
public Evaluation evaluate( Path path, BranchState state )
{
Expand All @@ -147,7 +147,7 @@ public Evaluation evaluate( Path path, BranchState state )
@SuppressWarnings( "rawtypes" )
public static PathEvaluator includingDepths( final int minDepth, final int maxDepth )
{
return new AbstractPathEvaluator()
return new PathEvaluator.Adapter()
{
public Evaluation evaluate( Path path, BranchState state )
{
Expand Down Expand Up @@ -181,7 +181,7 @@ public static PathEvaluator lastRelationshipTypeIs( final Evaluation evaluationI
{
if ( orAnyOfTheseTypes.length == 0 )
{
return new AbstractPathEvaluator()
return new PathEvaluator.Adapter()
{
@Override
public Evaluation evaluate( Path path, BranchState state )
Expand All @@ -199,7 +199,7 @@ public Evaluation evaluate( Path path, BranchState state )
expectedTypes.add( otherType.name() );
}

return new AbstractPathEvaluator()
return new PathEvaluator.Adapter()
{
@Override
public Evaluation evaluate( Path path, BranchState state )
Expand Down Expand Up @@ -283,7 +283,7 @@ public static PathEvaluator endNodeIs( final Evaluation evaluationIfMatch, final
if ( possibleEndNodes.length == 1 )
{
final Node target = possibleEndNodes[0];
return new AbstractPathEvaluator()
return new PathEvaluator.Adapter()
{
@Override
public Evaluation evaluate( Path path, BranchState state )
Expand All @@ -294,7 +294,7 @@ public Evaluation evaluate( Path path, BranchState state )
}

final Set<Node> endNodes = new HashSet<Node>( asList( possibleEndNodes ) );
return new AbstractPathEvaluator()
return new PathEvaluator.Adapter()
{
@Override
public Evaluation evaluate( Path path, BranchState state )
Expand Down Expand Up @@ -345,7 +345,7 @@ public static PathEvaluator includeIfContainsAll( final Node... nodes )
{
if ( nodes.length == 1 )
{
return new AbstractPathEvaluator()
return new PathEvaluator.Adapter()
{
@Override
public Evaluation evaluate( Path path, BranchState state )
Expand All @@ -364,7 +364,7 @@ public Evaluation evaluate( Path path, BranchState state )
else
{
final Set<Node> fullSet = new HashSet<Node>( Arrays.asList( nodes ) );
return new AbstractPathEvaluator()
return new PathEvaluator.Adapter()
{
@Override
public Evaluation evaluate( Path path, BranchState state )
Expand Down Expand Up @@ -395,7 +395,7 @@ public Evaluation evaluate( Path path, BranchState state )
@SuppressWarnings( "rawtypes" )
public static PathEvaluator includeIfAcceptedByAny( final PathEvaluator... evaluators )
{
return new AbstractPathEvaluator()
return new PathEvaluator.Adapter()
{
@SuppressWarnings( "unchecked" )
@Override
Expand Down Expand Up @@ -425,7 +425,7 @@ public Evaluation evaluate( Path path, BranchState state )
@SuppressWarnings( "rawtypes" )
public static PathEvaluator includeIfAcceptedByAny( final Evaluator... evaluators )
{
return new AbstractPathEvaluator()
return new PathEvaluator.Adapter()
{
@Override
public Evaluation evaluate( Path path, BranchState state )
Expand Down
Loading

0 comments on commit fb70f96

Please sign in to comment.