Search in sources :

Example 1 with HasStep

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);
                    }
                }
            }
        }
    });
}
Also used : TitanVertex(com.thinkaurelius.titan.core.TitanVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) TitanVertex(com.thinkaurelius.titan.core.TitanVertex) HasStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.HasStep) EdgeVertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.EdgeVertexStep) EdgeOtherVertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.EdgeOtherVertexStep) HasStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.HasStep) EdgeOtherVertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.EdgeOtherVertexStep) EdgeVertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.EdgeVertexStep) IsStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.IsStep) IdentityStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep) GraphStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.GraphStep) TraversalFilterStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.TraversalFilterStep) VertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexStep) Direction(org.apache.tinkerpop.gremlin.structure.Direction) EdgeOtherVertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.EdgeOtherVertexStep) EdgeVertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.EdgeVertexStep) VertexStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexStep) TraversalFilterStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.TraversalFilterStep) IdentityStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep) IsStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.IsStep)

Example 2 with HasStep

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

Aggregations

HasStep (org.apache.tinkerpop.gremlin.process.traversal.step.filter.HasStep)2 IdentityStep (org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep)2 TitanVertex (com.thinkaurelius.titan.core.TitanVertex)1 ArrayList (java.util.ArrayList)1 Comparator (java.util.Comparator)1 List (java.util.List)1 IsStep (org.apache.tinkerpop.gremlin.process.traversal.step.filter.IsStep)1 TraversalFilterStep (org.apache.tinkerpop.gremlin.process.traversal.step.filter.TraversalFilterStep)1 EdgeOtherVertexStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.EdgeOtherVertexStep)1 EdgeVertexStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.EdgeVertexStep)1 OrderGlobalStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.OrderGlobalStep)1 VertexStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexStep)1 GraphStep (org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.GraphStep)1 ElementValueComparator (org.apache.tinkerpop.gremlin.process.traversal.step.util.ElementValueComparator)1 Direction (org.apache.tinkerpop.gremlin.structure.Direction)1 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)1