Search in sources :

Example 71 with Path

use of org.neo4j.graphdb.Path in project neo4j by neo4j.

the class TestExactDepthPathFinder method shouldHandleNeighbouringNodesWhenNotAlone.

@Test
public void shouldHandleNeighbouringNodesWhenNotAlone() {
    // (a) - (b)
    //  |
    // (c)
    graph.makeEdge("a", "b");
    graph.makeEdge("a", "c");
    Node a = graph.getNode("a");
    Node b = graph.getNode("b");
    ExactDepthPathFinder pathFinder = new ExactDepthPathFinder(PathExpanders.allTypesAndDirections(), 1, Integer.MAX_VALUE, false);
    Iterable<Path> allPaths = pathFinder.findAllPaths(a, b);
    assertPaths(new ExactDepthPathFinder(PathExpanders.allTypesAndDirections(), 1, Integer.MAX_VALUE, false).findAllPaths(a, b), "a,b");
    assertPaths(new ExactDepthPathFinder(PathExpanders.allTypesAndDirections(), 1, Integer.MAX_VALUE, false).findAllPaths(a, b), "a,b");
}
Also used : Path(org.neo4j.graphdb.Path) Node(org.neo4j.graphdb.Node) ExactDepthPathFinder(org.neo4j.graphalgo.impl.path.ExactDepthPathFinder) Test(org.junit.Test)

Example 72 with Path

use of org.neo4j.graphdb.Path in project graphdb by neo4j-attic.

the class TreeGraphTest method testDepthFirstTraversalReturnsNodesOnCorrectDepths.

@Test
public void testDepthFirstTraversalReturnsNodesOnCorrectDepths() throws Exception {
    Traverser traverser = Traversal.description().depthFirst().traverse(referenceNode());
    int i = 0;
    for (Path pos : traverser) {
        assertEquals(expectedDepth(i++), pos.length());
    }
    assertEquals(13, i);
}
Also used : Path(org.neo4j.graphdb.Path) Traverser(org.neo4j.graphdb.traversal.Traverser) Test(org.junit.Test)

Example 73 with Path

use of org.neo4j.graphdb.Path in project graphdb by neo4j-attic.

the class TreeGraphTest method pathsIteratorReturnAllNodes.

@Test
public void pathsIteratorReturnAllNodes() throws Exception {
    Traverser traverser = Traversal.description().traverse(referenceNode());
    int count = 0;
    for (Path path : traverser) {
        assertNotNull("returned paths should not be null. path #" + count, path);
        count++;
    }
    assertEquals(13, count);
}
Also used : Path(org.neo4j.graphdb.Path) Traverser(org.neo4j.graphdb.traversal.Traverser) Test(org.junit.Test)

Example 74 with Path

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

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;
}
Also used : Path(org.neo4j.graphdb.Path) Relationship(org.neo4j.graphdb.Relationship)

Example 75 with Path

use of org.neo4j.graphdb.Path 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)

Aggregations

Path (org.neo4j.graphdb.Path)112 Test (org.junit.Test)66 Node (org.neo4j.graphdb.Node)49 Relationship (org.neo4j.graphdb.Relationship)30 WeightedPath (org.neo4j.graphalgo.WeightedPath)20 Transaction (org.neo4j.graphdb.Transaction)18 ArrayList (java.util.ArrayList)15 Traverser (org.neo4j.graphdb.traversal.Traverser)15 TraversalDescription (org.neo4j.graphdb.traversal.TraversalDescription)12 HashSet (java.util.HashSet)9 Evaluator (org.neo4j.graphdb.traversal.Evaluator)9 LinkedList (java.util.LinkedList)6 RelationshipType (org.neo4j.graphdb.RelationshipType)6 TraversalBranch (org.neo4j.graphdb.traversal.TraversalBranch)6 Predicate (org.neo4j.helpers.Predicate)6 HashMap (java.util.HashMap)5 Map (java.util.Map)5 PathFinder (org.neo4j.graphalgo.PathFinder)4 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)4 ExactDepthPathFinder (org.neo4j.graphalgo.impl.path.ExactDepthPathFinder)3