Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Cleaned up PathEvaluator and InitialBranchState

  • Loading branch information...
commit fb70f96867ad4e462e17ccbcb51218217f8adfb0 1 parent 64085e1
@tinwelint tinwelint authored
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
View
14 graph-algo/src/main/java/org/neo4j/graphalgo/GraphAlgoFactory.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.
*/
View
8 graph-algo/src/main/java/org/neo4j/graphalgo/impl/path/AStar.java
@@ -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 );
View
8 graph-algo/src/main/java/org/neo4j/graphalgo/impl/path/ShortestPath.java
@@ -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++;
View
2  graph-algo/src/main/java/org/neo4j/graphalgo/impl/shortestpath/SingleSourceShortestPathBFS.java
@@ -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()
View
15 graph-algo/src/test/java/org/neo4j/graphalgo/path/TestDijkstra.java
@@ -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>()
{
View
33 kernel/src/main/java/org/neo4j/graphdb/traversal/AbstractPathEvaluator.java
@@ -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 );
- }
-}
View
19 kernel/src/main/java/org/neo4j/graphdb/traversal/BranchState.java
@@ -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" );
+ }
+ };
}
View
27 kernel/src/main/java/org/neo4j/graphdb/traversal/Evaluator.java
@@ -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 );
+ }
+ }
}
View
26 kernel/src/main/java/org/neo4j/graphdb/traversal/Evaluators.java
@@ -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 )
View
39 kernel/src/main/java/org/neo4j/graphdb/traversal/InitialBranchState.java
@@ -42,11 +42,48 @@ public InitialBranchState reverse()
return this;
}
};
-
+
/**
* Creates a version of this state factory which produces reversed initial state,
* used in bidirectional traversals.
* @return an instance which produces reversed initial state.
*/
InitialBranchState<STATE> reverse();
+
+ public static abstract class Adapter<STATE> implements InitialBranchState<STATE>
+ {
+ @Override
+ public InitialBranchState<STATE> reverse()
+ {
+ return this;
+ }
+ }
+
+ /**
+ * Branch state evaluator for an initial state.
+ * @param <STATE>
+ */
+ public static class State<STATE> extends Adapter<STATE>
+ {
+ private final STATE initialState;
+ private final STATE reversedInitialState;
+
+ public State( STATE initialState, STATE reversedInitialState )
+ {
+ this.initialState = initialState;
+ this.reversedInitialState = reversedInitialState;
+ }
+
+ @Override
+ public InitialBranchState<STATE> reverse()
+ {
+ return new State( reversedInitialState, initialState );
+ }
+
+ @Override
+ public STATE initialState( Path path )
+ {
+ return initialState;
+ }
+ }
}
View
27 kernel/src/main/java/org/neo4j/graphdb/traversal/InitialStateFactory.java
@@ -52,8 +52,33 @@ public Object initialState( Path path )
* {@link PathExpander} to becomes the new state from that point in that branch
* and downwards.
*
- * @param branch the start branch to return the initial state for.
+ * @param path the start branch to return the initial state for.
* @return an initial state for the traversal branch.
*/
STATE initialState( Path path );
+
+ /**
+ * Wraps an {@link InitialStateFactory} in a {@link InitialBranchState}
+ */
+ public static class AsInitialBranchState<STATE> implements InitialBranchState<STATE>
+ {
+ private final InitialStateFactory<STATE> factory;
+
+ public AsInitialBranchState( InitialStateFactory<STATE> factory )
+ {
+ this.factory = factory;
+ }
+
+ @Override
+ public InitialBranchState<STATE> reverse()
+ {
+ return this;
+ }
+
+ @Override
+ public STATE initialState( Path path )
+ {
+ return factory.initialState( path );
+ }
+ }
}
View
25 kernel/src/main/java/org/neo4j/graphdb/traversal/PathEvaluator.java
@@ -21,6 +21,18 @@
import org.neo4j.graphdb.Path;
+/**
+ * A PathEvaluator controls what's to be returned from a traversal and also how
+ * pruning is done. It looks at a {@link Path} and {@link BranchState}and decides
+ * whether or not it should be included in the traversal result. It also decides
+ * whether the traverser should continue down that path or if it should be pruned
+ * so that the traverser won't continue down that path.
+ *
+ * @author Mattias Persson
+ * @see Evaluation
+ * @see Evaluators
+ * @see TraversalDescription#evaluator(PathEvaluator)
+ */
public interface PathEvaluator<STATE> extends Evaluator
{
/**
@@ -38,4 +50,17 @@
* down that path.
*/
Evaluation evaluate( Path path, BranchState<STATE> state );
+
+ /**
+ * Adapter for {@link PathEvaluator}.
+ * @param <STATE>
+ */
+ public static abstract class Adapter<STATE> implements PathEvaluator<STATE>
+ {
+ @Override
+ public Evaluation evaluate( Path path )
+ {
+ return evaluate( path, BranchState.NO_STATE );
+ }
+ }
}
View
3  kernel/src/main/java/org/neo4j/graphdb/traversal/Sorting.java
@@ -25,7 +25,6 @@
import org.neo4j.graphdb.Path;
import org.neo4j.graphdb.PathExpander;
import org.neo4j.helpers.collection.IteratorUtil;
-import org.neo4j.kernel.Traversal;
import org.neo4j.kernel.impl.util.SingleNodePath;
/**
@@ -99,7 +98,7 @@ protected int compareNodes( Node endNode1, Node endNode2 )
private Integer count( Node node, PathExpander expander )
{
- return IteratorUtil.count( expander.expand( new SingleNodePath( node ), Traversal.NO_BRANCH_STATE ) );
+ return IteratorUtil.count( expander.expand( new SingleNodePath( node ), BranchState.NO_STATE ) );
}
};
}
View
21 kernel/src/main/java/org/neo4j/graphdb/traversal/TraversalDescription.java
@@ -96,7 +96,26 @@
* @return a new traversal description with the new modifications.
*/
TraversalDescription evaluator( Evaluator evaluator );
-
+
+ /**
+ * Adds {@code evaluator} to the list of evaluators which will control the
+ * behaviour of the traversal. Each {@link PathEvaluator} can decide whether or
+ * not to include a position in the traverser result, i.e. return it from
+ * the {@link Traverser} iterator and also whether to continue down that
+ * path or to prune, so that the traverser won't continue further down that
+ * path.
+ *
+ * Multiple {@link PathEvaluator}s can be added. For a path to be included in
+ * the result, all evaluators must agree to include it, i.e. returning
+ * either {@link Evaluation#INCLUDE_AND_CONTINUE} or
+ * {@link Evaluation#INCLUDE_AND_PRUNE}. For making the traversal continue
+ * down that path all evaluators must agree to continue from that path, i.e.
+ * returning either {@link Evaluation#INCLUDE_AND_CONTINUE} or
+ * {@link Evaluation#EXCLUDE_AND_CONTINUE}.
+ *
+ * @param evaluator
+ * @return a new traversal description with the new modifications.
+ */
TraversalDescription evaluator( PathEvaluator evaluator );
/**
View
2  kernel/src/main/java/org/neo4j/kernel/StandardExpander.java
@@ -890,7 +890,7 @@ boolean exclude( Path path )
public final Expansion<Relationship> expand( Node node )
{
- return new RelationshipExpansion( this, new SingleNodePath( node ), Traversal.NO_BRANCH_STATE );
+ return new RelationshipExpansion( this, new SingleNodePath( node ), BranchState.NO_STATE );
}
public final Expansion<Relationship> expand( Path path, BranchState state )
View
78 kernel/src/main/java/org/neo4j/kernel/Traversal.java
@@ -30,20 +30,15 @@
import org.neo4j.graphdb.RelationshipExpander;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.traversal.BidirectionalTraversalDescription;
+import org.neo4j.graphdb.traversal.BranchCollisionDetector;
import org.neo4j.graphdb.traversal.BranchOrderingPolicy;
-import org.neo4j.graphdb.traversal.BranchState;
-import org.neo4j.graphdb.traversal.Evaluator;
import org.neo4j.graphdb.traversal.Evaluators;
-import org.neo4j.graphdb.traversal.InitialBranchState;
import org.neo4j.graphdb.traversal.InitialStateFactory;
-import org.neo4j.graphdb.traversal.BranchCollisionDetector;
-import org.neo4j.graphdb.traversal.PathEvaluator;
import org.neo4j.graphdb.traversal.SideSelectorPolicy;
import org.neo4j.graphdb.traversal.TraversalBranch;
import org.neo4j.graphdb.traversal.TraversalDescription;
import org.neo4j.graphdb.traversal.UniquenessFactory;
import org.neo4j.kernel.impl.traversal.BidirectionalTraversalDescriptionImpl;
-import org.neo4j.kernel.impl.traversal.EvaluatorAsPathEvaluator;
import org.neo4j.kernel.impl.traversal.FinalTraversalBranch;
import org.neo4j.kernel.impl.traversal.TraversalDescriptionImpl;
@@ -94,28 +89,7 @@ public static BidirectionalTraversalDescription bidirectionalTraversal()
{
return new BidirectionalTraversalDescriptionImpl();
}
-
- @SuppressWarnings( "rawtypes" )
- public static final BranchState NO_BRANCH_STATE = new BranchState()
- {
- @Override
- public Object getState()
- {
- throw new UnsupportedOperationException( "Branch state disabled, pass in an initial state to enable it" );
- }
-
- @Override
- public void setState( Object state )
- {
- throw new UnsupportedOperationException( "Branch state disabled, pass in an initial state to enable it" );
- }
- };
-
- public static <STATE> BranchState<STATE> noBranchState()
- {
- return NO_BRANCH_STATE;
- }
-
+
/**
* {@link InitialStateFactory} which always returns the supplied {@code initialState}.
* @param initialState the initial state for a traversal branch.
@@ -602,52 +576,4 @@ public static PathDescription path()
{
return new PathDescription();
}
-
- /**
- * Wraps an {@link InitialStateFactory} in a {@link InitialBranchState}
- * (which has got {@link InitialBranchState#reverse() reverse method}).
- * @param state the {@link InitialStateFactory} to wrap.
- * @return state as an {@link InitialBranchState} object.
- * @deprecated together with {@link InitialStateFactory}.
- */
- public static <STATE> InitialBranchState<STATE> wrapInitialStateFactory( final InitialStateFactory<STATE> state )
- {
- return new InitialBranchState<STATE>()
- {
- @Override
- public STATE initialState( Path path )
- {
- return state.initialState( path );
- }
-
- @Override
- public InitialBranchState<STATE> reverse()
- {
- return this;
- }
- };
- }
-
- public static <STATE> InitialBranchState<STATE> initialState( final STATE initialState, final STATE reversedInitialState )
- {
- return new InitialBranchState<STATE>()
- {
- @Override
- public STATE initialState( Path path )
- {
- return initialState;
- }
-
- @Override
- public InitialBranchState<STATE> reverse()
- {
- return Traversal.initialState( reversedInitialState, initialState );
- }
- };
- }
-
- public static PathEvaluator wrapEvaluator( Evaluator evaluator )
- {
- return new EvaluatorAsPathEvaluator( evaluator );
- }
}
View
3  kernel/src/main/java/org/neo4j/kernel/impl/traversal/BidirectionalTraversalDescriptionImpl.java
@@ -20,7 +20,6 @@
package org.neo4j.kernel.impl.traversal;
import static org.neo4j.kernel.Traversal.traversal;
-import static org.neo4j.kernel.Traversal.wrapEvaluator;
import static org.neo4j.kernel.impl.traversal.TraversalDescriptionImpl.addEvaluator;
import static org.neo4j.kernel.impl.traversal.TraversalDescriptionImpl.nullCheck;
@@ -103,7 +102,7 @@ public BidirectionalTraversalDescription collisionEvaluator( PathEvaluator colli
@Override
public BidirectionalTraversalDescription collisionEvaluator( Evaluator collisionEvaluator )
{
- return collisionEvaluator( wrapEvaluator( collisionEvaluator ) );
+ return collisionEvaluator( new Evaluator.AsPathEvaluator( collisionEvaluator ) );
}
@Override
View
48 kernel/src/main/java/org/neo4j/kernel/impl/traversal/EvaluatorAsPathEvaluator.java
@@ -1,48 +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.kernel.impl.traversal;
-
-import org.neo4j.graphdb.Path;
-import org.neo4j.graphdb.traversal.BranchState;
-import org.neo4j.graphdb.traversal.Evaluation;
-import org.neo4j.graphdb.traversal.Evaluator;
-import org.neo4j.graphdb.traversal.PathEvaluator;
-
-public class EvaluatorAsPathEvaluator<STATE> implements PathEvaluator<STATE>
-{
- private Evaluator evaluator;
-
- public EvaluatorAsPathEvaluator( Evaluator evaluator )
- {
- this.evaluator = evaluator;
- }
-
- @Override
- public Evaluation evaluate( Path path )
- {
- return this.evaluator.evaluate( path );
- }
-
- @Override
- public Evaluation evaluate( Path path, BranchState state )
- {
- return this.evaluator.evaluate( path );
- }
-}
View
3  kernel/src/main/java/org/neo4j/kernel/impl/traversal/MultiEvaluator.java
@@ -20,7 +20,6 @@
package org.neo4j.kernel.impl.traversal;
import org.neo4j.graphdb.Path;
-import org.neo4j.graphdb.traversal.AbstractPathEvaluator;
import org.neo4j.graphdb.traversal.BranchState;
import org.neo4j.graphdb.traversal.Evaluation;
import org.neo4j.graphdb.traversal.Evaluator;
@@ -30,7 +29,7 @@
* Evaluator which can hold multiple {@link Evaluator}s and delegate to them
* all for evaluation requests.
*/
-public class MultiEvaluator<STATE> extends AbstractPathEvaluator<STATE>
+public class MultiEvaluator<STATE> extends PathEvaluator.Adapter<STATE>
{
private final PathEvaluator[] evaluators;
View
3  kernel/src/main/java/org/neo4j/kernel/impl/traversal/TraversalBranchImpl.java
@@ -26,6 +26,7 @@
import org.neo4j.graphdb.PathExpander;
import org.neo4j.graphdb.PropertyContainer;
import org.neo4j.graphdb.Relationship;
+import org.neo4j.graphdb.traversal.BranchState;
import org.neo4j.graphdb.traversal.Evaluation;
import org.neo4j.graphdb.traversal.TraversalBranch;
import org.neo4j.graphdb.traversal.TraversalContext;
@@ -110,7 +111,7 @@ protected void expandRelationships( PathExpander expander )
protected Iterator<Relationship> expandRelationshipsWithoutChecks( PathExpander expander )
{
- return expander.expand( this, Traversal.noBranchState() ).iterator();
+ return expander.expand( this, BranchState.NO_STATE ).iterator();
}
protected boolean hasExpandedRelationships()
View
8 kernel/src/main/java/org/neo4j/kernel/impl/traversal/TraversalDescriptionImpl.java
@@ -19,8 +19,6 @@
*/
package org.neo4j.kernel.impl.traversal;
-import static org.neo4j.kernel.Traversal.wrapInitialStateFactory;
-
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
@@ -33,9 +31,9 @@
import org.neo4j.graphdb.RelationshipExpander;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.traversal.BranchOrderingPolicy;
+import org.neo4j.graphdb.traversal.InitialBranchState;
import org.neo4j.graphdb.traversal.Evaluator;
import org.neo4j.graphdb.traversal.Evaluators;
-import org.neo4j.graphdb.traversal.InitialBranchState;
import org.neo4j.graphdb.traversal.InitialStateFactory;
import org.neo4j.graphdb.traversal.PathEvaluator;
import org.neo4j.graphdb.traversal.TraversalDescription;
@@ -117,7 +115,7 @@ public TraversalDescription uniqueness( UniquenessFactory uniqueness,
public TraversalDescription evaluator( Evaluator evaluator )
{
- return evaluator( new EvaluatorAsPathEvaluator( evaluator ) );
+ return evaluator( new Evaluator.AsPathEvaluator( evaluator) );
}
public TraversalDescription evaluator( PathEvaluator evaluator )
@@ -222,7 +220,7 @@ public TraversalDescription expand( PathExpander<?> expander )
public <STATE> TraversalDescription expand( PathExpander<STATE> expander, InitialStateFactory<STATE> initialState )
{
return new TraversalDescriptionImpl( expander, uniqueness,
- uniquenessParameter, evaluator, wrapInitialStateFactory( initialState ), branchOrdering, sorting, endNodes );
+ uniquenessParameter, evaluator, new InitialStateFactory.AsInitialBranchState<STATE>( initialState ), branchOrdering, sorting, endNodes );
}
@Override
View
37 kernel/src/test/java/org/neo4j/kernel/impl/traversal/TestBidirectionalTraversal.java
@@ -19,20 +19,6 @@
*/
package org.neo4j.kernel.impl.traversal;
-import static java.util.Arrays.asList;
-import static org.junit.Assert.assertEquals;
-import static org.neo4j.graphdb.Direction.OUTGOING;
-import static org.neo4j.graphdb.DynamicRelationshipType.withName;
-import static org.neo4j.graphdb.traversal.Evaluators.includeIfContainsAll;
-import static org.neo4j.helpers.collection.IteratorUtil.count;
-import static org.neo4j.helpers.collection.IteratorUtil.single;
-import static org.neo4j.kernel.Traversal.bidirectionalTraversal;
-import static org.neo4j.kernel.Traversal.initialState;
-import static org.neo4j.kernel.Traversal.pathExpanderForTypes;
-import static org.neo4j.kernel.Traversal.traversal;
-import static org.neo4j.kernel.Uniqueness.NODE_PATH;
-import static org.neo4j.kernel.Uniqueness.RELATIONSHIP_PATH;
-
import org.junit.Test;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Path;
@@ -41,6 +27,7 @@
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.traversal.BidirectionalTraversalDescription;
import org.neo4j.graphdb.traversal.BranchCollisionDetector;
+import org.neo4j.graphdb.traversal.InitialBranchState;
import org.neo4j.graphdb.traversal.Evaluator;
import org.neo4j.graphdb.traversal.Evaluators;
import org.neo4j.graphdb.traversal.TraversalBranch;
@@ -50,6 +37,19 @@
import org.neo4j.kernel.Traversal;
import org.neo4j.kernel.Uniqueness;
+import static java.util.Arrays.asList;
+import static org.junit.Assert.assertEquals;
+import static org.neo4j.graphdb.Direction.OUTGOING;
+import static org.neo4j.graphdb.DynamicRelationshipType.withName;
+import static org.neo4j.graphdb.traversal.Evaluators.includeIfContainsAll;
+import static org.neo4j.helpers.collection.IteratorUtil.count;
+import static org.neo4j.helpers.collection.IteratorUtil.single;
+import static org.neo4j.kernel.Traversal.bidirectionalTraversal;
+import static org.neo4j.kernel.Traversal.pathExpanderForTypes;
+import static org.neo4j.kernel.Traversal.traversal;
+import static org.neo4j.kernel.Uniqueness.NODE_PATH;
+import static org.neo4j.kernel.Uniqueness.RELATIONSHIP_PATH;
+
public class TestBidirectionalTraversal extends AbstractTestBase
{
RelationshipType to = withName( "TO" );
@@ -202,10 +202,11 @@ protected boolean includePath( Path path, TraversalBranch startPath, TraversalBr
};
}
};
+
count( bidirectionalTraversal()
- // Just make up a number bigger than the path length (in this case 10) so that we can assert it in the collision policy later
- .mirroredSides( traversal( NODE_PATH ).expand( pathExpanderForTypes( to ), initialState( (Object)0, (Object)10 ) ) )
- .collisionPolicy( collisionPolicy )
- .traverse( getNodeWithName( "a" ), getNodeWithName( "d" ) ) );
+ // Just make up a number bigger than the path length (in this case 10) so that we can assert it in the collision policy later
+ .mirroredSides( traversal( NODE_PATH ).expand( Traversal.<Integer>pathExpanderForTypes( to ), new InitialBranchState.State<Integer>( 0, 10 ) ) )
+ .collisionPolicy( collisionPolicy )
+ .traverse( getNodeWithName( "a" ), getNodeWithName( "d" ) ) );
}
}
View
28 kernel/src/test/java/org/neo4j/kernel/impl/traversal/TestBranchState.java
@@ -19,12 +19,14 @@
*/
package org.neo4j.kernel.impl.traversal;
-import org.junit.Ignore;
import org.junit.Test;
-import org.neo4j.graphdb.*;
-import org.neo4j.graphdb.traversal.AbstractPathEvaluator;
+import org.neo4j.graphdb.Direction;
+import org.neo4j.graphdb.Path;
+import org.neo4j.graphdb.PathExpander;
+import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.traversal.BranchState;
import org.neo4j.graphdb.traversal.Evaluation;
+import org.neo4j.graphdb.traversal.InitialBranchState;
import org.neo4j.graphdb.traversal.PathEvaluator;
import static junit.framework.Assert.assertEquals;
@@ -103,7 +105,7 @@ public void everyOtherDepthAsState() throws Exception
}
}
- @Ignore @Test
+ @Test
public void evaluateState() throws Exception
{
/*
@@ -113,17 +115,8 @@ public void evaluateState() throws Exception
* (e)-5->(f)
*/
createGraph( "a TO b", "b TO c", "c TO d", "a TO e", "e TO f", "f TO c" );
- Transaction tx = beginTx();
- setRelationshipProperty( "a", "b", "weight", 1 );
- setRelationshipProperty( "b", "c", "weight", 2 );
- setRelationshipProperty( "c", "d", "weight", 3 );
- setRelationshipProperty( "a", "e", "weight", 4 );
- setRelationshipProperty( "e", "f", "weight", 5 );
- setRelationshipProperty( "f", "c", "weight", 6 );
- tx.success();
- tx.finish();
-
- PathEvaluator<Integer> evaluator = new AbstractPathEvaluator<Integer>()
+
+ PathEvaluator<Integer> evaluator = new PathEvaluator.Adapter<Integer>()
{
@Override
public Evaluation evaluate( Path path, BranchState<Integer> state )
@@ -132,7 +125,7 @@ public Evaluation evaluate( Path path, BranchState<Integer> state )
}
};
- expectPaths( traversal( NODE_PATH ).expand( new RelationshipWeightExpander(), initialState( 0, 0 ) )
+ expectPaths( traversal( NODE_PATH ).expand( new RelationshipWeightExpander(), new InitialBranchState.State<Integer>( 1, 1 ) )
.evaluator( evaluator ).traverse( getNodeWithName( "a" ) ), "a,b,c" );
}
@@ -141,8 +134,7 @@ public Evaluation evaluate( Path path, BranchState<Integer> state )
@Override
public Iterable<Relationship> expand( Path path, BranchState<Integer> state )
{
- if ( path.length() > 0 )
- state.setState( state.getState() + (Integer)path.lastRelationship().getProperty( "weight" ) );
+ state.setState( state.getState() + 1 );
return path.endNode().getRelationships( OUTGOING );
}
Please sign in to comment.
Something went wrong with that request. Please try again.