use of org.neo4j.graphdb.PathExpander in project neo4j by neo4j.
the class RelationshipExpanderBuilderTest method shouldInterpretNoSpecifiedRelationshipsAsAll.
@Test
public void shouldInterpretNoSpecifiedRelationshipsAsAll() throws Exception {
// GIVEN
Node node = createSomeData();
PathExpander expander = RelationshipExpanderBuilder.describeRelationships(map());
// WHEN
Set<Relationship> expanded;
try (Transaction tx = db.beginTx()) {
expanded = asSet(expander.expand(singleNodePath(node), NO_STATE));
tx.success();
}
try (Transaction tx = db.beginTx()) {
// THEN
assertEquals(asSet(node.getRelationships()), expanded);
tx.success();
}
}
use of org.neo4j.graphdb.PathExpander in project neo4j by neo4j.
the class DijkstraBidirectional method traverser.
private Traverser traverser(Node start, final Node end, PathInterest interest) {
final MutableDouble shortestSoFar = new MutableDouble(Double.MAX_VALUE);
final MutableDouble startSideShortest = new MutableDouble(0);
final MutableDouble endSideShortest = new MutableDouble(0);
PathExpander dijkstraExpander = new DijkstraBidirectionalPathExpander(expander, shortestSoFar, true, startSideShortest, endSideShortest, epsilon);
GraphDatabaseService db = start.getGraphDatabase();
TraversalDescription side = db.traversalDescription().expand(dijkstraExpander, stateFactory).order(new DijkstraSelectorFactory(interest, costEvaluator)).evaluator(new DijkstraBidirectionalEvaluator(costEvaluator)).uniqueness(Uniqueness.NODE_PATH);
TraversalDescription startSide = side;
TraversalDescription endSide = side.reverse();
BidirectionalTraversalDescription traversal = db.bidirectionalTraversalDescription().startSide(startSide).endSide(endSide).collisionEvaluator(Evaluators.all()).collisionPolicy(new BranchCollisionPolicy() {
@Override
public BranchCollisionDetector create(Evaluator evaluator, Predicate<Path> pathPredicate) {
return new DijkstraBranchCollisionDetector(evaluator, costEvaluator, shortestSoFar, epsilon, pathPredicate);
}
});
lastTraverser = traversal.traverse(start, end);
return lastTraverser;
}
use of org.neo4j.graphdb.PathExpander in project neo4j by neo4j.
the class TestAStar method canUseBranchState.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void canUseBranchState() throws Exception {
// This test doesn't use the predefined finder, which only means an unnecessary instantiation
// if such an object. And this test will be run twice (once for each finder type in data()).
/**
* <pre>
* 012345 A - B: 2
* +------>y A - B: 2
* 0|A B - C: 3
* 1| A - C: 10
* 2| B
* 3|
* 4|
* 5|
* 6|
* 7|C
* V
* x
*
* </pre>
*/
Node nodeA = graph.makeNode("A", "x", 0d, "y", 0d);
Node nodeB = graph.makeNode("B", "x", 2d, "y", 1d);
Node nodeC = graph.makeNode("C", "x", 7d, "y", 0d);
graph.makeEdge("A", "B", "length", 2d);
graph.makeEdge("A", "B", "length", 2d);
graph.makeEdge("B", "C", "length", 3d);
graph.makeEdge("A", "C", "length", 10d);
final Map<Node, Double> seenBranchStates = new HashMap<Node, Double>();
PathExpander<Double> expander = new PathExpander<Double>() {
@Override
public Iterable<Relationship> expand(Path path, BranchState<Double> state) {
double newState = state.getState();
if (path.length() > 0) {
newState += (Double) path.lastRelationship().getProperty("length");
state.setState(newState);
}
seenBranchStates.put(path.endNode(), newState);
return path.endNode().getRelationships(OUTGOING);
}
@Override
public PathExpander<Double> reverse() {
throw new UnsupportedOperationException();
}
};
double initialStateValue = 0D;
PathFinder<WeightedPath> traversalFinder = new TraversalAStar(expander, new InitialBranchState.State(initialStateValue, initialStateValue), doubleCostEvaluator("length"), ESTIMATE_EVALUATOR);
WeightedPath path = traversalFinder.findSinglePath(nodeA, nodeC);
assertEquals((Double) 5.0D, (Double) path.weight());
assertPathDef(path, "A", "B", "C");
assertEquals(MapUtil.<Node, Double>genericMap(nodeA, 0D, nodeB, 2D), seenBranchStates);
}
use of org.neo4j.graphdb.PathExpander in project neo4j by neo4j.
the class PathShellApp method exec.
@Override
protected Continuation exec(AppCommandParser parser, Session session, Output out) throws Exception {
String fromString = parser.options().get("from");
String toString = parser.argument(0, "Must supply a 'to' node as first argument");
String algo = parser.options().get("a");
String maxDepthString = parser.options().get("m");
boolean quietPrint = parser.options().containsKey("q");
boolean caseInsensitiveFilters = parser.options().containsKey("i");
boolean looseFilters = parser.options().containsKey("l");
int maxDepth = maxDepthString != null ? Integer.parseInt(maxDepthString) : Integer.MAX_VALUE;
fromString = fromString != null ? fromString : String.valueOf(this.getCurrent(session).getId());
Map<String, Object> filter = parseFilter(parser.options().get("f"), out);
PathExpander expander = toExpander(getServer().getDb(), Direction.BOTH, filter, caseInsensitiveFilters, looseFilters);
PathFinder<Path> finder = expander != null ? getPathFinder(algo, expander, maxDepth, out) : null;
if (finder != null) {
Node fromNode = getNodeById(Long.parseLong(fromString));
Node toNode = getNodeById(Long.parseLong(toString));
boolean single = parser.options().containsKey("s");
Iterable<Path> paths = single ? Arrays.asList(finder.findSinglePath(fromNode, toNode)) : finder.findAllPaths(fromNode, toNode);
for (Path path : paths) {
printPath(path, quietPrint, session, out);
}
}
return Continuation.INPUT_COMPLETE;
}
use of org.neo4j.graphdb.PathExpander in project neo4j by neo4j.
the class AncestorTestCase method test.
@Test
@Graph({ "root contains child1", "child1 contains child11", "child1 contains child12", "root contains child2", "child12 contains child121", "child1 contains child13" })
public void test() {
PathExpander expander = PathExpanders.forTypeAndDirection(Rels.contains, Direction.INCOMING);
List<Node> nodeSet = new ArrayList<Node>();
Map<String, Node> graph = data.get();
nodeSet.add(graph.get("child1"));
nodeSet.add(graph.get("root"));
try (Transaction transaction = gdb.beginTx()) {
Node ancestor = AncestorsUtil.lowestCommonAncestor(nodeSet, expander);
assertEquals(graph.get("root"), ancestor);
nodeSet.clear();
nodeSet.add(graph.get("child12"));
nodeSet.add(graph.get("child11"));
ancestor = AncestorsUtil.lowestCommonAncestor(nodeSet, expander);
assertEquals(graph.get("child1"), ancestor);
nodeSet.clear();
nodeSet.add(graph.get("child121"));
nodeSet.add(graph.get("child12"));
ancestor = AncestorsUtil.lowestCommonAncestor(nodeSet, expander);
assertEquals(graph.get("child12"), ancestor);
nodeSet.clear();
nodeSet.add(graph.get("child11"));
nodeSet.add(graph.get("child13"));
ancestor = AncestorsUtil.lowestCommonAncestor(nodeSet, expander);
assertEquals(graph.get("child1"), ancestor);
nodeSet.clear();
nodeSet.add(graph.get("child2"));
nodeSet.add(graph.get("child121"));
ancestor = AncestorsUtil.lowestCommonAncestor(nodeSet, expander);
assertEquals(graph.get("root"), ancestor);
nodeSet.clear();
nodeSet.add(graph.get("child11"));
nodeSet.add(graph.get("child12"));
nodeSet.add(graph.get("child13"));
ancestor = AncestorsUtil.lowestCommonAncestor(nodeSet, expander);
assertEquals(graph.get("child1"), ancestor);
nodeSet.clear();
nodeSet.add(graph.get("child11"));
nodeSet.add(graph.get("child12"));
nodeSet.add(graph.get("child13"));
nodeSet.add(graph.get("child121"));
ancestor = AncestorsUtil.lowestCommonAncestor(nodeSet, expander);
assertEquals(graph.get("child1"), ancestor);
nodeSet.clear();
nodeSet.add(graph.get("child11"));
nodeSet.add(graph.get("child12"));
nodeSet.add(graph.get("child13"));
nodeSet.add(graph.get("child121"));
nodeSet.add(graph.get("child2"));
ancestor = AncestorsUtil.lowestCommonAncestor(nodeSet, expander);
assertEquals(graph.get("root"), ancestor);
nodeSet.clear();
nodeSet.add(graph.get("child11"));
nodeSet.add(graph.get("child12"));
nodeSet.add(graph.get("child13"));
nodeSet.add(graph.get("child121"));
nodeSet.add(graph.get("child12"));
nodeSet.add(graph.get("root"));
ancestor = AncestorsUtil.lowestCommonAncestor(nodeSet, expander);
assertEquals(graph.get("root"), ancestor);
}
}
Aggregations