Permalink
Browse files

Cleaned up PathEvaluator and InitialBranchState

  • Loading branch information...
1 parent 64085e1 commit fb70f96867ad4e462e17ccbcb51218217f8adfb0 @tinwelint tinwelint committed Sep 27, 2012
Showing with 222 additions and 255 deletions.
  1. +5 −9 graph-algo/src/main/java/org/neo4j/graphalgo/GraphAlgoFactory.java
  2. +4 −4 graph-algo/src/main/java/org/neo4j/graphalgo/impl/path/AStar.java
  3. +4 −4 graph-algo/src/main/java/org/neo4j/graphalgo/impl/path/ShortestPath.java
  4. +1 −1 graph-algo/src/main/java/org/neo4j/graphalgo/impl/shortestpath/SingleSourceShortestPathBFS.java
  5. +1 −14 graph-algo/src/test/java/org/neo4j/graphalgo/path/TestDijkstra.java
  6. +0 −33 kernel/src/main/java/org/neo4j/graphdb/traversal/AbstractPathEvaluator.java
  7. +19 −0 kernel/src/main/java/org/neo4j/graphdb/traversal/BranchState.java
  8. +26 −1 kernel/src/main/java/org/neo4j/graphdb/traversal/Evaluator.java
  9. +13 −13 kernel/src/main/java/org/neo4j/graphdb/traversal/Evaluators.java
  10. +38 −1 kernel/src/main/java/org/neo4j/graphdb/traversal/InitialBranchState.java
  11. +26 −1 kernel/src/main/java/org/neo4j/graphdb/traversal/InitialStateFactory.java
  12. +25 −0 kernel/src/main/java/org/neo4j/graphdb/traversal/PathEvaluator.java
  13. +1 −2 kernel/src/main/java/org/neo4j/graphdb/traversal/Sorting.java
  14. +20 −1 kernel/src/main/java/org/neo4j/graphdb/traversal/TraversalDescription.java
  15. +1 −1 kernel/src/main/java/org/neo4j/kernel/StandardExpander.java
  16. +2 −76 kernel/src/main/java/org/neo4j/kernel/Traversal.java
  17. +1 −2 kernel/src/main/java/org/neo4j/kernel/impl/traversal/BidirectionalTraversalDescriptionImpl.java
  18. +0 −48 kernel/src/main/java/org/neo4j/kernel/impl/traversal/EvaluatorAsPathEvaluator.java
  19. +1 −2 kernel/src/main/java/org/neo4j/kernel/impl/traversal/MultiEvaluator.java
  20. +2 −1 kernel/src/main/java/org/neo4j/kernel/impl/traversal/TraversalBranchImpl.java
  21. +3 −5 kernel/src/main/java/org/neo4j/kernel/impl/traversal/TraversalDescriptionImpl.java
  22. +19 −18 kernel/src/test/java/org/neo4j/kernel/impl/traversal/TestBidirectionalTraversal.java
  23. +10 −18 kernel/src/test/java/org/neo4j/kernel/impl/traversal/TestBranchState.java
@@ -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;
@@ -157,7 +155,7 @@
* {@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.
*/
@@ -177,7 +175,7 @@
* {@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.
*/
@@ -373,15 +371,14 @@
* @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 );
}
/**
@@ -393,8 +390,7 @@
* @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.
*/
@@ -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;
@@ -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>
{
@@ -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 );
@@ -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;
@@ -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
@@ -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++;
@@ -211,7 +211,7 @@ public Integer getCost( 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()
@@ -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>()
{
@@ -1,33 +0,0 @@
-/**
- * Copyright (c) 2002-2012 "Neo Technology,"
- * Network Engine for Objects in Lund AB [http://neotechnology.com]
- *
- * This file is part of Neo4j.
- *
- * Neo4j is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-package org.neo4j.graphdb.traversal;
-
-import org.neo4j.graphdb.Path;
-import org.neo4j.kernel.Traversal;
-
-public abstract class AbstractPathEvaluator<STATE> implements PathEvaluator<STATE>
-{
- @SuppressWarnings( "unchecked" )
- @Override
- public Evaluation evaluate( Path path )
- {
- return evaluate( path, Traversal.NO_BRANCH_STATE );
- }
-}
@@ -47,4 +47,23 @@
* 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" );
+ }
+ };
}
@@ -32,7 +32,6 @@
* @see Evaluation
* @see Evaluators
* @see TraversalDescription#evaluator(Evaluator)
- * @deprecated replaced by {@link PathEvaluator}
*/
public interface Evaluator
{
@@ -50,4 +49,30 @@
* 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 );
+ }
+ }
}
@@ -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 )
{
@@ -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 )
{
@@ -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 )
{
@@ -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 )
{
@@ -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 )
{
@@ -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 )
@@ -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 )
@@ -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 )
@@ -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 )
@@ -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 )
@@ -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 )
@@ -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
@@ -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 )
Oops, something went wrong.

0 comments on commit fb70f96

Please sign in to comment.