use of org.neo4j.graphdb.Path in project neo4j by neo4j.
the class ProcedureIT method shouldCallProcedureReturningPaths.
@Test
public void shouldCallProcedureReturningPaths() throws Throwable {
// Given
try (Transaction ignore = db.beginTx()) {
Node node1 = db.createNode();
Node node2 = db.createNode();
Relationship rel = node1.createRelationshipTo(node2, RelationshipType.withName("KNOWS"));
// When
Result res = db.execute("CALL org.neo4j.procedure.nodePaths({node}) YIELD path RETURN path", map("node", node1));
// Then
assertTrue(res.hasNext());
Map<String, Object> value = res.next();
Path path = (Path) value.get("path");
assertThat(path.length(), equalTo(1));
assertThat(path.startNode(), equalTo(node1));
assertThat(asList(path.relationships()), equalTo(singletonList(rel)));
assertThat(path.endNode(), equalTo(node2));
assertFalse(res.hasNext());
}
}
use of org.neo4j.graphdb.Path in project graphdb by neo4j-attic.
the class TraversalPath method equals.
@Override
public boolean equals(Object obj) {
ensureEntitiesAreGathered();
if (this == obj) {
return true;
} else if (obj instanceof TraversalPath) {
TraversalPath other = (TraversalPath) obj;
return startNode().equals(other.startNode()) && relationships.equals(other.relationships);
} else if (obj instanceof Path) {
Path other = (Path) obj;
if (startNode().equals(other.startNode())) {
Iterator<Relationship> these = relationships().iterator();
Iterator<Relationship> those = other.relationships().iterator();
while (these.hasNext() && those.hasNext()) {
if (!these.next().equals(those.next())) {
return false;
}
}
if (these.hasNext() || those.hasNext()) {
return false;
}
return true;
}
}
return false;
}
use of org.neo4j.graphdb.Path in project neo4j-mobile-android by neo4j-contrib.
the class Traversal method combineSourcePaths.
/**
* Combines two {@link TraversalBranch}s with a common
* {@link TraversalBranch#node() head node} in order to obtain an
* {@link TraversalBranch} representing a path from the start node of the
* <code>source</code> {@link TraversalBranch} to the start node of the
* <code>target</code> {@link TraversalBranch}. The resulting
* {@link TraversalBranch} will not {@link TraversalBranch#next() expand
* further}, and does not provide a {@link TraversalBranch#parent() parent}
* {@link TraversalBranch}.
*
* @param source the {@link TraversalBranch} where the resulting path starts
* @param target the {@link TraversalBranch} where the resulting path ends
* @throws IllegalArgumentException if the {@link TraversalBranch#node()
* head nodes} of the supplied {@link TraversalBranch}s does not
* match
* @return an {@link TraversalBranch} that represents the path from the
* start node of the <code>source</code> {@link TraversalBranch} to
* the start node of the <code>target</code> {@link TraversalBranch}
*/
public static TraversalBranch combineSourcePaths(TraversalBranch source, TraversalBranch target) {
if (!source.node().equals(target.node())) {
throw new IllegalArgumentException("The nodes of the head and tail must match");
}
Path headPath = source.position(), tailPath = target.position();
Relationship[] relationships = new Relationship[headPath.length() + tailPath.length()];
Iterator<Relationship> iter = headPath.relationships().iterator();
for (int i = 0; iter.hasNext(); i++) {
relationships[i] = iter.next();
}
iter = tailPath.relationships().iterator();
for (int i = relationships.length - 1; iter.hasNext(); i--) {
relationships[i] = iter.next();
}
return new FinalTraversalBranch(tailPath.startNode(), relationships);
}
use of org.neo4j.graphdb.Path in project neo4j-mobile-android by neo4j-contrib.
the class AStar method findSinglePath.
public WeightedPath findSinglePath(Node start, Node end) {
Doer doer = new Doer(start, end);
while (doer.hasNext()) {
Node node = doer.next();
GraphDatabaseService graphDb = node.getGraphDatabase();
if (node.equals(end)) {
// Hit, return path
double weight = doer.score.get(node.getId()).wayLength;
LinkedList<Relationship> rels = new LinkedList<Relationship>();
Relationship rel = graphDb.getRelationshipById(doer.cameFrom.get(node.getId()));
while (rel != null) {
rels.addFirst(rel);
node = rel.getOtherNode(node);
Long nextRelId = doer.cameFrom.get(node.getId());
rel = nextRelId == null ? null : graphDb.getRelationshipById(nextRelId);
}
Path path = toPath(start, rels);
return new WeightedPathImpl(weight, path);
}
}
return null;
}
use of org.neo4j.graphdb.Path in project neo4j-mobile-android by neo4j-contrib.
the class ExactDepthPathFinder method paths.
private Iterator<Path> paths(final Node start, final Node end) {
TraversalDescription base = Traversal.description().uniqueness(Uniqueness.RELATIONSHIP_PATH).order(new BranchOrderingPolicy() {
public BranchSelector create(TraversalBranch startSource) {
return new LiteDepthFirstSelector(startSource, startThreshold);
}
});
final int firstHalf = onDepth / 2;
Traverser startTraverser = base.prune(Traversal.pruneAfterDepth(firstHalf)).expand(expander).filter(new Predicate<Path>() {
public boolean accept(Path item) {
return item.length() == firstHalf;
}
}).traverse(start);
final int secondHalf = onDepth - firstHalf;
Traverser endTraverser = base.prune(Traversal.pruneAfterDepth(secondHalf)).expand(expander.reversed()).filter(new Predicate<Path>() {
public boolean accept(Path item) {
return item.length() == secondHalf;
}
}).traverse(end);
final Iterator<Path> startIterator = startTraverser.iterator();
final Iterator<Path> endIterator = endTraverser.iterator();
final Map<Node, Visit> visits = new HashMap<Node, Visit>();
return new PrefetchingIterator<Path>() {
@Override
protected Path fetchNextOrNull() {
Path[] found = null;
while (found == null && (startIterator.hasNext() || endIterator.hasNext())) {
found = goOneStep(start, startIterator, visits);
if (found == null) {
found = goOneStep(end, endIterator, visits);
}
}
return found != null ? toPath(found, start) : null;
}
};
}
Aggregations