Search in sources :

Example 91 with Path

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

the class TestMultipleFilters method testNarrowingFilters.

@Test
public void testNarrowingFilters() {
    Evaluator mustBeConnectedToK = new MustBeConnectedToNodeFilter(getNodeWithName("k"));
    Evaluator mustNotHaveMoreThanTwoOutRels = new Evaluator() {

        public Evaluation evaluate(Path path) {
            return Evaluation.ofIncludes(IteratorUtil.count(path.endNode().getRelationships(Direction.OUTGOING)) <= 2);
        }
    };
    TraversalDescription description = Traversal.description().evaluator(mustBeConnectedToK);
    expectNodes(description.traverse(referenceNode()), "b", "c");
    expectNodes(description.evaluator(mustNotHaveMoreThanTwoOutRels).traverse(referenceNode()), "c");
}
Also used : Path(org.neo4j.graphdb.Path) TraversalDescription(org.neo4j.graphdb.traversal.TraversalDescription) Evaluator(org.neo4j.graphdb.traversal.Evaluator) Test(org.junit.Test)

Example 92 with Path

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

the class TestPath method testPathIterator.

@Test
public void testPathIterator() {
    Path path = Traversal.description().evaluator(Evaluators.atDepth(4)).traverse(referenceNode()).iterator().next();
    assertPathIsCorrect(path);
}
Also used : Path(org.neo4j.graphdb.Path) Test(org.junit.Test)

Example 93 with Path

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

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

Example 94 with Path

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

the class CircularGraphTest method testCircularBug.

@Test
public void testCircularBug() {
    final long timestamp = 3;
    try (Transaction tx = beginTx()) {
        getNodeWithName("2").setProperty("timestamp", 1L);
        getNodeWithName("3").setProperty("timestamp", 2L);
        tx.success();
    }
    try (Transaction tx2 = beginTx()) {
        final RelationshipType type = RelationshipType.withName("TO");
        Iterator<Node> nodes = getGraphDb().traversalDescription().depthFirst().relationships(type, Direction.OUTGOING).evaluator(path -> {
            Relationship rel = path.lastRelationship();
            boolean relIsOfType = rel != null && rel.isType(type);
            boolean prune = relIsOfType && (Long) path.endNode().getProperty("timestamp") >= timestamp;
            return Evaluation.of(relIsOfType, !prune);
        }).traverse(node("1")).nodes().iterator();
        assertEquals("2", nodes.next().getProperty("name"));
        assertEquals("3", nodes.next().getProperty("name"));
        assertFalse(nodes.hasNext());
    }
}
Also used : Evaluator(org.neo4j.graphdb.traversal.Evaluator) Iterator(java.util.Iterator) Direction(org.neo4j.graphdb.Direction) Test(org.junit.Test) Node(org.neo4j.graphdb.Node) Evaluation(org.neo4j.graphdb.traversal.Evaluation) Path(org.neo4j.graphdb.Path) Relationship(org.neo4j.graphdb.Relationship) Assert.assertFalse(org.junit.Assert.assertFalse) RelationshipType(org.neo4j.graphdb.RelationshipType) Transaction(org.neo4j.graphdb.Transaction) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) Transaction(org.neo4j.graphdb.Transaction) Relationship(org.neo4j.graphdb.Relationship) Node(org.neo4j.graphdb.Node) RelationshipType(org.neo4j.graphdb.RelationshipType) Test(org.junit.Test)

Example 95 with Path

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

the class TestBidirectionalTraversal method ensureCorrectPathEntitiesInShortPath.

@Test
public void ensureCorrectPathEntitiesInShortPath() throws Exception {
    /*
         * (a)-->(b)
         */
    createGraph("a TO b");
    Node a = getNodeWithName("a");
    Node b = getNodeWithName("b");
    Relationship r = a.getSingleRelationship(to, OUTGOING);
    Path path = Iterables.single(getGraphDb().bidirectionalTraversalDescription().mirroredSides(getGraphDb().traversalDescription().relationships(to, OUTGOING).uniqueness(NODE_PATH)).collisionEvaluator(Evaluators.atDepth(1)).sideSelector(SideSelectorPolicies.LEVEL, 1).traverse(a, b));
    assertContainsInOrder(path.nodes(), a, b);
    assertContainsInOrder(path.reverseNodes(), b, a);
    assertContainsInOrder(path.relationships(), r);
    assertContainsInOrder(path.reverseRelationships(), r);
    assertContainsInOrder(path, a, r, b);
    assertEquals(a, path.startNode());
    assertEquals(b, path.endNode());
    assertEquals(r, path.lastRelationship());
}
Also used : Path(org.neo4j.graphdb.Path) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) Test(org.junit.Test)

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