org.apache.tinkerpop.gremlin.process.traversal.step.util.AbstractStep Java Examples

The following examples show how to use org.apache.tinkerpop.gremlin.process.traversal.step.util.AbstractStep. 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: GraphStrategy.java    From sqlg with MIT License 6 votes vote down vote up
@Override
protected boolean doFirst(ListIterator<Step<?, ?>> stepIterator, Step<?, ?> step, MutableInt pathCount) {
    this.currentReplacedStep = ReplacedStep.from(
            this.sqlgGraph.getTopology(),
            (AbstractStep<?, ?>) step,
            pathCount.getValue()
    );
    handleHasSteps(stepIterator, pathCount.getValue());
    handleOrderGlobalSteps(stepIterator, pathCount);
    handleRangeGlobalSteps(stepIterator, pathCount);
    handleConnectiveSteps(stepIterator);
    this.sqlgStep = constructSqlgStep(step);
    this.currentTreeNodeNode = this.sqlgStep.addReplacedStep(this.currentReplacedStep);
    replaceStepInTraversal(step, this.sqlgStep);
    if (this.sqlgStep instanceof SqlgGraphStep && ((SqlgGraphStep) this.sqlgStep).getIds().length > 0) {
        addHasContainerForIds((SqlgGraphStep) this.sqlgStep);
    }
    if (!this.currentReplacedStep.hasLabels()) {
        boolean precedesPathStep = precedesPathOrTreeStep(this.traversal);
        if (precedesPathStep) {
            this.currentReplacedStep.addLabel(pathCount.getValue() + BaseStrategy.PATH_LABEL_SUFFIX + BaseStrategy.SQLG_PATH_FAKE_LABEL);
        }
    }
    pathCount.increment();
    return true;
}
 
Example #2
Source File: ReplacedStep.java    From sqlg with MIT License 5 votes vote down vote up
public static <S, E> ReplacedStep from(Topology topology, AbstractStep<S, E> step, int pathCount) {
    ReplacedStep replacedStep = new ReplacedStep<>();
    replacedStep.step = step;
    replacedStep.labels = step.getLabels().stream().map(l -> pathCount + BaseStrategy.PATH_LABEL_SUFFIX + l).collect(Collectors.toSet());
    replacedStep.topology = topology;
    replacedStep.fake = false;
    return replacedStep;
}
 
Example #3
Source File: BaseStrategy.java    From sqlg with MIT License 5 votes vote down vote up
private void handleVertexStep(ListIterator<Step<?, ?>> stepIterator, AbstractStep<?, ?> step, MutableInt pathCount) {
    this.currentReplacedStep = ReplacedStep.from(
            this.sqlgGraph.getTopology(),
            step,
            pathCount.getValue()
    );
    //Important to add the replacedStep before collecting the additional steps.
    //In particular the orderGlobalStep needs to the currentStepDepth setted.
    ReplacedStepTree.TreeNode treeNodeNode = this.sqlgStep.addReplacedStep(this.currentReplacedStep);
    handleHasSteps(stepIterator, pathCount.getValue());
    handleOrderGlobalSteps(stepIterator, pathCount);
    handleRangeGlobalSteps(stepIterator, pathCount);
    handleConnectiveSteps(stepIterator);
    //if called from ChooseStep then the VertexStep is nested inside the ChooseStep and not one of the traversal's direct steps.
    int index = TraversalHelper.stepIndex(step, this.traversal);
    if (index != -1) {
        this.traversal.removeStep(step);
    }
    if (!this.currentReplacedStep.hasLabels()) {
        boolean precedesPathStep = precedesPathOrTreeStep(this.traversal);
        if (precedesPathStep) {
            this.currentReplacedStep.addLabel(pathCount.getValue() + BaseStrategy.PATH_LABEL_SUFFIX + BaseStrategy.SQLG_PATH_FAKE_LABEL);
        }
    }
    pathCount.increment();
    this.currentTreeNodeNode = treeNodeNode;
}
 
Example #4
Source File: ReplacedStep.java    From sqlg with MIT License 4 votes vote down vote up
public AbstractStep<S, E> getStep() {
    return step;
}
 
Example #5
Source File: BaseStrategy.java    From sqlg with MIT License 4 votes vote down vote up
private void handleOptionalStep(int optionalStepNestedCount, OptionalStep<?> optionalStep, Traversal.Admin<?, ?> traversal, MutableInt pathCount) {
    //The currentTreeNode here is the node that will need the left join in the sql generation
    this.optionalStepStack.add(this.currentTreeNodeNode);
    Preconditions.checkState(this.optionalStepStack.size() == optionalStepNestedCount);

    Traversal.Admin<?, ?> optionalTraversal = optionalStep.getLocalChildren().get(0);

    ReplacedStep<?, ?> previousReplacedStep = this.sqlgStep.getReplacedSteps().get(this.sqlgStep.getReplacedSteps().size() - 1);
    previousReplacedStep.setLeftJoin(true);

    @SuppressWarnings("unchecked")
    List<Step<?, ?>> optionalTraversalSteps = new ArrayList(optionalTraversal.getSteps());
    ListIterator<Step<?, ?>> optionalStepsIterator = optionalTraversalSteps.listIterator();
    while (optionalStepsIterator.hasNext()) {
        Step internalOptionalStep = optionalStepsIterator.next();
        removeTinkerPopLabels(internalOptionalStep);
        if (internalOptionalStep instanceof VertexStep || internalOptionalStep instanceof EdgeVertexStep || internalOptionalStep instanceof EdgeOtherVertexStep) {
            handleVertexStep(optionalStepsIterator, (AbstractStep<?, ?>) internalOptionalStep, pathCount);
            //if the chooseStepStack size is greater than the chooseStepNestedCount then it means the just executed
            //handleVertexStep is after nested chooseSteps.
            //This means that this VertexStep applies to the nested chooseSteps where the chooseStep was not chosen.
            //I.e. there was no results for the chooseSteps traversal.
            for (int i = optionalStepNestedCount; i < this.chooseStepStack.size(); i++) {
                ReplacedStepTree.TreeNode treeNode = this.chooseStepStack.get(i);
                this.currentReplacedStep.markAsJoinToLeftJoin();
                treeNode.addReplacedStep(this.currentReplacedStep);
            }
        } else if (internalOptionalStep instanceof OptionalStep) {
            handleOptionalStep(optionalStepNestedCount + 1, (OptionalStep) internalOptionalStep, traversal, pathCount);
        } else if (internalOptionalStep instanceof ComputerAwareStep.EndStep) {
            break;
        } else if (internalOptionalStep instanceof HasStep) {
            handleHasSteps(optionalStepsIterator, pathCount.getValue());
        } else {
            throw new IllegalStateException("Unhandled step nested in OptionalStep " + internalOptionalStep.getClass().getName());
        }
    }
    //the chooseStep might be a ChooseStep nested inside another ChooseStep.
    //In that case it will not be a direct step of the traversal.
    if (traversal.getSteps().contains(optionalStep)) {
        traversal.removeStep(optionalStep);
    }
}
 
Example #6
Source File: BaseStrategy.java    From sqlg with MIT License 4 votes vote down vote up
private void handleChooseStep(int chooseStepNestedCount, ChooseStep<?, ?, ?> chooseStep, Traversal.Admin<?, ?> traversal, MutableInt pathCount) {
    //The currentTreeNode here is the node that will need the left join in the sql generation
    this.chooseStepStack.add(this.currentTreeNodeNode);
    Preconditions.checkState(this.chooseStepStack.size() == chooseStepNestedCount);
    List<? extends Traversal.Admin<?, ?>> globalChildren = chooseStep.getGlobalChildren();
    Preconditions.checkState(globalChildren.size() == 2, "ChooseStep's globalChildren must have size 2, one for true and one for false");

    ReplacedStep<?, ?> previousReplacedStep = this.sqlgStep.getReplacedSteps().get(this.sqlgStep.getReplacedSteps().size() - 1);
    previousReplacedStep.setLeftJoin(true);
    Traversal.Admin<?, ?> trueTraversal;
    Traversal.Admin<?, ?> a = globalChildren.get(0);
    Traversal.Admin<?, ?> b = globalChildren.get(1);
    if (a.getSteps().stream().anyMatch(s -> s instanceof IdentityStep<?>)) {
        trueTraversal = b;
    } else {
        trueTraversal = a;
    }
    @SuppressWarnings("unchecked")
    List<Step<?, ?>> trueTraversalSteps = new ArrayList(trueTraversal.getSteps());
    ListIterator<Step<?, ?>> trueTraversalStepsIterator = trueTraversalSteps.listIterator();
    while (trueTraversalStepsIterator.hasNext()) {
        Step internalChooseStep = trueTraversalStepsIterator.next();
        if (internalChooseStep instanceof VertexStep || internalChooseStep instanceof EdgeVertexStep || internalChooseStep instanceof EdgeOtherVertexStep) {
            handleVertexStep(trueTraversalStepsIterator, (AbstractStep<?, ?>) internalChooseStep, pathCount);
            //if the chooseStepStack size is greater than the chooseStepNestedCount then it means the just executed
            //handleVertexStep is after nested chooseSteps.
            //This means that this VertexStep applies to the nested chooseSteps where the chooseStep was not chosen.
            //I.e. there was no results for the chooseSteps traversal.
            for (int i = chooseStepNestedCount; i < this.chooseStepStack.size(); i++) {
                ReplacedStepTree.TreeNode treeNode = this.chooseStepStack.get(i);
                this.currentReplacedStep.markAsJoinToLeftJoin();
                treeNode.addReplacedStep(this.currentReplacedStep);
            }
        } else if (internalChooseStep instanceof ChooseStep) {
            handleChooseStep(chooseStepNestedCount + 1, (ChooseStep) internalChooseStep, traversal, pathCount);
        } else if (internalChooseStep instanceof ComputerAwareStep.EndStep) {
            break;
        } else {
            throw new IllegalStateException("Unhandled step nested in ChooseStep " + internalChooseStep.getClass().getName());
        }
    }
    //the chooseStep might be a ChooseStep nested inside another ChooseStep.
    //In that case it will not be a direct step of the traversal.
    if (traversal.getSteps().contains(chooseStep)) {
        traversal.removeStep(chooseStep);
    }
}