org.apache.tinkerpop.gremlin.process.traversal.step.map.GraphStep Java Examples

The following examples show how to use org.apache.tinkerpop.gremlin.process.traversal.step.map.GraphStep. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HugeGraphStep.java    From hugegraph with Apache License 2.0 6 votes vote down vote up
public HugeGraphStep(final GraphStep<S, E> originGraphStep) {
    super(originGraphStep.getTraversal(),
          originGraphStep.getReturnClass(),
          originGraphStep.isStartStep(),
          originGraphStep.getIds());

    originGraphStep.getLabels().forEach(this::addLabel);

    boolean queryVertex = this.returnsVertex();
    boolean queryEdge = this.returnsEdge();
    assert queryVertex || queryEdge;
    this.setIteratorSupplier(() -> {
        Iterator<E> results = queryVertex ? this.vertices() : this.edges();
        this.lastTimeResults = results;
        return results;
    });
}
 
Example #2
Source File: TopologyStrategy.java    From sqlg with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void apply(Traversal.Admin<?, ?> traversal) {
    Preconditions.checkState(!(this.sqlgSchema && this.globalUniqueIndex), "sqlgSchema and globalUnique are mutually exclusive. Both can not be true.");
    if (!TraversalHelper.getStepsOfAssignableClass(GraphStep.class, traversal).isEmpty() || !TraversalHelper.getStepsOfAssignableClass(VertexStep.class, traversal).isEmpty()) {
        if (this.sqlgSchema) {
            TraversalHelper.insertAfterStep(
                    new HasStep(traversal, new HasContainer(TOPOLOGY_SELECTION_SQLG_SCHEMA, P.eq(this.sqlgSchema))),
                    traversal.getStartStep(),
                    traversal
            );
        }
        if (this.globalUniqueIndex) {
            TraversalHelper.insertAfterStep(
                    new HasStep(traversal, new HasContainer(TOPOLOGY_SELECTION_GLOBAL_UNIQUE_INDEX, P.eq(this.globalUniqueIndex))),
                    traversal.getStartStep(),
                    traversal
            );
        }
    }

}
 
Example #3
Source File: ReplacedStepTree.java    From sqlg with MIT License 6 votes vote down vote up
void walkReplacedSteps(Set<SchemaTableTree> schemaTableTrees) {
    //The tree only has one linear path from root to the deepest leaf node.
    //This represents the regular path where each ReplacedStep goes one step deeper down the graph.
    //First build the SchemaTableTrees for this path.
    //The other nodes in this ReplacedStepTree are nodes that need to join onto the left join nodes coming from optional steps.
    List<ReplacedStep<?, ?>> replacedSteps = linearPathToLeafNode();

    for (ReplacedStep<?, ?> replacedStep : replacedSteps) {
        //skip the graph step
        if (replacedStep.getStep() instanceof GraphStep) {
            continue;
        }
        if (!replacedStep.isFake() && !(replacedStep.getStep() instanceof OrderGlobalStep) && !(replacedStep.getStep() instanceof RangeGlobalStep)) {
            //This schemaTableTree represents the tree nodes as build up to this depth. Each replacedStep goes a level further
            schemaTableTrees = replacedStep.calculatePathForStep(schemaTableTrees);
        }
    }
}
 
Example #4
Source File: Neo4jGraphStepStrategy.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    for (final GraphStep originalGraphStep : TraversalHelper.getStepsOfClass(GraphStep.class, traversal)) {
        final Neo4jGraphStep<?, ?> neo4jGraphStep = new Neo4jGraphStep<>(originalGraphStep);
        TraversalHelper.replaceStep(originalGraphStep, neo4jGraphStep, traversal);
        Step<?, ?> currentStep = neo4jGraphStep.getNextStep();
        while (currentStep instanceof HasStep || currentStep instanceof NoOpBarrierStep) {
            if (currentStep instanceof HasStep) {
                for (final HasContainer hasContainer : ((HasContainerHolder) currentStep).getHasContainers()) {
                    if (!GraphStep.processHasContainerIds(neo4jGraphStep, hasContainer))
                        neo4jGraphStep.addHasContainer(hasContainer);
                }
                TraversalHelper.copyLabels(currentStep, currentStep.getPreviousStep(), false);
                traversal.removeStep(currentStep);
            }
            currentStep = currentStep.getNextStep();
        }
    }
}
 
Example #5
Source File: SparkStarBarrierInterceptor.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
public static boolean isLegal(final Traversal.Admin<?, ?> traversal) {
    final Step<?, ?> startStep = traversal.getStartStep();
    final Step<?, ?> endStep = traversal.getEndStep();
    // right now this is not supported because of how the SparkStarBarrierInterceptor mutates the traversal prior to local evaluation
    if (traversal.getStrategies().getStrategy(SubgraphStrategy.class).isPresent())
        return false;
    if (!startStep.getClass().equals(GraphStep.class) || ((GraphStep) startStep).returnsEdge())
        return false;
    if (!endStep.getClass().equals(CountGlobalStep.class) &&
            !endStep.getClass().equals(SumGlobalStep.class) &&
            !endStep.getClass().equals(MeanGlobalStep.class) &&
            !endStep.getClass().equals(MaxGlobalStep.class) &&
            !endStep.getClass().equals(MinGlobalStep.class) &&
            !endStep.getClass().equals(FoldStep.class) &&
            !endStep.getClass().equals(GroupStep.class) &&
            !endStep.getClass().equals(GroupCountStep.class))
        // TODO: tree()
        return false;
    if (TraversalHelper.getStepsOfAssignableClassRecursively(Scope.global, Barrier.class, traversal).size() != 1)
        return false;
    if (traversal.getTraverserRequirements().contains(TraverserRequirement.SACK))
        return false;
    return TraversalHelper.isLocalStarGraph(traversal);
}
 
Example #6
Source File: TinkerGraphCountStrategy.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    if (!(traversal.isRoot()) || TraversalHelper.onGraphComputer(traversal))
        return;
    final List<Step> steps = traversal.getSteps();
    if (steps.size() < 2 ||
            !(steps.get(0) instanceof GraphStep) ||
            0 != ((GraphStep) steps.get(0)).getIds().length ||
            !(steps.get(steps.size() - 1) instanceof CountGlobalStep))
        return;
    for (int i = 1; i < steps.size() - 1; i++) {
        final Step current = steps.get(i);
        if (!(//current instanceof MapStep ||  // MapSteps will not necessarily emit an element as demonstrated in https://issues.apache.org/jira/browse/TINKERPOP-1958
                current instanceof IdentityStep ||
                current instanceof NoOpBarrierStep ||
                current instanceof CollectingBarrierStep) ||
                (current instanceof TraversalParent &&
                        TraversalHelper.anyStepRecursively(s -> (s instanceof SideEffectStep || s instanceof AggregateGlobalStep), (TraversalParent) current)))
            return;
    }
    final Class<? extends Element> elementClass = ((GraphStep<?, ?>) steps.get(0)).getReturnClass();
    TraversalHelper.removeAllSteps(traversal);
    traversal.addStep(new TinkerCountGlobalStep<>(traversal, elementClass));
}
 
Example #7
Source File: TinkerGraphStepStrategy.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    if (TraversalHelper.onGraphComputer(traversal))
        return;

    for (final GraphStep originalGraphStep : TraversalHelper.getStepsOfClass(GraphStep.class, traversal)) {
        final TinkerGraphStep<?, ?> tinkerGraphStep = new TinkerGraphStep<>(originalGraphStep);
        TraversalHelper.replaceStep(originalGraphStep, tinkerGraphStep, traversal);
        Step<?, ?> currentStep = tinkerGraphStep.getNextStep();
        while (currentStep instanceof HasStep || currentStep instanceof NoOpBarrierStep) {
            if (currentStep instanceof HasStep) {
                for (final HasContainer hasContainer : ((HasContainerHolder) currentStep).getHasContainers()) {
                    if (!GraphStep.processHasContainerIds(tinkerGraphStep, hasContainer))
                        tinkerGraphStep.addHasContainer(hasContainer);
                }
                TraversalHelper.copyLabels(currentStep, currentStep.getPreviousStep(), false);
                traversal.removeStep(currentStep);
            }
            currentStep = currentStep.getNextStep();
        }
    }
}
 
Example #8
Source File: SubgraphStrategy.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
private static final char processesPropertyType(Step step) {
    while (!(step instanceof EmptyStep)) {
        if (step instanceof FilterStep || step instanceof SideEffectStep)
            step = step.getPreviousStep();
        else if (step instanceof GraphStep && ((GraphStep) step).returnsVertex())
            return 'v';
        else if (step instanceof EdgeVertexStep)
            return 'v';
        else if (step instanceof VertexStep)
            return ((VertexStep) step).returnsVertex() ? 'v' : 'p';
        else if (step instanceof PropertyMapStep || step instanceof PropertiesStep)
            return 'p';
        else
            return 'x';
    }
    return 'x';
}
 
Example #9
Source File: HBaseGraphStepStrategy.java    From hgraphdb with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    for (final GraphStep originalGraphStep : TraversalHelper.getStepsOfClass(GraphStep.class, traversal)) {
        final HBaseGraphStep<?, ?> hbaseGraphStep = new HBaseGraphStep<>(originalGraphStep);
        TraversalHelper.replaceStep(originalGraphStep, hbaseGraphStep, traversal);
        Step<?, ?> currentStep = hbaseGraphStep.getNextStep();
        while (currentStep instanceof HasStep || currentStep instanceof NoOpBarrierStep) {
            if (currentStep instanceof HasStep) {
                for (final HasContainer hasContainer : ((HasContainerHolder) currentStep).getHasContainers()) {
                    if (!GraphStep.processHasContainerIds(hbaseGraphStep, hasContainer))
                        hbaseGraphStep.addHasContainer(hasContainer);
                }
                TraversalHelper.copyLabels(currentStep, currentStep.getPreviousStep(), false);
                traversal.removeStep(currentStep);
            }
            currentStep = currentStep.getNextStep();
        }
    }
}
 
Example #10
Source File: BitsyTraversalStrategy.java    From bitsy with Apache License 2.0 6 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
	for (final GraphStep originalGraphStep : TraversalHelper.getStepsOfClass(GraphStep.class, traversal)) {
		final BitsyGraphStep<?, ?> bitsyGraphStep = new BitsyGraphStep<>(originalGraphStep);
		TraversalHelper.replaceStep(originalGraphStep, bitsyGraphStep, traversal);
		Step<?, ?> currentStep = bitsyGraphStep.getNextStep();
		while (currentStep instanceof HasStep || currentStep instanceof NoOpBarrierStep) {
			if (currentStep instanceof HasStep) {
				for (final HasContainer hasContainer : ((HasContainerHolder) currentStep).getHasContainers()) {
					if (!GraphStep.processHasContainerIds(bitsyGraphStep, hasContainer))
						bitsyGraphStep.addHasContainer(hasContainer);
				}
				TraversalHelper.copyLabels(currentStep, currentStep.getPreviousStep(), false);
				traversal.removeStep(currentStep);
			}
			currentStep = currentStep.getNextStep();
		}
	}
}
 
Example #11
Source File: TinkerGraphStepStrategy.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    if (TraversalHelper.onGraphComputer(traversal))
        return;

    for (final GraphStep originalGraphStep : TraversalHelper.getStepsOfClass(GraphStep.class, traversal)) {
        final TinkerGraphStep<?, ?> tinkerGraphStep = new TinkerGraphStep<>(originalGraphStep);
        TraversalHelper.replaceStep(originalGraphStep, tinkerGraphStep, traversal);
        Step<?, ?> currentStep = tinkerGraphStep.getNextStep();
        while (currentStep instanceof HasStep || currentStep instanceof NoOpBarrierStep) {
            if (currentStep instanceof HasStep) {
                for (final HasContainer hasContainer : ((HasContainerHolder) currentStep).getHasContainers()) {
                    if (!GraphStep.processHasContainerIds(tinkerGraphStep, hasContainer))
                        tinkerGraphStep.addHasContainer(hasContainer);
                }
                TraversalHelper.copyLabels(currentStep, currentStep.getPreviousStep(), false);
                traversal.removeStep(currentStep);
            }
            currentStep = currentStep.getNextStep();
        }
    }
}
 
Example #12
Source File: HugeGraphStepStrategy.java    From hugegraph with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public void apply(Traversal.Admin<?, ?> traversal) {
    TraversalUtil.convAllHasSteps(traversal);

    // Extract conditions in GraphStep
    List<GraphStep> steps = TraversalHelper.getStepsOfClass(
                            GraphStep.class, traversal);
    for (GraphStep originStep : steps) {
        HugeGraphStep<?, ?> newStep = new HugeGraphStep<>(originStep);
        TraversalHelper.replaceStep(originStep, newStep, traversal);

        TraversalUtil.extractHasContainer(newStep, traversal);

        // TODO: support order-by optimize
        // TraversalUtil.extractOrder(newStep, traversal);

        TraversalUtil.extractRange(newStep, traversal, false);

        TraversalUtil.extractCount(newStep, traversal);
    }
}
 
Example #13
Source File: TraversalUtil.java    From hugegraph with Apache License 2.0 6 votes vote down vote up
public static void extractHasContainer(HugeGraphStep<?, ?> newStep,
                                       Traversal.Admin<?, ?> traversal) {
    Step<?, ?> step = newStep;
    do {
        step = step.getNextStep();
        if (step instanceof HasStep) {
            HasContainerHolder holder = (HasContainerHolder) step;
            for (HasContainer has : holder.getHasContainers()) {
                if (!GraphStep.processHasContainerIds(newStep, has)) {
                    newStep.addHasContainer(has);
                }
            }
            TraversalHelper.copyLabels(step, step.getPreviousStep(), false);
            traversal.removeStep(step);
        }
    } while (step instanceof HasStep || step instanceof NoOpBarrierStep);
}
 
Example #14
Source File: TinkerGraphCountStrategy.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    if (!(traversal.getParent() instanceof EmptyStep) || TraversalHelper.onGraphComputer(traversal))
        return;
    final List<Step> steps = traversal.getSteps();
    if (steps.size() < 2 ||
            !(steps.get(0) instanceof GraphStep) ||
            0 != ((GraphStep) steps.get(0)).getIds().length ||
            !(steps.get(steps.size() - 1) instanceof CountGlobalStep))
        return;
    for (int i = 1; i < steps.size() - 1; i++) {
        final Step current = steps.get(i);
        if (!(//current instanceof MapStep ||  // MapSteps will not necessarily emit an element as demonstrated in https://issues.apache.org/jira/browse/TINKERPOP-1958
                current instanceof IdentityStep ||
                current instanceof NoOpBarrierStep ||
                current instanceof CollectingBarrierStep) ||
                (current instanceof TraversalParent &&
                        TraversalHelper.anyStepRecursively(s -> (s instanceof SideEffectStep || s instanceof AggregateStep), (TraversalParent) current)))
            return;
    }
    final Class<? extends Element> elementClass = ((GraphStep<?, ?>) steps.get(0)).getReturnClass();
    TraversalHelper.removeAllSteps(traversal);
    traversal.addStep(new TinkerCountGlobalStep<>(traversal, elementClass));
}
 
Example #15
Source File: JanusGraphStepStrategy.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void apply(Traversal.Admin<?, ?> traversal) {
    if (TraversalHelper.onGraphComputer(traversal)) {
        return;
    }

    TraversalHelper.getStepsOfClass(GraphStep.class, traversal).forEach(originalGraphStep -> {
        if (originalGraphStep.getIds() == null || originalGraphStep.getIds().length == 0) {
            //Try to optimize for index calls
            JanusGraphStep<?, ?> janusGraphStep = new JanusGraphStep<>(originalGraphStep);
            TraversalHelper.replaceStep(originalGraphStep, janusGraphStep, traversal);
            HasStepFolder.foldInIds(janusGraphStep, traversal);
            HasStepFolder.foldInHasContainer(janusGraphStep, traversal, traversal);
            HasStepFolder.foldInOrder(janusGraphStep, janusGraphStep.getNextStep(), traversal, traversal, janusGraphStep.returnsVertex(), null);
            HasStepFolder.foldInRange(janusGraphStep, JanusGraphTraversalUtil.getNextNonIdentityStep(janusGraphStep), traversal, null);
        } else {
            //Make sure that any provided "start" elements are instantiated in the current transaction
            Object[] ids = originalGraphStep.getIds();
            ElementUtils.verifyArgsMustBeEitherIdOrElement(ids);
            if (ids[0] instanceof Element) {
                //GraphStep constructor ensures that the entire array is elements
                final Object[] elementIds = new Object[ids.length];
                for (int i = 0; i < ids.length; i++) {
                    elementIds[i] = ((Element) ids[i]).id();
                }
                originalGraphStep.setIteratorSupplier(() -> originalGraphStep.returnsVertex() ?
                        ((Graph) originalGraphStep.getTraversal().getGraph().get()).vertices(elementIds) :
                        ((Graph) originalGraphStep.getTraversal().getGraph().get()).edges(elementIds));
            }
        }

    });
}
 
Example #16
Source File: TinkerGraphStepStrategyTest.java    From tinkergraph-gremlin with Apache License 2.0 5 votes vote down vote up
private static GraphTraversal.Admin<?, ?> g_V(final Object... hasKeyValues) {
    final GraphTraversal.Admin<?, ?> traversal = new DefaultGraphTraversal<>();
    final TinkerGraphStep<Vertex, Vertex> graphStep = new TinkerGraphStep<>(new GraphStep<>(traversal, Vertex.class, true));
    for (int i = 0; i < hasKeyValues.length; i = i + 2) {
        graphStep.addHasContainer(new HasContainer((String) hasKeyValues[i], (P) hasKeyValues[i + 1]));
    }
    return traversal.addStep(graphStep);
}
 
Example #17
Source File: GraphStrategy.java    From sqlg with MIT License 5 votes vote down vote up
@Override
protected SqlgStep constructSqlgStep(Step startStep) {
    Preconditions.checkArgument(startStep instanceof GraphStep, "Expected a GraphStep, found instead a " + startStep.getClass().getName());
    GraphStep<?, ?> graphStep = (GraphStep) startStep;
    //noinspection unchecked
    return new SqlgGraphStep(this.sqlgGraph, this.traversal, graphStep.getReturnClass(), graphStep.isStartStep(), graphStep.getIds());
}
 
Example #18
Source File: GraphStrategy.java    From sqlg with MIT License 5 votes vote down vote up
void apply() {
    final Step<?, ?> startStep = traversal.getStartStep();

    if (!(startStep instanceof GraphStep)) {
        return;
    }
    final GraphStep originalGraphStep = (GraphStep) startStep;

    if (this.sqlgGraph.features().supportsBatchMode() && this.sqlgGraph.tx().isInNormalBatchMode()) {
        this.sqlgGraph.tx().flush();
    }

    if (originalGraphStep.getIds().length > 0) {
        Object id = originalGraphStep.getIds()[0];
        if (id != null) {
            Class clazz = id.getClass();
            //noinspection unchecked
            if (!Stream.of(originalGraphStep.getIds()).allMatch(i -> clazz.isAssignableFrom(i.getClass())))
                throw Graph.Exceptions.idArgsMustBeEitherIdOrElement();
        }
    }
    if (this.canNotBeOptimized()) {
        logger.debug("gremlin not optimized due to path or tree step. " + this.traversal.toString() + "\nPath to gremlin:\n" + ExceptionUtils.getStackTrace(new Throwable()));
        return;
    }
    combineSteps();
}
 
Example #19
Source File: Neo4jGraphStepStrategyTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private static GraphStep<?, ?> V(final Object... hasKeyValues) {
    final Neo4jGraphStep<Vertex, Vertex> graphStep = new Neo4jGraphStep<>(new GraphStep<>(EmptyTraversal.instance(), Vertex.class, true));
    for (int i = 0; i < hasKeyValues.length; i = i + 2) {
        graphStep.addHasContainer(new HasContainer((String) hasKeyValues[i], (P) hasKeyValues[i + 1]));
    }
    return graphStep;
}
 
Example #20
Source File: Neo4jGraphStepStrategyTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private static GraphTraversal.Admin<?, ?> g_V(final Object... hasKeyValues) {
    final GraphTraversal.Admin<?, ?> traversal = new DefaultGraphTraversal<>();
    final Neo4jGraphStep<Vertex, Vertex> graphStep = new Neo4jGraphStep<>(new GraphStep<>(traversal, Vertex.class, true));
    for (int i = 0; i < hasKeyValues.length; i = i + 2) {
        graphStep.addHasContainer(new HasContainer((String) hasKeyValues[i], (P) hasKeyValues[i + 1]));
    }
    return traversal.addStep(graphStep);
}
 
Example #21
Source File: AtlasJanusGraph.java    From atlas with Apache License 2.0 5 votes vote down vote up
@Override
public AtlasGraphTraversal<AtlasVertex, AtlasEdge> V(final Object... vertexIds) {
    AtlasGraphTraversal traversal = new AtlasJanusGraphTraversal(this, getGraph().traversal());
    traversal.getBytecode().addStep(GraphTraversal.Symbols.V, vertexIds);
    traversal.addStep(new GraphStep<>(traversal, Vertex.class, true, vertexIds));
    return traversal;
}
 
Example #22
Source File: TinkerGraphStepStrategyTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private static GraphStep<?, ?> V(final Object... hasKeyValues) {
    final TinkerGraphStep<Vertex, Vertex> graphStep = new TinkerGraphStep<>(new GraphStep<>(EmptyTraversal.instance(), Vertex.class, true));
    for (int i = 0; i < hasKeyValues.length; i = i + 2) {
        graphStep.addHasContainer(new HasContainer((String) hasKeyValues[i], (P) hasKeyValues[i + 1]));
    }
    return graphStep;
}
 
Example #23
Source File: TinkerGraphStepStrategyTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private static GraphTraversal.Admin<?, ?> g_V(final Object... hasKeyValues) {
    final GraphTraversal.Admin<?, ?> traversal = new DefaultGraphTraversal<>();
    final TinkerGraphStep<Vertex, Vertex> graphStep = new TinkerGraphStep<>(new GraphStep<>(traversal, Vertex.class, true));
    for (int i = 0; i < hasKeyValues.length; i = i + 2) {
        graphStep.addHasContainer(new HasContainer((String) hasKeyValues[i], (P) hasKeyValues[i + 1]));
    }
    return traversal.addStep(graphStep);
}
 
Example #24
Source File: AtlasJanusGraph.java    From atlas with Apache License 2.0 5 votes vote down vote up
@Override
public AtlasGraphTraversal<AtlasVertex, AtlasEdge> E(final Object... edgeIds) {
    AtlasGraphTraversal traversal = new AtlasJanusGraphTraversal(this, getGraph().traversal());
    traversal.getBytecode().addStep(GraphTraversal.Symbols.E, edgeIds);
    traversal.addStep(new GraphStep<>(traversal, Vertex.class, true, edgeIds));
    return traversal;
}
 
Example #25
Source File: TinkerGraphStep.java    From tinkergraph-gremlin with Apache License 2.0 5 votes vote down vote up
public TinkerGraphStep(final GraphStep<S, E> originalGraphStep) {
    super(originalGraphStep.getTraversal(), originalGraphStep.getReturnClass(), originalGraphStep.isStartStep(), originalGraphStep.getIds());
    originalGraphStep.getLabels().forEach(this::addLabel);

    // we used to only setIteratorSupplier() if there were no ids OR the first id was instanceof Element,
    // but that allowed the filter in g.V(v).has('k','v') to be ignored.  this created problems for
    // PartitionStrategy which wants to prevent someone from passing "v" from one TraversalSource to
    // another TraversalSource using a different partition
    this.setIteratorSupplier(() -> (Iterator<E>) (Vertex.class.isAssignableFrom(this.returnClass) ? this.vertices() : this.edges()));
}
 
Example #26
Source File: TinkerGraphStep.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
public TinkerGraphStep(final GraphStep<S, E> originalGraphStep) {
    super(originalGraphStep.getTraversal(), originalGraphStep.getReturnClass(), originalGraphStep.isStartStep(), originalGraphStep.getIds());
    originalGraphStep.getLabels().forEach(this::addLabel);

    // we used to only setIteratorSupplier() if there were no ids OR the first id was instanceof Element,
    // but that allowed the filter in g.V(v).has('k','v') to be ignored.  this created problems for
    // PartitionStrategy which wants to prevent someone from passing "v" from one TraversalSource to
    // another TraversalSource using a different partition
    this.setIteratorSupplier(() -> (Iterator<E>) (Vertex.class.isAssignableFrom(this.returnClass) ? this.vertices() : this.edges()));
}
 
Example #27
Source File: PartitionStrategyTraverseTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
public static GraphTraversal create(final Class<? extends Element> clazz) {
    final Graph mockedGraph = mock(Graph.class);
    final Graph.Features features = mock(Graph.Features.class);
    final Graph.Features.VertexFeatures vertexFeatures = mock(Graph.Features.VertexFeatures.class);
    when(mockedGraph.features()).thenReturn(features);
    when(features.vertex()).thenReturn(vertexFeatures);
    when(vertexFeatures.getCardinality(any())).thenReturn(VertexProperty.Cardinality.single);
    final DefaultGraphTraversal t = new DefaultGraphTraversal<>(mockedGraph);
    if (clazz != null) t.asAdmin().addStep(new GraphStep<>(t.asAdmin(), clazz, true));
    return t;
}
 
Example #28
Source File: SocialPackageTraversalSourceDsl.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
public GraphTraversal<Vertex, Vertex> persons(String... names) {
    GraphTraversalSource clone = this.clone();

    clone.getBytecode().addStep(GraphTraversal.Symbols.V);
    GraphTraversal<Vertex, Vertex> traversal = new DefaultGraphTraversal<>(clone);
    traversal.asAdmin().addStep(new GraphStep<>(traversal.asAdmin(), Vertex.class, true));

    traversal = traversal.hasLabel("person");
    if (names.length > 0) traversal = traversal.has("name", P.within(names));

    return traversal;
}
 
Example #29
Source File: ComputerVerificationStrategy.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    if (!TraversalHelper.onGraphComputer(traversal))
        return;

    if (traversal.getParent() instanceof TraversalVertexProgramStep) {
        if (TraversalHelper.getStepsOfAssignableClassRecursively(GraphStep.class, traversal).size() > 1)
            throw new VerificationException("Mid-traversal V()/E() is currently not supported on GraphComputer", traversal);
        if (TraversalHelper.hasStepOfAssignableClassRecursively(ProfileStep.class, traversal) && TraversalHelper.getStepsOfAssignableClass(VertexProgramStep.class, TraversalHelper.getRootTraversal(traversal)).size() > 1)
            throw new VerificationException("Profiling a multi-VertexProgramStep traversal is currently not supported on GraphComputer", traversal);
    }

    // this is a problem because sideEffect.merge() is transient on the OLAP reduction
    if (TraversalHelper.getRootTraversal(traversal).getTraverserRequirements().contains(TraverserRequirement.ONE_BULK))
        throw new VerificationException("One bulk is currently not supported on GraphComputer: " + traversal, traversal);

    // you can not traverse past the local star graph with localChildren (e.g. by()-modulators).
    if (!TraversalHelper.isGlobalChild(traversal) && !TraversalHelper.isLocalStarGraph(traversal))
        throw new VerificationException("Local traversals may not traverse past the local star-graph on GraphComputer: " + traversal, traversal);

    for (final Step<?, ?> step : traversal.getSteps()) {
        if (step instanceof PathProcessor && ((PathProcessor) step).getMaxRequirement() != PathProcessor.ElementRequirement.ID)
            throw new VerificationException("It is not possible to access more than a path element's id on GraphComputer: " + step + " requires " + ((PathProcessor) step).getMaxRequirement(), traversal);

        if (UNSUPPORTED_STEPS.stream().filter(c -> c.isAssignableFrom(step.getClass())).findFirst().isPresent())
            throw new VerificationException("The following step is currently not supported on GraphComputer: " + step, traversal);
    }

    Step<?, ?> nextParentStep = traversal.getParent().asStep();
    while (!(nextParentStep instanceof EmptyStep)) {
        if (nextParentStep instanceof PathProcessor && ((PathProcessor) nextParentStep).getMaxRequirement() != PathProcessor.ElementRequirement.ID)
            throw new VerificationException("The following path processor step requires more than the element id on GraphComputer: " + nextParentStep + " requires " + ((PathProcessor) nextParentStep).getMaxRequirement(), traversal);
        nextParentStep = nextParentStep.getNextStep();
    }
}
 
Example #30
Source File: TinkerGraphStepStrategyTest.java    From tinkergraph-gremlin with Apache License 2.0 5 votes vote down vote up
private static GraphStep<?, ?> V(final Object... hasKeyValues) {
    final TinkerGraphStep<Vertex, Vertex> graphStep = new TinkerGraphStep<>(new GraphStep<>(EmptyTraversal.instance(), Vertex.class, true));
    for (int i = 0; i < hasKeyValues.length; i = i + 2) {
        graphStep.addHasContainer(new HasContainer((String) hasKeyValues[i], (P) hasKeyValues[i + 1]));
    }
    return graphStep;
}