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

The following examples show how to use org.apache.tinkerpop.gremlin.process.traversal.step.map.PropertiesStep. 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: SqlgRestrictPropertiesStrategy.java    From sqlg with MIT License 6 votes vote down vote up
private Collection<String> getRestrictedProperties(Step<?, ?> step) {
    Collection<String> ret = null;
    if (step instanceof PropertiesStep<?>) {
        PropertiesStep<?> ps = (PropertiesStep<?>) step;
        ret = Arrays.asList(ps.getPropertyKeys());
    } else if (step instanceof PropertyMapStep<?, ?>) {
        PropertyMapStep<?, ?> pms = (PropertyMapStep<?, ?>) step;
        ret = Arrays.asList(pms.getPropertyKeys());
    }
    // if no property keys are provided, all properties should be returned
    if (ret != null && ret.isEmpty()) {
        ret = null;
    }

    return ret;
}
 
Example #2
Source File: ElementIdStrategyTraverseTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldAlterTraversalToIncludeIdWhereNecessary() {
    final ElementIdStrategy strategy = ElementIdStrategy.build().create();
    strategy.apply(traversal.asAdmin());

    final Step step = (Step) traversal.asAdmin().getSteps().get(expectedInsertedSteps);
    if (step instanceof AddVertexStep)
        assertTrue(((AddVertexStep) step).getParameters().contains(strategy.getIdPropertyKey()));
    else if (step instanceof AddVertexStartStep)
        assertTrue(((AddVertexStartStep) step).getParameters().contains(strategy.getIdPropertyKey()));
    else if (step instanceof AddEdgeStep)
        assertTrue(((AddEdgeStep) step).getParameters().contains(strategy.getIdPropertyKey()));
    else if (step instanceof PropertiesStep)
        assertEquals(strategy.getIdPropertyKey(), ((PropertiesStep) step).getPropertyKeys()[0]);
    else
        fail("Check test definition - the expectedInsertedSteps should be the index of the step to trigger the ID substitution");
}
 
Example #3
Source File: TraversalHelperTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldRemoveStepsCorrectly() {
    final Traversal.Admin traversal = new DefaultTraversal<>(EmptyGraph.instance());
    traversal.asAdmin().addStep(new IdentityStep(traversal));
    traversal.asAdmin().addStep(new HasStep(traversal));
    traversal.asAdmin().addStep(new LambdaFilterStep(traversal, traverser -> true));

    traversal.asAdmin().addStep(new PropertiesStep(traversal, PropertyType.VALUE, "marko"));
    traversal.asAdmin().removeStep(3);
    validateToyTraversal(traversal);

    traversal.asAdmin().addStep(0, new PropertiesStep(traversal, PropertyType.PROPERTY, "marko"));
    traversal.asAdmin().removeStep(0);
    validateToyTraversal(traversal);

    traversal.asAdmin().removeStep(1);
    traversal.asAdmin().addStep(1, new HasStep(traversal));
    validateToyTraversal(traversal);
}
 
Example #4
Source File: SubgraphStrategy.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
private static final char processesPropertyType(Step step) {
    while (!(step instanceof EmptyStep)) {
        if (step instanceof FilterStep || step instanceof SideEffectStep)
            step = step.getPreviousStep();
        else if (step instanceof GraphStep && ((GraphStep) step).returnsVertex())
            return 'v';
        else if (step instanceof EdgeVertexStep)
            return 'v';
        else if (step instanceof VertexStep)
            return ((VertexStep) step).returnsVertex() ? 'v' : 'p';
        else if (step instanceof PropertyMapStep || step instanceof PropertiesStep)
            return 'p';
        else
            return 'x';
    }
    return 'x';
}
 
Example #5
Source File: ByModulatorOptimizationStrategy.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
private void optimizeForStep(final TraversalParent step, final Traversal.Admin<?, ?> traversal, final Step singleStep) {
    if (singleStep instanceof PropertiesStep) {
        final PropertiesStep ps = (PropertiesStep) singleStep;
        if (ps.getReturnType().equals(PropertyType.VALUE) && ps.getPropertyKeys().length == 1) {
            step.replaceLocalChild(traversal, new ValueTraversal<>(ps.getPropertyKeys()[0]));
        }
    } else if (singleStep instanceof IdStep) {
        step.replaceLocalChild(traversal, new TokenTraversal<>(T.id));
    } else if (singleStep instanceof LabelStep) {
        step.replaceLocalChild(traversal, new TokenTraversal<>(T.label));
    } else if (singleStep instanceof PropertyKeyStep) {
        step.replaceLocalChild(traversal, new TokenTraversal<>(T.key));
    } else if (singleStep instanceof PropertyValueStep) {
        step.replaceLocalChild(traversal, new TokenTraversal<>(T.value));
    } else if (singleStep instanceof IdentityStep) {
        step.replaceLocalChild(traversal, new IdentityTraversal<>());
    }
}
 
Example #6
Source File: JanusPreviousPropertyStepStrategy.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void apply(Traversal.Admin<?, ?> traversal) {

    // Retrieve all graph (`V()`) steps - this is the step the strategy should replace
    List<GraphStep> graphSteps = TraversalHelper.getStepsOfAssignableClass(GraphStep.class, traversal);

    for (GraphStep graphStep : graphSteps) {
        // For each graph step, confirm it follows this pattern:
        // `V().filter(__.properties(a).where(P.eq(b)))`

        if (!(graphStep.getNextStep() instanceof TraversalFilterStep)) continue;
        TraversalFilterStep<Vertex> filterStep = (TraversalFilterStep<Vertex>) graphStep.getNextStep();

        // Retrieve the filter steps e.g. `__.properties(a).where(P.eq(b))`
        List<Step> steps = stepsFromFilterStep(filterStep);

        if (steps.size() < 2) continue;
        Step propertiesStep = steps.get(0); // This is `properties(a)`
        Step whereStep = steps.get(1);      // This is `filter(__.where(P.eq(b)))`

        // Get the property key `a`
        if (!(propertiesStep instanceof PropertiesStep)) continue;
        Optional<String> propertyKey = propertyFromPropertiesStep((PropertiesStep<Vertex>) propertiesStep);
        if (!propertyKey.isPresent()) continue;

        // Get the step label `b`
        if (!(whereStep instanceof WherePredicateStep)) continue;
        Optional<String> label = labelFromWhereEqPredicate((WherePredicateStep<Vertex>) whereStep);
        if (!label.isPresent()) continue;

        executeStrategy(traversal, graphStep, filterStep, propertyKey.get(), label.get());
    }
}
 
Example #7
Source File: TestPropertyValues.java    From sqlg with MIT License 5 votes vote down vote up
@Test
public void testSelectOneFollowedByValues() {
    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")
            .<Vertex>select("c")
            .<String>values("xxxx");
    printTraversalForm(traversal);
    Assert.assertEquals(4, 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 SelectOneStep);
    Assert.assertTrue(traversal.getSteps().get(3) 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 #8
Source File: MasterExecutor.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private static boolean isLocalElement(final Step<?, ?> step) {
    return step instanceof PropertiesStep || step instanceof PropertyMapStep ||
            step instanceof IdStep || step instanceof LabelStep || step instanceof SackStep ||
            step instanceof PropertyKeyStep || step instanceof PropertyValueStep ||
            step instanceof TailGlobalStep || step instanceof RangeGlobalStep || step instanceof HasStep ||
            step instanceof ConnectiveStep;
}
 
Example #9
Source File: ElementIdStrategy.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    TraversalHelper.getStepsOfAssignableClass(HasStep.class, traversal).stream()
            .filter(hasStep -> ((HasStep<?>) hasStep).getHasContainers().get(0).getKey().equals(T.id.getAccessor()))
            .forEach(hasStep -> ((HasStep<?>) hasStep).getHasContainers().get(0).setKey(this.idPropertyKey));

    if (traversal.getStartStep() instanceof GraphStep) {
        final GraphStep graphStep = (GraphStep) traversal.getStartStep();
        // only need to apply the custom id if ids were assigned - otherwise we want the full iterator.
        // note that it is then only necessary to replace the step if the id is a non-element.  other tests
        // in the suite validate that items in getIds() is uniform so it is ok to just test the first item
        // in the list.
        if (graphStep.getIds().length > 0 && !(graphStep.getIds()[0] instanceof Element)) {
            if (graphStep instanceof HasContainerHolder)
                ((HasContainerHolder) graphStep).addHasContainer(new HasContainer(this.idPropertyKey, P.within(Arrays.asList(graphStep.getIds()))));
            else
                TraversalHelper.insertAfterStep(new HasStep(traversal, new HasContainer(this.idPropertyKey, P.within(Arrays.asList(graphStep.getIds())))), graphStep, traversal);
            graphStep.clearIds();
        }
    }
    TraversalHelper.getStepsOfAssignableClass(IdStep.class, traversal).stream().forEach(step -> {
        TraversalHelper.replaceStep(step, new PropertiesStep(traversal, PropertyType.VALUE, idPropertyKey), traversal);
    });

    // in each case below, determine if the T.id is present and if so, replace T.id with the idPropertyKey or if
    // it is not present then shove it in there and generate an id
    traversal.getSteps().forEach(step -> {
        if (step instanceof AddVertexStep || step instanceof AddVertexStartStep || step instanceof AddEdgeStep) {
            final Parameterizing parameterizing = (Parameterizing) step;
            if (parameterizing.getParameters().contains(T.id))
                parameterizing.getParameters().rename(T.id, this.idPropertyKey);
            else if (!parameterizing.getParameters().contains(this.idPropertyKey))
                parameterizing.getParameters().set(null, this.idPropertyKey, idMaker.get());
        }
    });
}
 
Example #10
Source File: TitanPropertiesStep.java    From titan1withtp3.1 with Apache License 2.0 4 votes vote down vote up
public TitanPropertiesStep(PropertiesStep<E> originalStep) {
    super(originalStep.getTraversal(), originalStep.getReturnType(), originalStep.getPropertyKeys());
    originalStep.getLabels().forEach(this::addLabel);
    this.hasContainers = new ArrayList<>();
    this.limit = Query.NO_LIMIT;
}
 
Example #11
Source File: JanusGraphPropertiesStep.java    From grakn with GNU Affero General Public License v3.0 4 votes vote down vote up
public JanusGraphPropertiesStep(PropertiesStep<E> originalStep) {
    super(originalStep.getTraversal(), originalStep.getReturnType(), originalStep.getPropertyKeys());
    originalStep.getLabels().forEach(this::addLabel);
    this.hasContainers = new ArrayList<>();
    this.limit = Query.NO_LIMIT;
}
 
Example #12
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 #13
Source File: JanusPreviousPropertyStepStrategy.java    From grakn with GNU Affero General Public License v3.0 4 votes vote down vote up
private Optional<String> propertyFromPropertiesStep(PropertiesStep<Vertex> propertiesStep) {
    String[] propertyKeys = propertiesStep.getPropertyKeys();
    if (propertyKeys.length != 1) return Optional.empty();
    return Optional.of(propertyKeys[0]);
}
 
Example #14
Source File: TraversalUtil.java    From hugegraph with Apache License 2.0 4 votes vote down vote up
public static void extractAggregateFunc(Step<?, ?> newStep,
                                        Traversal.Admin<?, ?> traversal) {
    PropertiesStep<?> propertiesStep = null;
    Step<?, ?> step = newStep;
    do {
        step = step.getNextStep();
        if (step instanceof PropertiesStep) {
            @SuppressWarnings("resource")
            PropertiesStep<?> propStep = (PropertiesStep<?>) step;
            if (propStep.getReturnType() == PropertyType.VALUE &&
                propStep.getPropertyKeys().length == 1) {
                propertiesStep = propStep;
            }
        } else if (propertiesStep != null &&
                   step instanceof ReducingBarrierStep) {
            AggregateFunc aggregateFunc;
            if (step instanceof CountGlobalStep) {
                aggregateFunc = AggregateFunc.COUNT;
            } else if (step instanceof MaxGlobalStep) {
                aggregateFunc = AggregateFunc.MAX;
            } else if (step instanceof MinGlobalStep) {
                aggregateFunc = AggregateFunc.MIN;
            } else if (step instanceof MeanGlobalStep) {
                aggregateFunc = AggregateFunc.AVG;
            } else if (step instanceof SumGlobalStep) {
                aggregateFunc = AggregateFunc.SUM;
            } else {
                aggregateFunc = null;
            }

            if (aggregateFunc != null) {
                QueryHolder holder = (QueryHolder) newStep;
                holder.setAggregate(aggregateFunc,
                                    propertiesStep.getPropertyKeys()[0]);
                traversal.removeStep(step);
                traversal.removeStep(propertiesStep);
            }
        }
    } while (step instanceof FilterStep ||
             step instanceof PropertiesStep ||
             step instanceof IdentityStep ||
             step instanceof NoOpBarrierStep);
}
 
Example #15
Source File: GraphTraversal.java    From tinkerpop with Apache License 2.0 3 votes vote down vote up
/**
 * Filters vertices, edges and vertex properties based on the value of the specified property key.
 *
 * @param propertyKey       the key of the property to filter on
 * @param propertyTraversal the traversal to filter the property value by
 * @return the traversal with an appended {@link HasStep}
 * @see <a href="http://tinkerpop.apache.org/docs/${project.version}/reference/#has-step" target="_blank">Reference Documentation - Has Step</a>
 * @since 3.0.0-incubating
 */
public default GraphTraversal<S, E> has(final String propertyKey, final Traversal<?, ?> propertyTraversal) {
    this.asAdmin().getBytecode().addStep(Symbols.has, propertyKey, propertyTraversal);
    return this.asAdmin().addStep(
            new TraversalFilterStep<>(this.asAdmin(), propertyTraversal.asAdmin().addStep(0,
                    new PropertiesStep(propertyTraversal.asAdmin(), PropertyType.VALUE, propertyKey))));
}
 
Example #16
Source File: AdjacentToIncidentStrategy.java    From tinkerpop with Apache License 2.0 2 votes vote down vote up
/**
 * Checks whether a given step is optimizable or not.
 *
 * @param step the step to check
 * @return <code>true</code> if the step is optimizable, otherwise <code>false</code>
 */
private static boolean isOptimizable(final Step step) {
    return ((step instanceof VertexStep && ((VertexStep) step).returnsVertex()) ||
            (step instanceof PropertiesStep && PropertyType.VALUE.equals(((PropertiesStep) step).getReturnType()))) && (step.getTraversal().getEndStep().getLabels().isEmpty() || step.getNextStep() instanceof CountGlobalStep);
}
 
Example #17
Source File: GraphTraversal.java    From tinkerpop with Apache License 2.0 2 votes vote down vote up
/**
 * Map the {@link Element} to the values of the associated properties given the provide property keys.
 * If no property keys are provided, then all property values are emitted.
 *
 * @param propertyKeys the properties to retrieve their value from
 * @param <E2>         the value type of the properties
 * @return the traversal with an appended {@link PropertiesStep}.
 * @see <a href="http://tinkerpop.apache.org/docs/${project.version}/reference/#values-step" target="_blank">Reference Documentation - Values Step</a>
 * @since 3.0.0-incubating
 */
public default <E2> GraphTraversal<S, E2> values(final String... propertyKeys) {
    this.asAdmin().getBytecode().addStep(Symbols.values, propertyKeys);
    return this.asAdmin().addStep(new PropertiesStep<>(this.asAdmin(), PropertyType.VALUE, propertyKeys));
}
 
Example #18
Source File: GraphTraversal.java    From tinkerpop with Apache License 2.0 2 votes vote down vote up
/**
 * Map the {@link Element} to its associated properties given the provide property keys.
 * If no property keys are provided, then all properties are emitted.
 *
 * @param propertyKeys the properties to retrieve
 * @param <E2>         the value type of the returned properties
 * @return the traversal with an appended {@link PropertiesStep}.
 * @see <a href="http://tinkerpop.apache.org/docs/${project.version}/reference/#properties-step" target="_blank">Reference Documentation - Properties Step</a>
 * @since 3.0.0-incubating
 */
public default <E2> GraphTraversal<S, ? extends Property<E2>> properties(final String... propertyKeys) {
    this.asAdmin().getBytecode().addStep(Symbols.properties, propertyKeys);
    return this.asAdmin().addStep(new PropertiesStep<>(this.asAdmin(), PropertyType.PROPERTY, propertyKeys));
}