Search in sources :

Example 21 with TraversalDescription

use of org.neo4j.graphdb.traversal.TraversalDescription in project graphdb by neo4j-attic.

the class OldTraverserWrapper method traverse.

public static org.neo4j.graphdb.Traverser traverse(Node node, Order traversalOrder, StopEvaluator stopEvaluator, ReturnableEvaluator returnableEvaluator, Object... relationshipTypesAndDirections) {
    assertNotNull(traversalOrder, "order");
    assertNotNull(stopEvaluator, "stop evaluator");
    assertNotNull(returnableEvaluator, "returnable evaluator");
    if (relationshipTypesAndDirections.length % 2 != 0 || relationshipTypesAndDirections.length == 0) {
        throw new IllegalArgumentException();
    }
    TraverserImpl result = new TraverserImpl();
    TraversalDescription description = traversal(result, traversalOrder, stopEvaluator, returnableEvaluator);
    description = description.expand(toExpander(relationshipTypesAndDirections));
    result.iter = description.traverse(node).iterator();
    return result;
}
Also used : TraversalDescription(org.neo4j.graphdb.traversal.TraversalDescription)

Example 22 with TraversalDescription

use of org.neo4j.graphdb.traversal.TraversalDescription 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;
        }
    };
}
Also used : Path(org.neo4j.graphdb.Path) PrefetchingIterator(org.neo4j.helpers.collection.PrefetchingIterator) BranchOrderingPolicy(org.neo4j.graphdb.traversal.BranchOrderingPolicy) HashMap(java.util.HashMap) Node(org.neo4j.graphdb.Node) BranchSelector(org.neo4j.graphdb.traversal.BranchSelector) Predicate(org.neo4j.helpers.Predicate) Traverser(org.neo4j.graphdb.traversal.Traverser) TraversalDescription(org.neo4j.graphdb.traversal.TraversalDescription) LiteDepthFirstSelector(org.neo4j.graphalgo.impl.util.LiteDepthFirstSelector) TraversalBranch(org.neo4j.graphdb.traversal.TraversalBranch)

Example 23 with TraversalDescription

use of org.neo4j.graphdb.traversal.TraversalDescription in project neo4j-mobile-android by neo4j-contrib.

the class OldTraverserWrapper method traversal.

private static TraversalDescription traversal(TraverserImpl traverser, Order order, StopEvaluator stopEvaluator, ReturnableEvaluator returnableEvaluator) {
    TraversalDescription description = BASE_DESCRIPTION;
    switch(order) {
        case BREADTH_FIRST:
            description = description.breadthFirst();
            break;
        case DEPTH_FIRST:
            description = description.depthFirst();
            break;
        default:
            throw new IllegalArgumentException("Onsupported traversal order: " + order);
    }
    description = description.prune(new Pruner(traverser, stopEvaluator));
    description = description.filter(new Filter(traverser, returnableEvaluator));
    return description;
}
Also used : TraversalDescription(org.neo4j.graphdb.traversal.TraversalDescription)

Example 24 with TraversalDescription

use of org.neo4j.graphdb.traversal.TraversalDescription in project neo4j by neo4j.

the class Trav method exec.

@Override
protected Continuation exec(AppCommandParser parser, Session session, Output out) throws ShellException, RemoteException {
    assertCurrentIsNode(session);
    Node node = this.getCurrent(session).asNode();
    boolean caseInsensitiveFilters = parser.options().containsKey("i");
    boolean looseFilters = parser.options().containsKey("l");
    boolean quiet = parser.options().containsKey("q");
    // Order
    TraversalDescription description = getServer().getDb().traversalDescription();
    String order = parser.options().get("o");
    if (order != null) {
        description = description.order(parseOrder(order));
    }
    // Relationship types / expander
    String relationshipTypes = parser.options().get("r");
    if (relationshipTypes != null) {
        Map<String, Object> types = parseFilter(relationshipTypes, out);
        description = description.expand(toExpander(getServer().getDb(), null, types, caseInsensitiveFilters, looseFilters));
    }
    // Uniqueness
    String uniqueness = parser.options().get("u");
    if (uniqueness != null) {
        description = description.uniqueness(parseUniqueness(uniqueness));
    }
    // Depth limit
    String depthLimit = parser.options().get("d");
    if (depthLimit != null) {
        description = description.evaluator(toDepth(parseInt(depthLimit)));
    }
    String filterString = parser.options().get("f");
    Map<String, Object> filterMap = filterString != null ? parseFilter(filterString, out) : null;
    String commandToRun = parser.options().get("c");
    Collection<String> commandsToRun = new ArrayList<>();
    if (commandToRun != null) {
        commandsToRun.addAll(Arrays.asList(commandToRun.split(Pattern.quote("&&"))));
    }
    for (Path path : description.traverse(node)) {
        boolean hit = false;
        if (filterMap == null) {
            hit = true;
        } else {
            Node endNode = path.endNode();
            Map<String, Boolean> matchPerFilterKey = new HashMap<>();
            for (String key : endNode.getPropertyKeys()) {
                for (Map.Entry<String, Object> filterEntry : filterMap.entrySet()) {
                    String filterKey = filterEntry.getKey();
                    if (matchPerFilterKey.containsKey(filterKey)) {
                        continue;
                    }
                    if (matches(newPattern(filterKey, caseInsensitiveFilters), key, caseInsensitiveFilters, looseFilters)) {
                        Object value = endNode.getProperty(key);
                        String filterPattern = filterEntry.getValue() != null ? filterEntry.getValue().toString() : null;
                        if (matches(newPattern(filterPattern, caseInsensitiveFilters), value.toString(), caseInsensitiveFilters, looseFilters)) {
                            matchPerFilterKey.put(filterKey, true);
                        }
                    }
                }
            }
            if (matchPerFilterKey.size() == filterMap.size()) {
                hit = true;
            }
        }
        if (hit) {
            if (commandsToRun.isEmpty()) {
                printPath(path, quiet, session, out);
            } else {
                printAndInterpretTemplateLines(commandsToRun, false, true, NodeOrRelationship.wrap(path.endNode()), getServer(), session, out);
            }
        }
    }
    return Continuation.INPUT_COMPLETE;
}
Also used : Path(org.neo4j.graphdb.Path) HashMap(java.util.HashMap) Node(org.neo4j.graphdb.Node) ArrayList(java.util.ArrayList) TraversalDescription(org.neo4j.graphdb.traversal.TraversalDescription) HashMap(java.util.HashMap) Map(java.util.Map)

Example 25 with TraversalDescription

use of org.neo4j.graphdb.traversal.TraversalDescription in project graphdb by neo4j-attic.

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;
        }
    };
}
Also used : Path(org.neo4j.graphdb.Path) PrefetchingIterator(org.neo4j.helpers.collection.PrefetchingIterator) BranchOrderingPolicy(org.neo4j.graphdb.traversal.BranchOrderingPolicy) HashMap(java.util.HashMap) Node(org.neo4j.graphdb.Node) BranchSelector(org.neo4j.graphdb.traversal.BranchSelector) Predicate(org.neo4j.helpers.Predicate) Traverser(org.neo4j.graphdb.traversal.Traverser) TraversalDescription(org.neo4j.graphdb.traversal.TraversalDescription) LiteDepthFirstSelector(org.neo4j.graphalgo.impl.util.LiteDepthFirstSelector) TraversalBranch(org.neo4j.graphdb.traversal.TraversalBranch)

Aggregations

TraversalDescription (org.neo4j.graphdb.traversal.TraversalDescription)37 Test (org.junit.Test)20 Node (org.neo4j.graphdb.Node)12 Path (org.neo4j.graphdb.Path)12 Transaction (org.neo4j.graphdb.Transaction)7 Evaluator (org.neo4j.graphdb.traversal.Evaluator)6 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)5 HashMap (java.util.HashMap)3 WeightedPath (org.neo4j.graphalgo.WeightedPath)3 LiteDepthFirstSelector (org.neo4j.graphalgo.impl.util.LiteDepthFirstSelector)3 BidirectionalTraversalDescription (org.neo4j.graphdb.traversal.BidirectionalTraversalDescription)3 BranchOrderingPolicy (org.neo4j.graphdb.traversal.BranchOrderingPolicy)3 BranchSelector (org.neo4j.graphdb.traversal.BranchSelector)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Direction (org.neo4j.graphdb.Direction)2 PathExpander (org.neo4j.graphdb.PathExpander)2 Relationship (org.neo4j.graphdb.Relationship)2 RelationshipType (org.neo4j.graphdb.RelationshipType)2 TraversalBranch (org.neo4j.graphdb.traversal.TraversalBranch)2