use of org.apache.tinkerpop.gremlin.process.traversal.step.map.GraphStep in project grakn by graknlabs.
the class JanusPreviousPropertyStepStrategy method apply.
@Override
public void apply(Traversal.Admin<?, ?> traversal) {
// Retrieve all graph (`V()`) steps - this is the step the strategy should replace
List<GraphStep> graphSteps = TraversalHelper.getStepsOfAssignableClass(GraphStep.class, traversal);
for (GraphStep graphStep : graphSteps) {
if (!(graphStep.getNextStep() instanceof TraversalFilterStep))
continue;
TraversalFilterStep<Vertex> filterStep = (TraversalFilterStep<Vertex>) graphStep.getNextStep();
// Retrieve the filter steps e.g. `__.properties(a).where(P.eq(b))`
List<Step> steps = stepsFromFilterStep(filterStep);
if (steps.size() < 2)
continue;
// This is `properties(a)`
Step propertiesStep = steps.get(0);
// This is `filter(__.where(P.eq(b)))`
Step whereStep = steps.get(1);
// Get the property key `a`
if (!(propertiesStep instanceof PropertiesStep))
continue;
Optional<String> propertyKey = propertyFromPropertiesStep((PropertiesStep<Vertex>) propertiesStep);
if (!propertyKey.isPresent())
continue;
// Get the step label `b`
if (!(whereStep instanceof WherePredicateStep))
continue;
Optional<String> label = labelFromWhereEqPredicate((WherePredicateStep<Vertex>) whereStep);
if (!label.isPresent())
continue;
executeStrategy(traversal, graphStep, filterStep, propertyKey.get(), label.get());
}
}
use of org.apache.tinkerpop.gremlin.process.traversal.step.map.GraphStep in project janusgraph by JanusGraph.
the class HasStepFolder method foldInIds.
static void foldInIds(final HasStepFolder janusgraphStep, final Traversal.Admin<?, ?> traversal) {
Step<?, ?> currentStep = janusgraphStep.getNextStep();
while (true) {
if (currentStep instanceof HasContainerHolder) {
final HasContainerHolder hasContainerHolder = (HasContainerHolder) currentStep;
final GraphStep graphStep = (GraphStep) janusgraphStep;
// HasContainer collection that we get back is immutable so we keep track of which containers
// need to be deleted after they've been folded into the JanusGraphStep and then remove them from their
// step using HasContainer.removeHasContainer
final List<HasContainer> removableHasContainers = new ArrayList<>();
final Set<String> stepLabels = currentStep.getLabels();
hasContainerHolder.getHasContainers().forEach(hasContainer -> {
if (GraphStep.processHasContainerIds(graphStep, hasContainer)) {
stepLabels.forEach(janusgraphStep::addLabel);
// this has container is no longer needed because its ids will be folded into the JanusGraphStep
removableHasContainers.add(hasContainer);
}
});
if (!removableHasContainers.isEmpty()) {
removableHasContainers.forEach(hasContainerHolder::removeHasContainer);
}
// if all has containers have been removed, the current step can be removed
if (hasContainerHolder.getHasContainers().isEmpty()) {
traversal.removeStep(currentStep);
}
} else if (currentStep instanceof IdentityStep) {
// do nothing, has no impact
} else if (currentStep instanceof NoOpBarrierStep) {
// do nothing, has no impact
} else {
break;
}
currentStep = currentStep.getNextStep();
}
}
Aggregations