Search in sources :

Example 86 with Path

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

the class ExactDepthPathFinder method toPath.

private Path toPath(Path[] found, Node start) {
    Path startPath = found[0];
    Path endPath = found[1];
    if (!startPath.startNode().equals(start)) {
        Path tmpPath = startPath;
        startPath = endPath;
        endPath = tmpPath;
    }
    return toBuilder(startPath).build(toBuilder(endPath));
}
Also used : Path(org.neo4j.graphdb.Path)

Example 87 with Path

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

the class ExperimentalAStar method findAllPaths.

public Iterable<WeightedPath> findAllPaths(Node start, final Node end) {
    Predicate<Path> filter = new Predicate<Path>() {

        public boolean accept(Path position) {
            return position.endNode().equals(end);
        }
    };
    final Traverser traverser = traversalDescription.order(new SelectorFactory(end)).filter(filter).traverse(start);
    return new Iterable<WeightedPath>() {

        public Iterator<WeightedPath> iterator() {
            return new StopAfterWeightIterator(traverser.iterator(), costEvaluator);
        }
    };
}
Also used : Path(org.neo4j.graphdb.Path) WeightedPath(org.neo4j.graphalgo.WeightedPath) WeightedPath(org.neo4j.graphalgo.WeightedPath) BestFirstSelectorFactory(org.neo4j.graphalgo.impl.util.BestFirstSelectorFactory) Traverser(org.neo4j.graphdb.traversal.Traverser) StopAfterWeightIterator(org.neo4j.graphalgo.impl.util.StopAfterWeightIterator) Predicate(org.neo4j.helpers.Predicate)

Example 88 with Path

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

the class PathFindingExamplesTest method shortestPathExample.

@Test
public void shortestPathExample() {
    // START SNIPPET: shortestPathUsage
    Node startNode = graphDb.createNode();
    Node middleNode1 = graphDb.createNode();
    Node middleNode2 = graphDb.createNode();
    Node middleNode3 = graphDb.createNode();
    Node endNode = graphDb.createNode();
    createRelationshipsBetween(startNode, middleNode1, endNode);
    createRelationshipsBetween(startNode, middleNode2, middleNode3, endNode);
    // Will find the shortest path between startNode and endNode via
    // "MY_TYPE" relationships (in OUTGOING direction), like f.ex:
    //
    // (startNode)-->(middleNode1)-->(endNode)
    //
    PathFinder<Path> finder = GraphAlgoFactory.shortestPath(Traversal.expanderForTypes(ExampleTypes.MY_TYPE, Direction.OUTGOING), 15);
    Iterable<Path> paths = finder.findAllPaths(startNode, endNode);
    // END SNIPPET: shortestPathUsage
    Path path = paths.iterator().next();
    assertEquals(2, path.length());
    assertEquals(startNode, path.startNode());
    assertEquals(endNode, path.endNode());
    Iterator<Node> iterator = path.nodes().iterator();
    iterator.next();
    assertEquals(middleNode1, iterator.next());
}
Also used : Path(org.neo4j.graphdb.Path) WeightedPath(org.neo4j.graphalgo.WeightedPath) Node(org.neo4j.graphdb.Node) Test(org.junit.Test)

Example 89 with Path

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

the class TestMultiPruneEvaluators method makeSurePruneIsntCalledForStartNode.

@Test
public void makeSurePruneIsntCalledForStartNode() {
    final boolean[] calledForStartPosition = new boolean[1];
    PruneEvaluator evaluator = new PruneEvaluator() {

        public boolean pruneAfter(Path position) {
            if (position.length() == 0) {
                calledForStartPosition[0] = true;
            }
            return false;
        }
    };
    IteratorUtil.lastOrNull(Traversal.description().prune(evaluator).traverse(referenceNode()));
    assertFalse(calledForStartPosition[0]);
}
Also used : Path(org.neo4j.graphdb.Path) PruneEvaluator(org.neo4j.graphdb.traversal.PruneEvaluator) Test(org.junit.Test)

Example 90 with Path

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

the class TestMultiRelTypesAndDirections method testCIsReturnedOnDepthTwo.

private void testCIsReturnedOnDepthTwo(TraversalDescription description) {
    description = description.relationships(ONE, Direction.OUTGOING);
    int i = 0;
    for (Path position : description.traverse(referenceNode())) {
        assertEquals(i++, position.length());
    }
}
Also used : Path(org.neo4j.graphdb.Path)

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