Search in sources :

Example 1 with PathExpanderBuilder

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();
}
Also used : ShellException.stackTraceAsString(org.neo4j.shell.ShellException.stackTraceAsString) PathExpanderBuilder(org.neo4j.graphdb.PathExpanderBuilder) Direction(org.neo4j.graphdb.Direction) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 2 with PathExpanderBuilder

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();
}
Also used : RelationshipType(org.neo4j.graphdb.RelationshipType) Collection(java.util.Collection) PathExpanderBuilder(org.neo4j.graphdb.PathExpanderBuilder) Map(java.util.Map)

Example 3 with PathExpanderBuilder

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;
}
Also used : RelationshipType(org.neo4j.graphdb.RelationshipType) Collection(java.util.Collection) PathExpanderBuilder(org.neo4j.graphdb.PathExpanderBuilder) Map(java.util.Map)

Example 4 with PathExpanderBuilder

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;
}
Also used : Path(org.neo4j.graphdb.Path) Transaction(org.neo4j.graphdb.Transaction) ArrayList(java.util.ArrayList) PathExpanderBuilder(org.neo4j.graphdb.PathExpanderBuilder) PluginTarget(org.neo4j.server.plugins.PluginTarget) Description(org.neo4j.server.plugins.Description)

Example 5 with PathExpanderBuilder

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();
}
Also used : ShellException.stackTraceAsString(org.neo4j.shell.ShellException.stackTraceAsString) PathExpanderBuilder(org.neo4j.graphdb.PathExpanderBuilder) Direction(org.neo4j.graphdb.Direction) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Aggregations

PathExpanderBuilder (org.neo4j.graphdb.PathExpanderBuilder)6 Map (java.util.Map)4 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 TreeMap (java.util.TreeMap)2 Direction (org.neo4j.graphdb.Direction)2 RelationshipType (org.neo4j.graphdb.RelationshipType)2 ShellException.stackTraceAsString (org.neo4j.shell.ShellException.stackTraceAsString)2 ArrayList (java.util.ArrayList)1 Node (org.neo4j.graphdb.Node)1 Path (org.neo4j.graphdb.Path)1 PathExpander (org.neo4j.graphdb.PathExpander)1 Transaction (org.neo4j.graphdb.Transaction)1 Description (org.neo4j.server.plugins.Description)1 PluginTarget (org.neo4j.server.plugins.PluginTarget)1