Search in sources :

Example 21 with Traverser

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

the class Dijkstra 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 = TRAVERSAL.expand(expander).order(new SelectorFactory(costEvaluator)).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 22 with Traverser

use of org.neo4j.graphdb.traversal.Traverser in project graphdb by neo4j-attic.

the class TreeGraphTest method relationshipsIteratorReturnAllNodes.

@Test
public void relationshipsIteratorReturnAllNodes() throws Exception {
    Traverser traverser = Traversal.description().traverse(referenceNode());
    int count = 0;
    for (Relationship relationship : traverser.relationships()) {
        assertNotNull("returned relationships should not be. relationship #" + count, relationship);
        count++;
    }
    assertEquals(12, count);
}
Also used : Traverser(org.neo4j.graphdb.traversal.Traverser) Relationship(org.neo4j.graphdb.Relationship) Test(org.junit.Test)

Example 23 with Traverser

use of org.neo4j.graphdb.traversal.Traverser in project graphdb by neo4j-attic.

the class TreeGraphTest method testBreadthFirst.

@Test
public void testBreadthFirst() throws Exception {
    Traverser traverser = Traversal.description().breadthFirst().traverse(referenceNode());
    Stack<Set<String>> levels = new Stack<Set<String>>();
    levels.push(new HashSet<String>(Arrays.asList("5", "6", "7", "8", "9", "A", "B", "C", "D")));
    levels.push(new HashSet<String>(Arrays.asList("2", "3", "4")));
    levels.push(new HashSet<String>(Arrays.asList("1")));
    assertLevels(traverser, levels);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) Traverser(org.neo4j.graphdb.traversal.Traverser) Stack(java.util.Stack) Test(org.junit.Test)

Example 24 with Traverser

use of org.neo4j.graphdb.traversal.Traverser in project graphdb by neo4j-attic.

the class TreeGraphTest method testPostorderDepthFirstReturnsDeeperNodesFirst.

@Test
public void testPostorderDepthFirstReturnsDeeperNodesFirst() {
    Traverser traverser = Traversal.description().order(Traversal.postorderDepthFirst()).traverse(referenceNode());
    int i = 0;
    List<String> encounteredNodes = new ArrayList<String>();
    for (Path pos : traverser) {
        encounteredNodes.add((String) pos.endNode().getProperty("name"));
        assertEquals(expectedDepth((12 - i++)), pos.length());
    }
    assertEquals(13, i);
    assertTrue(encounteredNodes.indexOf("5") < encounteredNodes.indexOf("2"));
    assertTrue(encounteredNodes.indexOf("6") < encounteredNodes.indexOf("2"));
    assertTrue(encounteredNodes.indexOf("7") < encounteredNodes.indexOf("2"));
    assertTrue(encounteredNodes.indexOf("8") < encounteredNodes.indexOf("3"));
    assertTrue(encounteredNodes.indexOf("9") < encounteredNodes.indexOf("3"));
    assertTrue(encounteredNodes.indexOf("A") < encounteredNodes.indexOf("3"));
    assertTrue(encounteredNodes.indexOf("B") < encounteredNodes.indexOf("4"));
    assertTrue(encounteredNodes.indexOf("C") < encounteredNodes.indexOf("4"));
    assertTrue(encounteredNodes.indexOf("D") < encounteredNodes.indexOf("4"));
    assertTrue(encounteredNodes.indexOf("2") < encounteredNodes.indexOf("1"));
    assertTrue(encounteredNodes.indexOf("3") < encounteredNodes.indexOf("1"));
    assertTrue(encounteredNodes.indexOf("4") < encounteredNodes.indexOf("1"));
}
Also used : Path(org.neo4j.graphdb.Path) Traverser(org.neo4j.graphdb.traversal.Traverser) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 25 with Traverser

use of org.neo4j.graphdb.traversal.Traverser in project neo4j by neo4j.

the class GraphCloner method clonedSubgraph.

@PluginTarget(Node.class)
public Node clonedSubgraph(@Source Node startNode, @Parameter(name = "depth", optional = false) Integer depth) {
    GraphDatabaseService graphDb = startNode.getGraphDatabase();
    try (Transaction tx = graphDb.beginTx()) {
        Traverser traverse = traverseToDepth(graphDb, startNode, depth);
        Iterator<Node> nodes = traverse.nodes().iterator();
        HashMap<Node, Node> clonedNodes = cloneNodes(graphDb, nodes);
        for (Node oldNode : clonedNodes.keySet()) {
            // give me the matching new node
            Node newStartNode = clonedNodes.get(oldNode);
            // Now let's go through the relationships and copy them over
            Iterator<Relationship> oldRelationships = oldNode.getRelationships(Direction.OUTGOING).iterator();
            while (oldRelationships.hasNext()) {
                Relationship oldRelationship = oldRelationships.next();
                Node newEndNode = clonedNodes.get(oldRelationship.getEndNode());
                if (newEndNode != null) {
                    Relationship newRelationship = newStartNode.createRelationshipTo(newEndNode, oldRelationship.getType());
                    cloneProperties(oldRelationship, newRelationship);
                }
            }
        }
        tx.success();
        return clonedNodes.get(startNode);
    }
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) Traverser(org.neo4j.graphdb.traversal.Traverser) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship)

Aggregations

Traverser (org.neo4j.graphdb.traversal.Traverser)44 Test (org.junit.Test)19 Path (org.neo4j.graphdb.Path)15 Transaction (org.neo4j.graphdb.Transaction)10 HashSet (java.util.HashSet)6 Node (org.neo4j.graphdb.Node)6 Predicate (org.neo4j.helpers.Predicate)6 Set (java.util.Set)4 Stack (java.util.Stack)4 WeightedPath (org.neo4j.graphalgo.WeightedPath)4 BestFirstSelectorFactory (org.neo4j.graphalgo.impl.util.BestFirstSelectorFactory)4 StopAfterWeightIterator (org.neo4j.graphalgo.impl.util.StopAfterWeightIterator)4 Relationship (org.neo4j.graphdb.Relationship)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LiteDepthFirstSelector (org.neo4j.graphalgo.impl.util.LiteDepthFirstSelector)2 BranchOrderingPolicy (org.neo4j.graphdb.traversal.BranchOrderingPolicy)2 BranchSelector (org.neo4j.graphdb.traversal.BranchSelector)2 TraversalBranch (org.neo4j.graphdb.traversal.TraversalBranch)2 TraversalDescription (org.neo4j.graphdb.traversal.TraversalDescription)2