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

The following examples show how to use org.apache.tinkerpop.gremlin.process.traversal.step.map.OrderGlobalStep. 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: 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 #2
Source File: HasStepFolder.java    From grakn with GNU Affero General Public License v3.0 6 votes vote down vote up
static boolean validJanusGraphOrder(OrderGlobalStep orderGlobalStep, Traversal rootTraversal, boolean isVertexOrder) {
    List<Pair<Traversal.Admin, Object>> comparators = orderGlobalStep.getComparators();
    for (Pair<Traversal.Admin, Object> comp : comparators) {
        String key;
        if (comp.getValue0() instanceof ElementValueTraversal &&
                comp.getValue1() instanceof Order) {
            key = ((ElementValueTraversal) comp.getValue0()).getPropertyKey();
        } else if (comp.getValue1() instanceof ElementValueComparator) {
            ElementValueComparator evc = (ElementValueComparator) comp.getValue1();
            if (!(evc.getValueComparator() instanceof Order)) return false;
            key = evc.getPropertyKey();
        } else {
            // do not fold comparators that include nested traversals that are not simple ElementValues
            return false;
        }
        JanusGraphTransaction tx = JanusGraphTraversalUtil.getTx(rootTraversal.asAdmin());
        PropertyKey pKey = tx.getPropertyKey(key);
        if (pKey == null
                || !(Comparable.class.isAssignableFrom(pKey.dataType()))
                || (isVertexOrder && pKey.cardinality() != Cardinality.SINGLE)) {
            return false;
        }
    }
    return true;
}
 
Example #3
Source File: ReplacedStepTree.java    From sqlg with MIT License 6 votes vote down vote up
void walkReplacedSteps(Set<SchemaTableTree> schemaTableTrees) {
    //The tree only has one linear path from root to the deepest leaf node.
    //This represents the regular path where each ReplacedStep goes one step deeper down the graph.
    //First build the SchemaTableTrees for this path.
    //The other nodes in this ReplacedStepTree are nodes that need to join onto the left join nodes coming from optional steps.
    List<ReplacedStep<?, ?>> replacedSteps = linearPathToLeafNode();

    for (ReplacedStep<?, ?> replacedStep : replacedSteps) {
        //skip the graph step
        if (replacedStep.getStep() instanceof GraphStep) {
            continue;
        }
        if (!replacedStep.isFake() && !(replacedStep.getStep() instanceof OrderGlobalStep) && !(replacedStep.getStep() instanceof RangeGlobalStep)) {
            //This schemaTableTree represents the tree nodes as build up to this depth. Each replacedStep goes a level further
            schemaTableTrees = replacedStep.calculatePathForStep(schemaTableTrees);
        }
    }
}
 
Example #4
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 #5
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 #6
Source File: HasStepFolder.java    From titan1withtp3.1 with Apache License 2.0 5 votes vote down vote up
public static boolean validTitanOrder(OrderGlobalStep ostep, Traversal rootTraversal,
                                      boolean isVertexOrder) {
    for (Comparator comp : (List<Comparator>) ostep.getComparators()) {
        if (!(comp instanceof ElementValueComparator)) return false;
        ElementValueComparator evc = (ElementValueComparator) comp;
        if (!(evc.getValueComparator() instanceof Order)) return false;

        TitanTransaction tx = TitanTraversalUtil.getTx(rootTraversal.asAdmin());
        String key = evc.getPropertyKey();
        PropertyKey pkey = tx.getPropertyKey(key);
        if (pkey == null || !(Comparable.class.isAssignableFrom(pkey.dataType()))) return false;
        if (isVertexOrder && pkey.cardinality() != Cardinality.SINGLE) return false;
    }
    return true;
}
 
Example #7
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 #8
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 #9
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 #10
Source File: OrderLimitStrategyTest.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Test
public void doTest() {
    traversal.asAdmin().setParent(new TraversalVertexProgramStep(EmptyTraversal.instance(), EmptyTraversal.instance())); // trick it
    applyOrderLimitStrategyStrategy(traversal);
    assertEquals(limit, TraversalHelper.getFirstStepOfAssignableClass(OrderGlobalStep.class, traversal.asAdmin()).get().getLimit());
}
 
Example #11
Source File: GraphTraversal.java    From tinkerpop with Apache License 2.0 2 votes vote down vote up
/**
 * Order all the objects in the traversal up to this point and then emit them one-by-one in their ordered sequence.
 *
 * @return the traversal with an appended {@link OrderGlobalStep}.
 * @see <a href="http://tinkerpop.apache.org/docs/${project.version}/reference/#order-step" target="_blank">Reference Documentation - Order Step</a>
 * @since 3.0.0-incubating
 */
public default GraphTraversal<S, E> order() {
    this.asAdmin().getBytecode().addStep(Symbols.order);
    return this.asAdmin().addStep(new OrderGlobalStep<>(this.asAdmin()));
}
 
Example #12
Source File: GraphTraversal.java    From tinkerpop with Apache License 2.0 2 votes vote down vote up
/**
 * Order either the {@link Scope#local} object (e.g. a list, map, etc.) or the entire {@link Scope#global} traversal stream.
 *
 * @param scope whether the ordering is the current local object or the entire global stream.
 * @return the traversal with an appended {@link OrderGlobalStep} or {@link OrderLocalStep} depending on the {@code scope}.
 * @see <a href="http://tinkerpop.apache.org/docs/${project.version}/reference/#order-step" target="_blank">Reference Documentation - Order Step</a>
 * @since 3.0.0-incubating
 */
public default GraphTraversal<S, E> order(final Scope scope) {
    this.asAdmin().getBytecode().addStep(Symbols.order, scope);
    return this.asAdmin().addStep(scope.equals(Scope.global) ? new OrderGlobalStep<>(this.asAdmin()) : new OrderLocalStep<>(this.asAdmin()));
}