Search in sources :

Example 56 with Path

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

the class ValuePathTest method canConstructPathWithLengthZero.

@Test
public void canConstructPathWithLengthZero() {
    // Given A
    Path path = PATH_WITH_LENGTH_ZERO;
    // Then
    assertThat(path.length(), equalTo(0));
    assertThat(path.startNode(), equalTo(ALICE));
    assertThat(path.endNode(), equalTo(ALICE));
    assertThat(nodes(path), equalTo(nodes(ALICE)));
    assertThat(path.lastRelationship(), nullValue());
    assertThat(relationships(path), equalTo(relationships()));
}
Also used : Path(org.neo4j.graphdb.Path) Test(org.junit.Test)

Example 57 with Path

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

the class ExportTest method testFromPathCypherResult.

@Test
public void testFromPathCypherResult() throws Exception {
    Node n1 = gdb.createNode();
    Node n2 = gdb.createNode();
    final Relationship rel = n1.createRelationshipTo(n2, RelationshipType.withName("REL"));
    final Path path = new PathImpl.Builder(n1).push(rel).build();
    final ExecutionResult result = result("path", path);
    final SubGraph graph = CypherResultSubGraph.from(result, gdb, true);
    assertEquals("create (_0)" + lineSeparator() + "create (_1)" + lineSeparator() + "create (_0)-[:`REL`]->(_1)" + lineSeparator() + ";" + lineSeparator(), doExportGraph(graph));
}
Also used : Path(org.neo4j.graphdb.Path) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) ExecutionResult(org.neo4j.cypher.internal.javacompat.ExecutionResult) Test(org.junit.Test)

Example 58 with Path

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

the class AStar method findSinglePath.

@Override
public WeightedPath findSinglePath(Node start, Node end) {
    lastMetadata = new Metadata();
    AStarIterator iterator = new AStarIterator(start, end);
    while (iterator.hasNext()) {
        Node node = iterator.next();
        GraphDatabaseService graphDb = node.getGraphDatabase();
        if (node.equals(end)) {
            // Hit, return path
            double weight = iterator.visitData.get(node.getId()).wayLength;
            final Path path;
            if (start.getId() == end.getId()) {
                // Nothing to iterate over
                path = PathImpl.singular(start);
            } else {
                LinkedList<Relationship> rels = new LinkedList<Relationship>();
                Relationship rel = graphDb.getRelationshipById(iterator.visitData.get(node.getId()).cameFromRelationship);
                while (rel != null) {
                    rels.addFirst(rel);
                    node = rel.getOtherNode(node);
                    long nextRelId = iterator.visitData.get(node.getId()).cameFromRelationship;
                    rel = nextRelId == -1 ? null : graphDb.getRelationshipById(nextRelId);
                }
                path = toPath(start, rels);
            }
            lastMetadata.paths++;
            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) TraversalMetadata(org.neo4j.graphdb.traversal.TraversalMetadata) LinkedList(java.util.LinkedList)

Example 59 with Path

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

the class ShortestPath method hitToPaths.

private static Collection<Path> hitToPaths(Hit hit, Node start, Node end, boolean stopAsap) {
    Collection<Path> paths = new ArrayList<>();
    Iterable<LinkedList<Relationship>> startPaths = getPaths(hit.connectingNode, hit.start, stopAsap);
    Iterable<LinkedList<Relationship>> endPaths = getPaths(hit.connectingNode, hit.end, stopAsap);
    for (LinkedList<Relationship> startPath : startPaths) {
        PathImpl.Builder startBuilder = toBuilder(start, startPath);
        for (LinkedList<Relationship> endPath : endPaths) {
            PathImpl.Builder endBuilder = toBuilder(end, endPath);
            Path path = startBuilder.build(endBuilder);
            paths.add(path);
        }
    }
    return paths;
}
Also used : Path(org.neo4j.graphdb.Path) Relationship(org.neo4j.graphdb.Relationship) ArrayList(java.util.ArrayList) PathImpl(org.neo4j.graphalgo.impl.util.PathImpl) LinkedList(java.util.LinkedList) Builder(org.neo4j.graphalgo.impl.util.PathImpl.Builder)

Example 60 with Path

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

the class TestExactDepthPathFinder method testSingle.

@Test
public void testSingle() {
    createGraph();
    PathFinder<Path> finder = newFinder();
    Path path = finder.findSinglePath(graph.getNode("SOURCE"), graph.getNode("TARGET"));
    assertNotNull(path);
    assertPathDef(path, "SOURCE", "z", "9", "0", "TARGET");
}
Also used : Path(org.neo4j.graphdb.Path) 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