Java Code Examples for org.apache.tinkerpop.gremlin.structure.Vertex#addEdge()

The following examples show how to use org.apache.tinkerpop.gremlin.structure.Vertex#addEdge() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestLocalDateArray.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void testZonedDateTimeArrayOnEdge() {
    Assume.assumeTrue(this.sqlgGraph.getSqlDialect().supportsZonedDateTimeArrayValues());
    ZoneId zoneIdShanghai = ZoneId.of("Asia/Shanghai");
    ZonedDateTime zonedDateTimeAGT = ZonedDateTime.of(LocalDateTime.now(), zoneIdShanghai);
    ZoneId zoneIdHarare = ZoneId.of("Africa/Harare");
    ZonedDateTime zonedDateTimeAGTHarare = ZonedDateTime.of(LocalDateTime.now(), zoneIdHarare);
    ZonedDateTime[] zonedDateTimes = {zonedDateTimeAGT, zonedDateTimeAGTHarare};

    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "zonedDateTimes", zonedDateTimes);
    Vertex a2 = this.sqlgGraph.addVertex(T.label, "A", "zonedDateTimes", zonedDateTimes);
    a1.addEdge("aa", a2, "zonedDateTimes", zonedDateTimes);
    this.sqlgGraph.tx().commit();

    //Create a new sqlgGraph
    try (SqlgGraph sqlgGraph1 = SqlgGraph.open(configuration)) {
        List<? extends Property<ZonedDateTime[]>> properties = sqlgGraph1.traversal().E().hasLabel("aa").<ZonedDateTime[]>properties("zonedDateTimes").toList();
        Assert.assertEquals(1, properties.size());
        Assert.assertTrue(properties.get(0).isPresent());
        ZonedDateTime[] localDateTimesFromDb = properties.get(0).value();
        Assert.assertArrayEquals(zonedDateTimes, localDateTimesFromDb);
    }
}
 
Example 2
Source File: TestLoadingAdjacent.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void testLoadEdges() {
    Vertex v1 = this.sqlgGraph.addVertex(T.label, "Person", "name", "john1");
    Vertex v2 = this.sqlgGraph.addVertex(T.label, "Person", "name", "john2");
    Edge e = v1.addEdge("friend", v2, "weight", 1);
    this.sqlgGraph.tx().commit();

    Iterator<Edge> adjacents = v1.edges(Direction.OUT);

    Assert.assertTrue(adjacents.hasNext());

    Edge adjacent = adjacents.next();

    Assert.assertFalse(adjacents.hasNext());

    Assert.assertEquals(e.id(), adjacent.id());
    Assert.assertEquals(toList(e.properties()), toList(adjacent.properties()));
}
 
Example 3
Source File: GraphSONMessageSerializerGremlinV1d0Test.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldSerializeEdgeProperty() throws Exception {
    final Graph graph = TinkerGraph.open();
    final Vertex v1 = graph.addVertex();
    final Vertex v2 = graph.addVertex();
    final Edge e = v1.addEdge("test", v2);
    e.property("abc", 123);

    final Iterable<Property<Object>> iterable = IteratorUtils.list(e.properties("abc"));
    final ResponseMessage response = convert(iterable);
    assertCommon(response);

    final List<Map<String, Object>> propertyList = (List<Map<String, Object>>) response.getResult().getData();
    assertEquals(1, propertyList.size());
    assertEquals(123, propertyList.get(0).get("value"));
}
 
Example 4
Source File: TestBatchedStreaming.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void testStreamingWithBatchSizeWithCallBack() throws InterruptedException {
    LinkedHashMap<String, Object> properties = new LinkedHashMap<>();
    List<Vertex> persons = new ArrayList<>();
    this.sqlgGraph.tx().streamingWithLockBatchModeOn();
    for (int i = 1; i <= 10; i++) {
        String uuid1 = UUID.randomUUID().toString();
        properties.put("id", uuid1);
        persons.add(this.sqlgGraph.addVertex("Person", properties));
    }
    this.sqlgGraph.tx().flush();
    Vertex previous = null;
    for (Vertex person : persons) {
        if (previous == null) {
            previous = person;
        } else {
            previous.addEdge("friend", person);
        }
    }
    this.sqlgGraph.tx().commit();
    testStreamingWithBatchSizeWithCallBack_assert(this.sqlgGraph);
    if (this.sqlgGraph1 != null) {
        Thread.sleep(1000);
        testStreamingWithBatchSizeWithCallBack_assert(this.sqlgGraph1);
    }
}
 
Example 5
Source File: TestRangeLimit.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void testRange() {
    Vertex a = this.sqlgGraph.addVertex(T.label, "A", "name", "a");
    for (int i = 0; i < 10; i++) {
        Vertex b = this.sqlgGraph.addVertex(T.label, "B", "name", "b" + i);
        a.addEdge("ab", b);
    }
    this.sqlgGraph.tx().commit();
    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal()
            .V().hasLabel("A")
            .out()
            .range(5, 6);
    Assert.assertEquals(4, traversal.getSteps().size());
    List<Vertex> vertices = traversal.toList();
    Assert.assertEquals(1, traversal.getSteps().size());
    Assert.assertTrue(traversal.getSteps().get(0) instanceof SqlgGraphStep);
    SqlgGraphStep sqlgGraphStep = (SqlgGraphStep) traversal.getSteps().get(0);
    assertStep(sqlgGraphStep, true, false, false, true, true);
    Assert.assertEquals(1, vertices.size());
}
 
Example 6
Source File: TestHas.java    From sqlg with MIT License 5 votes vote down vote up
@Test
public void testHasOnTableThatDoesNotExist() {
    Vertex v1 = this.sqlgGraph.addVertex(T.label, "Person");
    Vertex v2 = this.sqlgGraph.addVertex(T.label, "Person");
    v1.addEdge("friend", v2);
    this.sqlgGraph.tx().commit();
    Assert.assertEquals(0, this.sqlgGraph.traversal().V().has(T.label, "friend").has("weight", "5").count().next(), 0);
    Assert.assertFalse(this.sqlgGraph.traversal().V().has(T.label, "xxx").hasNext());
    Assert.assertFalse(this.sqlgGraph.traversal().V().has(T.label, "public.xxx").hasNext());
}
 
Example 7
Source File: TestRepeatStepGraphOut.java    From sqlg with MIT License 5 votes vote down vote up
@Test
public void testDuplicatePathToSelf() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "name", "a1");
    Vertex a2 = this.sqlgGraph.addVertex(T.label, "A", "name", "a2");
    Vertex a3 = this.sqlgGraph.addVertex(T.label, "A", "name", "a3");

    a1.addEdge("knows", a2);
    a2.addEdge("knows", a3);
    this.sqlgGraph.tx().commit();

    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal()
            .V().repeat(__.out("knows")).times(2).emit();
    Assert.assertEquals(2, traversal.getSteps().size());
    List<Vertex> vertices = traversal.toList();
    Assert.assertEquals(1, traversal.getSteps().size());
    Assert.assertEquals(3, vertices.size());
    Assert.assertTrue(vertices.remove(a2));
    Assert.assertTrue(vertices.remove(a3));
    Assert.assertTrue(vertices.remove(a3));
    Assert.assertEquals(0, vertices.size());

    DefaultGraphTraversal<Vertex, Path> traversal1 = (DefaultGraphTraversal<Vertex, Path>) this.sqlgGraph.traversal()
            .V().repeat(__.out("knows")).emit().times(2).path();
    Assert.assertEquals(3, traversal1.getSteps().size());
    List<Path> paths = traversal1.toList();
    Assert.assertEquals(2, traversal1.getSteps().size());
    Assert.assertEquals(3, paths.size());
    Assert.assertTrue(paths.stream().anyMatch(p -> p.size() == 2 && p.get(0).equals(a1) && p.get(1).equals(a2)));
    paths.remove(paths.stream().filter(p -> p.size() == 2 && p.get(0).equals(a1) && p.get(1).equals(a2)).findAny().orElseThrow(IllegalStateException::new));
    Assert.assertTrue(paths.stream().anyMatch(p -> p.size() == 3 && p.get(0).equals(a1) && p.get(1).equals(a2) && p.get(2).equals(a3)));
    paths.remove(paths.stream().filter(p -> p.size() == 3 && p.get(0).equals(a1) && p.get(1).equals(a2) && p.get(2).equals(a3)).findAny().orElseThrow(IllegalStateException::new));
    Assert.assertTrue(paths.stream().anyMatch(p -> p.size() == 2 && p.get(0).equals(a2) && p.get(1).equals(a3)));
    paths.remove(paths.stream().filter(p -> p.size() == 2 && p.get(0).equals(a2) && p.get(1).equals(a3)).findAny().orElseThrow(IllegalStateException::new));
    Assert.assertTrue(paths.isEmpty());
}
 
Example 8
Source File: TestGlobalUniqueIndex.java    From sqlg with MIT License 5 votes vote down vote up
@SuppressWarnings("OptionalGetWithoutIsPresent")
@Test
public void testGlobalUniqueIndexAcrossDifferentEdges() {
    Map<String, PropertyType> properties = new HashMap<>();
    properties.put("a", PropertyType.STRING);
    VertexLabel aVertexLabel = this.sqlgGraph.getTopology().getPublicSchema().ensureVertexLabelExist("A", properties);
    VertexLabel bVertexLabel = this.sqlgGraph.getTopology().getPublicSchema().ensureVertexLabelExist("B", properties);
    EdgeLabel abEdgeLabel = aVertexLabel.ensureEdgeLabelExist("ab", bVertexLabel, properties);
    VertexLabel cVertexLabel = this.sqlgGraph.getTopology().getPublicSchema().ensureVertexLabelExist("C", properties);
    VertexLabel dVertexLabel = this.sqlgGraph.getTopology().getPublicSchema().ensureVertexLabelExist("D", properties);
    EdgeLabel cdEdgeLabel = cVertexLabel.ensureEdgeLabelExist("cd", dVertexLabel, properties);

    PropertyColumn abEdgeProperty = abEdgeLabel.getProperty("a").get();
    PropertyColumn cdEdgeProperty = cdEdgeLabel.getProperty("a").get();
    Set<PropertyColumn> globalUniqueIndexProperties = new HashSet<>();
    globalUniqueIndexProperties.add(abEdgeProperty);
    globalUniqueIndexProperties.add(cdEdgeProperty);
    this.sqlgGraph.getTopology().ensureGlobalUniqueIndexExist(globalUniqueIndexProperties);
    this.sqlgGraph.tx().commit();

    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "a", "132");
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B", "a", "132");
    a1.addEdge("ab", b1, "a", "123");
    Vertex c1 = this.sqlgGraph.addVertex(T.label, "C", "a", "132");
    Vertex d1 = this.sqlgGraph.addVertex(T.label, "D", "a", "132");
    try {
        c1.addEdge("cd", d1, "a", "123");
        fail("GlobalUniqueIndex should prevent this from happening");
    } catch (Exception e) {
        //swallow
    }
}
 
Example 9
Source File: TestUserSuppliedPKBulkMode.java    From sqlg with MIT License 5 votes vote down vote up
@Test
public void testVertexAndEdgeLabelUserSuppliedBulkMode() {
    @SuppressWarnings("Duplicates")
    VertexLabel aVertexLabel = this.sqlgGraph.getTopology().ensureVertexLabelExist(
            "A",
            new HashMap<String, PropertyType>() {{
                put("name", PropertyType.STRING);
            }},
            ListOrderedSet.listOrderedSet(Collections.singletonList("name"))
    );
    VertexLabel bVertexLabel = this.sqlgGraph.getTopology().ensureVertexLabelExist(
            "B",
            new HashMap<String, PropertyType>() {{
                put("name", PropertyType.STRING);
            }},
            ListOrderedSet.listOrderedSet(Collections.singletonList("name"))
    );
    aVertexLabel.ensureEdgeLabelExist(
            "ab",
            bVertexLabel,
            new HashMap<String, PropertyType>() {{
                put("uid", PropertyType.STRING);
                put("country", PropertyType.STRING);
            }},
            ListOrderedSet.listOrderedSet(Arrays.asList("uid", "country")));
    this.sqlgGraph.tx().commit();
    this.sqlgGraph.tx().normalBatchModeOn();
    for (int i = 0; i < 100; i++) {
        Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "name", "a" + i);
        Vertex b1 = this.sqlgGraph.addVertex(T.label, "B", "name", "b" + i);
        a1.addEdge("ab", b1, "uid", UUID.randomUUID().toString(), "country", "SA");
    }
    this.sqlgGraph.tx().commit();

    Assert.assertEquals(100, this.sqlgGraph.traversal().V().hasLabel("A").out().toList().size());
}
 
Example 10
Source File: TestBulkWithout.java    From sqlg with MIT License 5 votes vote down vote up
@Test
public void testBulkWithout() throws InterruptedException {
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    if (this.sqlgGraph.getSqlDialect().supportsBatchMode()) {
        this.sqlgGraph.tx().normalBatchModeOn();
    }
    Vertex god = this.sqlgGraph.addVertex(T.label, "God");
    List<String> uuids = new ArrayList<>();
    for (int i = 0; i < 100; i++) {
        String uuid = UUID.randomUUID().toString();
        uuids.add(uuid);
        Vertex person = this.sqlgGraph.addVertex(T.label, "Person", "idNumber", uuid);
        god.addEdge("creator", person);
    }
    this.sqlgGraph.tx().commit();
    stopWatch.stop();
    System.out.println(stopWatch.toString());
    stopWatch.reset();
    stopWatch.start();
    testBulkWithout_assert(this.sqlgGraph, uuids);
    if (this.sqlgGraph1 != null) {
        Thread.sleep(SLEEP_TIME);
        testBulkWithout_assert(this.sqlgGraph1, uuids);
    }
    stopWatch.stop();
    System.out.println(stopWatch.toString());
}
 
Example 11
Source File: TestGraphStepOrderBy.java    From sqlg with MIT License 5 votes vote down vote up
@Test
public void testOrderOnEdgeMultipleLabels() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A");
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B");
    Vertex b2 = this.sqlgGraph.addVertex(T.label, "B");
    Vertex b3 = this.sqlgGraph.addVertex(T.label, "B");
    Vertex c1 = this.sqlgGraph.addVertex(T.label, "C");
    Vertex c2 = this.sqlgGraph.addVertex(T.label, "C");
    Vertex c3 = this.sqlgGraph.addVertex(T.label, "C");
    Edge e1 = a1.addEdge("ab", b1, "order", 0);
    Edge e2 = a1.addEdge("ab", b2, "order", 1);
    Edge e3 = a1.addEdge("ab", b3, "order", 2);
    Edge ec1 = a1.addEdge("ac", c1, "order", 3);
    Edge ec2 = a1.addEdge("ac", c2, "order", 4);
    Edge ec3 = a1.addEdge("ac", c3, "order", 5);
    this.sqlgGraph.tx().commit();
    DefaultGraphTraversal<Vertex, Edge> traversal = (DefaultGraphTraversal<Vertex, Edge>) this.sqlgGraph.traversal()
            .V().hasLabel("A")
            .outE()
            .order().by("order", Order.decr);
    Assert.assertEquals(4, traversal.getSteps().size());
    List<Edge> edges = traversal.toList();
    Assert.assertEquals(1, traversal.getSteps().size());
    assertStep(traversal.getSteps().get(0), true, true, true, true);
    Assert.assertEquals(6, edges.size());
    Assert.assertEquals(ec3, edges.get(0));
    Assert.assertEquals(ec2, edges.get(1));
    Assert.assertEquals(ec1, edges.get(2));
    Assert.assertEquals(e3, edges.get(3));
    Assert.assertEquals(e2, edges.get(4));
    Assert.assertEquals(e1, edges.get(5));
}
 
Example 12
Source File: TestRepeatStepVertexOut.java    From sqlg with MIT License 4 votes vote down vote up
@Test
public void testEmitAfterTimesAfterAndBefore() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "name", "a1");
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B", "name", "b1");
    Vertex c1 = this.sqlgGraph.addVertex(T.label, "C", "name", "c1");
    Vertex d1 = this.sqlgGraph.addVertex(T.label, "D", "name", "d1");
    a1.addEdge("ab", b1);
    b1.addEdge("bc", c1);
    c1.addEdge("cd", d1);
    this.sqlgGraph.tx().commit();

    DefaultGraphTraversal<Vertex, Path> traversal = (DefaultGraphTraversal<Vertex, Path>) this.sqlgGraph.traversal()
            .V(a1).repeat(__.out()).emit().times(2).path();
    Assert.assertEquals(3, traversal.getSteps().size());
    List<Path> paths = traversal.toList();
    Assert.assertEquals(2, traversal.getSteps().size());
    Assert.assertEquals(2, paths.size());
    Assert.assertTrue(paths.stream().anyMatch(p -> p.size() == 2 && p.get(0).equals(a1) && p.get(1).equals(b1)));
    paths.remove(paths.stream().filter(p -> p.size() == 2 && p.get(0).equals(a1) && p.get(1).equals(b1)).findAny().get());
    Assert.assertTrue(paths.stream().anyMatch(p -> p.size() == 3 && p.get(0).equals(a1) && p.get(1).equals(b1) && p.get(2).equals(c1)));
    paths.remove(paths.stream().filter(p -> p.size() == 3 && p.get(0).equals(a1) && p.get(1).equals(b1) && p.get(2).equals(c1)).findAny().get());
    Assert.assertTrue(paths.isEmpty());

    DefaultGraphTraversal<Vertex, Path> traversal1 = (DefaultGraphTraversal<Vertex, Path>) this.sqlgGraph.traversal()
            .V(a1).repeat(__.out()).emit().times(3).path();
    Assert.assertEquals(3, traversal1.getSteps().size());
    paths = traversal1.toList();
    Assert.assertEquals(2, traversal1.getSteps().size());
    Assert.assertEquals(3, paths.size());
    Assert.assertTrue(paths.stream().anyMatch(p -> p.size() == 2 && p.get(0).equals(a1) && p.get(1).equals(b1)));
    paths.remove(paths.stream().filter(p -> p.size() == 2 && p.get(0).equals(a1) && p.get(1).equals(b1)).findAny().get());
    Assert.assertTrue(paths.stream().anyMatch(p -> p.size() == 3 && p.get(0).equals(a1) && p.get(1).equals(b1) && p.get(2).equals(c1)));
    paths.remove(paths.stream().filter(p -> p.size() == 3 && p.get(0).equals(a1) && p.get(1).equals(b1) && p.get(2).equals(c1)).findAny().get());
    Assert.assertTrue(paths.stream().anyMatch(p -> p.size() == 4 && p.get(0).equals(a1) && p.get(1).equals(b1) && p.get(2).equals(c1) && p.get(3).equals(d1)));
    paths.remove(paths.stream().filter(p -> p.size() == 4 && p.get(0).equals(a1) && p.get(1).equals(b1) && p.get(2).equals(c1) && p.get(3).equals(d1)).findAny().get());
    Assert.assertTrue(paths.isEmpty());

    DefaultGraphTraversal<Vertex, Path> traversal2 = (DefaultGraphTraversal<Vertex, Path>) this.sqlgGraph.traversal()
            .V(a1).times(2).repeat(__.out()).emit().path();
    Assert.assertEquals(3, traversal2.getSteps().size());
    paths = traversal2.toList();
    Assert.assertEquals(2, traversal2.getSteps().size());
    Assert.assertEquals(3, paths.size());
    Assert.assertTrue(paths.stream().anyMatch(p -> p.size() == 2 && p.get(0).equals(a1) && p.get(1).equals(b1)));
    paths.remove(paths.stream().filter(p -> p.size() == 2 && p.get(0).equals(a1) && p.get(1).equals(b1)).findAny().get());
    Assert.assertTrue(paths.stream().anyMatch(p -> p.size() == 3 && p.get(0).equals(a1) && p.get(1).equals(b1) && p.get(2).equals(c1)));
    paths.remove(paths.stream().filter(p -> p.size() == 3 && p.get(0).equals(a1) && p.get(1).equals(b1) && p.get(2).equals(c1)).findAny().get());
    Assert.assertTrue(paths.stream().anyMatch(p -> p.size() == 3 && p.get(0).equals(a1) && p.get(1).equals(b1) && p.get(2).equals(c1)));
    paths.remove(paths.stream().filter(p -> p.size() == 3 && p.get(0).equals(a1) && p.get(1).equals(b1) && p.get(2).equals(c1)).findAny().get());
    Assert.assertTrue(paths.isEmpty());
}
 
Example 13
Source File: SqlFactory.java    From sql-gremlin with Apache License 2.0 4 votes vote down vote up
public static Graph createSpaceGraph(Graph graph) {
        // companies
        Vertex acmeSpaceCo = graph.addVertex(label, "company", "name", "Acme Space");
        Vertex newFrontiers = graph.addVertex(label, "company", "name", "New Frontiers");
        Vertex tomorrowUnlimited = graph.addVertex(label, "company", "name", "Tomorrow Unlimited");
        Vertex spaceTruckers = graph.addVertex(label, "company", "name", "Space Truckers");
        Vertex bankruptCo = graph.addVertex(label, "company", "name", "Bankrupt Co.");

        // planets
        Vertex earth = graph.addVertex(label, "planet", "name", "earth");
        Vertex mars = graph.addVertex(label, "planet", "name", "mars");
        Vertex saturn = graph.addVertex(label, "planet", "name", "saturn");
        Vertex jupiter = graph.addVertex(label, "planet", "name", "jupiter");

        // astronauts
        Vertex tom = graph.addVertex(label, "person", "name", "Tom", "age", 35);
        Vertex patty = graph.addVertex(label, "person", "name", "Patty", "age", 29);
        Vertex phil = graph.addVertex(label, "person", "name", "Phil", "age", 30);
        Vertex susan = graph.addVertex(label, "person", "name", "Susan", "age", 45);
        Vertex juanita = graph.addVertex(label, "person", "name", "Juanita", "age", 50);
        Vertex pavel = graph.addVertex(label, "person", "name", "Pavel", "age", 30);

        // spaceships
        Vertex spaceship1 = graph.addVertex(label, "spaceship", "name", "Ship 1", "model", "delta 1");
        Vertex spaceship2 = graph.addVertex(label, "spaceship", "name", "Ship 2", "model", "delta 1");
        Vertex spaceship3 = graph.addVertex(label, "spaceship", "name", "Ship 3", "model", "delta 2");
        Vertex spaceship4 = graph.addVertex(label, "spaceship", "name", "Ship 4", "model", "delta 3");

        // satellite
        Vertex satellite1 = graph.addVertex(label, "satellite", "name", "sat1");
        Vertex satellite2 = graph.addVertex(label, "satellite", "name", "sat2");
        Vertex satellite3 = graph.addVertex(label, "satellite", "name", "sat3");

        // rocket fuel
        Vertex s1Fuel = graph.addVertex(label, "sensor", "type", "rocket fuel");
//        Vertex s2Fuel = graph.addVertex(label, "sensor", "type", "rocket fuel");
//        Vertex s3Fuel = graph.addVertex(label, "sensor", "type", "rocket fuel");
//        Vertex s4Fuel = graph.addVertex(label, "sensor", "type", "rocket fuel");

        // astronaut company relationships
        tom.addEdge("worksFor", acmeSpaceCo, "yearsWorked", 5);
        patty.addEdge("worksFor", acmeSpaceCo, "yearsWorked", 1);
        phil.addEdge("worksFor", newFrontiers, "yearsWorked", 9);
        susan.addEdge("worksFor", tomorrowUnlimited, "yearsWorked", 4);
        juanita.addEdge("worksFor", spaceTruckers, "yearsWorked", 4);
        pavel.addEdge("worksFor", spaceTruckers, "yearsWorked", 10);

        // astronaut spaceship
        tom.addEdge("pilots", spaceship1);
        patty.addEdge("pilots", spaceship1);
        phil.addEdge("pilots", spaceship2);
        susan.addEdge("pilots", spaceship3);
        juanita.addEdge("pilots", spaceship4);
        pavel.addEdge("pilots", spaceship4);

        // astronauts to planets
        tom.addEdge("fliesTo", earth).property("trips", 10);
        tom.addEdge("fliesTo", mars).property("trips", 3);
        patty.addEdge("fliesTo", mars).property("trips", 1);
        phil.addEdge("fliesTo", saturn).property("trips", 9);
        phil.addEdge("fliesTo", earth).property("trips", 4);
        susan.addEdge("fliesTo", jupiter).property("trips", 20);
        juanita.addEdge("fliesTo", earth).property("trips", 4);
        juanita.addEdge("fliesTo", saturn).property("trips", 7);
        juanita.addEdge("fliesTo", jupiter).property("trips", 9);
        pavel.addEdge("fliesTo", mars).property("trips", 0);

        // astronaut friends
        tom.addEdge("friendsWith", patty);
        patty.addEdge("friendsWith", juanita);
        phil.addEdge("friendsWith", susan);
        susan.addEdge("friendsWith", pavel);

        // satellites to planets
        satellite1.addEdge("orbits", earth).property("launched", 1995);
        satellite2.addEdge("orbits", mars).property("launched", 2020);
        satellite3.addEdge("orbits", jupiter).property("launched", 2005);

        // fuel sensor readings
        long timestamp = 1765258774000L;
        for(int i = 0; i < 10; i++) {
            Vertex s1Reading = graph.addVertex(label, "sensorReading", "timestamp", timestamp, "date", timestamp, "value", 10.0);
            s1Fuel.addEdge("hasReading", s1Reading);
            timestamp += TimeUnit.MINUTES.toMillis(5);
        }

        return graph;
    }
 
Example 14
Source File: TestSimpleJoinGremlin.java    From sqlg with MIT License 4 votes vote down vote up
@Test
public void testSinglePath() {
    VertexLabel person = this.sqlgGraph.getTopology().ensureVertexLabelExist(
            "Person",
            new HashMap<String, PropertyType>(){{
                put("name", PropertyType.varChar(100));
                put("surname", PropertyType.varChar(100));
            }},
            ListOrderedSet.listOrderedSet(Arrays.asList("name", "surname"))
    );
    VertexLabel address = this.sqlgGraph.getTopology().ensureVertexLabelExist(
            "Address",
            new HashMap<String, PropertyType>(){{
                put("street", PropertyType.varChar(100));
                put("suburb", PropertyType.varChar(100));
            }},
            ListOrderedSet.listOrderedSet(Arrays.asList("street", "suburb"))
    );
    @SuppressWarnings("unused")
    EdgeLabel livesAt = person.ensureEdgeLabelExist(
            "livesAt",
            address,
            new HashMap<String, PropertyType>() {{
                put("country", PropertyType.STRING);
            }}
    );
    this.sqlgGraph.tx().commit();

    Vertex person1 = this.sqlgGraph.addVertex(T.label, "Person", "name", "John", "surname", "Smith");
    Vertex address1 = this.sqlgGraph.addVertex(T.label, "Address", "street", "X", "suburb", "Y");
    Edge livesAt1 = person1.addEdge("livesAt", address1, "country", "moon");
    this.sqlgGraph.tx().commit();

    List<Edge> livesAtEdges = this.sqlgGraph.traversal().V().hasLabel("Person").outE().toList();
    Assert.assertEquals(1, livesAtEdges.size());
    Assert.assertEquals(livesAt1, livesAtEdges.get(0));

    List<Vertex> livesAts = this.sqlgGraph.traversal().V().hasLabel("Person").out().toList();
    Assert.assertEquals(1, livesAts.size());
    Assert.assertEquals(address1, livesAts.get(0));
    Assert.assertEquals("X", livesAts.get(0).value("street"));
    Assert.assertEquals("Y", livesAts.get(0).value("suburb"));

    List<Map<String, Object>> result = this.sqlgGraph.traversal()
            .V().hasLabel("Person").as("a")
            .outE().as("b")
            .otherV().as("c")
            .select("a", "b", "c").toList();
    Assert.assertEquals(1, result.size());
    Assert.assertEquals(3, result.get(0).size());
    Assert.assertEquals(person1, result.get(0).get("a"));
    Assert.assertEquals(livesAt1, result.get(0).get("b"));
    Assert.assertEquals(address1, result.get(0).get("c"));

    livesAtEdges = this.sqlgGraph.traversal().V().hasLabel("Address").inE().toList();
    Assert.assertEquals(1, livesAtEdges.size());
    Assert.assertEquals(livesAt1, livesAtEdges.get(0));

    List<Vertex> persons = this.sqlgGraph.traversal().V().hasLabel("Address").in().toList();
    Assert.assertEquals(1, persons.size());
    Assert.assertEquals(person1, persons.get(0));
    Assert.assertEquals("John", persons.get(0).value("name"));
    Assert.assertEquals("Smith", persons.get(0).value("surname"));

    result = this.sqlgGraph.traversal()
            .V().hasLabel("Address").as("a")
            .inE().as("b")
            .otherV().as("c")
            .select("a", "b", "c").toList();
    Assert.assertEquals(1, result.size());
    Assert.assertEquals(3, result.get(0).size());
    Assert.assertEquals(address1, result.get(0).get("a"));
    Assert.assertEquals(livesAt1, result.get(0).get("b"));
    Assert.assertEquals(person1, result.get(0).get("c"));
}
 
Example 15
Source File: TestLocalVertexStepRepeatStep.java    From sqlg with MIT License 4 votes vote down vote up
@Test
public void testRepeatWithEmitFirst() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "name", "a1");
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B", "name", "b1");
    Vertex b2 = this.sqlgGraph.addVertex(T.label, "B", "name", "b2");
    Vertex b3 = this.sqlgGraph.addVertex(T.label, "B", "name", "b3");
    a1.addEdge("ab", b1);
    a1.addEdge("ab", b2);
    a1.addEdge("ab", b3);
    Vertex c1 = this.sqlgGraph.addVertex(T.label, "C", "name", "c1");
    Vertex c2 = this.sqlgGraph.addVertex(T.label, "C", "name", "c2");
    Vertex c3 = this.sqlgGraph.addVertex(T.label, "C", "name", "c3");
    b1.addEdge("bc", c1);
    b1.addEdge("bc", c1);
    b1.addEdge("bc", c2);
    b1.addEdge("bc", c3);
    Vertex d1 = this.sqlgGraph.addVertex(T.label, "D", "name", "d1");
    Vertex d2 = this.sqlgGraph.addVertex(T.label, "D", "name", "d2");
    Vertex d3 = this.sqlgGraph.addVertex(T.label, "D", "name", "d3");
    c1.addEdge("cd", d1);
    c1.addEdge("cd", d2);
    c1.addEdge("cd", d3);
    this.sqlgGraph.tx().commit();

    List<Vertex> vertices = this.sqlgGraph.traversal().V().hasLabel("A").local(__.<Vertex>emit().repeat(__.out("ab", "bc", "cd")).times(3)).toList();
    assertEquals(14, vertices.size());
    assertTrue(vertices.remove(a1));
    assertTrue(vertices.remove(b1));
    assertTrue(vertices.remove(b2));
    assertTrue(vertices.remove(b3));
    assertTrue(vertices.remove(c1));
    assertTrue(vertices.remove(c1));
    assertTrue(vertices.remove(c2));
    assertTrue(vertices.remove(c3));
    assertTrue(vertices.remove(d1));
    assertTrue(vertices.remove(d2));
    assertTrue(vertices.remove(d3));
    assertTrue(vertices.remove(d1));
    assertTrue(vertices.remove(d2));
    assertTrue(vertices.remove(d3));
    assertTrue(vertices.isEmpty());
}
 
Example 16
Source File: TestHasLabelAndId.java    From sqlg with MIT License 4 votes vote down vote up
@Test
public void testHasLabelAndIdOnInEdge() {
    Vertex a = this.sqlgGraph.addVertex(T.label, "A");
    Vertex b = this.sqlgGraph.addVertex(T.label, "B");
    Vertex c = this.sqlgGraph.addVertex(T.label, "C");
    Vertex d = this.sqlgGraph.addVertex(T.label, "D");
    Edge ab = b.addEdge("ab", a);
    Edge ac = c.addEdge("ac", a);
    Edge ad = d.addEdge("ad", a);
    this.sqlgGraph.tx().commit();

    GraphTraversal<Vertex, Edge> traversal = this.sqlgGraph.traversal().V().hasLabel("A").inE().hasLabel("ab", "ac");
    List<Edge> edges = traversal.toList();
    Assert.assertEquals(2, edges.size());
    Assert.assertTrue(edges.contains(ab) && edges.contains(ac));

    traversal = this.sqlgGraph.traversal().V().hasLabel("A").inE().hasLabel("ab").hasLabel("ac");
    edges = traversal.toList();
    Assert.assertEquals(0, edges.size());

    traversal = this.sqlgGraph.traversal().V().hasLabel("A").inE().hasLabel("ab", "ac").hasLabel("ac");
    edges = traversal.toList();
    Assert.assertEquals(1, edges.size());
    Assert.assertEquals(ac, edges.get(0));

    traversal = this.sqlgGraph.traversal().V().hasLabel("A").inE().hasLabel("ab", "ac").hasId(ac.id());
    edges = traversal.toList();
    Assert.assertEquals(1, edges.size());
    Assert.assertEquals(ac, edges.get(0));

    traversal = this.sqlgGraph.traversal().V().hasLabel("A").inE().hasLabel("ab", "ac").has(T.id, P.within(ab.id(), ac.id()));
    edges = traversal.toList();
    Assert.assertEquals(2, edges.size());
    Assert.assertTrue(edges.contains(ab) && edges.contains(ac));

    traversal = this.sqlgGraph.traversal().V().hasLabel("A").inE().hasLabel("ab", "ac").has(T.id, P.without(ab.id(), ac.id()));
    edges = traversal.toList();
    Assert.assertEquals(0, edges.size());

    traversal = this.sqlgGraph.traversal().V().hasLabel("A").inE().hasLabel("ab", "ac").has(T.id, P.neq(ab.id()));
    edges = traversal.toList();
    Assert.assertEquals(1, edges.size());
    Assert.assertEquals(ac, edges.get(0));
}
 
Example 17
Source File: TestUnoptimizedRepeatStep.java    From sqlg with MIT License 4 votes vote down vote up
@Test
public void testUnoptimizedRepeatDeep() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "hub", true);
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B", "hub", false);
    Vertex b2 = this.sqlgGraph.addVertex(T.label, "B", "hub", false);
    Vertex b3 = this.sqlgGraph.addVertex(T.label, "B", "hub", false);
    a1.addEdge("ab", b1);
    a1.addEdge("ab", b2);
    a1.addEdge("ab", b3);
    Vertex c11 = this.sqlgGraph.addVertex(T.label, "C", "hub", false);
    Vertex c12 = this.sqlgGraph.addVertex(T.label, "C", "hub", false);
    Vertex c13 = this.sqlgGraph.addVertex(T.label, "C", "hub", false);
    b1.addEdge("bc", c11);
    b1.addEdge("bc", c12);
    b1.addEdge("bc", c13);
    Vertex c21 = this.sqlgGraph.addVertex(T.label, "C", "hub", false);
    Vertex c22 = this.sqlgGraph.addVertex(T.label, "C", "hub", false);
    Vertex c23 = this.sqlgGraph.addVertex(T.label, "C", "hub", false);
    b2.addEdge("bc", c21);
    b2.addEdge("bc", c22);
    b2.addEdge("bc", c23);
    Vertex c31 = this.sqlgGraph.addVertex(T.label, "C", "hub", false);
    Vertex c32 = this.sqlgGraph.addVertex(T.label, "C", "hub", false);
    Vertex c33 = this.sqlgGraph.addVertex(T.label, "C", "hub", false);
    b3.addEdge("bc", c31);
    b3.addEdge("bc", c32);
    b3.addEdge("bc", c33);
    Vertex d = this.sqlgGraph.addVertex(T.label, "D", "hub", true);
    c11.addEdge("cd", d);
    c12.addEdge("cd", d);
    c13.addEdge("cd", d);
    c21.addEdge("cd", d);
    c22.addEdge("cd", d);
    c23.addEdge("cd", d);
    c31.addEdge("cd", d);
    c32.addEdge("cd", d);
    c33.addEdge("cd", d);
    this.sqlgGraph.tx().commit();

    List<Vertex> vertices = this.sqlgGraph.traversal()
            .V().hasLabel("A")
            .repeat(
                    __.out()
            )
            .until(__.has("hub", true))
            .toList();

    Assert.assertEquals(9, vertices.size());

}
 
Example 18
Source File: BitsyGraphIT.java    From bitsy with Apache License 2.0 4 votes vote down vote up
private Edge addEdge(Graph graph, Vertex vOut, Vertex vIn, String label) {
	return vOut.addEdge(label, vIn);
}
 
Example 19
Source File: TestLocalVertexStepOptionalWithOrder.java    From sqlg with MIT License 4 votes vote down vote up
@Test
public void testSimpleButLessSo() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "order", 1);
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B", "order", 1);
    Vertex b2 = this.sqlgGraph.addVertex(T.label, "B", "order", 2);
    Vertex b3 = this.sqlgGraph.addVertex(T.label, "B", "order", 3);
    Vertex a12 = this.sqlgGraph.addVertex(T.label, "A", "order", 10);
    Vertex b12 = this.sqlgGraph.addVertex(T.label, "B", "order", 1);
    Vertex b22 = this.sqlgGraph.addVertex(T.label, "B", "order", 2);
    Vertex b32 = this.sqlgGraph.addVertex(T.label, "B", "order", 3);
    a1.addEdge("ab", b1);
    a1.addEdge("ab", b2);
    a1.addEdge("ab", b3);
    a12.addEdge("ab", b12);
    a12.addEdge("ab", b22);
    a12.addEdge("ab", b32);
    this.sqlgGraph.tx().commit();
    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal()
            .V().hasLabel("A").as("a")
            .local(
                    __.out().as("b")
            )
            .order()
            .by(__.select("a").by("order"), Order.decr)
            .by(__.select("b").by("order"), Order.decr);
    Assert.assertEquals(4, traversal.getSteps().size());
    List<Vertex> vertices = traversal.toList();
    Assert.assertEquals(4, traversal.getSteps().size());
    Assert.assertTrue(traversal.getSteps().get(2) instanceof SqlgLocalStepBarrier);
    SqlgLocalStepBarrier<?, ?> localStep = (SqlgLocalStepBarrier<?, ?>) traversal.getSteps().get(2);
    List<SqlgVertexStep> sqlgVertexSteps = TraversalHelper.getStepsOfAssignableClassRecursively(SqlgVertexStep.class, localStep.getLocalChildren().get(0));
    Assert.assertEquals(1, sqlgVertexSteps.size());
    SqlgVertexStep sqlgVertexStep = sqlgVertexSteps.get(0);
    assertStep(sqlgVertexStep, false, false, false, true);
    Assert.assertEquals(6, vertices.size());
    Assert.assertEquals(b32, vertices.get(0));
    Assert.assertEquals(b22, vertices.get(1));
    Assert.assertEquals(b12, vertices.get(2));
    Assert.assertEquals(b3, vertices.get(3));
    Assert.assertEquals(b2, vertices.get(4));
    Assert.assertEquals(b1, vertices.get(5));
}
 
Example 20
Source File: EdgeManipulator.java    From timbuctoo with GNU General Public License v3.0 4 votes vote down vote up
private static Edge createDuplicate(Edge edgeToDuplicate) {
  Vertex sourceOfEdge = edgeToDuplicate.outVertex();
  Vertex targetOfEdge = edgeToDuplicate.inVertex();

  return sourceOfEdge.addEdge(edgeToDuplicate.label(), targetOfEdge);
}