org.apache.tinkerpop.gremlin.process.traversal.step.filter.TraversalFilterStep Java Examples

The following examples show how to use org.apache.tinkerpop.gremlin.process.traversal.step.filter.TraversalFilterStep. 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: SubgraphStrategyTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldNotRetainMarkers() {
    final SubgraphStrategy strategy = SubgraphStrategy.build().vertices(__.<Vertex>out().hasLabel("person")).create();
    final Traversal.Admin<?, ?> t = out().inE().asAdmin();
    t.setStrategies(t.getStrategies().clone().addStrategies(strategy, StandardVerificationStrategy.instance()));
    t.applyStrategies();
    assertEquals(t.getSteps().get(0).getClass(), VertexStep.class);
    assertEquals(t.getSteps().get(1).getClass(), TraversalFilterStep.class);
    assertEquals(AndStep.class, ((TraversalFilterStep<?>) t.getSteps().get(1)).getLocalChildren().get(0).getStartStep().getClass());
    assertEquals(0, ((TraversalFilterStep<?>) t.getSteps().get(1)).getLocalChildren().get(0).getStartStep().getLabels().size());
    assertEquals(t.getSteps().get(2).getClass(), EdgeVertexStep.class);
    assertEquals(t.getSteps().get(3).getClass(), TraversalFilterStep.class);
    assertEquals(VertexStep.class, ((TraversalFilterStep<?>) t.getSteps().get(3)).getLocalChildren().get(0).getStartStep().getClass());
    assertEquals(0, ((TraversalFilterStep<?>) t.getSteps().get(3)).getLocalChildren().get(0).getStartStep().getLabels().size());
    TraversalHelper.getStepsOfAssignableClassRecursively(Step.class, t).forEach(step -> assertTrue(step.getLabels().isEmpty()));
}
 
Example #2
Source File: GraphTraversal.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
/**
 * Filters vertices, edges and vertex properties based on their value of {@link T} where only {@link T#id} and
 * {@link T#label} are supported.
 *
 * @param accessor          the {@link T} accessor of the property to filter on
 * @param propertyTraversal the traversal to filter the accessor 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.1.0-incubating
 */
public default GraphTraversal<S, E> has(final T accessor, final Traversal<?, ?> propertyTraversal) {
    this.asAdmin().getBytecode().addStep(Symbols.has, accessor, propertyTraversal);
    switch (accessor) {
        case id:
            return this.asAdmin().addStep(
                    new TraversalFilterStep<>(this.asAdmin(), propertyTraversal.asAdmin().addStep(0,
                            new IdStep<>(propertyTraversal.asAdmin()))));
        case label:
            return this.asAdmin().addStep(
                    new TraversalFilterStep<>(this.asAdmin(), propertyTraversal.asAdmin().addStep(0,
                            new LabelStep<>(propertyTraversal.asAdmin()))));
        default:
            throw new IllegalArgumentException("has(T,Traversal) can only take id or label as its argument");
    }

}
 
Example #3
Source File: TraversalHelperTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldIdentifyLocalChildren() {
    final Traversal.Admin<?, ?> localChild = __.as("x").select("a", "b").by("name").asAdmin();
    new LocalStep<>(new DefaultTraversal(), localChild);
    assertFalse(TraversalHelper.isGlobalChild(localChild));
    ///
    new WhereTraversalStep<>(new DefaultTraversal(), localChild);
    assertFalse(TraversalHelper.isGlobalChild(localChild));
    ///
    new TraversalFilterStep<>(new DefaultTraversal(), localChild);
    assertFalse(TraversalHelper.isGlobalChild(localChild));
    ///
    new TraversalMapStep<>(new DefaultTraversal(), localChild);
    assertFalse(TraversalHelper.isGlobalChild(localChild));
    ///
    new TraversalFlatMapStep<>(new DefaultTraversal(), localChild);
    assertFalse(TraversalHelper.isGlobalChild(localChild));
    ///
    final Traversal.Admin<?, ?> remoteLocalChild = __.repeat(localChild).asAdmin();
    new LocalStep<>(new DefaultTraversal<>(), remoteLocalChild);
    assertFalse(TraversalHelper.isGlobalChild(localChild));
}
 
Example #4
Source File: AdjacentToIncidentStrategy.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    final List<Step> steps = traversal.getSteps();
    final int size = steps.size() - 1;
    Step prev = null;
    for (int i = 0; i <= size; i++) {
        final Step curr = steps.get(i);
        if (i == size && isOptimizable(curr)) {
            final TraversalParent parent = curr.getTraversal().getParent();
            if (parent instanceof NotStep || parent instanceof TraversalFilterStep || parent instanceof WhereTraversalStep || parent instanceof ConnectiveStep) {
                optimizeStep(traversal, curr);
            }
        } else if (isOptimizable(prev)) {
            if (curr instanceof CountGlobalStep) {
                optimizeStep(traversal, prev);
            }
        }
        if (!(curr instanceof RangeGlobalStep)) {
            prev = curr;
        }
    }
}
 
Example #5
Source File: InlineFilterStrategy.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    boolean changed = true; // recursively walk child traversals trying to inline them into the current traversal line.
    while (changed) {
        changed = false;
        final Iterator<FilterStep> filterStepIterator = TraversalHelper.getStepsOfAssignableClass(FilterStep.class, traversal).iterator();
        while (!changed && filterStepIterator.hasNext()) {
            final FilterStep<?> step = filterStepIterator.next();
            changed = step instanceof HasStep && InlineFilterStrategy.processHasStep((HasStep) step, traversal) ||
                    step instanceof TraversalFilterStep && InlineFilterStrategy.processTraversalFilterStep((TraversalFilterStep) step, traversal) ||
                    step instanceof OrStep && InlineFilterStrategy.processOrStep((OrStep) step, traversal) ||
                    step instanceof AndStep && InlineFilterStrategy.processAndStep((AndStep) step, traversal);
        }
        if (!changed && traversal.isRoot()) {
            final Iterator<MatchStep> matchStepIterator = TraversalHelper.getStepsOfClass(MatchStep.class, traversal).iterator();
            while (!changed && matchStepIterator.hasNext()) {
                if (InlineFilterStrategy.processMatchStep(matchStepIterator.next(), traversal))
                    changed = true;
            }
        }
    }
}
 
Example #6
Source File: InlineFilterStrategy.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
private static final boolean processTraversalFilterStep(final TraversalFilterStep<?> step, final Traversal.Admin<?, ?> traversal) {
    final Traversal.Admin<?, ?> childTraversal = step.getLocalChildren().get(0);
    if (TraversalHelper.hasAllStepsOfClass(childTraversal, FilterStep.class) &&
            !TraversalHelper.hasStepOfClass(childTraversal,
                    DropStep.class,
                    RangeGlobalStep.class,
                    DedupGlobalStep.class,
                    LambdaHolder.class)) {
        TraversalHelper.applySingleLevelStrategies(traversal, childTraversal, InlineFilterStrategy.class);
        final Step<?, ?> finalStep = childTraversal.getEndStep();
        TraversalHelper.insertTraversal((Step) step, childTraversal, traversal);
        TraversalHelper.copyLabels(step, finalStep, false);
        traversal.removeStep(step);
        return true;
    }
    return false;
}
 
Example #7
Source File: SqlgTraversalFilterStepStrategy.java    From sqlg with MIT License 5 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    //Only optimize SqlgGraph. StarGraph also passes through here.
    if (!(traversal.getGraph().orElseThrow(IllegalStateException::new) instanceof SqlgGraph)) {
        return;
    }
    if (!SqlgTraversalUtil.mayOptimize(traversal)) {
        return;
    }
    List<TraversalFilterStep> traversalFilterSteps = TraversalHelper.getStepsOfAssignableClass(TraversalFilterStep.class, traversal);
    for (@SuppressWarnings("unchecked") TraversalFilterStep<S> traversalFilterStep : traversalFilterSteps) {

        List<Traversal.Admin<S, ?>> filterTraversals = traversalFilterStep.getLocalChildren();
        Preconditions.checkState(filterTraversals.size() == 1);
        Traversal.Admin<S, ?> filterTraversal = filterTraversals.get(0);

        //reducing barrier steps like count does not work with Sqlg's barrier optimizations
        List<ReducingBarrierStep> reducingBarrierSteps = TraversalHelper.getStepsOfAssignableClassRecursively(ReducingBarrierStep.class, filterTraversal);
        if (!reducingBarrierSteps.isEmpty()) {
            continue;
        }

        SqlgTraversalFilterStepBarrier sqlgTraversalFilterStepBarrier = new SqlgTraversalFilterStepBarrier<>(
                traversal,
                filterTraversal
        );
        for (String label : traversalFilterStep.getLabels()) {
            sqlgTraversalFilterStepBarrier.addLabel(label);
        }
        //noinspection unchecked
        TraversalHelper.replaceStep(
                traversalFilterStep,
                sqlgTraversalFilterStepBarrier,
                traversalFilterStep.getTraversal()
        );
    }
}
 
Example #8
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 #9
Source File: SubgraphStrategyTraverseTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldSubgraph() {
    final SubgraphStrategy strategy = SubgraphStrategy.build().edges(__.identity()).vertices(__.identity()).create();
    strategy.apply(traversal.asAdmin());

    final List<TraversalFilterStep> steps = TraversalHelper.getStepsOfClass(TraversalFilterStep.class, traversal.asAdmin());
    assertEquals(expectedInsertedSteps, steps.size());
}
 
Example #10
Source File: SubgraphStrategyTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldAddBothFiltersAfterVertex() {
    final SubgraphStrategy strategy = SubgraphStrategy.build().edges(__.identity()).vertices(__.identity()).create();
    final Traversal t = __.inE();
    strategy.apply(t.asAdmin());
    final VertexStep vertexStep = (VertexStep) t.asAdmin().getStartStep();
    assertEquals(TraversalFilterStep.class, vertexStep.getNextStep().getClass());
    final TraversalFilterStep h = (TraversalFilterStep) t.asAdmin().getEndStep();
    assertEquals(1, h.getLocalChildren().size());
    assertThat(((DefaultGraphTraversal) h.getLocalChildren().get(0)).getEndStep(), CoreMatchers.instanceOf(TraversalFilterStep.class));
}
 
Example #11
Source File: SubgraphStrategyTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldAddFilterAfterEdge() {
    final SubgraphStrategy strategy = SubgraphStrategy.build().edges(__.identity()).create();
    final Traversal t = __.inE();
    strategy.apply(t.asAdmin());
    final VertexStep vertexStep = (VertexStep) t.asAdmin().getStartStep();
    assertEquals(TraversalFilterStep.class, vertexStep.getNextStep().getClass());
    final TraversalFilterStep h = (TraversalFilterStep) t.asAdmin().getEndStep();
    assertEquals(1, h.getLocalChildren().size());
    assertThat(((DefaultGraphTraversal) h.getLocalChildren().get(0)).getEndStep(), CoreMatchers.instanceOf(IdentityStep.class));
}
 
Example #12
Source File: SubgraphStrategyTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldAddFilterAfterVertex() {
    final SubgraphStrategy strategy = SubgraphStrategy.build().vertices(__.identity()).create();
    final Traversal t = __.inV();
    strategy.apply(t.asAdmin());
    final EdgeVertexStep edgeVertexStep = (EdgeVertexStep) t.asAdmin().getStartStep();
    assertEquals(TraversalFilterStep.class, edgeVertexStep.getNextStep().getClass());
    final TraversalFilterStep h = (TraversalFilterStep) t.asAdmin().getEndStep();
    assertEquals(1, h.getLocalChildren().size());
    assertThat(((DefaultGraphTraversal) h.getLocalChildren().get(0)).getEndStep(), CoreMatchers.instanceOf(IdentityStep.class));
}
 
Example #13
Source File: SubgraphStrategy.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private static void applyCriterion(final List<Step> stepsToApplyCriterionAfter, final Traversal.Admin traversal,
                                   final Traversal.Admin<? extends Element, ?> criterion) {
    for (final Step<?, ?> step : stepsToApplyCriterionAfter) {
        // re-assign the step label to the criterion because the label should apply seamlessly after the filter
        final Step filter = new TraversalFilterStep<>(traversal, criterion.clone());
        TraversalHelper.insertAfterStep(filter, step, traversal);
        TraversalHelper.copyLabels(step, filter, true);
    }
}
 
Example #14
Source File: SubgraphStrategy.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private SubgraphStrategy(final Builder builder) {

        this.vertexCriterion = null == builder.vertexCriterion ? null : builder.vertexCriterion.asAdmin().clone();
        this.checkAdjacentVertices = builder.checkAdjacentVertices;

        // if there is no vertex predicate there is no need to test either side of the edge - also this option can
        // be simply configured in the builder to not be used
        if (null == this.vertexCriterion || !checkAdjacentVertices) {
            this.edgeCriterion = null == builder.edgeCriterion ? null : builder.edgeCriterion.asAdmin().clone();
        } else {
            final Traversal.Admin<Edge, ?> vertexPredicate;
            vertexPredicate = __.<Edge>and(
                    __.inV().filter(this.vertexCriterion),
                    __.outV().filter(this.vertexCriterion)).asAdmin();

            // if there is a vertex predicate then there is an implied edge filter on vertices even if there is no
            // edge predicate provided by the user.
            this.edgeCriterion = null == builder.edgeCriterion ?
                    vertexPredicate :
                    builder.edgeCriterion.asAdmin().clone().addStep(new TraversalFilterStep<>(builder.edgeCriterion.asAdmin(), vertexPredicate));
        }

        this.vertexPropertyCriterion = null == builder.vertexPropertyCriterion ? null : builder.vertexPropertyCriterion.asAdmin().clone();

        if (null != this.vertexCriterion)
            TraversalHelper.applyTraversalRecursively(t -> t.getStartStep().addLabel(MARKER), this.vertexCriterion);
        if (null != this.edgeCriterion)
            TraversalHelper.applyTraversalRecursively(t -> t.getStartStep().addLabel(MARKER), this.edgeCriterion);
        if (null != this.vertexPropertyCriterion)
            TraversalHelper.applyTraversalRecursively(t -> t.getStartStep().addLabel(MARKER), this.vertexPropertyCriterion);
    }
 
Example #15
Source File: FilterRankingStrategy.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/**
 * Ranks the given step. Steps with lower ranks can be moved in front of steps with higher ranks. 0 means that
 * the step has no rank and thus is not exchangeable with its neighbors.
 *
 * @param step the step to get a ranking for
 * @return The rank of the given step.
 */
private static int getStepRank(final Step step) {
    final int rank;
    if (!(step instanceof FilterStep || step instanceof OrderGlobalStep))
        return 0;
    else if (step instanceof IsStep || step instanceof ClassFilterStep)
        rank = 1;
    else if (step instanceof HasStep)
        rank = 2;
    else if (step instanceof WherePredicateStep && ((WherePredicateStep) step).getLocalChildren().isEmpty())
        rank = 3;
    else if (step instanceof TraversalFilterStep || step instanceof NotStep)
        rank = 4;
    else if (step instanceof WhereTraversalStep)
        rank = 5;
    else if (step instanceof OrStep)
        rank = 6;
    else if (step instanceof AndStep)
        rank = 7;
    else if (step instanceof WherePredicateStep) // has by()-modulation
        rank = 8;
    else if (step instanceof DedupGlobalStep)
        rank = 9;
    else if (step instanceof OrderGlobalStep)
        rank = 10;
    else
        return 0;
    ////////////
    if (step instanceof TraversalParent)
        return getMaxStepRank((TraversalParent) step, rank);
    else
        return rank;
}
 
Example #16
Source File: JanusPreviousPropertyStepStrategy.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Replace the {@code graphStep} and {@code filterStep} with a new JanusPreviousPropertyStep in the given
 * {@code traversal}.
 */
private void executeStrategy(
        Traversal.Admin<?, ?> traversal, GraphStep<?, ?> graphStep, TraversalFilterStep<Vertex> filterStep,
        String propertyKey, String label) {

    JanusPreviousPropertyStep newStep = new JanusPreviousPropertyStep(traversal, propertyKey, label);
    traversal.removeStep(filterStep);
    TraversalHelper.replaceStep(graphStep, newStep, traversal);
}
 
Example #17
Source File: AdjacentVertexFilterOptimizerStrategy.java    From grakn with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public void apply(Traversal.Admin<?, ?> traversal) {

    TraversalHelper.getStepsOfClass(TraversalFilterStep.class, traversal).forEach(originalStep -> {
        // Check if this filter traversal matches the pattern: _.inV/outV/otherV.is(x)
        Traversal.Admin<?, ?> filterTraversal = (Traversal.Admin<?, ?>) originalStep.getLocalChildren().get(0);
        List<Step> steps = filterTraversal.getSteps();
        if (steps.size() == 2 &&
                (steps.get(0) instanceof EdgeVertexStep || steps.get(0) instanceof EdgeOtherVertexStep) &&
                (steps.get(1) instanceof IsStep)) {
            //Get the direction in which we filter on the adjacent vertex (or null if not a valid adjacency filter)
            Direction direction = null;
            if (steps.get(0) instanceof EdgeVertexStep) {
                EdgeVertexStep evs = (EdgeVertexStep) steps.get(0);
                if (evs.getDirection() != Direction.BOTH) direction = evs.getDirection();
            } else {
                direction = Direction.BOTH;
            }
            P predicate = ((IsStep) steps.get(1)).getPredicate();
            //Check that we have a valid direction and a valid vertex filter predicate
            if (direction != null && predicate.getBiPredicate() == Compare.eq && predicate.getValue() instanceof Vertex) {
                Vertex vertex = (Vertex) predicate.getValue();

                //Now, check that this step is preceded by VertexStep that returns edges
                Step<?, ?> currentStep = originalStep.getPreviousStep();
                while (currentStep != EmptyStep.instance()) {
                    if (!(currentStep instanceof HasStep) && !(currentStep instanceof IdentityStep)) {
                        break;
                    } //We can jump over other steps as we move backward
                    currentStep = currentStep.getPreviousStep();
                }
                if (currentStep instanceof VertexStep) {
                    VertexStep vertexStep = (VertexStep) currentStep;
                    if (vertexStep.returnsEdge()
                            && (direction == Direction.BOTH || direction.equals(vertexStep.getDirection().opposite()))) {
                        //Now replace the step with a has condition
                        TraversalHelper.replaceStep(originalStep,
                            new HasStep(traversal, new HasContainer(ImplicitKey.ADJACENT_ID.name(), P.eq(vertex))),
                            traversal);
                    }
                }

            }
        }

    });

}
 
Example #18
Source File: SubgraphStrategyProcessTest.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Test
@LoadGraphWith(CREW)
public void shouldFilterVertexProperties() throws Exception {
    GraphTraversalSource sg = g.withStrategies(SubgraphStrategy.create(new MapConfiguration(new HashMap<String, Object>() {{
        put(SubgraphStrategy.VERTEX_PROPERTIES, has("startTime", P.gt(2005)));
    }})));
    checkResults(Arrays.asList("purcellville", "baltimore", "oakland", "seattle", "aachen"), sg.V().properties("location").value());
    checkResults(Arrays.asList("purcellville", "baltimore", "oakland", "seattle", "aachen"), sg.V().values("location"));
    if (sg.getStrategies().getStrategy(InlineFilterStrategy.class).isPresent())
        assertFalse(TraversalHelper.hasStepOfAssignableClassRecursively(TraversalFilterStep.class, sg.V().properties("location").value().iterate().asAdmin()));
    // check to make sure edge properties are not analyzed
    sg = g.withStrategies(SubgraphStrategy.build().vertexProperties(has("startTime", P.gt(2005))).create());
    checkResults(Arrays.asList("purcellville", "baltimore", "oakland", "seattle", "aachen"), sg.V().as("a").properties("location").as("b").select("a").outE().properties().select("b").value().dedup());
    checkResults(Arrays.asList("purcellville", "baltimore", "oakland", "seattle", "aachen"), sg.V().as("a").values("location").as("b").select("a").outE().properties().select("b").dedup());
    if (sg.getStrategies().getStrategy(InlineFilterStrategy.class).isPresent())
        assertFalse(TraversalHelper.hasStepOfAssignableClassRecursively(TraversalFilterStep.class, sg.V().as("a").values("location").as("b").select("a").outE().properties().select("b").dedup().iterate().asAdmin()));
    //
    sg = g.withStrategies(SubgraphStrategy.build().vertices(has("name", P.neq("stephen"))).vertexProperties(has("startTime", P.gt(2005))).create());
    checkResults(Arrays.asList("baltimore", "oakland", "seattle", "aachen"), sg.V().properties("location").value());
    checkResults(Arrays.asList("baltimore", "oakland", "seattle", "aachen"), sg.V().values("location"));
    //
    sg = g.withStrategies(SubgraphStrategy.build().vertices(has("name", P.not(P.within("stephen", "daniel")))).vertexProperties(has("startTime", P.gt(2005))).create());
    checkResults(Arrays.asList("baltimore", "oakland", "seattle"), sg.V().properties("location").value());
    checkResults(Arrays.asList("baltimore", "oakland", "seattle"), sg.V().values("location"));
    //
    sg = g.withStrategies(SubgraphStrategy.build().vertices(has("name", P.eq("matthias"))).vertexProperties(has("startTime", P.gte(2014))).create());
    checkResults(makeMapList(1, "seattle", 1L), sg.V().groupCount().by("location"));
    //
    sg = g.withStrategies(SubgraphStrategy.build().vertices(has("location")).vertexProperties(hasNot("endTime")).create());
    checkOrderedResults(Arrays.asList("aachen", "purcellville", "santa fe", "seattle"), sg.V().order().by("location", Order.asc).values("location"));
    //
    sg = g.withStrategies(SubgraphStrategy.create(new MapConfiguration(new HashMap<String, Object>() {{
        put(SubgraphStrategy.VERTICES, has("location"));
        put(SubgraphStrategy.VERTEX_PROPERTIES, hasNot("endTime"));
    }})));
    checkResults(Arrays.asList("aachen", "purcellville", "santa fe", "seattle"), sg.V().valueMap("location").select(Column.values).unfold().unfold());
    checkResults(Arrays.asList("aachen", "purcellville", "santa fe", "seattle"), sg.V().propertyMap("location").select(Column.values).unfold().unfold().value());
    //
    sg = g.withStrategies(SubgraphStrategy.build().edges(__.<Edge>hasLabel("uses").has("skill", 5)).create());
    checkResults(Arrays.asList(5, 5, 5), sg.V().outE().valueMap().select(Column.values).unfold());
    checkResults(Arrays.asList(5, 5, 5), sg.V().outE().propertyMap().select(Column.values).unfold().value());
    //
    sg = g.withStrategies(SubgraphStrategy.build().vertexProperties(hasNot("skill")).create());
    checkResults(Arrays.asList(3, 3, 3, 4, 4, 5, 5, 5), sg.V().outE("uses").values("skill"));
    checkResults(Arrays.asList(3, 3, 3, 4, 4, 5, 5, 5), sg.V().as("a").properties().select("a").dedup().outE().values("skill"));
    checkResults(Arrays.asList(3, 3, 3, 4, 4, 5, 5, 5), sg.V().as("a").properties().select("a").dedup().outE().properties("skill").as("b").identity().select("b").by(__.value()));
}
 
Example #19
Source File: JanusPreviousPropertyStepStrategy.java    From grakn with GNU Affero General Public License v3.0 4 votes vote down vote up
private List<Step> stepsFromFilterStep(TraversalFilterStep<Vertex> filterStep) {
    // TraversalFilterStep always has exactly one child, so this is safe
    return filterStep.getLocalChildren().get(0).getSteps();
}
 
Example #20
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 #21
Source File: GraphTraversal.java    From tinkerpop with Apache License 2.0 3 votes vote down vote up
/**
 * Filters the current object based on the object itself or the path history.
 *
 * @param whereTraversal the filter to apply
 * @return the traversal with an appended {@link WherePredicateStep}
 * @see <a href="http://tinkerpop.apache.org/docs/${project.version}/reference/#where-step" target="_blank">Reference Documentation - Where Step</a>
 * @see <a href="http://tinkerpop.apache.org/docs/${project.version}/reference/#using-where-with-match" target="_blank">Reference Documentation - Where with Match</a>
 * @see <a href="http://tinkerpop.apache.org/docs/${project.version}/reference/#using-where-with-select" target="_blank">Reference Documentation - Where with Select</a>
 * @since 3.0.0-incubating
 */
public default GraphTraversal<S, E> where(final Traversal<?, ?> whereTraversal) {
    this.asAdmin().getBytecode().addStep(Symbols.where, whereTraversal);
    return TraversalHelper.getVariableLocations(whereTraversal.asAdmin()).isEmpty() ?
            this.asAdmin().addStep(new TraversalFilterStep<>(this.asAdmin(), (Traversal) whereTraversal)) :
            this.asAdmin().addStep(new WhereTraversalStep<>(this.asAdmin(), whereTraversal));
}
 
Example #22
Source File: GraphTraversal.java    From tinkerpop with Apache License 2.0 2 votes vote down vote up
/**
 * Filters vertices, edges and vertex properties based on the existence of properties.
 *
 * @param propertyKey the key of the property to filter on for existence
 * @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) {
    this.asAdmin().getBytecode().addStep(Symbols.has, propertyKey);
    return this.asAdmin().addStep(new TraversalFilterStep(this.asAdmin(), __.values(propertyKey)));
}
 
Example #23
Source File: GraphTraversal.java    From tinkerpop with Apache License 2.0 2 votes vote down vote up
/**
 * Map the {@link Traverser} to either {@code true} or {@code false}, where {@code false} will not pass the
 * traverser to the next step.
 *
 * @param filterTraversal the filter traversal to apply
 * @return the traversal with the {@link TraversalFilterStep} added
 * @see <a href="http://tinkerpop.apache.org/docs/${project.version}/reference/#general-steps" target="_blank">Reference Documentation - General Steps</a>
 * @since 3.0.0-incubating
 */
public default GraphTraversal<S, E> filter(final Traversal<?, ?> filterTraversal) {
    this.asAdmin().getBytecode().addStep(Symbols.filter, filterTraversal);
    return this.asAdmin().addStep(new TraversalFilterStep<>(this.asAdmin(), (Traversal) filterTraversal));
}