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);
}
};
}
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);
}
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);
}
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"));
}
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);
}
}
Aggregations