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