org.apache.tinkerpop.gremlin.process.traversal.step.map.SelectStep Java Examples

The following examples show how to use org.apache.tinkerpop.gremlin.process.traversal.step.map.SelectStep. 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: GraqlTraversalImpl.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
private static <S> GraphTraversal<S, Map<String, Vertex>> selectVars(GraphTraversal<S, Vertex> traversal, Set<Variable> vars) {
    if (vars.isEmpty()) {
        // Produce an empty result
        return traversal.constant(ImmutableMap.of());
    } else if (vars.size() == 1) {
        String label = vars.iterator().next().symbol();
        return traversal.select(label, label);
    } else {
        String[] labelArray = vars.stream().map(Variable::symbol).toArray(String[]::new);
        return traversal.asAdmin().addStep(new SelectStep<>(traversal.asAdmin(), null, labelArray));
    }
}
 
Example #2
Source File: MatchPredicateStrategy.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    if (!TraversalHelper.hasStepOfClass(MatchStep.class, traversal))
        return;

    TraversalHelper.getStepsOfClass(MatchStep.class, traversal).forEach(matchStep -> {
        // match().select().where() --> match(where()).select()
        // match().select().dedup() --> match(dedup()).select()
        Step<?, ?> nextStep = matchStep.getNextStep();
        while (nextStep instanceof WherePredicateStep ||
                nextStep instanceof WhereTraversalStep ||
                (nextStep instanceof DedupGlobalStep && !((DedupGlobalStep) nextStep).getScopeKeys().isEmpty() && ((DedupGlobalStep) nextStep).getLocalChildren().isEmpty()) ||
                (nextStep instanceof SelectStep && ((SelectStep) nextStep).getLocalChildren().isEmpty()) ||
                (nextStep instanceof SelectOneStep && ((SelectOneStep) nextStep).getLocalChildren().isEmpty())) {
            if (nextStep instanceof WherePredicateStep || nextStep instanceof WhereTraversalStep) {
                traversal.removeStep(nextStep);
                matchStep.addGlobalChild(traversal instanceof GraphTraversal ?
                        new DefaultGraphTraversal<>().addStep(nextStep) :
                        new DefaultTraversal<>().addStep(nextStep));
                nextStep = matchStep.getNextStep();
            } else if (nextStep instanceof DedupGlobalStep && !((DedupGlobalStep) nextStep).getScopeKeys().isEmpty() && ((DedupGlobalStep) nextStep).getLocalChildren().isEmpty() && !TraversalHelper.onGraphComputer(traversal)) {
                traversal.removeStep(nextStep);
                matchStep.setDedupLabels(((DedupGlobalStep<?>) nextStep).getScopeKeys());
                nextStep = matchStep.getNextStep();
            } else if (nextStep.getLabels().isEmpty()) {
                nextStep = nextStep.getNextStep();
            } else
                break;
        }
    });
}
 
Example #3
Source File: TestPropertyValues.java    From sqlg with MIT License 4 votes vote down vote up
@Test
public void testSelectFollowedByValues() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "name", "a1");
    Vertex a2 = this.sqlgGraph.addVertex(T.label, "A", "name", "a2");
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B", "name", "b1");
    Vertex b2 = this.sqlgGraph.addVertex(T.label, "B", "name", "b2");
    Vertex c1 = this.sqlgGraph.addVertex(T.label, "C", "xxxx", "c1", "yyyy", "y1", "zzzz", "z1");
    Vertex c2 = this.sqlgGraph.addVertex(T.label, "C", "xxxx", "c2", "yyyy", "y2", "zzzz", "z2");
    Vertex d1 = this.sqlgGraph.addVertex(T.label, "D", "name", "d1");
    Vertex d2 = this.sqlgGraph.addVertex(T.label, "D", "name", "d2");
    a1.addEdge("ab", b1);
    a2.addEdge("ab", b2);
    b1.addEdge("bc", c1);
    b2.addEdge("bc", c2);
    c1.addEdge("cd", d1);
    c2.addEdge("cd", d2);
    this.sqlgGraph.tx().commit();
    DefaultGraphTraversal<Vertex, String> traversal = (DefaultGraphTraversal<Vertex, String>)this.sqlgGraph.traversal().V()
            .hasLabel("C")
            .has("yyyy", "y1")
            .as("c")
            .in("bc")
            .in("ab")
            .has("name", "a1")
            .as("a")
            .<Vertex>select("a", "c")
            .select("c")
            .<String>values("xxxx");
    printTraversalForm(traversal);
    Assert.assertEquals(6, traversal.getSteps().size());
    Assert.assertTrue(traversal.getSteps().get(0) instanceof SqlgGraphStep);
    Assert.assertTrue(traversal.getSteps().get(1) instanceof IdentityStep);
    Assert.assertTrue(traversal.getSteps().get(2) instanceof IdentityStep);
    Assert.assertTrue(traversal.getSteps().get(3) instanceof SelectStep);
    Assert.assertTrue(traversal.getSteps().get(4) instanceof SelectOneStep);
    Assert.assertTrue(traversal.getSteps().get(5) instanceof PropertiesStep);
    List<String> names = traversal.toList();
    Assert.assertEquals(1, names.size());
    Assert.assertEquals("c1", names.get(0));
    checkRestrictedProperties(SqlgGraphStep.class, traversal, 0,  "xxxx");
}
 
Example #4
Source File: GraphTraversal.java    From tinkerpop with Apache License 2.0 3 votes vote down vote up
/**
 * Map the {@link Traverser} to a {@link Map} projection of sideEffect values, map values, and/or path values.
 *
 * @param pop             if there are multiple objects referenced in the path, the {@link Pop} to use.
 * @param selectKey1      the first key to project
 * @param selectKey2      the second key to project
 * @param otherSelectKeys the third+ keys to project
 * @param <E2>            the type of the objects projected
 * @return the traversal with an appended {@link SelectStep}.
 * @see <a href="http://tinkerpop.apache.org/docs/${project.version}/reference/#select-step" target="_blank">Reference Documentation - Select Step</a>
 * @since 3.0.0-incubating
 */
public default <E2> GraphTraversal<S, Map<String, E2>> select(final Pop pop, final String selectKey1, final String selectKey2, String... otherSelectKeys) {
    final String[] selectKeys = new String[otherSelectKeys.length + 2];
    selectKeys[0] = selectKey1;
    selectKeys[1] = selectKey2;
    System.arraycopy(otherSelectKeys, 0, selectKeys, 2, otherSelectKeys.length);
    this.asAdmin().getBytecode().addStep(Symbols.select, pop, selectKey1, selectKey2, otherSelectKeys);
    return this.asAdmin().addStep(new SelectStep<>(this.asAdmin(), pop, selectKeys));
}
 
Example #5
Source File: GraphTraversal.java    From tinkerpop with Apache License 2.0 3 votes vote down vote up
/**
 * Map the {@link Traverser} to a {@link Map} projection of sideEffect values, map values, and/or path values.
 *
 * @param selectKey1      the first key to project
 * @param selectKey2      the second key to project
 * @param otherSelectKeys the third+ keys to project
 * @param <E2>            the type of the objects projected
 * @return the traversal with an appended {@link SelectStep}.
 * @see <a href="http://tinkerpop.apache.org/docs/${project.version}/reference/#select-step" target="_blank">Reference Documentation - Select Step</a>
 * @since 3.0.0-incubating
 */
public default <E2> GraphTraversal<S, Map<String, E2>> select(final String selectKey1, final String selectKey2, String... otherSelectKeys) {
    final String[] selectKeys = new String[otherSelectKeys.length + 2];
    selectKeys[0] = selectKey1;
    selectKeys[1] = selectKey2;
    System.arraycopy(otherSelectKeys, 0, selectKeys, 2, otherSelectKeys.length);
    this.asAdmin().getBytecode().addStep(Symbols.select, selectKey1, selectKey2, otherSelectKeys);
    return this.asAdmin().addStep(new SelectStep<>(this.asAdmin(), Pop.last, selectKeys));
}