Java Code Examples for org.apache.tinkerpop.gremlin.process.traversal.Path#get()

The following examples show how to use org.apache.tinkerpop.gremlin.process.traversal.Path#get() . 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: Scoping.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
/**
 * Finds the object with the specified key for the current traverser and throws an exception if the key cannot
 * be found.
 *
 * @throws KeyNotFoundException if the key does not exist
 */
public default <S> S getScopeValue(final Pop pop, final Object key, final Traverser.Admin<?> traverser) throws KeyNotFoundException {
    final Object object = traverser.get();
    if (object instanceof Map && ((Map) object).containsKey(key))
        return (S) ((Map) object).get(key);

    if (key instanceof String) {
        final String k = (String) key;
        if (traverser.getSideEffects().exists(k))
            return traverser.getSideEffects().get(k);

        final Path path = traverser.path();
        if (path.hasLabel(k))
            return null == pop ? path.get(k) : path.get(pop, k);
    }

    throw new KeyNotFoundException(key, this);
}
 
Example 2
Source File: ReferenceFactoryTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldDetachPathToReferenceWithEmbeddedLists() {
    final Path path = MutablePath.make();
    path.extend(DetachedVertex.build().setId(1).setLabel("person").
            addProperty(new DetachedVertexProperty<>(
                    101, "name", "stephen", Collections.emptyMap())).create(), Collections.singleton("a"));
    path.extend(Collections.singletonList(DetachedVertex.build().setId(2).setLabel("person").
            addProperty(new DetachedVertexProperty<>(
                    102, "name", "vadas", Collections.emptyMap())).create()), Collections.singleton("a"));
    path.extend(Collections.singletonList(Collections.singletonList(DetachedVertex.build().setId(3).setLabel("person").
            addProperty(new DetachedVertexProperty<>(
                    103, "name", "josh", Collections.emptyMap())).create())), Collections.singleton("a"));

    final Path detached = ReferenceFactory.detach(path);
    final Vertex v1  = detached.get(0);
    assertThat(v1, instanceOf(ReferenceVertex.class));
    assertThat(v1.properties().hasNext(), is(false));

    final Vertex v2  = (Vertex) ((List) detached.get(1)).get(0);
    assertThat(v2, instanceOf(ReferenceVertex.class));
    assertThat(v2.properties().hasNext(), is(false));

    final Vertex v3  = (Vertex) ((List) ((List) detached.get(2)).get(0)).get(0);
    assertThat(v3, instanceOf(ReferenceVertex.class));
    assertThat(v3.properties().hasNext(), is(false));
}
 
Example 3
Source File: DetachedFactoryTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDetachPathToReferenceWithEmbeddedLists() {
    final Path path = MutablePath.make();
    path.extend(DetachedVertex.build().setId(1).setLabel("person").
            addProperty(new DetachedVertexProperty<>(
                    101, "name", "stephen", Collections.emptyMap())).create(), Collections.singleton("a"));
    path.extend(Collections.singletonList(DetachedVertex.build().setId(2).setLabel("person").
            addProperty(new DetachedVertexProperty<>(
                    102, "name", "vadas", Collections.emptyMap())).create()), Collections.singleton("a"));
    path.extend(Collections.singletonList(Collections.singletonList(DetachedVertex.build().setId(3).setLabel("person").
            addProperty(new DetachedVertexProperty<>(
                    103, "name", "josh", Collections.emptyMap())).create())), Collections.singleton("a"));

    final Path detached = DetachedFactory.detach(path, true);
    final Vertex v1  = detached.get(0);
    assertThat(v1, instanceOf(DetachedVertex.class));
    assertEquals("stephen", v1.values("name").next());
    assertEquals(1, IteratorUtils.count(v1.properties()));

    final Vertex v2  = (Vertex) ((List) detached.get(1)).get(0);
    assertThat(v2, instanceOf(DetachedVertex.class));
    assertEquals("vadas", v2.values("name").next());
    assertEquals(1, IteratorUtils.count(v2.properties()));

    final Vertex v3  = (Vertex) ((List) ((List) detached.get(2)).get(0)).get(0);
    assertThat(v3, instanceOf(DetachedVertex.class));
    assertEquals("josh", v3.values("name").next());
    assertEquals(1, IteratorUtils.count(v3.properties()));
}
 
Example 4
Source File: SerializationTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
@LoadGraphWith(LoadGraphWith.GraphData.MODERN)
public void shouldSerializePath() throws Exception {
    final ObjectMapper mapper = graph.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().version(GraphSONVersion.V2_0).create().createMapper();
    final Path p = g.V(convertToVertexId("marko")).as("a").outE().as("b").inV().as("c").path()
            .filter(t -> ((Vertex) t.get().objects().get(2)).value("name").equals("lop")).next();
    final String json = mapper.writeValueAsString(p);
    final Path detached = mapper.readValue(json, Path.class);

    assertNotNull(detached);
    assertEquals(p.labels().size(), detached.labels().size());
    assertEquals(p.labels().get(0).size(), detached.labels().get(0).size());
    assertEquals(p.labels().get(1).size(), detached.labels().get(1).size());
    assertEquals(p.labels().get(2).size(), detached.labels().get(2).size());
    assertTrue(p.labels().stream().flatMap(Collection::stream).allMatch(detached::hasLabel));

    final Vertex vOut = p.get("a");
    final Vertex detachedVOut = detached.get("a");
    assertEquals(vOut.label(), detachedVOut.label());
    assertEquals(vOut.id(), detachedVOut.id());

    // this is a SimpleTraverser so no properties are present in detachment
    assertFalse(detachedVOut.properties().hasNext());

    final Edge e = p.get("b");
    final Edge detachedE = detached.get("b");
    assertEquals(e.label(), detachedE.label());
    assertEquals(e.id(), detachedE.id());

    // this is a SimpleTraverser so no properties are present in detachment
    assertFalse(detachedE.properties().hasNext());

    final Vertex vIn = p.get("c");
    final Vertex detachedVIn = detached.get("c");
    assertEquals(vIn.label(), detachedVIn.label());
    assertEquals(vIn.id(), detachedVIn.id());

    // this is a SimpleTraverser so no properties are present in detachment
    assertFalse(detachedVIn.properties().hasNext());
}
 
Example 5
Source File: SerializationTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
@LoadGraphWith(LoadGraphWith.GraphData.MODERN)
public void shouldSerializePath() throws Exception {
    final ObjectMapper mapper = graph.io(GraphSONIo.build(GraphSONVersion.V3_0)).mapper().version(GraphSONVersion.V3_0).create().createMapper();
    final Path p = g.V(convertToVertexId("marko")).as("a").outE().as("b").inV().as("c").path()
            .filter(t -> ((Vertex) t.get().objects().get(2)).value("name").equals("lop")).next();
    final String json = mapper.writeValueAsString(p);
    final Path detached = mapper.readValue(json, Path.class);

    assertNotNull(detached);
    assertEquals(p.labels().size(), detached.labels().size());
    assertEquals(p.labels().get(0).size(), detached.labels().get(0).size());
    assertEquals(p.labels().get(1).size(), detached.labels().get(1).size());
    assertEquals(p.labels().get(2).size(), detached.labels().get(2).size());
    assertTrue(p.labels().stream().flatMap(Collection::stream).allMatch(detached::hasLabel));

    final Vertex vOut = p.get("a");
    final Vertex detachedVOut = detached.get("a");
    assertEquals(vOut.label(), detachedVOut.label());
    assertEquals(vOut.id(), detachedVOut.id());

    // this is a SimpleTraverser so no properties are present in detachment
    assertFalse(detachedVOut.properties().hasNext());

    final Edge e = p.get("b");
    final Edge detachedE = detached.get("b");
    assertEquals(e.label(), detachedE.label());
    assertEquals(e.id(), detachedE.id());

    // this is a SimpleTraverser so no properties are present in detachment
    assertFalse(detachedE.properties().hasNext());

    final Vertex vIn = p.get("c");
    final Vertex detachedVIn = detached.get("c");
    assertEquals(vIn.label(), detachedVIn.label());
    assertEquals(vIn.id(), detachedVIn.id());

    // this is a SimpleTraverser so no properties are present in detachment
    assertFalse(detachedVIn.properties().hasNext());
}
 
Example 6
Source File: SerializationTest.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Test
@LoadGraphWith(LoadGraphWith.GraphData.MODERN)
public void shouldSerializePathAsDetached() throws Exception {
    final GryoIo gryoIo = graph.io(GryoIo.build(GryoVersion.V1_0));
    final GryoWriter gryoWriter = gryoIo.writer().create();
    final GryoReader gryoReader = gryoIo.reader().create();

    final Path p = g.V(convertToVertexId("marko")).as("a").outE().as("b").inV().as("c").path()
            .filter(t -> ((Vertex) t.get().objects().get(2)).value("name").equals("lop")).next();
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    gryoWriter.writeObject(outputStream, p);

    final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
    final Path detached = gryoReader.readObject(inputStream, DetachedPath.class);
    assertNotNull(detached);
    assertEquals(p.labels().size(), detached.labels().size());
    assertEquals(p.labels().get(0).size(), detached.labels().get(0).size());
    assertEquals(p.labels().get(1).size(), detached.labels().get(1).size());
    assertEquals(p.labels().get(2).size(), detached.labels().get(2).size());
    assertTrue(p.labels().stream().flatMap(Collection::stream).allMatch(detached::hasLabel));

    final Vertex vOut = p.get("a");
    final Vertex detachedVOut = detached.get("a");
    assertEquals(vOut.label(), detachedVOut.label());
    assertEquals(vOut.id(), detachedVOut.id());

    // this is a SimpleTraverser so no properties are present in detachment
    assertFalse(detachedVOut.properties().hasNext());

    final Edge e = p.get("b");
    final Edge detachedE = detached.get("b");
    assertEquals(e.label(), detachedE.label());
    assertEquals(e.id(), detachedE.id());

    // this is a SimpleTraverser so no properties are present in detachment
    assertFalse(detachedE.properties().hasNext());

    final Vertex vIn = p.get("c");
    final Vertex detachedVIn = detached.get("c");
    assertEquals(vIn.label(), detachedVIn.label());
    assertEquals(vIn.id(), detachedVIn.id());

    // this is a SimpleTraverser so no properties are present in detachment
    assertFalse(detachedVIn.properties().hasNext());
}
 
Example 7
Source File: SerializationTest.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Test
@LoadGraphWith(LoadGraphWith.GraphData.MODERN)
public void shouldSerializePathAsDetached() throws Exception {
    final GryoIo gryoIo = graph.io(GryoIo.build(GryoVersion.V3_0));
    final GryoWriter gryoWriter = gryoIo.writer().create();
    final GryoReader gryoReader = gryoIo.reader().create();

    final Path p = g.V(convertToVertexId("marko")).as("a").outE().as("b").inV().as("c").path()
            .filter(t -> ((Vertex) t.get().objects().get(2)).value("name").equals("lop")).next();
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    gryoWriter.writeObject(outputStream, p);

    final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
    final Path detached = gryoReader.readObject(inputStream, DetachedPath.class);
    assertNotNull(detached);
    assertEquals(p.labels().size(), detached.labels().size());
    assertEquals(p.labels().get(0).size(), detached.labels().get(0).size());
    assertEquals(p.labels().get(1).size(), detached.labels().get(1).size());
    assertEquals(p.labels().get(2).size(), detached.labels().get(2).size());
    assertTrue(p.labels().stream().flatMap(Collection::stream).allMatch(detached::hasLabel));

    final Vertex vOut = p.get("a");
    final Vertex detachedVOut = detached.get("a");
    assertEquals(vOut.label(), detachedVOut.label());
    assertEquals(vOut.id(), detachedVOut.id());

    // this is a SimpleTraverser so no properties are present in detachment
    assertFalse(detachedVOut.properties().hasNext());

    final Edge e = p.get("b");
    final Edge detachedE = detached.get("b");
    assertEquals(e.label(), detachedE.label());
    assertEquals(e.id(), detachedE.id());

    // this is a SimpleTraverser so no properties are present in detachment
    assertFalse(detachedE.properties().hasNext());

    final Vertex vIn = p.get("c");
    final Vertex detachedVIn = detached.get("c");
    assertEquals(vIn.label(), detachedVIn.label());
    assertEquals(vIn.id(), detachedVIn.id());

    // this is a SimpleTraverser so no properties are present in detachment
    assertFalse(detachedVIn.properties().hasNext());
}
 
Example 8
Source File: TestUnion.java    From sqlg with MIT License 4 votes vote down vote up
@Test
public void testUnionHasPath() {
    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");
    Vertex a4 = this.sqlgGraph.addVertex(T.label, "A", "name", "A4");
    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");
    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");
    a1.addEdge("toB", b1);
    a1.addEdge("toB", b2);
    a1.addEdge("toB", b3);
    b1.addEdge("toC", c1);
    b2.addEdge("toC", c2);
    b3.addEdge("toC", c3);

    GraphTraversal<Vertex, Path> traversal = this.sqlgGraph.traversal().V().has("A", "name", "A1")
            .union(
                    __.out("toB").has("name", P.eq("B1")).out("toC"),
                    __.out("toB").has("name", P.eq("B2")).out("toC"))
            .path();
    printTraversalForm(traversal);

    Set<Object> objs = new HashSet<>();
    while (traversal.hasNext()) {
        Path p = traversal.next();
        Assert.assertEquals(3, p.size());
        Object root0 = p.get(0);
        Assert.assertEquals(a1, root0);
        Object child0 = p.get(1);
        Assert.assertEquals("B", ((Vertex) child0).label());
        Object child1 = p.get(2);
        Assert.assertEquals("C", ((Vertex) child1).label());
        objs.add(child0);
        objs.add(child1);
    }
    Assert.assertEquals(4, objs.size());
    Assert.assertTrue(objs.contains(b1));
    Assert.assertTrue(objs.contains(b2));
    Assert.assertTrue(objs.contains(c1));
    Assert.assertTrue(objs.contains(c2));

    traversal = this.sqlgGraph.traversal().V().hasLabel("A")
            .union(
                    __.optional(__.out("toB").has("name", P.eq("B1")).optional(__.out("toC"))),
                    __.optional(__.out("toB").has("name", P.eq("B2")).optional(__.out("toC"))))
            .path();
    printTraversalForm(traversal);
    List<Path> paths = traversal.toList();
    Assert.assertEquals(8, paths.size());
    Assert.assertEquals(2, paths.stream().filter(p -> p.size() == 3).count());
}
 
Example 9
Source File: TestOptionalWithOrder.java    From sqlg with MIT License 4 votes vote down vote up
@Test
public void testOrder() {

    Vertex a1 = sqlgGraph.addVertex(T.label, "A", "name", "a1");
    Vertex a2 = sqlgGraph.addVertex(T.label, "A", "name", "a2");
    Vertex b1 = sqlgGraph.addVertex(T.label, "B", "name", "b1");
    Vertex b2 = sqlgGraph.addVertex(T.label, "B", "name", "b2");

    a1.addEdge("e_a", b1);
    a2.addEdge("e_a", b2);

    GraphTraversal<Vertex, Path> t = sqlgGraph.traversal().V(a1, a2).order().by("name").optional(__.out()).path();
    Assert.assertTrue(t.hasNext());
    Path p = t.next();
    Assert.assertEquals(2, p.size());
    Vertex v1 = p.get(0);
    Assert.assertEquals("a1", v1.property("name").value());
    Vertex v2 = p.get(1);
    Assert.assertEquals("b1", v2.property("name").value());
    Assert.assertTrue(t.hasNext());
    p = t.next();
    Assert.assertEquals(2, p.size());
    v1 = p.get(0);
    Assert.assertEquals("a2", v1.property("name").value());
    v2 = p.get(1);
    Assert.assertEquals("b2", v2.property("name").value());

    b1.remove();
    this.sqlgGraph.tx().commit();

    List<Path> paths  = sqlgGraph.traversal().V(a2, a1).order().by("name").optional(__.out()).path().toList();
    Assert.assertEquals(2, paths.size());
    Assert.assertEquals(1, paths.get(0).size());

    t = sqlgGraph.traversal().V(a2, a1).order().by("name").optional(__.out()).path();
    Assert.assertTrue(t.hasNext());
    p = t.next();
    Assert.assertEquals(1, p.size());
    v1 = p.get(0);
    Assert.assertEquals("a1", v1.property("name").value());
    Assert.assertTrue(t.hasNext());
    p = t.next();
    Assert.assertEquals(2, p.size());
    v1 = p.get(0);
    Assert.assertEquals("a2", v1.property("name").value());
    v2 = p.get(1);
    Assert.assertEquals("b2", v2.property("name").value());
}