use of org.umlg.sqlg.step.barrier.SqlgBranchStepBarrier in project sqlg by pietermartin.
the class SqlgChooseStepStrategy method apply.
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
// noinspection OptionalGetWithoutIsPresent
if (!(traversal.getGraph().get() instanceof SqlgGraph)) {
return;
}
List<ChooseStep> chooseSteps = TraversalHelper.getStepsOfAssignableClass(ChooseStep.class, traversal);
for (ChooseStep<S, E, M> chooseStep : chooseSteps) {
Traversal.Admin<S, M> predicateTraversal = chooseStep.getLocalChildren().get(0);
// The predicate branch step is a local traversal.
// As such if it contains a ReducingBarrierStep the SqlgBranchStepBarrier will not work.
List<ReducingBarrierStep> reducingBarrierSteps = TraversalHelper.getStepsOfAssignableClass(ReducingBarrierStep.class, predicateTraversal);
if (!reducingBarrierSteps.isEmpty()) {
continue;
}
if (predicateTraversal.getSteps().get(predicateTraversal.getSteps().size() - 1) instanceof HasNextStep) {
predicateTraversal.removeStep(predicateTraversal.getSteps().get(predicateTraversal.getSteps().size() - 1));
}
SqlgBranchStepBarrier sqlgBranchStepBarrier = new SqlgChooseStepBarrier<>(traversal, predicateTraversal);
for (String label : chooseStep.getLabels()) {
sqlgBranchStepBarrier.addLabel(label);
}
try {
Field traversalOptionsField = chooseStep.getClass().getSuperclass().getDeclaredField("traversalOptions");
traversalOptionsField.setAccessible(true);
Map<M, List<Traversal.Admin<S, E>>> traversalOptions = (Map<M, List<Traversal.Admin<S, E>>>) traversalOptionsField.get(chooseStep);
for (Map.Entry<M, List<Traversal.Admin<S, E>>> entry : traversalOptions.entrySet()) {
for (Traversal.Admin<S, E> admin : entry.getValue()) {
sqlgBranchStepBarrier.addGlobalChildOption(entry.getKey(), admin);
}
}
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
TraversalHelper.replaceStep(chooseStep, sqlgBranchStepBarrier, chooseStep.getTraversal());
}
}
Aggregations