use of com.baidu.hugegraph.structure.graph.Vertex in project incubator-hugegraph-toolchain by apache.
the class GraphService method addVertex.
public GraphView addVertex(int connId, VertexEntity entity) {
HugeClient client = this.client(connId);
Vertex vertex = this.buildVertex(connId, entity);
vertex = client.graph().addVertex(vertex);
return GraphView.builder().vertices(ImmutableSet.of(vertex)).edges(ImmutableSet.of()).build();
}
use of com.baidu.hugegraph.structure.graph.Vertex in project incubator-hugegraph-toolchain by apache.
the class GraphService method buildEdge.
private EdgeHolder buildEdge(int connId, EdgeEntity entity) {
HugeClient client = this.client(connId);
GraphManager graph = client.graph();
EdgeLabelEntity el = this.elService.get(entity.getLabel(), connId);
VertexLabelEntity sourceVl = this.vlService.get(el.getSourceLabel(), connId);
VertexLabelEntity targetVl = this.vlService.get(el.getTargetLabel(), connId);
Object realSourceId = this.convertVertexId(sourceVl.getIdStrategy(), entity.getSourceId());
Object realTargetId = this.convertVertexId(targetVl.getIdStrategy(), entity.getTargetId());
Vertex sourceVertex = graph.getVertex(realSourceId);
Vertex targetVertex = graph.getVertex(realTargetId);
Ex.check(el.getSourceLabel().equals(sourceVertex.label()) && el.getTargetLabel().equals(targetVertex.label()), "graph.edge.link-unmatched-vertex", entity.getLabel(), el.getSourceLabel(), el.getTargetLabel(), sourceVertex.label(), targetVertex.label());
Edge edge = new Edge(entity.getLabel());
edge.source(sourceVertex);
edge.target(targetVertex);
this.fillProperties(connId, el, edge, entity.getProperties());
return new EdgeHolder(edge, sourceVertex, targetVertex);
}
use of com.baidu.hugegraph.structure.graph.Vertex in project incubator-hugegraph-toolchain by apache.
the class GremlinQueryService method buildGraphView.
private GraphView buildGraphView(TypedResult result, HugeClient client) {
List<Object> data = result.getData();
if (!result.getType().isGraph() || CollectionUtils.isEmpty(data)) {
return GraphView.EMPTY;
}
Map<Object, Vertex> vertices = new HashMap<>();
Map<String, Edge> edges = new HashMap<>();
for (Object object : data) {
if (object instanceof Vertex) {
Vertex vertex = (Vertex) object;
vertices.put(vertex.id(), vertex);
} else if (object instanceof Edge) {
Edge edge = (Edge) object;
edges.put(edge.id(), edge);
} else if (object instanceof Path) {
List<Object> elements = ((Path) object).objects();
for (Object element : elements) {
if (element instanceof Vertex) {
Vertex vertex = (Vertex) element;
vertices.put(vertex.id(), vertex);
} else if (element instanceof Edge) {
Edge edge = (Edge) element;
edges.put(edge.id(), edge);
} else {
return GraphView.EMPTY;
}
}
}
}
if (!edges.isEmpty()) {
if (vertices.isEmpty()) {
vertices = this.verticesOfEdge(result, edges, client);
} else {
// TODO: reduce the number of requests
vertices.putAll(this.verticesOfEdge(result, edges, client));
}
} else {
if (!vertices.isEmpty()) {
edges = this.edgesOfVertex(result, vertices, client);
}
}
if (!edges.isEmpty()) {
Ex.check(!vertices.isEmpty(), "gremlin.edges.linked-vertex.not-exist");
}
return new GraphView(vertices.values(), edges.values());
}
use of com.baidu.hugegraph.structure.graph.Vertex in project incubator-hugegraph-toolchain by apache.
the class OltpAlgoService method buildPathGraphView.
private GraphView buildPathGraphView(Path result) {
Map<Object, Vertex> vertices = new HashMap<>();
Map<String, Edge> edges = new HashMap<>();
List<Object> elements = result.objects();
for (Object element : elements) {
if (element instanceof Vertex) {
Vertex vertex = (Vertex) element;
vertices.put(vertex.id(), vertex);
} else if (element instanceof Edge) {
Edge edge = (Edge) element;
edges.put(edge.id(), edge);
} else {
return GraphView.EMPTY;
}
}
return new GraphView(vertices.values(), new ArrayList<>());
}
use of com.baidu.hugegraph.structure.graph.Vertex in project incubator-hugegraph-toolchain by apache.
the class RestResultTest method testReadGremlinVertices.
@Test
public void testReadGremlinVertices() {
String json = "{" + "\"requestId\": \"b0fd8ead-333f-43ac-97b0-4d78784726ae\"," + "\"status\": {" + "\"message\": \"\"," + "\"code\": 200," + "\"attributes\": {}" + "}," + "\"result\": {" + "\"data\": [" + "{" + "\"id\": \"person:marko\"," + "\"label\": \"person\"," + "\"type\": \"vertex\"," + "\"properties\": {" + "\"city\": [\"Beijing\",\"Wuhan\",\"Beijing\"]," + "\"name\": \"marko\"," + "\"age\": 29" + "}" + "}," + "{" + "\"id\": \"software:lop\"," + "\"label\": \"software\"," + "\"type\": \"vertex\"," + "\"properties\": {" + "\"price\": 328," + "\"name\": \"lop\"," + "\"lang\": [\"java\",\"python\",\"c++\"]" + "}" + "}," + "{" + "\"id\": \"person:peter\"," + "\"label\": \"person\"," + "\"type\": \"vertex\"," + "\"properties\": {" + "\"city\": [\"Shanghai\"]," + "\"name\": \"peter\"," + "\"age\": 35" + "}" + "}" + "]," + "\"meta\": {}" + "}" + "}";
Mockito.when(this.mockResponse.getStatus()).thenReturn(200);
Mockito.when(this.mockResponse.getHeaders()).thenReturn(null);
Mockito.when(this.mockResponse.readEntity(String.class)).thenReturn(json);
RestResult restResult = new RestResult(this.mockResponse);
Assert.assertEquals(200, restResult.status());
Assert.assertNull(restResult.headers());
Response response = restResult.readObject(Response.class);
response.graphManager(graph());
Assert.assertEquals("b0fd8ead-333f-43ac-97b0-4d78784726ae", response.requestId());
Assert.assertEquals(200, response.status().code());
Vertex marko = new Vertex("person");
marko.id("person:marko");
marko.property("name", "marko");
marko.property("city", ImmutableList.of("Beijing", "Wuhan", "Beijing"));
marko.property("age", 29);
Vertex lop = new Vertex("software");
lop.id("software:lop");
lop.property("name", "lop");
lop.property("lang", ImmutableList.of("java", "python", "c++"));
lop.property("price", 328);
Vertex peter = new Vertex("person");
peter.id("person:peter");
peter.property("name", "peter");
peter.property("city", ImmutableList.of("Shanghai"));
peter.property("age", 35);
List<Vertex> vertices = new ArrayList<>(3);
vertices.add(peter);
vertices.add(marko);
vertices.add(lop);
Iterator<Result> results = response.result().iterator();
while (results.hasNext()) {
Result result = results.next();
Assert.assertEquals(Vertex.class, result.getObject().getClass());
Vertex vertex = result.getVertex();
Assert.assertTrue(Utils.contains(vertices, vertex));
}
}
Aggregations