use of org.neo4j.graphdb.PathExpander in project neo4j by neo4j.
the class DatabaseActions method getNodeRelationships.
@SuppressWarnings("unchecked")
public ListRepresentation getNodeRelationships(long nodeId, RelationshipDirection direction, Collection<String> types) throws NodeNotFoundException {
Node node = node(nodeId);
PathExpander expander;
if (types.isEmpty()) {
expander = PathExpanders.forDirection(direction.internal);
} else {
PathExpanderBuilder builder = PathExpanderBuilder.empty();
for (String type : types) {
builder = builder.add(RelationshipType.withName(type), direction.internal);
}
expander = builder.build();
}
return RelationshipRepresentation.list(expander.expand(Paths.singleNodePath(node), BranchState.NO_STATE));
}
use of org.neo4j.graphdb.PathExpander in project neo4j by neo4j.
the class RelationshipExpanderBuilderTest method shouldInterpretSomeSpecifiedRelationships.
@Test
public void shouldInterpretSomeSpecifiedRelationships() throws Exception {
// GIVEN
Node node = createSomeData();
PathExpander expander = RelationshipExpanderBuilder.describeRelationships(map("relationships", map("type", MyRelTypes.TEST.name(), "direction", RelationshipDirection.out.name())));
// 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(MyRelTypes.TEST)), expanded);
tx.success();
}
}
use of org.neo4j.graphdb.PathExpander in project neo4j by neo4j.
the class TestOrderByTypeExpander method evenDifferentDirectionsKeepsOrder.
@Test
void evenDifferentDirectionsKeepsOrder() {
PathExpander expander = new OrderedByTypeExpander().add(next, INCOMING).add(firstComment).add(comment).add(next, OUTGOING);
try (Transaction transaction = beginTx()) {
Iterator<Node> itr = transaction.traversalDescription().depthFirst().expand(expander).traverse(transaction.getNodeById(node("A2").getId())).nodes().iterator();
assertOrder(transaction, itr, "A2", "A1", "C1", "C2", "C3", "C4", "C5", "C6", "A3", "C7", "C8", "C9");
}
}
use of org.neo4j.graphdb.PathExpander in project neo4j by neo4j.
the class TestOrderByTypeExpander method makeSureNodesAreTraversedInCorrectOrder.
@Test
void makeSureNodesAreTraversedInCorrectOrder() {
try (Transaction transaction = beginTx()) {
PathExpander expander = new OrderedByTypeExpander().add(firstComment).add(comment).add(next);
Iterator<Node> itr = transaction.traversalDescription().depthFirst().expand(expander).traverse(transaction.getNodeById(node("A1").getId())).nodes().iterator();
assertOrder(transaction, itr, "A1", "C1", "C2", "C3", "A2", "C4", "C5", "C6", "A3", "C7", "C8", "C9");
expander = new OrderedByTypeExpander().add(next).add(firstComment).add(comment);
itr = transaction.traversalDescription().depthFirst().expand(expander).traverse(transaction.getNodeById(node("A1").getId())).nodes().iterator();
assertOrder(transaction, itr, "A1", "A2", "A3", "C7", "C8", "C9", "C4", "C5", "C6", "C1", "C2", "C3");
}
}
use of org.neo4j.graphdb.PathExpander in project neo4j by neo4j.
the class TestAStar method canUseBranchState.
@SuppressWarnings({ "rawtypes", "unchecked" })
@ParameterizedTest
@MethodSource("params")
void canUseBranchState(Function<EvaluationContext, PathFinder<WeightedPath>> finderFactory) {
/**
* <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>
*/
try (Transaction transaction = graphDb.beginTx()) {
Node nodeA = graph.makeNode(transaction, "A", "x", 0d, "y", 0d);
Node nodeB = graph.makeNode(transaction, "B", "x", 2d, "y", 1d);
Node nodeC = graph.makeNode(transaction, "C", "x", 7d, "y", 0d);
graph.makeEdge(transaction, "A", "B", "length", 2d);
graph.makeEdge(transaction, "A", "B", "length", 2d);
graph.makeEdge(transaction, "B", "C", "length", 3d);
graph.makeEdge(transaction, "A", "C", "length", 10d);
final Map<Node, Double> seenBranchStates = new HashMap<>();
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;
var context = new BasicEvaluationContext(transaction, graphDb);
PathFinder<WeightedPath> traversalFinder = new TraversalAStar<>(context, 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);
transaction.commit();
}
}
Aggregations