org.apache.tinkerpop.gremlin.process.traversal.step.util.EmptyStep Java Examples
The following examples show how to use
org.apache.tinkerpop.gremlin.process.traversal.step.util.EmptyStep.
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: TinkerGraphCountStrategy.java From tinkergraph-gremlin with Apache License 2.0 | 6 votes |
@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 #2
Source File: SubgraphStrategy.java From tinkerpop with Apache License 2.0 | 6 votes |
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 #3
Source File: PathUtil.java From tinkerpop with Apache License 2.0 | 6 votes |
public static Set<String> getReferencedLabels(final Step step) { final Set<String> referencedLabels = new HashSet<>(); if (step instanceof Scoping) { final Set<String> labels = new HashSet<>(((Scoping) step).getScopeKeys()); if (step instanceof MatchStep) { // if this is the last step, keep everything, else just add founds if (step.getNextStep() instanceof EmptyStep) { labels.addAll(((MatchStep) step).getMatchEndLabels()); labels.addAll(((MatchStep) step).getMatchStartLabels()); } } referencedLabels.addAll(labels); } return referencedLabels; }
Example #4
Source File: ProgramTest.java From tinkerpop with Apache License 2.0 | 5 votes |
@Override public void setup(final Memory memory) { MemoryTraversalSideEffects.setMemorySideEffects(this.traversal.get(), memory, ProgramPhase.SETUP); final Map<Vertex, Long> map = (Map<Vertex, Long>) this.haltedTraversers.iterator().next().get(); assertEquals(2, map.size()); assertTrue(map.values().contains(3l)); assertTrue(map.values().contains(1l)); final IndexedTraverserSet<Object,Vertex> activeTraversers = new IndexedTraverserSet.VertexIndexedTraverserSet(); map.keySet().forEach(vertex -> activeTraversers.add(this.haltedTraversers.peek().split(vertex, EmptyStep.instance()))); this.haltedTraversers.clear(); this.checkSideEffects(); memory.set(TraversalVertexProgram.ACTIVE_TRAVERSERS, activeTraversers); }
Example #5
Source File: TraversalHelperTest.java From tinkerpop with Apache License 2.0 | 5 votes |
private static void validateToyTraversal(final Traversal traversal) { assertEquals(traversal.asAdmin().getSteps().size(), 3); assertEquals(IdentityStep.class, traversal.asAdmin().getSteps().get(0).getClass()); assertEquals(HasStep.class, traversal.asAdmin().getSteps().get(1).getClass()); assertEquals(LambdaFilterStep.class, traversal.asAdmin().getSteps().get(2).getClass()); // IDENTITY STEP assertEquals(EmptyStep.class, ((Step) traversal.asAdmin().getSteps().get(0)).getPreviousStep().getClass()); assertEquals(HasStep.class, ((Step) traversal.asAdmin().getSteps().get(0)).getNextStep().getClass()); assertEquals(LambdaFilterStep.class, ((Step) traversal.asAdmin().getSteps().get(0)).getNextStep().getNextStep().getClass()); assertEquals(EmptyStep.class, ((Step) traversal.asAdmin().getSteps().get(0)).getNextStep().getNextStep().getNextStep().getClass()); // HAS STEP assertEquals(IdentityStep.class, ((Step) traversal.asAdmin().getSteps().get(1)).getPreviousStep().getClass()); assertEquals(EmptyStep.class, ((Step) traversal.asAdmin().getSteps().get(1)).getPreviousStep().getPreviousStep().getClass()); assertEquals(LambdaFilterStep.class, ((Step) traversal.asAdmin().getSteps().get(1)).getNextStep().getClass()); assertEquals(EmptyStep.class, ((Step) traversal.asAdmin().getSteps().get(1)).getNextStep().getNextStep().getClass()); // FILTER STEP assertEquals(HasStep.class, ((Step) traversal.asAdmin().getSteps().get(2)).getPreviousStep().getClass()); assertEquals(IdentityStep.class, ((Step) traversal.asAdmin().getSteps().get(2)).getPreviousStep().getPreviousStep().getClass()); assertEquals(EmptyStep.class, ((Step) traversal.asAdmin().getSteps().get(2)).getPreviousStep().getPreviousStep().getPreviousStep().getClass()); assertEquals(EmptyStep.class, ((Step) traversal.asAdmin().getSteps().get(2)).getNextStep().getClass()); assertEquals(3, traversal.asAdmin().getSteps().size()); }
Example #6
Source File: TraversalHelperTest.java From tinkerpop with Apache License 2.0 | 5 votes |
@Test public void shouldSetPreviousStepToEmptyStep() { final Traversal.Admin<?, ?> traversal = __.V().out().asAdmin(); //transform the traversal to __.V().not(out()) //the VertexStep's previousStep should be the EmptyStep Optional<VertexStep> vertexStepOpt = TraversalHelper.getFirstStepOfAssignableClass(VertexStep.class, traversal); assertTrue(vertexStepOpt.isPresent()); Traversal.Admin<?,?> inner = __.start().asAdmin(); inner.addStep(0, vertexStepOpt.get()); TraversalHelper.replaceStep(vertexStepOpt.get(), new NotStep<>(__.identity().asAdmin(), inner), traversal); List<VertexStep> vertexSteps = TraversalHelper.getStepsOfAssignableClassRecursively(VertexStep.class, traversal); assertEquals(1, vertexSteps.size()); VertexStep vertexStep = vertexSteps.get(0); assertTrue("Expected the previousStep to be an EmptyStep, found instead " + vertexStep.getPreviousStep().toString(),vertexStep.getPreviousStep() == EmptyStep.instance()); }
Example #7
Source File: VertexProgramStrategy.java From tinkerpop with Apache License 2.0 | 5 votes |
private static Step<?, ?> getLastLegalOLAPStep(Step<?, ?> currentStep) { while (currentStep instanceof VertexComputing) currentStep = currentStep.getNextStep(); while (!(currentStep instanceof EmptyStep)) { if (currentStep instanceof VertexComputing) return currentStep.getPreviousStep(); currentStep = currentStep.getNextStep(); } return EmptyStep.instance(); }
Example #8
Source File: VertexProgramStrategy.java From tinkerpop with Apache License 2.0 | 5 votes |
private static Step<?, ?> getFirstLegalOLAPStep(Step<?, ?> currentStep) { while (!(currentStep instanceof EmptyStep)) { if (!(currentStep instanceof VertexComputing)) return currentStep; currentStep = currentStep.getNextStep(); } return EmptyStep.instance(); }
Example #9
Source File: MessagePassingReductionStrategy.java From tinkerpop with Apache License 2.0 | 5 votes |
public static final boolean endsWithElement(Step<?, ?> currentStep) { while (!(currentStep instanceof EmptyStep)) { if (currentStep instanceof VertexStep) // only inE, in, and out send messages return (((VertexStep) currentStep).returnsVertex() || !((VertexStep) currentStep).getDirection().equals(Direction.OUT)); else if (currentStep instanceof EdgeVertexStep) // TODO: add GraphStep but only if its mid-traversal V()/E() return true; else if (currentStep instanceof TraversalFlatMapStep || currentStep instanceof TraversalMapStep || currentStep instanceof LocalStep) return endsWithElement(((TraversalParent) currentStep).getLocalChildren().get(0).getEndStep()); else if (!(currentStep instanceof FilterStep || currentStep instanceof SideEffectStep || currentStep instanceof IdentityStep || currentStep instanceof Barrier)) return false; currentStep = currentStep.getPreviousStep(); } return false; }
Example #10
Source File: VertexProgramStep.java From tinkerpop with Apache License 2.0 | 5 votes |
protected boolean previousTraversalVertexProgram() { Step<?, ?> currentStep = this; while (!(currentStep instanceof EmptyStep)) { if (currentStep instanceof TraversalVertexProgramStep) return true; currentStep = currentStep.getPreviousStep(); } return false; }
Example #11
Source File: ComputerVerificationStrategy.java From tinkerpop with Apache License 2.0 | 5 votes |
@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 #12
Source File: InlineFilterStrategy.java From tinkerpop with Apache License 2.0 | 5 votes |
private static final boolean processMatchStep(final MatchStep<?, ?> step, final Traversal.Admin<?, ?> traversal) { if (step.getPreviousStep() instanceof EmptyStep) return false; boolean changed = false; final String startLabel = MatchStep.Helper.computeStartLabel(step.getGlobalChildren()); for (final Traversal.Admin<?, ?> matchTraversal : new ArrayList<>(step.getGlobalChildren())) { if (TraversalHelper.hasAllStepsOfClass(matchTraversal, HasStep.class, MatchStep.MatchStartStep.class, MatchStep.MatchEndStep.class) && matchTraversal.getStartStep() instanceof MatchStep.MatchStartStep && startLabel.equals(((MatchStep.MatchStartStep) matchTraversal.getStartStep()).getSelectKey().orElse(null))) { changed = true; step.removeGlobalChild(matchTraversal); final String endLabel = ((MatchStep.MatchEndStep) matchTraversal.getEndStep()).getMatchKey().orElse(null); // why would this exist? but just in case matchTraversal.removeStep(0); // remove MatchStartStep matchTraversal.removeStep(matchTraversal.getSteps().size() - 1); // remove MatchEndStep TraversalHelper.applySingleLevelStrategies(traversal, matchTraversal, InlineFilterStrategy.class); matchTraversal.getEndStep().addLabel(startLabel); if (null != endLabel) matchTraversal.getEndStep().addLabel(endLabel); TraversalHelper.insertTraversal((Step) step.getPreviousStep(), matchTraversal, traversal); } } if (step.getGlobalChildren().isEmpty()) traversal.removeStep(step); return changed; }
Example #13
Source File: IdentityRemovalStrategy.java From tinkerpop with Apache License 2.0 | 5 votes |
@Override public void apply(final Traversal.Admin<?, ?> traversal) { if (traversal.getSteps().size() <= 1) return; for (final IdentityStep<?> identityStep : TraversalHelper.getStepsOfClass(IdentityStep.class, traversal)) { if (identityStep.getLabels().isEmpty() || !(identityStep.getPreviousStep() instanceof EmptyStep)) { TraversalHelper.copyLabels(identityStep, identityStep.getPreviousStep(), false); traversal.removeStep(identityStep); } } }
Example #14
Source File: ReferenceElementStrategy.java From tinkerpop with Apache License 2.0 | 5 votes |
@Override public void apply(final Traversal.Admin<?, ?> traversal) { if (traversal.getParent() == EmptyStep.instance()) { final Optional<ProfileSideEffectStep> profileStep = TraversalHelper.getFirstStepOfAssignableClass(ProfileSideEffectStep.class, traversal); final int index = profileStep.map(step -> traversal.getSteps().indexOf(step)) .orElseGet(() -> traversal.getSteps().size()); traversal.addStep(index, new ReferenceElementStep<>(traversal)); } }
Example #15
Source File: PathUtil.java From tinkerpop with Apache License 2.0 | 5 votes |
public static Set<String> getReferencedLabelsAfterStep(Step<?, ?> step) { final Set<String> labels = new HashSet<>(); while (!(step instanceof EmptyStep)) { labels.addAll(PathUtil.getReferencedLabels(step)); step = step.getNextStep(); } return labels; }
Example #16
Source File: DefaultTraversal.java From tinkerpop with Apache License 2.0 | 5 votes |
@Override public <S2, E2> Traversal.Admin<S2, E2> removeStep(final int index) throws IllegalStateException { if (this.locked) throw Exceptions.traversalIsLocked(); final Step previousStep = this.steps.size() > 0 && index != 0 ? steps.get(index - 1) : null; final Step nextStep = this.steps.size() > index + 1 ? steps.get(index + 1) : null; //this.steps.get(index).setTraversal(EmptyTraversal.instance()); this.steps.remove(index); if (null != previousStep) previousStep.setNextStep(null == nextStep ? EmptyStep.instance() : nextStep); if (null != nextStep) nextStep.setPreviousStep(null == previousStep ? EmptyStep.instance() : previousStep); return (Traversal.Admin<S2, E2>) this; }
Example #17
Source File: DefaultTraversal.java From tinkerpop with Apache License 2.0 | 5 votes |
@Override public <S2, E2> Traversal.Admin<S2, E2> addStep(final int index, final Step<?, ?> step) throws IllegalStateException { if (this.locked) throw Exceptions.traversalIsLocked(); step.setId(this.stepPosition.nextXId()); this.steps.add(index, step); final Step previousStep = this.steps.size() > 0 && index != 0 ? steps.get(index - 1) : null; final Step nextStep = this.steps.size() > index + 1 ? steps.get(index + 1) : null; step.setPreviousStep(null != previousStep ? previousStep : EmptyStep.instance()); step.setNextStep(null != nextStep ? nextStep : EmptyStep.instance()); if (null != previousStep) previousStep.setNextStep(step); if (null != nextStep) nextStep.setPreviousStep(step); step.setTraversal(this); return (Traversal.Admin<S2, E2>) this; }
Example #18
Source File: HasStepFolder.java From grakn with GNU Affero General Public License v3.0 | 5 votes |
static boolean validFoldInHasContainer(Step<?, ?> tinkerpopStep, boolean defaultValue) { Step<?, ?> currentStep = tinkerpopStep; Boolean toReturn = null; while (!(currentStep instanceof EmptyStep)) { if (currentStep instanceof HasContainerHolder) { Iterable<HasContainer> containers = ((HasContainerHolder) currentStep).getHasContainers(); toReturn = toReturn == null ? validJanusGraphHas(containers) : toReturn && validJanusGraphHas(containers); } else if (!(currentStep instanceof IdentityStep) && !(currentStep instanceof NoOpBarrierStep) && !(currentStep instanceof RangeGlobalStep) && !(currentStep instanceof OrderGlobalStep)) { toReturn = toReturn != null && (toReturn && defaultValue); break; } currentStep = currentStep.getNextStep(); } return Boolean.TRUE.equals(toReturn); }
Example #19
Source File: TraversalHelper.java From tinkerpop with Apache License 2.0 | 5 votes |
public static void reIdSteps(final StepPosition stepPosition, final Traversal.Admin<?, ?> traversal) { stepPosition.x = 0; stepPosition.y = -1; stepPosition.z = -1; stepPosition.parentId = null; Traversal.Admin<?, ?> current = traversal; while (!(current instanceof EmptyTraversal)) { stepPosition.y++; final TraversalParent parent = current.getParent(); if (null == stepPosition.parentId && !(parent instanceof EmptyStep)) stepPosition.parentId = parent.asStep().getId(); if (-1 == stepPosition.z) { final int globalChildrenSize = parent.getGlobalChildren().size(); for (int i = 0; i < globalChildrenSize; i++) { if (parent.getGlobalChildren().get(i) == current) { stepPosition.z = i; } } for (int i = 0; i < parent.getLocalChildren().size(); i++) { if (parent.getLocalChildren().get(i) == current) { stepPosition.z = i + globalChildrenSize; } } } current = parent.asStep().getTraversal(); } if (-1 == stepPosition.z) stepPosition.z = 0; if (null == stepPosition.parentId) stepPosition.parentId = ""; for (final Step<?, ?> step : traversal.getSteps()) { step.setId(stepPosition.nextXId()); } }
Example #20
Source File: TraversalHelper.java From tinkerpop with Apache License 2.0 | 5 votes |
public static <S, E> void removeToTraversal(final Step<S, ?> startStep, final Step<?, E> endStep, final Traversal.Admin<S, E> newTraversal) { final Traversal.Admin<?, ?> originalTraversal = startStep.getTraversal(); Step<?, ?> currentStep = startStep; while (currentStep != endStep && !(currentStep instanceof EmptyStep)) { final Step<?, ?> temp = currentStep.getNextStep(); originalTraversal.removeStep(currentStep); newTraversal.addStep(currentStep); currentStep = temp; } }
Example #21
Source File: AdjacentVertexFilterOptimizerStrategy.java From grakn with GNU Affero General Public License v3.0 | 4 votes |
@Override public void apply(Traversal.Admin<?, ?> traversal) { TraversalHelper.getStepsOfClass(TraversalFilterStep.class, traversal).forEach(originalStep -> { // Check if this filter traversal matches the pattern: _.inV/outV/otherV.is(x) 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)) { //Get the direction in which we filter on the adjacent vertex (or null if not a valid adjacency filter) Direction direction = null; if (steps.get(0) instanceof EdgeVertexStep) { EdgeVertexStep evs = (EdgeVertexStep) steps.get(0); if (evs.getDirection() != Direction.BOTH) direction = evs.getDirection(); } else { direction = Direction.BOTH; } P predicate = ((IsStep) steps.get(1)).getPredicate(); //Check that we have a valid direction and a valid vertex filter predicate if (direction != null && predicate.getBiPredicate() == Compare.eq && predicate.getValue() instanceof Vertex) { Vertex vertex = (Vertex) predicate.getValue(); //Now, check that this step is preceded by VertexStep that returns edges Step<?, ?> currentStep = originalStep.getPreviousStep(); while (currentStep != EmptyStep.instance()) { if (!(currentStep instanceof HasStep) && !(currentStep instanceof IdentityStep)) { break; } //We can jump over other steps as we move backward currentStep = currentStep.getPreviousStep(); } if (currentStep instanceof VertexStep) { VertexStep vertexStep = (VertexStep) currentStep; if (vertexStep.returnsEdge() && (direction == Direction.BOTH || direction.equals(vertexStep.getDirection().opposite()))) { //Now replace the step with a has condition TraversalHelper.replaceStep(originalStep, new HasStep(traversal, new HasContainer(ImplicitKey.ADJACENT_ID.name(), P.eq(vertex))), traversal); } } } } }); }
Example #22
Source File: DefaultTraversal.java From tinkerpop with Apache License 2.0 | 4 votes |
@Override public void setParent(final TraversalParent step) { this.parent = null == step ? EmptyStep.instance() : step; }
Example #23
Source File: AbstractLambdaTraversal.java From tinkerpop with Apache License 2.0 | 4 votes |
@Override public TraversalParent getParent() { return null == this.bypassTraversal ? EmptyStep.instance() : this.bypassTraversal.getParent(); }
Example #24
Source File: MatchStepTest.java From tinkerpop with Apache License 2.0 | 4 votes |
@Test public void testComputerAwareCountMatchAlgorithm() { // MAKE SURE OLAP JOBS ARE BIASED TOWARDS STAR GRAPH DATA final Consumer doNothing = s -> { }; final Traversal.Admin<?, ?> traversal = __.match( as("a").sideEffect(doNothing).as("b"), // 1 as("b").sideEffect(doNothing).as("c"), // 2 as("a").sideEffect(doNothing).as("d"), // 5 as("c").sideEffect(doNothing).as("e"), // 4 as("c").sideEffect(doNothing).as("f")) // 3 .asAdmin(); traversal.applyStrategies(); // necessary to enure step ids are unique final MatchStep.CountMatchAlgorithm countMatchAlgorithm = new MatchStep.CountMatchAlgorithm(); countMatchAlgorithm.initialize(true, ((MatchStep<?, ?>) traversal.getStartStep()).getGlobalChildren()); Traversal.Admin<Object, Object> firstPattern = ((MatchStep<?, ?>) traversal.getStartStep()).getGlobalChildren().get(0); Traversal.Admin<Object, Object> secondPattern = ((MatchStep<?, ?>) traversal.getStartStep()).getGlobalChildren().get(1); Traversal.Admin<Object, Object> thirdPattern = ((MatchStep<?, ?>) traversal.getStartStep()).getGlobalChildren().get(2); Traversal.Admin<Object, Object> forthPattern = ((MatchStep<?, ?>) traversal.getStartStep()).getGlobalChildren().get(3); Traversal.Admin<Object, Object> fifthPattern = ((MatchStep<?, ?>) traversal.getStartStep()).getGlobalChildren().get(4); countMatchAlgorithm.bundles.stream().forEach(bundle -> assertEquals(0.0d, bundle.multiplicity, 0.0d)); assertEquals(MatchStep.TraversalType.MATCH_TRAVERSAL, countMatchAlgorithm.getBundle(firstPattern).traversalType); assertEquals(MatchStep.TraversalType.MATCH_TRAVERSAL, countMatchAlgorithm.getBundle(secondPattern).traversalType); assertEquals(MatchStep.TraversalType.MATCH_TRAVERSAL, countMatchAlgorithm.getBundle(thirdPattern).traversalType); assertEquals(MatchStep.TraversalType.MATCH_TRAVERSAL, countMatchAlgorithm.getBundle(forthPattern).traversalType); assertEquals(MatchStep.TraversalType.MATCH_TRAVERSAL, countMatchAlgorithm.getBundle(fifthPattern).traversalType); assertEquals(firstPattern, countMatchAlgorithm.bundles.get(0).traversal); assertEquals(secondPattern, countMatchAlgorithm.bundles.get(1).traversal); assertEquals(thirdPattern, countMatchAlgorithm.bundles.get(2).traversal); assertEquals(forthPattern, countMatchAlgorithm.bundles.get(3).traversal); assertEquals(fifthPattern, countMatchAlgorithm.bundles.get(4).traversal); // MAKE THE SECOND PATTERN EXPENSIVE countMatchAlgorithm.recordStart(EmptyTraverser.instance(), secondPattern); countMatchAlgorithm.recordEnd(EmptyTraverser.instance(), secondPattern); countMatchAlgorithm.recordEnd(EmptyTraverser.instance(), secondPattern); countMatchAlgorithm.recordEnd(EmptyTraverser.instance(), secondPattern); countMatchAlgorithm.recordEnd(EmptyTraverser.instance(), secondPattern); countMatchAlgorithm.recordEnd(EmptyTraverser.instance(), secondPattern); countMatchAlgorithm.recordEnd(EmptyTraverser.instance(), secondPattern); // MAKE THE THIRD PATTERN MORE EXPENSIVE THAN FORTH countMatchAlgorithm.recordStart(EmptyTraverser.instance(), thirdPattern); countMatchAlgorithm.recordEnd(EmptyTraverser.instance(), thirdPattern); countMatchAlgorithm.recordEnd(EmptyTraverser.instance(), thirdPattern); countMatchAlgorithm.recordEnd(EmptyTraverser.instance(), thirdPattern); // MAKE THE FORTH PATTERN EXPENSIVE countMatchAlgorithm.recordStart(EmptyTraverser.instance(), forthPattern); countMatchAlgorithm.recordEnd(EmptyTraverser.instance(), forthPattern); countMatchAlgorithm.recordEnd(EmptyTraverser.instance(), forthPattern); // Traverser.Admin traverser = B_LP_O_P_S_SE_SL_TraverserGenerator.instance().generate(1, EmptyStep.instance(), 1l); traverser.addLabels(Collections.singleton("a")); assertEquals(firstPattern, countMatchAlgorithm.apply(traverser)); traverser = traverser.split(1, EmptyStep.instance()); traverser.getTags().add(firstPattern.getStartStep().getId()); traverser.addLabels(Collections.singleton("b")); // assertEquals(secondPattern, countMatchAlgorithm.apply(traverser)); traverser = traverser.split(1, EmptyStep.instance()); traverser.getTags().add(secondPattern.getStartStep().getId()); traverser.addLabels(Collections.singleton("c")); // assertEquals(fifthPattern, countMatchAlgorithm.apply(traverser)); traverser = traverser.split(1, EmptyStep.instance()); traverser.getTags().add(fifthPattern.getStartStep().getId()); traverser.addLabels(Collections.singleton("f")); // assertEquals(forthPattern, countMatchAlgorithm.apply(traverser)); traverser = traverser.split(1, EmptyStep.instance()); traverser.getTags().add(forthPattern.getStartStep().getId()); traverser.addLabels(Collections.singleton("e")); // assertEquals(thirdPattern, countMatchAlgorithm.apply(traverser)); traverser = traverser.split(1, EmptyStep.instance()); traverser.getTags().add(thirdPattern.getStartStep().getId()); traverser.addLabels(Collections.singleton("d")); }
Example #25
Source File: Traversal.java From tinkerpop with Apache License 2.0 | 4 votes |
/** * Determines if the traversal is at the root level. */ public default boolean isRoot() { return getParent() instanceof EmptyStep; }
Example #26
Source File: MessagePassingReductionStrategy.java From tinkerpop with Apache License 2.0 | 4 votes |
@Override public void apply(final Traversal.Admin<?, ?> traversal) { // only process the first traversal step in an OLAP chain TraversalHelper.getFirstStepOfAssignableClass(TraversalVertexProgramStep.class, traversal).ifPresent(step -> { final Graph graph = traversal.getGraph().orElse(EmptyGraph.instance()); // best guess at what the graph will be as its dynamically determined final Traversal.Admin<?, ?> compiledComputerTraversal = step.generateProgram(graph, EmptyMemory.instance()).getTraversal().get().clone(); if (!compiledComputerTraversal.isLocked()) compiledComputerTraversal.applyStrategies(); if (!TraversalHelper.hasStepOfAssignableClassRecursively(Arrays.asList(LocalStep.class, LambdaHolder.class), compiledComputerTraversal) && // don't do anything with lambdas or locals as this leads to unknown adjacencies !compiledComputerTraversal.getTraverserRequirements().contains(TraverserRequirement.PATH) && // when dynamic detachment is provided in 3.3.0, remove this (TODO) !compiledComputerTraversal.getTraverserRequirements().contains(TraverserRequirement.LABELED_PATH) && // when dynamic detachment is provided in 3.3.0, remove this (TODO) !(TraversalHelper.getStepsOfAssignableClass(TraversalParent.class, compiledComputerTraversal). // this is a strict precaution that could be loosed with deeper logic on barriers in global children stream(). filter(parent -> !parent.getGlobalChildren().isEmpty()).findAny().isPresent())) { final Traversal.Admin<?, ?> computerTraversal = step.computerTraversal.get().clone(); // apply the strategies up to this point final List<TraversalStrategy<?>> strategies = step.getTraversal().getStrategies().toList(); final int indexOfStrategy = strategies.indexOf(MessagePassingReductionStrategy.instance()); if (indexOfStrategy > 0) TraversalHelper.applySingleLevelStrategies(step.getTraversal(), computerTraversal, strategies.get(indexOfStrategy - 1).getClass()); if (computerTraversal.getSteps().size() > 1 && !(computerTraversal.getStartStep().getNextStep() instanceof Barrier) && TraversalHelper.hasStepOfAssignableClassRecursively(Arrays.asList(VertexStep.class, EdgeVertexStep.class), computerTraversal) && TraversalHelper.isLocalStarGraph(computerTraversal)) { Step barrier = (Step) TraversalHelper.getFirstStepOfAssignableClass(Barrier.class, computerTraversal).orElse(null); // if the barrier isn't present gotta check for uncapped profile() which can happen if you do // profile("metrics") - see below for more worries if (null == barrier) { final ProfileSideEffectStep pses = TraversalHelper.getFirstStepOfAssignableClass(ProfileSideEffectStep.class, computerTraversal).orElse(null); if (pses != null) barrier = pses.getPreviousStep(); } // if the barrier is a profile() then we'll mess stuff up if we wrap that in a local() as in: // local(..., ProfileSideEffectStep) // which won't compute right on OLAP (or anything??). By stepping back we cut things off at // just before the ProfileSideEffectStep to go inside the local() so that ProfileSideEffectStep // shows up just after it // // why does this strategy need to know so much about profile!?! if (barrier != null && barrier.getPreviousStep() instanceof ProfileSideEffectStep) barrier = barrier.getPreviousStep().getPreviousStep(); if (MessagePassingReductionStrategy.insertElementId(barrier)) // out().count() -> out().id().count() TraversalHelper.insertBeforeStep(new IdStep(computerTraversal), barrier, computerTraversal); if (!(MessagePassingReductionStrategy.endsWithElement(null == barrier ? computerTraversal.getEndStep() : barrier))) { Traversal.Admin newChildTraversal = new DefaultGraphTraversal<>(); TraversalHelper.removeToTraversal(computerTraversal.getStartStep() instanceof GraphStep ? computerTraversal.getStartStep().getNextStep() : (Step) computerTraversal.getStartStep(), null == barrier ? EmptyStep.instance() : barrier, newChildTraversal); newChildTraversal = newChildTraversal.getSteps().size() > 1 ? (Traversal.Admin) __.local(newChildTraversal) : newChildTraversal; if (null == barrier) TraversalHelper.insertTraversal(0, newChildTraversal, computerTraversal); else TraversalHelper.insertTraversal(barrier.getPreviousStep(), newChildTraversal, computerTraversal); } } step.setComputerTraversal(computerTraversal); } }); }
Example #27
Source File: TraversalVertexProgram.java From tinkerpop with Apache License 2.0 | 4 votes |
@Override public void loadState(final Graph graph, final Configuration configuration) { if (!configuration.containsKey(TRAVERSAL)) throw new IllegalArgumentException("The configuration does not have a traversal: " + TRAVERSAL); this.traversal = PureTraversal.loadState(configuration, TRAVERSAL, graph); if (!this.traversal.get().isLocked()) this.traversal.get().applyStrategies(); /// traversal is compiled and ready to be introspected this.traversalMatrix = new TraversalMatrix<>(this.traversal.get()); // get any master-traversal halted traversers this.haltedTraversers = TraversalVertexProgram.loadHaltedTraversers(configuration); // if results will be serialized out, don't save halted traversers across the cluster this.returnHaltedTraversers = (this.traversal.get().getParent().asStep().getNextStep() instanceof ComputerResultStep || // if its just going to stream it out, don't distribute this.traversal.get().getParent().asStep().getNextStep() instanceof EmptyStep || // same as above, but if using TraversalVertexProgramStep directly (this.traversal.get().getParent().asStep().getNextStep() instanceof ProfileStep && // same as above, but needed for profiling this.traversal.get().getParent().asStep().getNextStep().getNextStep() instanceof ComputerResultStep)); // determine how to store halted traversers final Iterator<?> itty = IteratorUtils.filter(this.traversal.get().getStrategies(), strategy -> strategy instanceof HaltedTraverserStrategy).iterator(); this.haltedTraverserStrategy = itty.hasNext() ? (HaltedTraverserStrategy) itty.next() : HaltedTraverserStrategy.reference(); // register traversal side-effects in memory this.memoryComputeKeys.addAll(MemoryTraversalSideEffects.getMemoryComputeKeys(this.traversal.get())); // register MapReducer memory compute keys for (final MapReducer<?, ?, ?, ?, ?> mapReducer : TraversalHelper.getStepsOfAssignableClassRecursively(MapReducer.class, this.traversal.get())) { this.mapReducers.add(mapReducer.getMapReduce()); this.memoryComputeKeys.add(MemoryComputeKey.of(mapReducer.getMapReduce().getMemoryKey(), Operator.assign, false, false)); } // register memory computing steps that use memory compute keys for (final MemoryComputing<?> memoryComputing : TraversalHelper.getStepsOfAssignableClassRecursively(MemoryComputing.class, this.traversal.get())) { this.memoryComputeKeys.add(memoryComputing.getMemoryComputeKey()); } // register profile steps (TODO: try to hide this) for (final ProfileStep profileStep : TraversalHelper.getStepsOfAssignableClassRecursively(ProfileStep.class, this.traversal.get())) { this.traversal.get().getSideEffects().register(profileStep.getId(), new MutableMetricsSupplier(profileStep.getPreviousStep()), ProfileStep.ProfileBiOperator.instance()); } // register TraversalVertexProgram specific memory compute keys this.memoryComputeKeys.add(MemoryComputeKey.of(VOTE_TO_HALT, Operator.and, false, true)); this.memoryComputeKeys.add(MemoryComputeKey.of(HALTED_TRAVERSERS, Operator.addAll, false, false)); this.memoryComputeKeys.add(MemoryComputeKey.of(ACTIVE_TRAVERSERS, Operator.addAll, true, true)); this.memoryComputeKeys.add(MemoryComputeKey.of(MUTATED_MEMORY_KEYS, Operator.addAll, false, true)); this.memoryComputeKeys.add(MemoryComputeKey.of(COMPLETED_BARRIERS, Operator.addAll, true, true)); // does the traversal need profile information this.profile = !TraversalHelper.getStepsOfAssignableClassRecursively(ProfileStep.class, this.traversal.get()).isEmpty(); }
Example #28
Source File: EmptyTraversal.java From tinkerpop with Apache License 2.0 | 4 votes |
@Override public TraversalParent getParent() { return EmptyStep.instance(); }
Example #29
Source File: ConnectiveStrategy.java From tinkerpop with Apache License 2.0 | 4 votes |
private static boolean legalCurrentStep(final Step<?, ?> step) { return !(step instanceof EmptyStep || step instanceof ProfileSideEffectStep || step instanceof HasNextStep || step instanceof ComputerAwareStep.EndStep || (step instanceof StartStep && !StartStep.isVariableStartStep(step)) || GraphStep.isStartStep(step)); }
Example #30
Source File: LazyBarrierStrategy.java From tinkerpop with Apache License 2.0 | 4 votes |
@Override public void apply(final Traversal.Admin<?, ?> traversal) { // drop() is a problem for bulked edge/meta properties because of Property equality changes in TINKERPOP-2318 // which made it so that a Property is equal if the key/value is equal. as a result, they bulk together which // is fine for almost all cases except when you wish to drop the property. if (TraversalHelper.onGraphComputer(traversal) || traversal.getTraverserRequirements().contains(TraverserRequirement.PATH) || TraversalHelper.hasStepOfAssignableClass(DropStep.class, traversal)) return; boolean foundFlatMap = false; boolean labeledPath = false; for (int i = 0; i < traversal.getSteps().size(); i++) { final Step<?, ?> step = traversal.getSteps().get(i); if (step instanceof PathProcessor) { final Set<String> keepLabels = ((PathProcessor) step).getKeepLabels(); if (null != keepLabels && keepLabels.isEmpty()) // if no more path data, then start barrier'ing again labeledPath = false; } if (step instanceof FlatMapStep && !(step instanceof VertexStep && ((VertexStep) step).returnsEdge()) || (step instanceof GraphStep && (i > 0 || ((GraphStep) step).getIds().length >= BIG_START_SIZE || (((GraphStep) step).getIds().length == 0 && !(step.getNextStep() instanceof HasStep))))) { // NoneStep, EmptyStep signify the end of the traversal where no barriers are really going to be // helpful after that. ProfileSideEffectStep means the traversal had profile() called on it and if // we don't account for that a barrier will inject at the end of the traversal where it wouldn't // be otherwise. LazyBarrierStrategy executes before the finalization strategy of ProfileStrategy // so additionally injected ProfileSideEffectStep instances should not have effect here. if (foundFlatMap && !labeledPath && !(step.getNextStep() instanceof Barrier) && !(step.getNextStep() instanceof NoneStep) && !(step.getNextStep() instanceof EmptyStep) && !(step.getNextStep() instanceof ProfileSideEffectStep)) { final Step noOpBarrierStep = new NoOpBarrierStep<>(traversal, MAX_BARRIER_SIZE); TraversalHelper.copyLabels(step, noOpBarrierStep, true); TraversalHelper.insertAfterStep(noOpBarrierStep, step, traversal); } else foundFlatMap = true; } if (!step.getLabels().isEmpty()) labeledPath = true; } }