Search in sources :

Example 96 with Node

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());
}
Also used : WeightedPath(org.neo4j.graphalgo.WeightedPath) Path(org.neo4j.graphdb.Path) WeightedPath(org.neo4j.graphalgo.WeightedPath) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) PathFinder(org.neo4j.graphalgo.PathFinder) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 97 with Node

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());
}
Also used : WeightedPath(org.neo4j.graphalgo.WeightedPath) Node(org.neo4j.graphdb.Node) PathFinder(org.neo4j.graphalgo.PathFinder) Test(org.junit.Test)

Example 98 with Node

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);
}
Also used : Node(org.neo4j.graphdb.Node)

Example 99 with Node

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);
}
Also used : Node(org.neo4j.graphdb.Node)

Example 100 with Node

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;
}
Also used : Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship)

Aggregations

Node (org.neo4j.graphdb.Node)1271 Test (org.junit.Test)781 Transaction (org.neo4j.graphdb.Transaction)540 Relationship (org.neo4j.graphdb.Relationship)372 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)146 NotFoundException (org.neo4j.graphdb.NotFoundException)78 File (java.io.File)65 HashMap (java.util.HashMap)57 Label (org.neo4j.graphdb.Label)57 RelationshipType (org.neo4j.graphdb.RelationshipType)57 LinkedList (java.util.LinkedList)56 Path (org.neo4j.graphdb.Path)52 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)46 HashSet (java.util.HashSet)45 Map (java.util.Map)44 WeightedPath (org.neo4j.graphalgo.WeightedPath)37 TestGraphDatabaseFactory (org.neo4j.test.TestGraphDatabaseFactory)35 ArrayList (java.util.ArrayList)30 List (java.util.List)27 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)26