Search in sources :

Example 76 with Path

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

the class TckSerializer method innerSerialize.

private String innerSerialize(Object obj) {
    if (obj == null) {
        sb.append("null");
    } else if (obj instanceof Node) {
        Node n = (Node) obj;
        sb.append("(");
        n.getLabels().forEach((l) -> sb.append(":").append(l.name()));
        sb.append(" {");
        String[] comma = new String[] { "" };
        n.getAllProperties().forEach((k, v) -> {
            sb.append(comma[0]).append(k).append(": ").append(serialize(v));
            comma[0] = ", ";
        });
        sb.append("})");
    } else if (obj instanceof Relationship) {
        Relationship r = (Relationship) obj;
        sb.append("[:");
        sb.append(r.getType().name());
        sb.append(" {");
        String[] comma = new String[] { "" };
        r.getAllProperties().forEach((k, v) -> {
            sb.append(comma[0]).append(k).append(": ").append(serialize(v));
            comma[0] = ", ";
        });
        sb.append("}]");
    } else if (obj instanceof Path) {
        Path p = (Path) obj;
        sb.append("<");
        sb.append(Paths.pathToString(p, new Paths.PathDescriptor<Path>() {

            @Override
            public String nodeRepresentation(Path path, Node node) {
                return serialize(node);
            }

            @Override
            public String relationshipRepresentation(Path path, Node from, Relationship relationship) {
                String prefix = "-", suffix = "-";
                if (from.equals(relationship.getEndNode())) {
                    prefix = "<-";
                } else {
                    suffix = "->";
                }
                return prefix + serialize(relationship) + suffix;
            }
        }));
        sb.append(">");
    } else if (obj instanceof String) {
        sb.append("'").append(obj.toString()).append("'");
    } else if (obj instanceof List) {
        List<?> list = (List) obj;
        List<String> output = new ArrayList<>(list.size());
        list.forEach(item -> output.add(serialize(item)));
        sb.append(output);
    } else if (obj.getClass().isArray()) {
        List<Object> list = new ArrayList<>();
        for (int i = 0; i < Array.getLength(obj); ++i) {
            list.add(Array.get(obj, i));
        }
        sb.append(serialize(list));
    } else if (obj instanceof Map) {
        Map<?, ?> map = (Map) obj;
        sb.append("{");
        String[] comma = new String[] { "" };
        map.forEach((k, v) -> {
            sb.append(comma[0]).append(k).append(": ").append(serialize(v));
            comma[0] = ", ";
        });
        sb.append("}");
    } else {
        sb.append(obj.toString());
    }
    return sb.toString();
}
Also used : Path(org.neo4j.graphdb.Path) List(java.util.List) Array(java.lang.reflect.Array) Relationship(org.neo4j.graphdb.Relationship) Paths(org.neo4j.graphdb.traversal.Paths) Map(java.util.Map) Node(org.neo4j.graphdb.Node) ArrayList(java.util.ArrayList) Path(org.neo4j.graphdb.Path) Node(org.neo4j.graphdb.Node) ArrayList(java.util.ArrayList) Relationship(org.neo4j.graphdb.Relationship) List(java.util.List) ArrayList(java.util.ArrayList) Paths(org.neo4j.graphdb.traversal.Paths) Map(java.util.Map)

Example 77 with Path

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

the class DijkstraTest method canGetMultiplePathsInASmallRoadNetwork.

@Test
public void canGetMultiplePathsInASmallRoadNetwork() throws Exception {
    Node nodeA = graph.makeNode("A");
    Node nodeB = graph.makeNode("B");
    Node nodeC = graph.makeNode("C");
    Node nodeD = graph.makeNode("D");
    Node nodeE = graph.makeNode("E");
    Node nodeF = graph.makeNode("F");
    graph.makeEdge("A", "B", "length", 2d);
    graph.makeEdge("A", "C", "length", 2.5d);
    graph.makeEdge("C", "D", "length", 7.3d);
    graph.makeEdge("B", "D", "length", 2.5d);
    graph.makeEdge("D", "E", "length", 3d);
    graph.makeEdge("C", "E", "length", 5d);
    graph.makeEdge("E", "F", "length", 5d);
    graph.makeEdge("C", "F", "length", 12d);
    graph.makeEdge("A", "F", "length", 25d);
    Dijkstra algo = new Dijkstra(Traversal.expanderForAllTypes(), CommonEvaluators.doubleCostEvaluator("length"));
    // Try the search in both directions.
    for (Node[] nodes : new Node[][] { { nodeA, nodeF }, { nodeF, nodeA } }) {
        int found = 0;
        Iterator<WeightedPath> paths = algo.findAllPaths(nodes[0], nodes[1]).iterator();
        for (int i = 0; i < 2; i++) {
            assertTrue("expected more paths", paths.hasNext());
            Path path = paths.next();
            if (path.length() != found && path.length() == 3) {
                assertContains(path.nodes(), nodeA, nodeC, nodeE, nodeF);
            } else if (path.length() != found && path.length() == 4) {
                assertContains(path.nodes(), nodeA, nodeB, nodeD, nodeE, nodeF);
            } else {
                fail("unexpected path length: " + path.length());
            }
            found = path.length();
        }
        assertFalse("expected at most two paths", paths.hasNext());
    }
}
Also used : Path(org.neo4j.graphdb.Path) WeightedPath(org.neo4j.graphalgo.WeightedPath) WeightedPath(org.neo4j.graphalgo.WeightedPath) Node(org.neo4j.graphdb.Node) Dijkstra(org.neo4j.graphalgo.impl.path.Dijkstra) Test(org.junit.Test)

Example 78 with Path

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

the class DijkstraTest method canGetMultiplePathsInTriangleGraph.

@Test
public void canGetMultiplePathsInTriangleGraph() throws Exception {
    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", 1d));
    Relationship expectedSecond = graph.makeEdge("B", "C", "length", 2d);
    graph.makeEdge("A", "C", "length", 5d);
    Dijkstra algo = new Dijkstra(Traversal.expanderForAllTypes(), CommonEvaluators.doubleCostEvaluator("length"));
    Iterator<WeightedPath> paths = algo.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 : Path(org.neo4j.graphdb.Path) WeightedPath(org.neo4j.graphalgo.WeightedPath) WeightedPath(org.neo4j.graphalgo.WeightedPath) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) HashSet(java.util.HashSet) Dijkstra(org.neo4j.graphalgo.impl.path.Dijkstra) Test(org.junit.Test)

Example 79 with Path

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

the class TestAStar method canGetMultiplePathsInASmallRoadNetwork.

/**
     * <pre>
     *   012345    A - B:  2
     *  +------>x  A - C:  2.5
     * 0|  C       C - D:  7.3
     * 1|A    F    B - D:  2.5
     * 2| B D      D - E:  3
     * 3|    E     C - E:  5
     *  V          E - F:  5
     *  x          C - F: 12
     *             A - F: 25
     * </pre>
     */
@Ignore("A* doesn't return multiple equal paths")
@Test
public void canGetMultiplePathsInASmallRoadNetwork() throws Exception {
    Node nodeA = graph.makeNode("A", "x", 1d, "y", 0d);
    Node nodeB = graph.makeNode("B", "x", 2d, "y", 1d);
    Node nodeC = graph.makeNode("C", "x", 0d, "y", 2d);
    Node nodeD = graph.makeNode("D", "x", 2d, "y", 3d);
    Node nodeE = graph.makeNode("E", "x", 3d, "y", 4d);
    Node nodeF = graph.makeNode("F", "x", 1d, "y", 5d);
    graph.makeEdge("A", "B", "length", 2d);
    graph.makeEdge("A", "C", "length", 2.5d);
    graph.makeEdge("C", "D", "length", 7.3d);
    graph.makeEdge("B", "D", "length", 2.5d);
    graph.makeEdge("D", "E", "length", 3d);
    graph.makeEdge("C", "E", "length", 5d);
    graph.makeEdge("E", "F", "length", 5d);
    graph.makeEdge("C", "F", "length", 12d);
    graph.makeEdge("A", "F", "length", 25d);
    PathFinder<WeightedPath> algo = newFinder();
    // Try the search in both directions.
    for (Node[] nodes : new Node[][] { { nodeA, nodeF }, { nodeF, nodeA } }) {
        int found = 0;
        Iterator<WeightedPath> paths = algo.findAllPaths(nodes[0], nodes[1]).iterator();
        for (int foundCount = 0; foundCount < 2; foundCount++) {
            assertTrue("expected more paths (found: " + foundCount + ")", paths.hasNext());
            Path path = paths.next();
            if (path.length() != found && path.length() == 3) {
                assertContains(path.nodes(), nodeA, nodeC, nodeE, nodeF);
            } else if (path.length() != found && path.length() == 4) {
                assertContains(path.nodes(), nodeA, nodeB, nodeD, nodeE, nodeF);
            } else {
                fail("unexpected path length: " + path.length());
            }
            found = path.length();
        }
        assertFalse("expected at most two paths", paths.hasNext());
    }
}
Also used : Path(org.neo4j.graphdb.Path) WeightedPath(org.neo4j.graphalgo.WeightedPath) WeightedPath(org.neo4j.graphalgo.WeightedPath) Node(org.neo4j.graphdb.Node) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 80 with Path

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

the class TestAStar method canGetMultiplePathsInTriangleGraph.

/**
     * <pre>
     *   01234567
     *  +-------->x  A - C: 10
     * 0|A      C    A - B:  2 (x2)
     * 1|  B         B - C:  6
     *  V
     *  y
     * </pre>
     */
@Ignore("A* doesn't return multiple equal paths")
@Test
public void canGetMultiplePathsInTriangleGraph() throws Exception {
    Node nodeA = graph.makeNode("A", "x", 0d, "y", 0d);
    Node nodeB = graph.makeNode("B", "x", 2d, "y", 1d);
    Node nodeC = graph.makeNode("C", "x", 7d, "y", 0d);
    Set<Relationship> expectedFirsts = new HashSet<Relationship>();
    expectedFirsts.add(graph.makeEdge("A", "B", "length", 2d));
    expectedFirsts.add(graph.makeEdge("A", "B", "length", 2d));
    Relationship expectedSecond = graph.makeEdge("B", "C", "length", 6d);
    graph.makeEdge("A", "C", "length", 10d);
    PathFinder<WeightedPath> algo = newFinder();
    Iterator<WeightedPath> paths = algo.findAllPaths(nodeA, nodeC).iterator();
    for (int foundCount = 0; foundCount < 2; foundCount++) {
        assertTrue("expected more paths (found: " + foundCount + ")", 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 : Path(org.neo4j.graphdb.Path) WeightedPath(org.neo4j.graphalgo.WeightedPath) WeightedPath(org.neo4j.graphalgo.WeightedPath) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) HashSet(java.util.HashSet) Ignore(org.junit.Ignore) Test(org.junit.Test)

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