org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep Java Examples

The following examples show how to use org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep. 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: HasStepFolder.java    From grakn with GNU Affero General Public License v3.0 6 votes vote down vote up
static void localFoldInHasContainer(HasStepFolder janusgraphStep, Step<?, ?> tinkerpopStep, Traversal.Admin<?, ?> traversal, Traversal<?, ?> rootTraversal) {
    Step<?, ?> currentStep = tinkerpopStep;
    while (true) {
        if (currentStep instanceof HasContainerHolder) {
            Iterable<HasContainer> containers = ((HasContainerHolder) currentStep).getHasContainers().stream().map(c -> JanusGraphPredicate.Converter.convert(c)).collect(Collectors.toList());
            List<HasContainer> hasContainers = janusgraphStep.addLocalAll(containers);
            currentStep.getLabels().forEach(janusgraphStep::addLabel);
            traversal.removeStep(currentStep);
            currentStep = foldInOrder(janusgraphStep, currentStep, traversal, rootTraversal, janusgraphStep instanceof JanusGraphStep && ((JanusGraphStep) janusgraphStep).returnsVertex(), hasContainers);
            foldInRange(janusgraphStep, currentStep, traversal, hasContainers);
        } else if (!(currentStep instanceof IdentityStep) && !(currentStep instanceof NoOpBarrierStep)) {
            break;
        }
        currentStep = currentStep.getNextStep();
    }
}
 
Example #2
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 #3
Source File: BranchStep.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public void addGlobalChildOption(final M pickToken, final Traversal.Admin<S, E> traversalOption) {
    if (pickToken instanceof Pick) {
        if (this.traversalPickOptions.containsKey(pickToken))
            this.traversalPickOptions.get(pickToken).add(traversalOption);
        else
            this.traversalPickOptions.put((Pick) pickToken, new ArrayList<>(Collections.singletonList(traversalOption)));
    } else {
        final Traversal.Admin pickOptionTraversal;
        if (pickToken instanceof Traversal) {
            pickOptionTraversal = ((Traversal) pickToken).asAdmin();
        } else {
            pickOptionTraversal = new PredicateTraversal(pickToken);
        }
        this.traversalOptions.add(Pair.with(pickOptionTraversal, traversalOption));
    }
    // adding an IdentityStep acts as a placeholder when reducing barriers get in the way - see the
    // standardAlgorithm() method for more information.
    if (TraversalHelper.hasStepOfAssignableClass(ReducingBarrierStep.class, traversalOption))
        traversalOption.addStep(0, new IdentityStep(traversalOption));
    traversalOption.addStep(new EndStep(traversalOption));

    if (!this.hasBarrier && !TraversalHelper.getStepsOfAssignableClassRecursively(Barrier.class, traversalOption).isEmpty())
        this.hasBarrier = true;
    this.integrateChild(traversalOption);
}
 
Example #4
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 #5
Source File: HasStepFolder.java    From titan1withtp3.1 with Apache License 2.0 6 votes vote down vote up
public static void foldInHasContainer(final HasStepFolder titanStep, final Traversal.Admin<?, ?> traversal) {

        Step<?, ?> currentStep = titanStep.getNextStep();
        while (true) {
            if (currentStep instanceof HasContainerHolder) {
                Iterable<HasContainer> containers = ((HasContainerHolder) currentStep).getHasContainers();
                if (validTitanHas(containers)) {
                    titanStep.addAll(containers);
                    currentStep.getLabels().forEach(titanStep::addLabel);
                    traversal.removeStep(currentStep);
                }
            } else if (currentStep instanceof IdentityStep) {
                // do nothing, has no impact
            } else {
                break;
            }
            currentStep = currentStep.getNextStep();
        }
    }
 
Example #6
Source File: ByModulatorOptimizationStrategy.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    final Step step = traversal.getParent().asStep();
    if (step instanceof ByModulating && step instanceof TraversalParent) {
        final TraversalParent byModulatingStep = (TraversalParent) step;
        if (step instanceof Grouping) {
            final Grouping grouping = (Grouping) step;
            optimizeByModulatingTraversal(byModulatingStep, grouping.getKeyTraversal());

            // the value by() needs different handling because by(Traversal) only equals by(String) or by(T)
            // if the traversal does a fold().
            final Traversal.Admin<?, ?> currentValueTraversal = grouping.getValueTraversal();
            final List<Step> stepsInCurrentValueTraversal = currentValueTraversal.getSteps();
            if (stepsInCurrentValueTraversal.size() == 1 && stepsInCurrentValueTraversal.get(0) instanceof IdentityStep)
                optimizeForStep(byModulatingStep, currentValueTraversal, stepsInCurrentValueTraversal.get(0));
            else if (stepsInCurrentValueTraversal.size() == 2 && stepsInCurrentValueTraversal.get(1) instanceof FoldStep)
                optimizeForStep(byModulatingStep, currentValueTraversal, stepsInCurrentValueTraversal.get(0));

        } else {
            for (final Traversal.Admin<?, ?> byModulatingTraversal : byModulatingStep.getLocalChildren()) {
                optimizeByModulatingTraversal(byModulatingStep, byModulatingTraversal);
            }
        }
    }
}
 
Example #7
Source File: HasStepFolder.java    From grakn with GNU Affero General Public License v3.0 6 votes vote down vote up
static void foldInRange(HasStepFolder janusgraphStep, Step<?, ?> tinkerpopStep, Traversal.Admin<?, ?> traversal, List<HasContainer> hasContainers) {
    Step<?, ?> nextStep = tinkerpopStep instanceof IdentityStep ? JanusGraphTraversalUtil.getNextNonIdentityStep(tinkerpopStep) : tinkerpopStep;
    if (nextStep instanceof RangeGlobalStep) {
        RangeGlobalStep range = (RangeGlobalStep) nextStep;
        int low = 0;
        if (janusgraphStep instanceof JanusGraphStep) {
            low = QueryUtil.convertLimit(range.getLowRange());
            low = QueryUtil.mergeLowLimits(low, hasContainers == null ? janusgraphStep.getLowLimit() : janusgraphStep.getLocalLowLimit(hasContainers));
        }
        int high = QueryUtil.convertLimit(range.getHighRange());
        high = QueryUtil.mergeHighLimits(high, hasContainers == null ? janusgraphStep.getHighLimit() : janusgraphStep.getLocalHighLimit(hasContainers));
        if (hasContainers == null) {
            janusgraphStep.setLimit(low, high);
        } else {
            janusgraphStep.setLocalLimit(hasContainers, low, high);
        }
        if (janusgraphStep instanceof JanusGraphStep || range.getLowRange() == 0) { //Range can be removed since there is JanusGraphStep or no offset
            nextStep.getLabels().forEach(janusgraphStep::addLabel);
            traversal.removeStep(nextStep);
        }
    }
}
 
Example #8
Source File: HasStepFolder.java    From grakn with GNU Affero General Public License v3.0 6 votes vote down vote up
static void foldInHasContainer(HasStepFolder janusgraphStep, Traversal.Admin<?, ?> traversal, Traversal<?, ?> rootTraversal) {
    Step<?, ?> currentStep = janusgraphStep.getNextStep();
    while (true) {
        if (currentStep instanceof OrStep && janusgraphStep instanceof JanusGraphStep) {
            for (Traversal.Admin<?, ?> child : ((OrStep<?>) currentStep).getLocalChildren()) {
                if (!validFoldInHasContainer(child.getStartStep(), false)) {
                    return;
                }
            }
            ((OrStep<?>) currentStep).getLocalChildren().forEach(t -> localFoldInHasContainer(janusgraphStep, t.getStartStep(), t, rootTraversal));
            traversal.removeStep(currentStep);
        } else if (currentStep instanceof HasContainerHolder) {
            Iterable<HasContainer> containers = ((HasContainerHolder) currentStep).getHasContainers().stream().map(c -> JanusGraphPredicate.Converter.convert(c)).collect(Collectors.toList());
            if (validFoldInHasContainer(currentStep, true)) {
                janusgraphStep.addAll(containers);
                currentStep.getLabels().forEach(janusgraphStep::addLabel);
                traversal.removeStep(currentStep);
            }
        } else if (!(currentStep instanceof IdentityStep) && !(currentStep instanceof NoOpBarrierStep) && !(currentStep instanceof HasContainerHolder)) {
            break;
        }
        currentStep = currentStep.getNextStep();
    }
}
 
Example #9
Source File: TinkerGraphCountStrategy.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    if (!(traversal.isRoot()) || TraversalHelper.onGraphComputer(traversal))
        return;
    final List<Step> steps = traversal.getSteps();
    if (steps.size() < 2 ||
            !(steps.get(0) instanceof GraphStep) ||
            0 != ((GraphStep) steps.get(0)).getIds().length ||
            !(steps.get(steps.size() - 1) instanceof CountGlobalStep))
        return;
    for (int i = 1; i < steps.size() - 1; i++) {
        final Step current = steps.get(i);
        if (!(//current instanceof MapStep ||  // MapSteps will not necessarily emit an element as demonstrated in https://issues.apache.org/jira/browse/TINKERPOP-1958
                current instanceof IdentityStep ||
                current instanceof NoOpBarrierStep ||
                current instanceof CollectingBarrierStep) ||
                (current instanceof TraversalParent &&
                        TraversalHelper.anyStepRecursively(s -> (s instanceof SideEffectStep || s instanceof AggregateGlobalStep), (TraversalParent) current)))
            return;
    }
    final Class<? extends Element> elementClass = ((GraphStep<?, ?>) steps.get(0)).getReturnClass();
    TraversalHelper.removeAllSteps(traversal);
    traversal.addStep(new TinkerCountGlobalStep<>(traversal, elementClass));
}
 
Example #10
Source File: TinkerGraphCountStrategy.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    if (!(traversal.getParent() instanceof EmptyStep) || TraversalHelper.onGraphComputer(traversal))
        return;
    final List<Step> steps = traversal.getSteps();
    if (steps.size() < 2 ||
            !(steps.get(0) instanceof GraphStep) ||
            0 != ((GraphStep) steps.get(0)).getIds().length ||
            !(steps.get(steps.size() - 1) instanceof CountGlobalStep))
        return;
    for (int i = 1; i < steps.size() - 1; i++) {
        final Step current = steps.get(i);
        if (!(//current instanceof MapStep ||  // MapSteps will not necessarily emit an element as demonstrated in https://issues.apache.org/jira/browse/TINKERPOP-1958
                current instanceof IdentityStep ||
                current instanceof NoOpBarrierStep ||
                current instanceof CollectingBarrierStep) ||
                (current instanceof TraversalParent &&
                        TraversalHelper.anyStepRecursively(s -> (s instanceof SideEffectStep || s instanceof AggregateStep), (TraversalParent) current)))
            return;
    }
    final Class<? extends Element> elementClass = ((GraphStep<?, ?>) steps.get(0)).getReturnClass();
    TraversalHelper.removeAllSteps(traversal);
    traversal.addStep(new TinkerCountGlobalStep<>(traversal, elementClass));
}
 
Example #11
Source File: BaseStrategy.java    From sqlg with MIT License 6 votes vote down vote up
void handleConnectiveSteps(ListIterator<Step<?, ?>> iterator) {
    //Collect the hasSteps
    int countToGoPrevious = 0;
    while (iterator.hasNext()) {
        Step<?, ?> currentStep = iterator.next();
        countToGoPrevious++;
        if (currentStep instanceof ConnectiveStep) {
            Optional<AndOrHasContainer> outerAndOrHasContainer = handleConnectiveStepInternal((ConnectiveStep) currentStep);
            if (outerAndOrHasContainer.isPresent()) {
                this.currentReplacedStep.addAndOrHasContainer(outerAndOrHasContainer.get());
                this.traversal.removeStep(currentStep);
                iterator.remove();
                countToGoPrevious--;
            }
        } else if (currentStep instanceof IdentityStep) {
            // do nothing
        } else {
            for (int i = 0; i < countToGoPrevious; i++) {
                iterator.previous();
            }
            break;
        }
    }
}
 
Example #12
Source File: TraversalUtil.java    From hugegraph with Apache License 2.0 6 votes vote down vote up
public static void extractOrder(Step<?, ?> newStep,
                                Traversal.Admin<?, ?> traversal) {
    Step<?, ?> step = newStep;
    do {
        step = step.getNextStep();
        if (step instanceof OrderGlobalStep) {
            QueryHolder holder = (QueryHolder) newStep;
            @SuppressWarnings("resource")
            OrderGlobalStep<?, ?> orderStep = (OrderGlobalStep<?, ?>) step;
            orderStep.getComparators().forEach(comp -> {
                ElementValueComparator<?> comparator =
                        (ElementValueComparator<?>) comp.getValue1();
                holder.orderBy(comparator.getPropertyKey(),
                               (Order) comparator.getValueComparator());
            });
            TraversalHelper.copyLabels(step, newStep, false);
            traversal.removeStep(step);
        }
        step = step.getNextStep();
    } while (step instanceof OrderGlobalStep ||
             step instanceof IdentityStep);
}
 
Example #13
Source File: TraversalHelperTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldInsertBeforeStep() {
    final Traversal.Admin traversal = new DefaultTraversal<>(EmptyGraph.instance());
    final HasStep hasStep = new HasStep(traversal);
    final IdentityStep identityStep = new IdentityStep(traversal);
    traversal.asAdmin().addStep(0, new HasStep(traversal));
    traversal.asAdmin().addStep(0, hasStep);
    traversal.asAdmin().addStep(0, new HasStep(traversal));

    TraversalHelper.insertBeforeStep(identityStep, hasStep, traversal);

    assertEquals(traversal.asAdmin().getSteps().get(1), identityStep);
    assertEquals(4, traversal.asAdmin().getSteps().size());
}
 
Example #14
Source File: TraversalHelperTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldChainTogetherStepsWithNextPreviousInALinkedListStructure() {
    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));
    validateToyTraversal(traversal);
}
 
Example #15
Source File: TraversalHelperTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReplaceStep() {
    final Traversal.Admin traversal = new DefaultTraversal<>(EmptyGraph.instance());
    final HasStep hasStep = new HasStep(traversal);
    final IdentityStep identityStep = new IdentityStep(traversal);
    traversal.asAdmin().addStep(0, new HasStep(traversal));
    traversal.asAdmin().addStep(0, hasStep);
    traversal.asAdmin().addStep(0, new HasStep(traversal));

    TraversalHelper.replaceStep(hasStep, identityStep, traversal);

    assertEquals(traversal.asAdmin().getSteps().get(1), identityStep);
    assertEquals(3, traversal.asAdmin().getSteps().size());
}
 
Example #16
Source File: TraversalHelperTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldAddStepsCorrectly() {
    Traversal.Admin traversal = new DefaultTraversal<>(EmptyGraph.instance());
    traversal.asAdmin().addStep(0, new LambdaFilterStep(traversal, traverser -> true));
    traversal.asAdmin().addStep(0, new HasStep(traversal));
    traversal.asAdmin().addStep(0, new IdentityStep(traversal));
    validateToyTraversal(traversal);

    traversal = new DefaultTraversal<>(EmptyGraph.instance());
    traversal.asAdmin().addStep(0, new IdentityStep(traversal));
    traversal.asAdmin().addStep(1, new HasStep(traversal));
    traversal.asAdmin().addStep(2, new LambdaFilterStep(traversal, traverser -> true));
    validateToyTraversal(traversal);
}
 
Example #17
Source File: TraversalHelperTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldInsertAfterStep() {
    final Traversal.Admin traversal = new DefaultTraversal<>(EmptyGraph.instance());
    final HasStep hasStep = new HasStep(traversal);
    final IdentityStep identityStep = new IdentityStep(traversal);
    traversal.asAdmin().addStep(0, new HasStep(traversal));
    traversal.asAdmin().addStep(0, hasStep);
    traversal.asAdmin().addStep(0, new HasStep(traversal));

    TraversalHelper.insertAfterStep(identityStep, hasStep, traversal);

    assertEquals(traversal.asAdmin().getSteps().get(2), identityStep);
    assertEquals(4, traversal.asAdmin().getSteps().size());
}
 
Example #18
Source File: MessagePassingReductionStrategy.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
public static final boolean endsWithElement(Step<?, ?> currentStep) {
    while (!(currentStep instanceof EmptyStep)) {
        if (currentStep instanceof VertexStep) // only inE, in, and out send messages
            return (((VertexStep) currentStep).returnsVertex() || !((VertexStep) currentStep).getDirection().equals(Direction.OUT));
        else if (currentStep instanceof EdgeVertexStep) // TODO: add GraphStep but only if its mid-traversal V()/E()
            return true;
        else if (currentStep instanceof TraversalFlatMapStep || currentStep instanceof TraversalMapStep || currentStep instanceof LocalStep)
            return endsWithElement(((TraversalParent) currentStep).getLocalChildren().get(0).getEndStep());
        else if (!(currentStep instanceof FilterStep || currentStep instanceof SideEffectStep || currentStep instanceof IdentityStep || currentStep instanceof Barrier))
            return false;
        currentStep = currentStep.getPreviousStep();
    }
    return false;
}
 
Example #19
Source File: TraversalHelperTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private static void validateToyTraversal(final Traversal traversal) {
    assertEquals(traversal.asAdmin().getSteps().size(), 3);

    assertEquals(IdentityStep.class, traversal.asAdmin().getSteps().get(0).getClass());
    assertEquals(HasStep.class, traversal.asAdmin().getSteps().get(1).getClass());
    assertEquals(LambdaFilterStep.class, traversal.asAdmin().getSteps().get(2).getClass());

    // IDENTITY STEP
    assertEquals(EmptyStep.class, ((Step) traversal.asAdmin().getSteps().get(0)).getPreviousStep().getClass());
    assertEquals(HasStep.class, ((Step) traversal.asAdmin().getSteps().get(0)).getNextStep().getClass());
    assertEquals(LambdaFilterStep.class, ((Step) traversal.asAdmin().getSteps().get(0)).getNextStep().getNextStep().getClass());
    assertEquals(EmptyStep.class, ((Step) traversal.asAdmin().getSteps().get(0)).getNextStep().getNextStep().getNextStep().getClass());

    // HAS STEP
    assertEquals(IdentityStep.class, ((Step) traversal.asAdmin().getSteps().get(1)).getPreviousStep().getClass());
    assertEquals(EmptyStep.class, ((Step) traversal.asAdmin().getSteps().get(1)).getPreviousStep().getPreviousStep().getClass());
    assertEquals(LambdaFilterStep.class, ((Step) traversal.asAdmin().getSteps().get(1)).getNextStep().getClass());
    assertEquals(EmptyStep.class, ((Step) traversal.asAdmin().getSteps().get(1)).getNextStep().getNextStep().getClass());

    // FILTER STEP
    assertEquals(HasStep.class, ((Step) traversal.asAdmin().getSteps().get(2)).getPreviousStep().getClass());
    assertEquals(IdentityStep.class, ((Step) traversal.asAdmin().getSteps().get(2)).getPreviousStep().getPreviousStep().getClass());
    assertEquals(EmptyStep.class, ((Step) traversal.asAdmin().getSteps().get(2)).getPreviousStep().getPreviousStep().getPreviousStep().getClass());
    assertEquals(EmptyStep.class, ((Step) traversal.asAdmin().getSteps().get(2)).getNextStep().getClass());

    assertEquals(3, traversal.asAdmin().getSteps().size());
}
 
Example #20
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 #21
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 #22
Source File: TestLocalVertexStepOptionalWithOrder.java    From sqlg with MIT License 5 votes vote down vote up
@Test
public void testOptionalWithOrder2() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "order", 13);
    Vertex a2 = this.sqlgGraph.addVertex(T.label, "A", "order", 12);
    Vertex a3 = this.sqlgGraph.addVertex(T.label, "A", "order", 11);
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B", "order", 3);
    Vertex b2 = this.sqlgGraph.addVertex(T.label, "B", "order", 2);
    Vertex b3 = this.sqlgGraph.addVertex(T.label, "B", "order", 1);
    a1.addEdge("ab", b1);
    a1.addEdge("ab", b2);
    a1.addEdge("ab", b3);
    this.sqlgGraph.tx().commit();

    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal()
            .V().hasLabel("A").as("a")
            .local(
                    __.optional(
                            __.outE().as("e").otherV().as("v")
                    )
            ).order().by("order");
    Assert.assertEquals(4, traversal.getSteps().size());
    List<Vertex> vertices = traversal.toList();
    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 SqlgLocalStepBarrier);
    Assert.assertTrue(traversal.getSteps().get(3) instanceof OrderGlobalStep);
    SqlgLocalStepBarrier<?, ?> localStep = (SqlgLocalStepBarrier<?, ?>) traversal.getSteps().get(2);
    List<SqlgVertexStep> sqlgVertexSteps = TraversalHelper.getStepsOfAssignableClassRecursively(SqlgVertexStep.class, localStep.getLocalChildren().get(0));
    Assert.assertEquals(1, sqlgVertexSteps.size());

    Assert.assertEquals(5, vertices.size());
    Assert.assertEquals(b3, vertices.get(0));
    Assert.assertEquals(b2, vertices.get(1));
    Assert.assertEquals(b1, vertices.get(2));
    Assert.assertEquals(a3, vertices.get(3));
    Assert.assertEquals(a2, vertices.get(4));
}
 
Example #23
Source File: TestOptionalWithOrder.java    From sqlg with MIT License 5 votes vote down vote up
@Test
public void testOptionalWithOrder2() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "order", 13);
    Vertex a2 = this.sqlgGraph.addVertex(T.label, "A", "order", 12);
    Vertex a3 = this.sqlgGraph.addVertex(T.label, "A", "order", 11);
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B", "order", 3);
    Vertex b2 = this.sqlgGraph.addVertex(T.label, "B", "order", 2);
    Vertex b3 = this.sqlgGraph.addVertex(T.label, "B", "order", 1);
    a1.addEdge("ab", b1);
    a1.addEdge("ab", b2);
    a1.addEdge("ab", b3);
    this.sqlgGraph.tx().commit();

    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal()
            .V().hasLabel("A").as("a")
            .optional(
                    __.outE().as("e").otherV().as("v")
            ).order().by("order");
    Assert.assertEquals(4, traversal.getSteps().size());
    List<Vertex> vertices = traversal.toList();
    Assert.assertEquals(3, traversal.getSteps().size());

    assertStep(traversal.getSteps().get(0), true, false, false, true);

    Assert.assertTrue(traversal.getSteps().get(1) instanceof IdentityStep);
    Assert.assertTrue(traversal.getSteps().get(2) instanceof OrderGlobalStep);
    Assert.assertEquals(5, vertices.size());
    Assert.assertEquals(b3, vertices.get(0));
    Assert.assertEquals(b2, vertices.get(1));
    Assert.assertEquals(b1, vertices.get(2));
    Assert.assertEquals(a3, vertices.get(3));
    Assert.assertEquals(a2, vertices.get(4));
}
 
Example #24
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 #25
Source File: TraversalHelperTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotFindStepOfAssignableClassInTraversal() {
    final Traversal.Admin traversal = new DefaultTraversal<>(EmptyGraph.instance());
    traversal.asAdmin().addStep(0, new HasStep(traversal));
    traversal.asAdmin().addStep(0, new HasStep(traversal));
    traversal.asAdmin().addStep(0, new HasStep(traversal));

    assertThat(TraversalHelper.hasStepOfAssignableClass(IdentityStep.class, traversal), is(false));
}
 
Example #26
Source File: TraversalUtil.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
public static void extractRange(Step<?, ?> newStep,
                                Traversal.Admin<?, ?> traversal,
                                boolean extractOnlyLimit) {
    QueryHolder holder = (QueryHolder) newStep;
    Step<?, ?> step = newStep;
    do {
        step = step.getNextStep();
        if (step instanceof RangeGlobalStep) {
            @SuppressWarnings("unchecked")
            RangeGlobalStep<Object> range = (RangeGlobalStep<Object>) step;
            /*
             * NOTE: keep the step to limit results after query from DB
             * due to `limit`(in DB) may not be implemented accurately.
             * but the backend driver should ensure `offset` accurately.
             */
            // TraversalHelper.copyLabels(step, newStep, false);
            // traversal.removeStep(step);
            if (extractOnlyLimit) {
                // May need to retain offset for multiple sub-queries
                holder.setRange(0, range.getHighRange());
            } else {
                long limit = holder.setRange(range.getLowRange(),
                                             range.getHighRange());
                RangeGlobalStep<Object> newRange = new RangeGlobalStep<>(
                                                   traversal, 0, limit);
                TraversalHelper.replaceStep(range, newRange, traversal);
            }
        }
    } while (step instanceof RangeGlobalStep ||
             step instanceof IdentityStep ||
             step instanceof NoOpBarrierStep);
}
 
Example #27
Source File: TraversalUtil.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
public static void extractCount(Step<?, ?> newStep,
                                Traversal.Admin<?, ?> traversal) {
    Step<?, ?> step = newStep;
    do {
        step = step.getNextStep();
        if (step instanceof CountGlobalStep) {
            QueryHolder holder = (QueryHolder) newStep;
            holder.setCount();
        }
    } while (step instanceof CountGlobalStep ||
             step instanceof FilterStep ||
             step instanceof IdentityStep ||
             step instanceof NoOpBarrierStep);
}
 
Example #28
Source File: HasStepFolder.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
static boolean validFoldInHasContainer(Step<?, ?> tinkerpopStep, boolean defaultValue) {
    Step<?, ?> currentStep = tinkerpopStep;
    Boolean toReturn = null;
    while (!(currentStep instanceof EmptyStep)) {
        if (currentStep instanceof HasContainerHolder) {
            Iterable<HasContainer> containers = ((HasContainerHolder) currentStep).getHasContainers();
            toReturn = toReturn == null ? validJanusGraphHas(containers) : toReturn && validJanusGraphHas(containers);
        } else if (!(currentStep instanceof IdentityStep) && !(currentStep instanceof NoOpBarrierStep) && !(currentStep instanceof RangeGlobalStep) && !(currentStep instanceof OrderGlobalStep)) {
            toReturn = toReturn != null && (toReturn && defaultValue);
            break;
        }
        currentStep = currentStep.getNextStep();
    }
    return Boolean.TRUE.equals(toReturn);
}
 
Example #29
Source File: FulgoraUtil.java    From titan1withtp3.1 with Apache License 2.0 5 votes vote down vote up
private static void verifyIncidentTraversal(FulgoraElementTraversal<Vertex,Edge> traversal) {
    //First step must be TitanVertexStep
    List<Step> steps = traversal.getSteps();
    Step<Vertex,?> startStep = steps.get(0);
    Preconditions.checkArgument(startStep instanceof TitanVertexStep &&
            TitanTraversalUtil.isEdgeReturnStep((TitanVertexStep) startStep),"Expected first step to be an edge step but found: %s",startStep);
    Optional<Step> violatingStep = steps.stream().filter(s -> !(s instanceof TitanVertexStep ||
            s instanceof OrderGlobalStep || s instanceof OrderLocalStep ||
                    s instanceof IdentityStep || s instanceof FilterStep)).findAny();
    if (violatingStep.isPresent()) throw new IllegalArgumentException("Encountered unsupported step in incident traversal: " + violatingStep.get());
}
 
Example #30
Source File: IdentityRemovalStrategy.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    if (traversal.getSteps().size() <= 1)
        return;

    for (final IdentityStep<?> identityStep : TraversalHelper.getStepsOfClass(IdentityStep.class, traversal)) {
        if (identityStep.getLabels().isEmpty() || !(identityStep.getPreviousStep() instanceof EmptyStep)) {
            TraversalHelper.copyLabels(identityStep, identityStep.getPreviousStep(), false);
            traversal.removeStep(identityStep);
        }
    }
}