use of org.apache.tinkerpop.gremlin.process.traversal.step.filter.HasStep in project titan by thinkaurelius.
the class AdjacentVertexFilterOptimizerStrategy method apply.
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
TraversalHelper.getStepsOfClass(TraversalFilterStep.class, traversal).forEach(originalStep -> {
Traversal.Admin<?, ?> filterTraversal = (Traversal.Admin<?, ?>) originalStep.getLocalChildren().get(0);
List<Step> steps = filterTraversal.getSteps();
if (steps.size() == 2 && (steps.get(0) instanceof EdgeVertexStep || steps.get(0) instanceof EdgeOtherVertexStep) && (steps.get(1) instanceof IsStep)) {
Direction direction = null;
if (steps.get(0) instanceof EdgeVertexStep) {
EdgeVertexStep evs = (EdgeVertexStep) steps.get(0);
if (evs.getDirection() != Direction.BOTH)
direction = evs.getDirection();
} else {
assert steps.get(0) instanceof EdgeOtherVertexStep;
direction = Direction.BOTH;
}
P predicate = ((IsStep) steps.get(1)).getPredicate();
if (direction != null && predicate.getBiPredicate() == Compare.eq && predicate.getValue() instanceof Vertex) {
TitanVertex vertex = TitanTraversalUtil.getTitanVertex((Vertex) predicate.getValue());
Step<?, ?> currentStep = originalStep.getPreviousStep();
while (true) {
if (currentStep instanceof HasStep || currentStep instanceof IdentityStep) {
} else
break;
}
if (currentStep instanceof VertexStep) {
VertexStep vstep = (VertexStep) currentStep;
if (vstep.returnsEdge() && (direction == Direction.BOTH || direction.equals(vstep.getDirection().opposite()))) {
TraversalHelper.replaceStep(originalStep, new HasStep(traversal, HasContainer.makeHasContainers(ImplicitKey.ADJACENT_ID.name(), P.eq(vertex))), traversal);
}
}
}
}
});
}
use of org.apache.tinkerpop.gremlin.process.traversal.step.filter.HasStep 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);
}
}
}
Aggregations