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; }
/** * Map the {@link Traverser} to a {@link Map} of bindings as specified by the provided match traversals. * * @param matchTraversals the traversal that maintain variables which must hold for the life of the traverser * @param <E2> the type of the obejcts bound in the variables * @return the traversal with an appended {@link MatchStep}. * @see <a href="http://tinkerpop.apache.org/docs/${project.version}/reference/#match-step" target="_blank">Reference Documentation - Match Step</a> * @since 3.0.0-incubating */ public default <E2> GraphTraversal<S, Map<String, E2>> match(final Traversal<?, ?>... matchTraversals) { this.asAdmin().getBytecode().addStep(Symbols.match, matchTraversals); return this.asAdmin().addStep(new MatchStep<>(this.asAdmin(), ConnectiveStep.Connective.AND, matchTraversals)); }
@Override public void apply(final Traversal.Admin<?, ?> traversal) { if (!TraversalHelper.hasStepOfClass(MatchStep.class, traversal)) return; TraversalHelper.getStepsOfClass(MatchStep.class, traversal).forEach(matchStep -> { // match().select().where() --> match(where()).select() // match().select().dedup() --> match(dedup()).select() Step<?, ?> nextStep = matchStep.getNextStep(); while (nextStep instanceof WherePredicateStep || nextStep instanceof WhereTraversalStep || (nextStep instanceof DedupGlobalStep && !((DedupGlobalStep) nextStep).getScopeKeys().isEmpty() && ((DedupGlobalStep) nextStep).getLocalChildren().isEmpty()) || (nextStep instanceof SelectStep && ((SelectStep) nextStep).getLocalChildren().isEmpty()) || (nextStep instanceof SelectOneStep && ((SelectOneStep) nextStep).getLocalChildren().isEmpty())) { if (nextStep instanceof WherePredicateStep || nextStep instanceof WhereTraversalStep) { traversal.removeStep(nextStep); matchStep.addGlobalChild(traversal instanceof GraphTraversal ? new DefaultGraphTraversal<>().addStep(nextStep) : new DefaultTraversal<>().addStep(nextStep)); nextStep = matchStep.getNextStep(); } else if (nextStep instanceof DedupGlobalStep && !((DedupGlobalStep) nextStep).getScopeKeys().isEmpty() && ((DedupGlobalStep) nextStep).getLocalChildren().isEmpty() && !TraversalHelper.onGraphComputer(traversal)) { traversal.removeStep(nextStep); matchStep.setDedupLabels(((DedupGlobalStep<?>) nextStep).getScopeKeys()); nextStep = matchStep.getNextStep(); } else if (nextStep.getLabels().isEmpty()) { nextStep = nextStep.getNextStep(); } else break; } }); }
public void addGlobalChild(final Traversal.Admin<?, ?> globalChildTraversal) { this.configureStartAndEndSteps(globalChildTraversal); this.matchTraversals.add(this.integrateChild(globalChildTraversal)); }
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; } }
if (this.first) { this.first = false; this.initializeMatchAlgorithm(true); if (null != this.keepLabels && this.keepLabels.containsAll(this.matchEndLabels) && if (!traverser.getTags().contains(this.getId())) { traverser.getTags().add(this.getId()); // so the traverser never returns to this branch ever again if (!this.hasPathLabel(traverser.path(), this.matchStartLabels)) traverser.addLabels(Collections.singleton(this.computedStartLabel)); // if the traverser doesn't have a legal start, then provide it the pre-computed one if (!this.isDuplicate(traverser)) { if (hasMatched(this.connective, traverser)) { traverser.setStepId(this.getNextStep().getId()); traverser.addLabels(this.labels); return IteratorUtils.of(traverser.split(this.getBindings(traverser), this)); final Traversal.Admin<Object, Object> matchTraversal = this.getMatchAlgorithm().apply(traverser); // determine which sub-pattern the traverser should try next traverser.getTags().add(matchTraversal.getStartStep().getId());
final MatchStep matchStep = new MatchStep(matchTraversal, startStep instanceof AndStep ? ConnectiveStep.Connective.AND : ConnectiveStep.Connective.OR, ((ConnectiveStep<?>) startStep).getLocalChildren().toArray(new Traversal[((ConnectiveStep<?>) startStep).getLocalChildren().size()])); TraversalHelper.removeToTraversal(startStep, startStep.getNextStep(), notTraversal); matchTraversal.addStep(0, new WhereTraversalStep<>(matchTraversal, notTraversal)); this.configureStartAndEndSteps(matchTraversal); } else if (StartStep.isVariableStartStep(startStep)) { final String label = startStep.getLabels().iterator().next(); } else if (startStep instanceof WhereTraversalStep) { // necessary for GraphComputer so the projection is not select'd from a path final WhereTraversalStep<?> whereStep = (WhereTraversalStep<?>) startStep; TraversalHelper.insertBeforeStep(new MatchStartStep(matchTraversal, this.pullOutVariableStartStepToParent(whereStep)), (Step) whereStep, matchTraversal); // where(as('a').out()) -> as('a').where(out()) } else if (startStep instanceof WherePredicateStep) { // necessary for GraphComputer so the projection is not select'd from a path final WherePredicateStep<?> whereStep = (WherePredicateStep<?>) startStep;
variables.add(Scoping.Variable.START); } else if (startStep instanceof MatchStep) { for (final Traversal.Admin<?, ?> global : ((MatchStep<?, ?>) startStep).getGlobalChildren()) { TraversalHelper.getVariableLocations(variables, global);
@Override protected Traverser.Admin<Object> processNextStart() throws NoSuchElementException { if (null == this.parent) this.parent = ((MatchStep) this.getTraversal().getParent().asStep()); while (true) { final Traverser.Admin traverser = this.starts.next(); // no end label if (null == this.matchKey) { // if (this.traverserStepIdAndLabelsSetByChild) -- traverser equality is based on stepId, lets ensure they are all at the parent traverser.setStepId(this.parent.getId()); this.parent.getMatchAlgorithm().recordEnd(traverser, this.getTraversal()); return this.retractUnnecessaryLabels(traverser); } // TODO: sideEffect check? // path check final Path path = traverser.path(); if (!path.hasLabel(this.matchKey) || traverser.get().equals(path.get(Pop.last, this.matchKey))) { // if (this.traverserStepIdAndLabelsSetByChild) -- traverser equality is based on stepId and thus, lets ensure they are all at the parent traverser.setStepId(this.parent.getId()); traverser.addLabels(this.matchKeyCollection); this.parent.getMatchAlgorithm().recordEnd(traverser, this.getTraversal()); return this.retractUnnecessaryLabels(traverser); } } }
@Override protected Traverser.Admin<Object> processNextStart() throws NoSuchElementException { if (null == this.parent) this.parent = (MatchStep<?, ?>) this.getTraversal().getParent(); final Traverser.Admin<Object> traverser = this.starts.next(); this.parent.getMatchAlgorithm().recordStart(traverser, this.getTraversal()); // TODO: sideEffect check? return null == this.selectKey ? traverser : traverser.split(traverser.path().get(Pop.last, this.selectKey), this); }
private <S> Traverser.Admin<S> retractUnnecessaryLabels(final Traverser.Admin<S> traverser) { if (null == this.parent.getKeepLabels()) return traverser; final Set<String> keepers = new HashSet<>(this.parent.getKeepLabels()); final Set<String> tags = traverser.getTags(); for (final Traversal.Admin<?, ?> matchTraversal : this.parent.matchTraversals) { // get remaining traversal patterns for the traverser final String startStepId = matchTraversal.getStartStep().getId(); if (!tags.contains(startStepId)) { keepers.addAll(this.parent.getReferencedLabelsMap().get(startStepId)); // get the reference labels required for those remaining traversals } } return PathProcessor.processTraverserPathLabels(traverser, keepers); // remove all reference labels that are no longer required }
@Override public Set<String> getScopeKeys() { if (null == this.scopeKeys) { // computer the first time and then save resultant keys this.scopeKeys = new HashSet<>(); if (null != this.selectKey) this.scopeKeys.add(this.selectKey); final Set<String> endLabels = ((MatchStep<?, ?>) this.getTraversal().getParent()).getMatchEndLabels(); TraversalHelper.anyStepRecursively(step -> { if (step instanceof WherePredicateStep || step instanceof WhereTraversalStep) { for (final String key : ((Scoping) step).getScopeKeys()) { if (endLabels.contains(key)) this.scopeKeys.add(key); } } return false; }, this.getTraversal()); // this is the old way but it only checked for where() as the next step, not arbitrarily throughout traversal // just going to keep this in case something pops up in the future and this is needed as an original reference. /* if (this.getNextStep() instanceof WhereTraversalStep || this.getNextStep() instanceof WherePredicateStep) this.scopeKeys.addAll(((Scoping) this.getNextStep()).getScopeKeys());*/ this.scopeKeys = Collections.unmodifiableSet(this.scopeKeys); } return this.scopeKeys; } }
if (this.first) { this.first = false; this.initializeMatchAlgorithm(false); if (null != this.keepLabels && this.keepLabels.containsAll(this.matchEndLabels) && if (this.standardAlgorithmBarrier.isEmpty()) { traverser = this.starts.next(); if (!traverser.getTags().contains(this.getId())) { traverser.getTags().add(this.getId()); // so the traverser never returns to this branch ever again if (!this.hasPathLabel(traverser.path(), this.matchStartLabels)) traverser.addLabels(Collections.singleton(this.computedStartLabel)); // if the traverser doesn't have a legal start, then provide it the pre-computed one if (!this.isDuplicate(traverser)) { if (hasMatched(this.connective, traverser)) return IteratorUtils.of(traverser.split(this.getBindings(traverser), this)); final Traversal.Admin<Object, Object> matchTraversal = this.getMatchAlgorithm().apply(traverser); traverser.getTags().add(matchTraversal.getStartStep().getId());
final MatchStep matchStep = new MatchStep(matchTraversal, startStep instanceof AndStep ? ConnectiveStep.Connective.AND : ConnectiveStep.Connective.OR, ((ConnectiveStep<?>) startStep).getLocalChildren().toArray(new Traversal[((ConnectiveStep<?>) startStep).getLocalChildren().size()])); TraversalHelper.removeToTraversal(startStep, startStep.getNextStep(), notTraversal); matchTraversal.addStep(0, new WhereTraversalStep<>(matchTraversal, notTraversal)); this.configureStartAndEndSteps(matchTraversal); } else if (StartStep.isVariableStartStep(startStep)) { final String label = startStep.getLabels().iterator().next(); } else if (startStep instanceof WhereTraversalStep) { // necessary for GraphComputer so the projection is not select'd from a path final WhereTraversalStep<?> whereStep = (WhereTraversalStep<?>) startStep; TraversalHelper.insertBeforeStep(new MatchStartStep(matchTraversal, this.pullOutVariableStartStepToParent(whereStep)), (Step) whereStep, matchTraversal); // where(as('a').out()) -> as('a').where(out()) } else if (startStep instanceof WherePredicateStep) { // necessary for GraphComputer so the projection is not select'd from a path final WherePredicateStep<?> whereStep = (WherePredicateStep<?>) startStep;
currentStep.getNextStep() instanceof DedupGlobalStep || currentStep.getNextStep() instanceof SelectOneStep && currentStep.getNextStep().getNextStep() instanceof FilterStep)) { pathProcessor.setKeepLabels(((MatchStep) currentStep).getMatchStartLabels()); pathProcessor.getKeepLabels().addAll(((MatchStep) currentStep).getMatchEndLabels()); } else { if (pathProcessor.getKeepLabels() == null) pathProcessor.setKeepLabels(((MatchStep) currentStep.getTraversal().getParent().asStep()).getMatchStartLabels()); pathProcessor.getKeepLabels().addAll(((MatchStep) currentStep.getTraversal().getParent().asStep()).getMatchEndLabels());
variables.add(Scoping.Variable.START); } else if (startStep instanceof MatchStep) { for (final Traversal.Admin<?, ?> global : ((MatchStep<?, ?>) startStep).getGlobalChildren()) { TraversalHelper.getVariableLocations(variables, global);
public void addGlobalChild(final Traversal.Admin<?, ?> globalChildTraversal) { this.configureStartAndEndSteps(globalChildTraversal); this.matchTraversals.add(this.integrateChild(globalChildTraversal)); }
@Override protected Traverser.Admin<Object> processNextStart() throws NoSuchElementException { if (null == this.parent) this.parent = ((MatchStep) this.getTraversal().getParent().asStep()); while (true) { final Traverser.Admin traverser = this.starts.next(); // no end label if (null == this.matchKey) { // if (this.traverserStepIdAndLabelsSetByChild) -- traverser equality is based on stepId, lets ensure they are all at the parent traverser.setStepId(this.parent.getId()); this.parent.getMatchAlgorithm().recordEnd(traverser, this.getTraversal()); return this.retractUnnecessaryLabels(traverser); } // TODO: sideEffect check? // path check final Path path = traverser.path(); if (!path.hasLabel(this.matchKey) || traverser.get().equals(path.get(Pop.last, this.matchKey))) { // if (this.traverserStepIdAndLabelsSetByChild) -- traverser equality is based on stepId and thus, lets ensure they are all at the parent traverser.setStepId(this.parent.getId()); traverser.addLabels(this.matchKeyCollection); this.parent.getMatchAlgorithm().recordEnd(traverser, this.getTraversal()); return this.retractUnnecessaryLabels(traverser); } } }
@Override protected Traverser.Admin<Object> processNextStart() throws NoSuchElementException { if (null == this.parent) this.parent = (MatchStep<?, ?>) this.getTraversal().getParent(); final Traverser.Admin<Object> traverser = this.starts.next(); this.parent.getMatchAlgorithm().recordStart(traverser, this.getTraversal()); // TODO: sideEffect check? return null == this.selectKey ? traverser : traverser.split(traverser.path().get(Pop.last, this.selectKey), this); }
private <S> Traverser.Admin<S> retractUnnecessaryLabels(final Traverser.Admin<S> traverser) { if (null == this.parent.getKeepLabels()) return traverser; final Set<String> keepers = new HashSet<>(this.parent.getKeepLabels()); final Set<String> tags = traverser.getTags(); for (final Traversal.Admin<?, ?> matchTraversal : this.parent.matchTraversals) { // get remaining traversal patterns for the traverser final String startStepId = matchTraversal.getStartStep().getId(); if (!tags.contains(startStepId)) { keepers.addAll(this.parent.getReferencedLabelsMap().get(startStepId)); // get the reference labels required for those remaining traversals } } return PathProcessor.processTraverserPathLabels(traverser, keepers); // remove all reference labels that are no longer required }