use of org.neo4j.graphdb.PathExpanderBuilder in project neo4j by neo4j.
the class TransactionProvidingApp method toExpander.
protected static PathExpander toExpander(GraphDatabaseService db, Direction defaultDirection, Map<String, Object> relationshipTypes, boolean caseInsensitiveFilters, boolean looseFilters) throws ShellException {
defaultDirection = defaultDirection != null ? defaultDirection : Direction.BOTH;
Map<String, Direction> matches = filterMapToTypes(db, defaultDirection, relationshipTypes, caseInsensitiveFilters, looseFilters);
if (matches == null) {
return PathExpanderBuilder.empty().build();
}
PathExpanderBuilder expander = PathExpanderBuilder.empty();
for (Map.Entry<String, Direction> entry : matches.entrySet()) {
expander = expander.add(RelationshipType.withName(entry.getKey()), entry.getValue());
}
return expander.build();
}
use of org.neo4j.graphdb.PathExpanderBuilder in project neo4j by neo4j.
the class RelationshipExpanderBuilder method describeRelationships.
@SuppressWarnings("unchecked")
public static PathExpander describeRelationships(Map<String, Object> description) {
PathExpanderBuilder expander = PathExpanderBuilder.allTypesAndDirections();
Object relationshipsDescription = description.get("relationships");
if (relationshipsDescription != null) {
Collection<Object> pairDescriptions;
if (relationshipsDescription instanceof Collection) {
pairDescriptions = (Collection<Object>) relationshipsDescription;
} else {
pairDescriptions = Arrays.asList(relationshipsDescription);
}
for (Object pairDescription : pairDescriptions) {
Map map = (Map) pairDescription;
String name = (String) map.get("type");
RelationshipType type = RelationshipType.withName(name);
String directionName = (String) map.get("direction");
expander = (directionName == null) ? expander.add(type) : expander.add(type, stringToEnum(directionName, RelationshipDirection.class, true).internal);
}
}
return expander.build();
}
use of org.neo4j.graphdb.PathExpanderBuilder in project neo4j by neo4j.
the class TraversalDescriptionBuilder method describeExpander.
@SuppressWarnings("unchecked")
private TraversalDescription describeExpander(TraversalDescription result, Map<String, Object> description) {
Object relationshipsDescription = description.get("relationships");
if (relationshipsDescription != null) {
Collection<Object> pairDescriptions;
if (relationshipsDescription instanceof Collection) {
pairDescriptions = (Collection<Object>) relationshipsDescription;
} else {
pairDescriptions = Arrays.asList(relationshipsDescription);
}
PathExpanderBuilder builder = createExpander(description);
for (Object pairDescription : pairDescriptions) {
Map map = (Map) pairDescription;
String name = (String) map.get("type");
RelationshipType type = RelationshipType.withName(name);
String directionName = (String) map.get("direction");
builder = directionName == null ? builder.add(type) : builder.add(type, stringToEnum(directionName, RelationshipDirection.class, true).internal);
}
PathExpander<Object> expander = builder.build();
result = result.expand(expander);
}
return result;
}
use of org.neo4j.graphdb.PathExpanderBuilder in project neo4j-documentation by neo4j.
the class ShortestPath method shortestPath.
@Description("Find the shortest path between two nodes.")
@PluginTarget(Node.class)
public Iterable<Path> shortestPath(@Source Node source, @Description("The node to find the shortest path to.") @Parameter(name = "target") Node target, @Description("The relationship types to follow when searching for the shortest path(s). " + "Order is insignificant, if omitted all types are followed.") @Parameter(name = "types", optional = true) String[] types, @Description("The maximum path length to search for, default value (if omitted) is 4.") @Parameter(name = "depth", optional = true) Integer depth) {
PathExpander<?> expander;
List<Path> paths = new ArrayList<>();
if (types == null) {
expander = PathExpanders.allTypesAndDirections();
} else {
PathExpanderBuilder expanderBuilder = PathExpanderBuilder.empty();
for (int i = 0; i < types.length; i++) {
expanderBuilder = expanderBuilder.add(RelationshipType.withName(types[i]));
}
expander = expanderBuilder.build();
}
try (Transaction tx = source.getGraphDatabase().beginTx()) {
PathFinder<Path> shortestPath = GraphAlgoFactory.shortestPath(expander, depth == null ? 4 : depth.intValue());
for (Path path : shortestPath.findAllPaths(source, target)) {
paths.add(path);
}
tx.success();
}
return paths;
}
use of org.neo4j.graphdb.PathExpanderBuilder in project neo4j by neo4j.
the class TransactionProvidingApp method toSortedExpander.
protected static PathExpander toSortedExpander(GraphDatabaseService db, Direction defaultDirection, Map<String, Object> relationshipTypes, boolean caseInsensitiveFilters, boolean looseFilters) throws ShellException {
defaultDirection = defaultDirection != null ? defaultDirection : Direction.BOTH;
Map<String, Direction> matches = filterMapToTypes(db, defaultDirection, relationshipTypes, caseInsensitiveFilters, looseFilters);
PathExpanderBuilder expander = PathExpanderBuilder.emptyOrderedByType();
for (Map.Entry<String, Direction> entry : matches.entrySet()) {
expander = expander.add(RelationshipType.withName(entry.getKey()), entry.getValue());
}
return expander.build();
}
Aggregations