use of org.neo4j.graphdb.Node in project neo4j by neo4j.
the class DijkstraTest method canGetMultiplePathsInTriangleGraph.
@Test
public void canGetMultiplePathsInTriangleGraph() throws Exception {
/* NODE (NAME/INDEX)
* ==> (two relationships)
*
* (A/0) ====== 1 =====> (B/1)
* \ /
* - 5 -> (C/2) <- 2 -
*/
Node nodeA = graph.makeNode("A");
Node nodeB = graph.makeNode("B");
Node nodeC = graph.makeNode("C");
Set<Relationship> expectedFirsts = new HashSet<Relationship>();
expectedFirsts.add(graph.makeEdge("A", "B", "length", 1d));
expectedFirsts.add(graph.makeEdge("A", "B", "length", 1));
Relationship expectedSecond = graph.makeEdge("B", "C", "length", 2L);
graph.makeEdge("A", "C", "length", 5d);
PathFinder finder = factory.dijkstra(PathExpanders.allTypesAndDirections());
Iterator<WeightedPath> paths = finder.findAllPaths(nodeA, nodeC).iterator();
for (int i = 0; i < 2; i++) {
assertTrue("expected more paths", paths.hasNext());
Path path = paths.next();
assertPath(path, nodeA, nodeB, nodeC);
Iterator<Relationship> relationships = path.relationships().iterator();
assertTrue("found shorter path than expected", relationships.hasNext());
assertTrue("path contained unexpected relationship", expectedFirsts.remove(relationships.next()));
assertTrue("found shorter path than expected", relationships.hasNext());
assertEquals(expectedSecond, relationships.next());
assertFalse("found longer path than expected", relationships.hasNext());
}
assertFalse("expected at most two paths", paths.hasNext());
}
use of org.neo4j.graphdb.Node in project neo4j by neo4j.
the class DijkstraTest method shouldOnlyFindTheShortestPaths.
@Test
public void shouldOnlyFindTheShortestPaths() {
/*
*
* ----- (e) - 1 - (f) ---
* / \
* / ------- (a) -------- \
* 1 / \ \ 2
* | 2 0 0 |
* | / \ \|
* (s) - 1 - (c) - 1 - (d) - 1 - (t)
* \ /
* -- 1 - (b) - 1 -
*
*/
Node s = graph.makeNode("s");
Node t = graph.makeNode("t");
Node a = graph.makeNode("a");
Node b = graph.makeNode("b");
Node c = graph.makeNode("c");
graph.makeEdgeChain("s,e,f", "length", 1.0);
graph.makeEdge("f", "t", "length", 2);
graph.makeEdge("s", "a", "length", 2);
graph.makeEdge("a", "t", "length", 0);
graph.makeEdge("s", "c", "length", 1);
graph.makeEdge("c", "d", "length", 1);
graph.makeEdge("s", "b", "length", 1);
graph.makeEdge("b", "d", "length", 1);
graph.makeEdge("d", "a", "length", 0);
graph.makeEdge("d", "t", "length", 1);
PathFinder finder = factory.dijkstra(PathExpanders.allTypesAndDirections());
Iterator<WeightedPath> paths = finder.findAllPaths(s, t).iterator();
for (int i = 1; i <= 3; i++) {
assertTrue("Expected at least " + i + " path(s)", paths.hasNext());
assertTrue("Expected 3 paths of cost 2", NoneStrictMath.equals(paths.next().weight(), 2));
}
assertFalse("Expected exactly 3 paths", paths.hasNext());
}
use of org.neo4j.graphdb.Node in project neo4j by neo4j.
the class Neo4jAlgoTestCase method assertPath.
protected void assertPath(Path path, Node... nodes) {
int i = 0;
for (Node node : path.nodes()) {
assertEquals("Wrong node " + i + " in " + getPathDef(path), nodes[i++].getProperty(SimpleGraphBuilder.KEY_ID), node.getProperty(SimpleGraphBuilder.KEY_ID));
}
assertEquals(nodes.length, i);
}
use of org.neo4j.graphdb.Node in project neo4j by neo4j.
the class Neo4jAlgoTestCase method assertPathDef.
protected void assertPathDef(Path path, String... names) {
int i = 0;
for (Node node : path.nodes()) {
assertEquals("Wrong node " + i + " in " + getPathDef(path), names[i++], node.getProperty(SimpleGraphBuilder.KEY_ID));
}
assertEquals(names.length, i);
}
use of org.neo4j.graphdb.Node in project neo4j by neo4j.
the class SimpleGraphBuilder method getRelationship.
/**
* @param node1Id
* @param node2Id
* @return One relationship between two given nodes, if there exists one,
* otherwise null.
*/
public Relationship getRelationship(String node1Id, String node2Id) {
Node node1 = getNode(node1Id);
Node node2 = getNode(node2Id);
if (node1 == null || node2 == null) {
return null;
}
Iterable<Relationship> relationships = node1.getRelationships();
for (Relationship relationship : relationships) {
if (relationship.getOtherNode(node1).equals(node2)) {
return relationship;
}
}
return null;
}
Aggregations