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));
}
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);
}
}
}
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);
}
}
}
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);
}
}
}
});
}
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));
}
Aggregations