Search in sources :

Example 66 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 67 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 68 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 69 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 70 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)

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