Search in sources :

Example 1 with Path

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

the class ExecutionResultSerializerTest method shouldProduceResultStreamWithLegacyRestFormat.

@Test
public void shouldProduceResultStreamWithLegacyRestFormat() throws Exception {
    // given
    Node[] node = { node(0, properties(property("name", "node0"))), node(1, properties(property("name", "node1"))), node(2, properties(property("name", "node2"))) };
    Relationship[] rel = { relationship(0, node[0], "KNOWS", node[1], property("name", "rel0")), relationship(1, node[2], "LOVES", node[1], property("name", "rel1")) };
    Path path = GraphMock.path(node[0], link(rel[0], node[1]), link(rel[1], node[2]));
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    ExecutionResultSerializer serializer = getSerializerWith(output, "http://base.uri/");
    // when
    serializer.statementResult(mockExecutionResult(map("node", node[0], "rel", rel[0], "path", path, "map", map("n1", node[1], "r1", rel[1]))), false, ResultDataContent.rest);
    serializer.finish();
    // then
    String result = output.toString(UTF_8.name());
    JsonNode json = jsonNode(result);
    Map<String, Integer> columns = new HashMap<>();
    int col = 0;
    JsonNode results = json.get("results").get(0);
    for (JsonNode column : results.get("columns")) {
        columns.put(column.getTextValue(), col++);
    }
    JsonNode row = results.get("data").get(0).get("rest");
    JsonNode jsonNode = row.get(columns.get("node"));
    JsonNode jsonRel = row.get(columns.get("rel"));
    JsonNode jsonPath = row.get(columns.get("path"));
    JsonNode jsonMap = row.get(columns.get("map"));
    assertEquals("http://base.uri/node/0", jsonNode.get("self").getTextValue());
    assertEquals("http://base.uri/relationship/0", jsonRel.get("self").getTextValue());
    assertEquals(2, jsonPath.get("length").getNumberValue());
    assertEquals("http://base.uri/node/0", jsonPath.get("start").getTextValue());
    assertEquals("http://base.uri/node/2", jsonPath.get("end").getTextValue());
    assertEquals("http://base.uri/node/1", jsonMap.get("n1").get("self").getTextValue());
    assertEquals("http://base.uri/relationship/1", jsonMap.get("r1").get("self").getTextValue());
}
Also used : Path(org.neo4j.graphdb.Path) HashMap(java.util.HashMap) JsonNode(org.codehaus.jackson.JsonNode) JsonHelper.jsonNode(org.neo4j.server.rest.domain.JsonHelper.jsonNode) Node(org.neo4j.graphdb.Node) JsonNode(org.codehaus.jackson.JsonNode) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Relationship(org.neo4j.graphdb.Relationship) Test(org.junit.Test) Neo4jJsonCodecTest(org.neo4j.server.rest.transactional.Neo4jJsonCodecTest)

Example 2 with Path

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

the class Neo4jJsonCodec method writeValue.

@Override
public void writeValue(JsonGenerator out, Object value) throws IOException {
    if (value instanceof PropertyContainer) {
        writePropertyContainer(out, (PropertyContainer) value, TransactionStateChecker.create(container));
    } else if (value instanceof Path) {
        writePath(out, ((Path) value).iterator(), TransactionStateChecker.create(container));
    } else if (value instanceof Iterable) {
        writeIterator(out, ((Iterable) value).iterator());
    } else if (value instanceof byte[]) {
        writeByteArray(out, (byte[]) value);
    } else if (value instanceof Map) {
        writeMap(out, (Map) value);
    } else if (value instanceof Geometry) {
        Geometry geom = (Geometry) value;
        Object coordinates = (geom instanceof Point) ? ((Point) geom).getCoordinate() : geom.getCoordinates();
        writeMap(out, genericMap(new LinkedHashMap<>(), "type", geom.getGeometryType(), "coordinates", coordinates, "crs", geom.getCRS()));
    } else if (value instanceof Coordinate) {
        Coordinate coordinate = (Coordinate) value;
        writeIterator(out, coordinate.getCoordinate().iterator());
    } else if (value instanceof CRS) {
        CRS crs = (CRS) value;
        writeMap(out, genericMap(new LinkedHashMap<>(), "name", crs.getType(), "type", "link", "properties", genericMap(new LinkedHashMap<>(), "href", crs.getHref() + "ogcwkt/", "type", "ogcwkt")));
    } else {
        super.writeValue(out, value);
    }
}
Also used : Path(org.neo4j.graphdb.Path) Geometry(org.neo4j.graphdb.spatial.Geometry) PropertyContainer(org.neo4j.graphdb.PropertyContainer) Coordinate(org.neo4j.graphdb.spatial.Coordinate) CRS(org.neo4j.graphdb.spatial.CRS) Point(org.neo4j.graphdb.spatial.Point) LinkedHashMap(java.util.LinkedHashMap) MapUtil.genericMap(org.neo4j.helpers.collection.MapUtil.genericMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with Path

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

the class Neo4jJsonCodec method writeMeta.

void writeMeta(JsonGenerator out, Object value) throws IOException {
    if (value instanceof Node) {
        Node node = (Node) value;
        writeNodeOrRelationshipMeta(out, node.getId(), "node", TransactionStateChecker.create(container).isNodeDeletedInCurrentTx(node.getId()));
    } else if (value instanceof Relationship) {
        Relationship relationship = (Relationship) value;
        writeNodeOrRelationshipMeta(out, relationship.getId(), "relationship", TransactionStateChecker.create(container).isRelationshipDeletedInCurrentTx(relationship.getId()));
    } else if (value instanceof Path) {
        writeMetaPath(out, (Path) value);
    } else if (value instanceof Iterable) {
        for (Object v : (Iterable) value) {
            writeMeta(out, v);
        }
    } else if (value instanceof Map) {
        Map map = (Map) value;
        for (Object key : map.keySet()) {
            writeMeta(out, map.get(key));
        }
    } else {
        out.writeNull();
    }
}
Also used : Path(org.neo4j.graphdb.Path) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) LinkedHashMap(java.util.LinkedHashMap) MapUtil.genericMap(org.neo4j.helpers.collection.MapUtil.genericMap) Map(java.util.Map)

Example 4 with Path

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

the class DatabaseActions method findSinglePath.

// Graph algos
@SuppressWarnings("rawtypes")
public PathRepresentation findSinglePath(long startId, long endId, Map<String, Object> map) {
    FindParams findParams = new FindParams(startId, endId, map).invoke();
    PathFinder finder = findParams.getFinder();
    Node startNode = findParams.getStartNode();
    Node endNode = findParams.getEndNode();
    Path path = finder.findSinglePath(startNode, endNode);
    if (path == null) {
        throw new NotFoundException();
    }
    return findParams.pathRepresentationOf(path);
}
Also used : Path(org.neo4j.graphdb.Path) WeightedPath(org.neo4j.graphalgo.WeightedPath) Node(org.neo4j.graphdb.Node) StartNodeNotFoundException(org.neo4j.server.rest.domain.StartNodeNotFoundException) NotFoundException(org.neo4j.graphdb.NotFoundException) EndNodeNotFoundException(org.neo4j.server.rest.domain.EndNodeNotFoundException) PathFinder(org.neo4j.graphalgo.PathFinder)

Example 5 with Path

use of org.neo4j.graphdb.Path 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)

Aggregations

Path (org.neo4j.graphdb.Path)170 Node (org.neo4j.graphdb.Node)73 Transaction (org.neo4j.graphdb.Transaction)51 Test (org.junit.jupiter.api.Test)49 Relationship (org.neo4j.graphdb.Relationship)47 Test (org.junit.Test)37 WeightedPath (org.neo4j.graphalgo.WeightedPath)25 Traverser (org.neo4j.graphdb.traversal.Traverser)24 BasicEvaluationContext (org.neo4j.graphalgo.BasicEvaluationContext)21 ArrayList (java.util.ArrayList)19 Map (java.util.Map)18 TraversalDescription (org.neo4j.graphdb.traversal.TraversalDescription)18 HashSet (java.util.HashSet)12 RelationshipType (org.neo4j.graphdb.RelationshipType)10 Entity (org.neo4j.graphdb.Entity)9 Evaluator (org.neo4j.graphdb.traversal.Evaluator)9 HashMap (java.util.HashMap)7 LinkedList (java.util.LinkedList)6 PathFinder (org.neo4j.graphalgo.PathFinder)6 PathExpander (org.neo4j.graphdb.PathExpander)6