Search in sources :

Example 1 with OrderGlobalStep

use of org.apache.tinkerpop.gremlin.process.traversal.step.map.OrderGlobalStep in project sqlg by pietermartin.

the class TestOptionalWithOrder method testOptionalWithOrder2.

@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));
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) DefaultGraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal) IdentityStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep) OrderGlobalStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.OrderGlobalStep) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 2 with OrderGlobalStep

use of org.apache.tinkerpop.gremlin.process.traversal.step.map.OrderGlobalStep in project titan by thinkaurelius.

the class HasStepFolder method foldInOrder.

//    public static boolean addLabeledStepAsIdentity(Step<?,?> currentStep, final Traversal.Admin<?, ?> traversal) {
//        if (currentStep.getLabel().isPresent()) {
//            final IdentityStep identityStep = new IdentityStep<>(traversal);
//            identityStep.setLabel(currentStep.getLabel().get());
//            TraversalHelper.insertAfterStep(identityStep, currentStep, traversal);
//            return true;
//        } else return false;
//    }
public static void foldInOrder(final HasStepFolder titanStep, final Traversal.Admin<?, ?> traversal, final Traversal<?, ?> rootTraversal, boolean isVertexOrder) {
    Step<?, ?> currentStep = titanStep.getNextStep();
    OrderGlobalStep<?> lastOrder = null;
    while (true) {
        if (currentStep instanceof OrderGlobalStep) {
            if (lastOrder != null) {
                //Previous orders are rendered irrelevant by next order (since re-ordered)
                lastOrder.getLabels().forEach(titanStep::addLabel);
                traversal.removeStep(lastOrder);
            }
            lastOrder = (OrderGlobalStep) currentStep;
        } else if (currentStep instanceof IdentityStep) {
        // do nothing, can be skipped
        } else if (currentStep instanceof HasStep) {
        // do nothing, can be skipped
        } else {
            break;
        }
        currentStep = currentStep.getNextStep();
    }
    if (lastOrder != null && lastOrder instanceof OrderGlobalStep) {
        if (validTitanOrder(lastOrder, rootTraversal, isVertexOrder)) {
            //Add orders to HasStepFolder
            for (Comparator comp : (List<Comparator>) ((OrderGlobalStep) lastOrder).getComparators()) {
                ElementValueComparator evc = (ElementValueComparator) comp;
                titanStep.orderBy(evc.getPropertyKey(), (Order) evc.getValueComparator());
            }
            lastOrder.getLabels().forEach(titanStep::addLabel);
            traversal.removeStep(lastOrder);
        }
    }
}
Also used : IdentityStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep) HasStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.HasStep) OrderGlobalStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.OrderGlobalStep) ArrayList(java.util.ArrayList) List(java.util.List) ElementValueComparator(org.apache.tinkerpop.gremlin.process.traversal.step.util.ElementValueComparator) ElementValueComparator(org.apache.tinkerpop.gremlin.process.traversal.step.util.ElementValueComparator) Comparator(java.util.Comparator)

Example 3 with OrderGlobalStep

use of org.apache.tinkerpop.gremlin.process.traversal.step.map.OrderGlobalStep in project janusgraph by JanusGraph.

the class HasStepFolder method foldInOrder.

static void foldInOrder(final HasStepFolder janusgraphStep, final Traversal.Admin<?, ?> traversal, final Traversal<?, ?> rootTraversal, boolean isVertexOrder) {
    Step<?, ?> currentStep = janusgraphStep.getNextStep();
    OrderGlobalStep<?, ?> lastOrder = null;
    while (true) {
        if (currentStep instanceof OrderGlobalStep) {
            if (lastOrder != null) {
                // Previous orders are rendered irrelevant by next order (since re-ordered)
                lastOrder.getLabels().forEach(janusgraphStep::addLabel);
                traversal.removeStep(lastOrder);
            }
            lastOrder = (OrderGlobalStep) currentStep;
        } else if (!(currentStep instanceof IdentityStep) && !(currentStep instanceof HasStep) && !(currentStep instanceof NoOpBarrierStep)) {
            break;
        }
        currentStep = currentStep.getNextStep();
    }
    if (lastOrder != null) {
        if (validJanusGraphOrder(lastOrder, rootTraversal, isVertexOrder)) {
            // Add orders to HasStepFolder
            for (Pair<Traversal.Admin<Object, Comparable>, Comparator<Object>> comp : (List<Pair<Traversal.Admin<Object, Comparable>, Comparator<Object>>>) ((OrderGlobalStep) lastOrder).getComparators()) {
                ElementValueTraversal evt = (ElementValueTraversal) comp.getValue0();
                janusgraphStep.orderBy(evt.getPropertyKey(), (Order) comp.getValue1());
            }
            lastOrder.getLabels().forEach(janusgraphStep::addLabel);
            traversal.removeStep(lastOrder);
        }
    }
}
Also used : IdentityStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep) HasStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.HasStep) OrderGlobalStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.OrderGlobalStep) NoOpBarrierStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.NoOpBarrierStep) ArrayList(java.util.ArrayList) List(java.util.List) ElementValueTraversal(org.apache.tinkerpop.gremlin.process.traversal.lambda.ElementValueTraversal) Comparator(java.util.Comparator)

Example 4 with OrderGlobalStep

use of org.apache.tinkerpop.gremlin.process.traversal.step.map.OrderGlobalStep in project unipop by unipop-graph.

the class UniGraphOrderStrategy method apply.

@Override
public void apply(Traversal.Admin<?, ?> traversal) {
    TraversalHelper.getStepsOfAssignableClass(OrderGlobalStep.class, traversal).forEach(orderGlobalStep -> {
        List<Pair<Traversal.Admin, Comparator>> comparators = orderGlobalStep.getComparators();
        List<Pair<String, Order>> collect = comparators.stream().filter(pair -> pair.getValue0() instanceof ElementValueTraversal).filter(pair -> pair.getValue1() instanceof Order).map(pair -> Pair.with(((ElementValueTraversal) pair.getValue0()).getPropertyKey(), ((Order) pair.getValue1()))).collect(Collectors.toList());
        Collection<Orderable> orderableStepOf = getOrderableStepOf(orderGlobalStep, traversal);
        if (orderableStepOf != null && orderableStepOf.size() == 1) {
            Orderable step = orderableStepOf.iterator().next();
            step.setOrders(collect);
            Step nextStep = orderGlobalStep.getNextStep();
            if (nextStep instanceof RangeGlobalStep) {
                if (step instanceof ReceivesPredicatesHolder) {
                    RangeGlobalStep rangeGlobalStep = (RangeGlobalStep) nextStep;
                    int limit = rangeGlobalStep.getHighRange() > Integer.MAX_VALUE ? -1 : (int) rangeGlobalStep.getHighRange();
                    ((ReceivesPredicatesHolder) step).setLimit(limit);
                }
            }
        }
    });
}
Also used : EmptyStep(org.apache.tinkerpop.gremlin.process.traversal.step.util.EmptyStep) WhereTraversalStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.WhereTraversalStep) TraversalHelper(org.apache.tinkerpop.gremlin.process.traversal.util.TraversalHelper) java.util(java.util) EdgeStepsStrategy(org.unipop.process.edge.EdgeStepsStrategy) UniGraphVertexStepStrategy(org.unipop.process.vertex.UniGraphVertexStepStrategy) UniGraphStepStrategy(org.unipop.process.graph.UniGraphStepStrategy) TraversalStrategy(org.apache.tinkerpop.gremlin.process.traversal.TraversalStrategy) DedupGlobalStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.DedupGlobalStep) UniGraphRepeatStepStrategy(org.unipop.process.repeat.UniGraphRepeatStepStrategy) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Pair(org.javatuples.Pair) ReceivesPredicatesHolder(org.unipop.process.predicate.ReceivesPredicatesHolder) Step(org.apache.tinkerpop.gremlin.process.traversal.Step) OrderGlobalStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.OrderGlobalStep) Stream(java.util.stream.Stream) AbstractTraversalStrategy(org.apache.tinkerpop.gremlin.process.traversal.strategy.AbstractTraversalStrategy) Order(org.apache.tinkerpop.gremlin.process.traversal.Order) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) ElementValueTraversal(org.apache.tinkerpop.gremlin.process.traversal.lambda.ElementValueTraversal) TraversalParent(org.apache.tinkerpop.gremlin.process.traversal.step.TraversalParent) RangeGlobalStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.RangeGlobalStep) Order(org.apache.tinkerpop.gremlin.process.traversal.Order) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) ElementValueTraversal(org.apache.tinkerpop.gremlin.process.traversal.lambda.ElementValueTraversal) OrderGlobalStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.OrderGlobalStep) EmptyStep(org.apache.tinkerpop.gremlin.process.traversal.step.util.EmptyStep) WhereTraversalStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.WhereTraversalStep) DedupGlobalStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.DedupGlobalStep) Step(org.apache.tinkerpop.gremlin.process.traversal.Step) OrderGlobalStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.OrderGlobalStep) RangeGlobalStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.RangeGlobalStep) ElementValueTraversal(org.apache.tinkerpop.gremlin.process.traversal.lambda.ElementValueTraversal) ReceivesPredicatesHolder(org.unipop.process.predicate.ReceivesPredicatesHolder) RangeGlobalStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.RangeGlobalStep) Pair(org.javatuples.Pair)

Example 5 with OrderGlobalStep

use of org.apache.tinkerpop.gremlin.process.traversal.step.map.OrderGlobalStep in project sqlg by pietermartin.

the class TestLocalVertexStepOptionalWithOrder method testOptionalWithOrder2.

@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));
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) DefaultGraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal) IdentityStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep) SqlgLocalStepBarrier(org.umlg.sqlg.step.barrier.SqlgLocalStepBarrier) OrderGlobalStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.OrderGlobalStep) SqlgGraphStep(org.umlg.sqlg.step.SqlgGraphStep) SqlgVertexStep(org.umlg.sqlg.step.SqlgVertexStep) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Aggregations

OrderGlobalStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.OrderGlobalStep)5 IdentityStep (org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep)4 ArrayList (java.util.ArrayList)2 Comparator (java.util.Comparator)2 List (java.util.List)2 DefaultGraphTraversal (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal)2 ElementValueTraversal (org.apache.tinkerpop.gremlin.process.traversal.lambda.ElementValueTraversal)2 HasStep (org.apache.tinkerpop.gremlin.process.traversal.step.filter.HasStep)2 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)2 Test (org.junit.Test)2 BaseTest (org.umlg.sqlg.test.BaseTest)2 Sets (com.google.common.collect.Sets)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 Order (org.apache.tinkerpop.gremlin.process.traversal.Order)1 Step (org.apache.tinkerpop.gremlin.process.traversal.Step)1 Traversal (org.apache.tinkerpop.gremlin.process.traversal.Traversal)1 TraversalStrategy (org.apache.tinkerpop.gremlin.process.traversal.TraversalStrategy)1 TraversalParent (org.apache.tinkerpop.gremlin.process.traversal.step.TraversalParent)1