Search in sources :

Example 91 with Vertex

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();
}
Also used : Vertex(com.baidu.hugegraph.structure.graph.Vertex) HugeClient(com.baidu.hugegraph.driver.HugeClient)

Example 92 with Vertex

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);
}
Also used : EdgeLabelEntity(com.baidu.hugegraph.entity.schema.EdgeLabelEntity) Vertex(com.baidu.hugegraph.structure.graph.Vertex) HugeClient(com.baidu.hugegraph.driver.HugeClient) GraphManager(com.baidu.hugegraph.driver.GraphManager) VertexLabelEntity(com.baidu.hugegraph.entity.schema.VertexLabelEntity) Edge(com.baidu.hugegraph.structure.graph.Edge)

Example 93 with Vertex

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());
}
Also used : Path(com.baidu.hugegraph.structure.graph.Path) Vertex(com.baidu.hugegraph.structure.graph.Vertex) HashMap(java.util.HashMap) Edge(com.baidu.hugegraph.structure.graph.Edge) GraphView(com.baidu.hugegraph.entity.query.GraphView)

Example 94 with Vertex

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<>());
}
Also used : Vertex(com.baidu.hugegraph.structure.graph.Vertex) HashMap(java.util.HashMap) Edge(com.baidu.hugegraph.structure.graph.Edge) GraphView(com.baidu.hugegraph.entity.query.GraphView)

Example 95 with Vertex

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));
    }
}
Also used : Response(com.baidu.hugegraph.structure.gremlin.Response) Vertex(com.baidu.hugegraph.structure.graph.Vertex) RestResult(com.baidu.hugegraph.rest.RestResult) ArrayList(java.util.ArrayList) Result(com.baidu.hugegraph.structure.gremlin.Result) RestResult(com.baidu.hugegraph.rest.RestResult) Test(org.junit.Test)

Aggregations

Vertex (com.baidu.hugegraph.structure.graph.Vertex)165 Test (org.junit.Test)110 Edge (com.baidu.hugegraph.structure.graph.Edge)33 HugeClient (com.baidu.hugegraph.driver.HugeClient)22 ArrayList (java.util.ArrayList)21 SchemaManager (com.baidu.hugegraph.driver.SchemaManager)18 BaseClientTest (com.baidu.hugegraph.BaseClientTest)17 BeforeClass (org.junit.BeforeClass)17 GraphManager (com.baidu.hugegraph.driver.GraphManager)14 BatchVertexRequest (com.baidu.hugegraph.structure.graph.BatchVertexRequest)13 Path (com.baidu.hugegraph.structure.graph.Path)11 Result (com.baidu.hugegraph.structure.gremlin.Result)10 ResultSet (com.baidu.hugegraph.structure.gremlin.ResultSet)10 List (java.util.List)10 BaseApiTest (com.baidu.hugegraph.api.BaseApiTest)9 ImmutableList (com.google.common.collect.ImmutableList)6 ImmutableMap (com.google.common.collect.ImmutableMap)6 Map (java.util.Map)6 RestResult (com.baidu.hugegraph.rest.RestResult)5 PathsWithVertices (com.baidu.hugegraph.structure.traverser.PathsWithVertices)5