Search in sources :

Example 6 with Vertex

use of com.baidu.hugegraph.structure.graph.Vertex in project incubator-hugegraph-toolchain by apache.

the class KneighborApiTest method testKneighborPostWithVertex.

@Test
public void testKneighborPostWithVertex() {
    Object markoId = getVertexId("person", "name", "marko");
    Object rippleId = getVertexId("software", "name", "ripple");
    Object joshId = getVertexId("person", "name", "josh");
    Object lopId = getVertexId("software", "name", "lop");
    Object vadasId = getVertexId("person", "name", "vadas");
    Object peterId = getVertexId("person", "name", "peter");
    KneighborRequest.Builder builder = KneighborRequest.builder();
    builder.source(markoId);
    builder.step().direction(Direction.BOTH);
    builder.maxDepth(1);
    builder.withPath(false);
    builder.withVertex(true);
    KneighborRequest request = builder.build();
    Kneighbor kneighborResult = kneighborAPI.post(request);
    Assert.assertEquals(3, kneighborResult.size());
    Set<Object> expected = ImmutableSet.of(vadasId, lopId, joshId);
    Assert.assertEquals(expected, kneighborResult.ids());
    Assert.assertEquals(3, kneighborResult.vertices().size());
    Set<Object> expectedVids = ImmutableSet.of(vadasId, lopId, joshId);
    for (Vertex vertex : kneighborResult.vertices()) {
        Assert.assertTrue(expectedVids.contains(vertex.id()));
    }
    builder = KneighborRequest.builder();
    builder.source(markoId);
    builder.step().direction(Direction.BOTH);
    builder.maxDepth(2);
    builder.withPath(false);
    builder.withVertex(true);
    request = builder.build();
    kneighborResult = kneighborAPI.post(request);
    Assert.assertEquals(5, kneighborResult.size());
    expected = ImmutableSet.of(vadasId, lopId, joshId, peterId, rippleId);
    Assert.assertEquals(expected, kneighborResult.ids());
    Assert.assertEquals(5, kneighborResult.vertices().size());
    expectedVids = ImmutableSet.of(vadasId, lopId, joshId, peterId, rippleId);
    for (Vertex vertex : kneighborResult.vertices()) {
        Assert.assertTrue(expectedVids.contains(vertex.id()));
    }
    builder = KneighborRequest.builder();
    builder.source(markoId);
    builder.step().direction(Direction.BOTH);
    builder.maxDepth(1);
    builder.withPath(true);
    builder.withVertex(true);
    request = builder.build();
    kneighborResult = kneighborAPI.post(request);
    Assert.assertEquals(3, kneighborResult.size());
    expected = ImmutableSet.of(vadasId, lopId, joshId);
    Assert.assertEquals(expected, kneighborResult.ids());
    Assert.assertEquals(3, kneighborResult.paths().size());
    Set<List<Object>> expectedPaths = ImmutableSet.of(ImmutableList.of(markoId, vadasId), ImmutableList.of(markoId, lopId), ImmutableList.of(markoId, joshId));
    for (Path path : kneighborResult.paths()) {
        Assert.assertTrue(expectedPaths.contains(path.objects()));
    }
    Assert.assertEquals(4, kneighborResult.vertices().size());
    expectedVids = ImmutableSet.of(markoId, vadasId, lopId, joshId);
    for (Vertex vertex : kneighborResult.vertices()) {
        Assert.assertTrue(expectedVids.contains(vertex.id()));
    }
    builder = KneighborRequest.builder();
    builder.source(markoId);
    builder.step().direction(Direction.BOTH);
    builder.maxDepth(2);
    builder.withPath(true);
    builder.withVertex(true);
    request = builder.build();
    kneighborResult = kneighborAPI.post(request);
    Assert.assertEquals(5, kneighborResult.size());
    expected = ImmutableSet.of(peterId, lopId, joshId, rippleId, vadasId);
    Assert.assertEquals(expected, kneighborResult.ids());
    Assert.assertEquals(5, kneighborResult.paths().size());
    expectedPaths = ImmutableSet.of(ImmutableList.of(markoId, vadasId), ImmutableList.of(markoId, lopId), ImmutableList.of(markoId, joshId), ImmutableList.of(markoId, lopId, peterId), ImmutableList.of(markoId, joshId, rippleId));
    for (Path path : kneighborResult.paths()) {
        Assert.assertTrue(expectedPaths.contains(path.objects()));
    }
    Assert.assertEquals(6, kneighborResult.vertices().size());
    expectedVids = ImmutableSet.of(markoId, peterId, lopId, joshId, rippleId, vadasId);
    for (Vertex vertex : kneighborResult.vertices()) {
        Assert.assertTrue(expectedVids.contains(vertex.id()));
    }
}
Also used : Path(com.baidu.hugegraph.structure.graph.Path) Kneighbor(com.baidu.hugegraph.structure.traverser.Kneighbor) Vertex(com.baidu.hugegraph.structure.graph.Vertex) KneighborRequest(com.baidu.hugegraph.structure.traverser.KneighborRequest) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Test(org.junit.Test) BaseApiTest(com.baidu.hugegraph.api.BaseApiTest)

Example 7 with Vertex

use of com.baidu.hugegraph.structure.graph.Vertex in project incubator-hugegraph-toolchain by apache.

the class NeighborRankApiTest method initNeighborRankGraph.

@BeforeClass
public static void initNeighborRankGraph() {
    GraphManager graph = graph();
    SchemaManager schema = schema();
    schema.propertyKey("name").asText().ifNotExist().create();
    schema.vertexLabel("person").properties("name").useCustomizeStringId().ifNotExist().create();
    schema.vertexLabel("movie").properties("name").useCustomizeStringId().ifNotExist().create();
    schema.edgeLabel("follow").sourceLabel("person").targetLabel("person").ifNotExist().create();
    schema.edgeLabel("like").sourceLabel("person").targetLabel("movie").ifNotExist().create();
    schema.edgeLabel("directedBy").sourceLabel("movie").targetLabel("person").ifNotExist().create();
    Vertex O = graph.addVertex(T.label, "person", T.id, "O", "name", "O");
    Vertex A = graph.addVertex(T.label, "person", T.id, "A", "name", "A");
    Vertex B = graph.addVertex(T.label, "person", T.id, "B", "name", "B");
    Vertex C = graph.addVertex(T.label, "person", T.id, "C", "name", "C");
    Vertex D = graph.addVertex(T.label, "person", T.id, "D", "name", "D");
    Vertex E = graph.addVertex(T.label, "movie", T.id, "E", "name", "E");
    Vertex F = graph.addVertex(T.label, "movie", T.id, "F", "name", "F");
    Vertex G = graph.addVertex(T.label, "movie", T.id, "G", "name", "G");
    Vertex H = graph.addVertex(T.label, "movie", T.id, "H", "name", "H");
    Vertex I = graph.addVertex(T.label, "movie", T.id, "I", "name", "I");
    Vertex J = graph.addVertex(T.label, "movie", T.id, "J", "name", "J");
    Vertex K = graph.addVertex(T.label, "person", T.id, "K", "name", "K");
    Vertex L = graph.addVertex(T.label, "person", T.id, "L", "name", "L");
    Vertex M = graph.addVertex(T.label, "person", T.id, "M", "name", "M");
    O.addEdge("follow", A);
    O.addEdge("follow", B);
    O.addEdge("follow", C);
    D.addEdge("follow", O);
    A.addEdge("follow", B);
    A.addEdge("like", E);
    A.addEdge("like", F);
    B.addEdge("like", G);
    B.addEdge("like", H);
    C.addEdge("like", I);
    C.addEdge("like", J);
    E.addEdge("directedBy", K);
    F.addEdge("directedBy", B);
    F.addEdge("directedBy", L);
    G.addEdge("directedBy", M);
}
Also used : Vertex(com.baidu.hugegraph.structure.graph.Vertex) GraphManager(com.baidu.hugegraph.driver.GraphManager) SchemaManager(com.baidu.hugegraph.driver.SchemaManager) BeforeClass(org.junit.BeforeClass)

Example 8 with Vertex

use of com.baidu.hugegraph.structure.graph.Vertex in project incubator-hugegraph-toolchain by apache.

the class NeighborRankApiTest method testNeighborRankWithIsolatedVertex.

@Test
public void testNeighborRankWithIsolatedVertex() {
    Vertex isolate = graph().addVertex(T.label, "person", T.id, "isolate", "name", "isolate-vertex");
    NeighborRankAPI.Request.Builder builder;
    builder = NeighborRankAPI.Request.builder();
    builder.source("isolate").alpha(0.9);
    builder.steps().direction(Direction.BOTH);
    NeighborRankAPI.Request request = builder.build();
    List<Ranks> ranks = neighborRankAPI.post(request);
    Assert.assertEquals(2, ranks.size());
    Assert.assertEquals(ImmutableMap.of("isolate", 1.0D), ranks.get(0));
    Assert.assertEquals(ImmutableMap.of(), ranks.get(1));
    graph().removeVertex(isolate.id());
}
Also used : Vertex(com.baidu.hugegraph.structure.graph.Vertex) Ranks(com.baidu.hugegraph.structure.traverser.Ranks) Test(org.junit.Test) BaseApiTest(com.baidu.hugegraph.api.BaseApiTest)

Example 9 with Vertex

use of com.baidu.hugegraph.structure.graph.Vertex in project incubator-hugegraph-toolchain by apache.

the class PathsApiTest method testPathsPostWithNearest.

@Test
public void testPathsPostWithNearest() {
    Object markoId = getVertexId("person", "name", "marko");
    Object rippleId = getVertexId("software", "name", "ripple");
    Object joshId = getVertexId("person", "name", "josh");
    Object lopId = getVertexId("software", "name", "lop");
    Vertex tom = graph().addVertex(T.label, "person", "name", "Tom", "age", 29, "city", "Shanghai");
    Vertex jim = graph().addVertex(T.label, "person", "name", "Jim", "age", 29, "city", "Shanghai");
    Vertex java = graph().addVertex(T.label, "software", "name", "java", "lang", "java", "price", 199);
    Object tomId = tom.id();
    Object jimId = jim.id();
    Object javaId = java.id();
    graph().addEdge(tomId, "created", rippleId, "date", "2016-01-10", "city", "Beijing");
    graph().addEdge(tomId, "created", javaId, "date", "2017-01-10", "city", "Hongkong");
    graph().addEdge(jimId, "created", javaId, "date", "2017-01-10", "city", "Hongkong");
    PathsRequest.Builder builder = PathsRequest.builder();
    builder.sources().ids(markoId);
    builder.targets().ids(jimId);
    builder.step().direction(Direction.BOTH);
    builder.maxDepth(6);
    PathsRequest request = builder.build();
    PathsWithVertices pathsWithVertices = pathsAPI.post(request);
    List<PathsWithVertices.Paths> paths = pathsWithVertices.paths();
    Assert.assertEquals(2, paths.size());
    List<List<Object>> expected = ImmutableList.of(ImmutableList.of(markoId, lopId, joshId, rippleId, tomId, javaId, jimId), ImmutableList.of(markoId, joshId, rippleId, tomId, javaId, jimId));
    for (PathsWithVertices.Paths path : paths) {
        Assert.assertTrue(expected.contains(path.objects()));
    }
    builder = PathsRequest.builder();
    builder.sources().ids(markoId);
    builder.targets().ids(jimId);
    builder.step().direction(Direction.BOTH);
    builder.nearest(true);
    builder.maxDepth(6);
    request = builder.build();
    pathsWithVertices = pathsAPI.post(request);
    paths = pathsWithVertices.paths();
    Assert.assertEquals(1, paths.size());
    expected = ImmutableList.of(ImmutableList.of(markoId, joshId, rippleId, tomId, javaId, jimId));
    for (PathsWithVertices.Paths path : paths) {
        Assert.assertTrue(expected.contains(path.objects()));
    }
}
Also used : Vertex(com.baidu.hugegraph.structure.graph.Vertex) PathsWithVertices(com.baidu.hugegraph.structure.traverser.PathsWithVertices) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) PathsRequest(com.baidu.hugegraph.structure.traverser.PathsRequest) Test(org.junit.Test) BaseApiTest(com.baidu.hugegraph.api.BaseApiTest)

Example 10 with Vertex

use of com.baidu.hugegraph.structure.graph.Vertex in project incubator-hugegraph-toolchain by apache.

the class RingsRaysApiTest method prepareSchemaAndGraph.

@BeforeClass
public static void prepareSchemaAndGraph() {
    BaseApiTest.initPropertyKey();
    BaseApiTest.initVertexLabel();
    BaseApiTest.initEdgeLabel();
    BaseApiTest.initIndexLabel();
    BaseApiTest.initVertex();
    BaseApiTest.initEdge();
    schema().vertexLabel("node").useCustomizeNumberId().ifNotExist().create();
    schema().edgeLabel("link").sourceLabel("node").targetLabel("node").ifNotExist().create();
    Vertex v1 = graph().addVertex(T.label, "node", T.id, 1);
    Vertex v2 = graph().addVertex(T.label, "node", T.id, 2);
    Vertex v3 = graph().addVertex(T.label, "node", T.id, 3);
    // Path length 5
    v1.addEdge("link", v2);
    v2.addEdge("link", v3);
    v3.addEdge("link", v1);
    v3.addEdge("link", v2);
}
Also used : Vertex(com.baidu.hugegraph.structure.graph.Vertex) BeforeClass(org.junit.BeforeClass)

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