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

The following examples show how to use org.apache.tinkerpop.gremlin.process.traversal.Path#extend() . 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: GraphSONSerializersV3d0.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public Path deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
    final Path p = MutablePath.make();

    List<Object> labels = new ArrayList<>();
    List<Object> objects = new ArrayList<>();
    while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
        if (jsonParser.getCurrentName().equals(GraphSONTokens.LABELS)) {
            jsonParser.nextToken();
            labels = deserializationContext.readValue(jsonParser, List.class);
        } else if (jsonParser.getCurrentName().equals(GraphSONTokens.OBJECTS)) {
            jsonParser.nextToken();
            objects = deserializationContext.readValue(jsonParser, List.class);
        }
    }

    for (int i = 0; i < objects.size(); i++) {
        p.extend(objects.get(i), (Set<String>) labels.get(i));
    }

    return p;
}
 
Example 2
Source File: GraphSONSerializersV2d0.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public Path deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
    final JsonNode n = jsonParser.readValueAsTree();
    final Path p = MutablePath.make();

    final ArrayNode labels = (ArrayNode) n.get(GraphSONTokens.LABELS);
    final ArrayNode objects = (ArrayNode) n.get(GraphSONTokens.OBJECTS);

    for (int i = 0; i < objects.size(); i++) {
        final JsonParser po = objects.get(i).traverse();
        po.nextToken();
        final JsonParser pl = labels.get(i).traverse();
        pl.nextToken();
        p.extend(deserializationContext.readValue(po, Object.class), deserializationContext.readValue(pl, setType));
    }

    return p;
}
 
Example 3
Source File: ImmutablePath.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public Path retract(final Set<String> labels) {
    if (labels.isEmpty())
        return this;

    // get all the immutable path sections
    final List<ImmutablePath> immutablePaths = new ArrayList<>();
    ImmutablePath currentPath = this;
    while (true) {
        if (currentPath.isTail())
            break;
        immutablePaths.add(0, currentPath);
        currentPath = currentPath.previousPath;
    }
    // build a new immutable path using the respective path sections that are not to be retracted
    Path newPath = TAIL_PATH;
    for (final ImmutablePath immutablePath : immutablePaths) {
        final Set<String> temp = new LinkedHashSet<>(immutablePath.currentLabels);
        temp.removeAll(labels);
        if (!temp.isEmpty())
            newPath = newPath.extend(immutablePath.currentObject, temp);
    }
    return newPath;
}
 
Example 4
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 5
Source File: ShortestPathTestHelper.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
public Path makePath(final boolean includeEdges, final String... names) {
    Path path = ImmutablePath.make();
    boolean first = true;
    for (final String name : names) {
        final Vertex vertex = vertexCache.computeIfAbsent(name, test::convertToVertex);
        if (!first) {
            if (includeEdges) {
                final Object id1 = ((Vertex) path.get(path.size() - 1)).id();
                final Object id2 = vertex.id();
                final Edge edge;
                if (edgeCache.containsKey(id1)) {
                    edge = edgeCache.get(id1).computeIfAbsent(id2, id -> getEdge(id1, id));
                } else if (edgeCache.containsKey(id2)) {
                    edge = edgeCache.get(id2).computeIfAbsent(id1, id -> getEdge(id, id2));
                } else {
                    edgeCache.put(id1, new HashMap<>());
                    edgeCache.get(id1).put(id2, edge = getEdge(id1, id2));
                }
                path = path.extend(edge, Collections.emptySet());
            }
        }
        path = path.extend(vertex, Collections.emptySet());
        first = false;
    }
    return path;
}
 
Example 6
Source File: ShortestPathVertexProgram.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private static Path extendPath(final Path currentPath, final Element... elements) {
    Path result = ImmutablePath.make();
    if (currentPath != null) {
        for (final Object o : currentPath.objects()) {
            result = result.extend(o, Collections.emptySet());
        }
    }
    for (final Element element : elements) {
        if (element != null) {
            result = result.extend(ReferenceFactory.detach(element), Collections.emptySet());
        }
    }
    return result;
}
 
Example 7
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()));
}