Search in sources :

Example 46 with Path

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

the class ProcedureIT method shouldCallProcedureReturningPaths.

@Test
public void shouldCallProcedureReturningPaths() throws Throwable {
    // Given
    try (Transaction ignore = db.beginTx()) {
        Node node1 = db.createNode();
        Node node2 = db.createNode();
        Relationship rel = node1.createRelationshipTo(node2, RelationshipType.withName("KNOWS"));
        // When
        Result res = db.execute("CALL org.neo4j.procedure.nodePaths({node}) YIELD path RETURN path", map("node", node1));
        // Then
        assertTrue(res.hasNext());
        Map<String, Object> value = res.next();
        Path path = (Path) value.get("path");
        assertThat(path.length(), equalTo(1));
        assertThat(path.startNode(), equalTo(node1));
        assertThat(asList(path.relationships()), equalTo(singletonList(rel)));
        assertThat(path.endNode(), equalTo(node2));
        assertFalse(res.hasNext());
    }
}
Also used : Path(org.neo4j.graphdb.Path) Transaction(org.neo4j.graphdb.Transaction) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) Result(org.neo4j.graphdb.Result) Test(org.junit.Test)

Example 47 with Path

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

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 48 with Path

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

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 49 with Path

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

the class AStar method findSinglePath.

public WeightedPath findSinglePath(Node start, Node end) {
    Doer doer = new Doer(start, end);
    while (doer.hasNext()) {
        Node node = doer.next();
        GraphDatabaseService graphDb = node.getGraphDatabase();
        if (node.equals(end)) {
            // Hit, return path
            double weight = doer.score.get(node.getId()).wayLength;
            LinkedList<Relationship> rels = new LinkedList<Relationship>();
            Relationship rel = graphDb.getRelationshipById(doer.cameFrom.get(node.getId()));
            while (rel != null) {
                rels.addFirst(rel);
                node = rel.getOtherNode(node);
                Long nextRelId = doer.cameFrom.get(node.getId());
                rel = nextRelId == null ? null : graphDb.getRelationshipById(nextRelId);
            }
            Path path = toPath(start, rels);
            return new WeightedPathImpl(weight, path);
        }
    }
    return null;
}
Also used : Path(org.neo4j.graphdb.Path) WeightedPath(org.neo4j.graphalgo.WeightedPath) WeightedPathImpl(org.neo4j.graphalgo.impl.util.WeightedPathImpl) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) LinkedList(java.util.LinkedList)

Example 50 with Path

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

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