MatchStep extends ComputerAwareStep<S, Map<String, E>> implements TraversalParent, Scoping, PathProcessor { @Override public List<Traversal.Admin<Object, Object>> getGlobalChildren() { return Collections.unmodifiableList(this.matchTraversals); } MatchStep(final Traversal.Admin traversal, final ConnectiveStep.Connective connective, final Traversal... matchTraversals); ConnectiveStep.Connective getConnective(); void addGlobalChild(final Traversal.Admin<?, ?> globalChildTraversal); @Override void removeGlobalChild(final Traversal.Admin<?, ?> globalChildTraversal); @Override List<Traversal.Admin<Object, Object>> getGlobalChildren(); @Override void setKeepLabels(final Set<String> keepLabels); @Override Set<String> getKeepLabels(); Set<String> getMatchEndLabels(); Set<String> getMatchStartLabels(); @Override Set<String> getScopeKeys(); @Override String toString(); @Override void reset(); void setMatchAlgorithm(final Class<? extends MatchAlgorithm> matchAlgorithmClass); MatchAlgorithm getMatchAlgorithm(); @Override MatchStep<S, E> clone(); @Override void setTraversal(final Traversal.Admin<?, ?> parentTraversal); void setDedupLabels(final Set<String> labels); @Override int hashCode(); @Override Set<TraverserRequirement> getRequirements();  }
@Test public void testPreCompilationOfStartAndEnds() { final Traversal.Admin<?, ?> traversal = __.match(as("a").out().as("b"), as("c").path().as("d")).asAdmin(); final MatchStep<?, ?> matchStep = (MatchStep<?, ?>) traversal.getStartStep(); assertEquals(MatchStep.class, traversal.getStartStep().getClass()); assertEquals(2, matchStep.getGlobalChildren().size()); Traversal.Admin<Object, Object> pattern = matchStep.getGlobalChildren().get(0); assertEquals("a", ((MatchStep.MatchStartStep) pattern.getStartStep()).getSelectKey().get()); assertEquals(VertexStep.class, pattern.getStartStep().getNextStep().getClass()); assertEquals("b", ((MatchStep.MatchEndStep) pattern.getEndStep()).getMatchKey().get()); pattern = matchStep.getGlobalChildren().get(1); assertEquals("c", ((MatchStep.MatchStartStep) pattern.getStartStep()).getSelectKey().get()); assertEquals(PathStep.class, pattern.getStartStep().getNextStep().getClass()); assertEquals("d", ((MatchStep.MatchEndStep) pattern.getEndStep()).getMatchKey().get()); }
@Test public void testPreCompilationOfWherePredicate() { final List<Traversal.Admin<?, ?>> traversals = Arrays.asList( __.match(as("a").out().as("b"), as("c").where(P.neq("d"))).asAdmin(), __.match(as("a").out().as("b"), where("c", P.neq("d"))).asAdmin()); assertEquals(1, new HashSet<>(traversals).size()); traversals.forEach(traversal -> { final MatchStep<?, ?> matchStep = (MatchStep<?, ?>) traversal.getStartStep(); assertEquals(2, matchStep.getGlobalChildren().size()); Traversal.Admin<Object, Object> pattern = matchStep.getGlobalChildren().get(0); assertEquals("a", ((MatchStep.MatchStartStep) pattern.getStartStep()).getSelectKey().get()); assertEquals(VertexStep.class, pattern.getStartStep().getNextStep().getClass()); assertEquals("b", ((MatchStep.MatchEndStep) pattern.getEndStep()).getMatchKey().get()); pattern = matchStep.getGlobalChildren().get(1); assertEquals(MatchStep.MatchStartStep.class, pattern.getStartStep().getClass()); assertEquals("c", ((MatchStep.MatchStartStep) pattern.getStartStep()).getSelectKey().get()); assertEquals(WherePredicateStep.class, pattern.getStartStep().getNextStep().getClass()); assertEquals(MatchStep.MatchEndStep.class, pattern.getStartStep().getNextStep().getNextStep().getClass()); assertFalse(((WherePredicateStep<?>) pattern.getStartStep().getNextStep()).getStartKey().isPresent()); assertEquals("d", ((WherePredicateStep<?>) pattern.getStartStep().getNextStep()).getPredicate().get().getOriginalValue()); }); }
@Test public void testCountMatchAlgorithm() { Traversal.Admin<?, ?> traversal = __.match(as("a").out().as("b"), as("c").in().as("d")).asAdmin(); MatchStep.CountMatchAlgorithm countMatchAlgorithm = new MatchStep.CountMatchAlgorithm(); countMatchAlgorithm.initialize(false, ((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); assertEquals(2, countMatchAlgorithm.bundles.size()); countMatchAlgorithm.bundles.stream().forEach(bundle -> assertEquals(0.0d, bundle.multiplicity, 0.0d)); assertEquals(firstPattern, countMatchAlgorithm.bundles.get(0).traversal); assertEquals(secondPattern, countMatchAlgorithm.bundles.get(1).traversal); countMatchAlgorithm.recordStart(EmptyTraverser.instance(), firstPattern); countMatchAlgorithm.recordStart(EmptyTraverser.instance(), firstPattern); countMatchAlgorithm.recordStart(EmptyTraverser.instance(), secondPattern); countMatchAlgorithm.recordStart(EmptyTraverser.instance(), secondPattern); countMatchAlgorithm.recordStart(EmptyTraverser.instance(), secondPattern); assertEquals(firstPattern, countMatchAlgorithm.bundles.get(0).traversal); assertEquals(secondPattern, countMatchAlgorithm.bundles.get(1).traversal); assertEquals(MatchStep.TraversalType.MATCH_TRAVERSAL, countMatchAlgorithm.getBundle(firstPattern).traversalType); assertEquals(MatchStep.TraversalType.MATCH_TRAVERSAL, countMatchAlgorithm.getBundle(secondPattern).traversalType); assertEquals(2l, countMatchAlgorithm.getBundle(firstPattern).startsCount); assertEquals(3l, countMatchAlgorithm.getBundle(secondPattern).startsCount); assertEquals(0l, countMatchAlgorithm.getBundle(firstPattern).endsCount); assertEquals(0l, countMatchAlgorithm.getBundle(secondPattern).endsCount); countMatchAlgorithm.recordEnd(EmptyTraverser.instance(), firstPattern); countMatchAlgorithm.recordEnd(EmptyTraverser.instance(), secondPattern); countMatchAlgorithm.recordEnd(EmptyTraverser.instance(), secondPattern); countMatchAlgorithm.recordEnd(EmptyTraverser.instance(), secondPattern); assertEquals(2l, countMatchAlgorithm.getBundle(firstPattern).startsCount); assertEquals(3l, countMatchAlgorithm.getBundle(secondPattern).startsCount); assertEquals(1l, countMatchAlgorithm.getBundle(firstPattern).endsCount); assertEquals(3l, countMatchAlgorithm.getBundle(secondPattern).endsCount); assertEquals(0.5d, countMatchAlgorithm.getBundle(firstPattern).multiplicity, 0.01d); assertEquals(1.0d, countMatchAlgorithm.getBundle(secondPattern).multiplicity, 0.01d); assertEquals(firstPattern, countMatchAlgorithm.bundles.get(0).traversal); assertEquals(secondPattern, countMatchAlgorithm.bundles.get(1).traversal); countMatchAlgorithm.recordEnd(EmptyTraverser.instance(), firstPattern); countMatchAlgorithm.recordEnd(EmptyTraverser.instance(), firstPattern); assertEquals(1.5d, countMatchAlgorithm.getBundle(firstPattern).multiplicity, 0.01d); assertEquals(1.0d, countMatchAlgorithm.getBundle(secondPattern).multiplicity, 0.01d); assertEquals(secondPattern, countMatchAlgorithm.bundles.get(0).traversal); assertEquals(firstPattern, countMatchAlgorithm.bundles.get(1).traversal); traversal = __.match(as("a").out().as("b"), as("c").in().as("d"), where("a", P.eq("b"))).asAdmin(); countMatchAlgorithm = new MatchStep.CountMatchAlgorithm(); countMatchAlgorithm.initialize(false, ((MatchStep<?, ?>) traversal.getStartStep()).getGlobalChildren()); assertEquals(3, countMatchAlgorithm.bundles.size()); firstPattern = ((MatchStep<?, ?>) traversal.getStartStep()).getGlobalChildren().get(0); secondPattern = ((MatchStep<?, ?>) traversal.getStartStep()).getGlobalChildren().get(1); Traversal.Admin<Object, Object> thirdPattern = ((MatchStep<?, ?>) traversal.getStartStep()).getGlobalChildren().get(2); 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.WHERE_PREDICATE, countMatchAlgorithm.getBundle(thirdPattern).traversalType); assertEquals(firstPattern, countMatchAlgorithm.bundles.get(0).traversal); assertEquals(secondPattern, countMatchAlgorithm.bundles.get(1).traversal); assertEquals(thirdPattern, countMatchAlgorithm.bundles.get(2).traversal); countMatchAlgorithm.recordStart(EmptyTraverser.instance(), firstPattern); countMatchAlgorithm.recordEnd(EmptyTraverser.instance(), firstPattern); assertEquals(1l, countMatchAlgorithm.getBundle(firstPattern).startsCount); assertEquals(0l, countMatchAlgorithm.getBundle(secondPattern).startsCount); assertEquals(0l, countMatchAlgorithm.getBundle(thirdPattern).startsCount); assertEquals(1l, countMatchAlgorithm.getBundle(firstPattern).endsCount); assertEquals(0l, countMatchAlgorithm.getBundle(secondPattern).endsCount); assertEquals(0l, countMatchAlgorithm.getBundle(thirdPattern).endsCount); assertEquals(1.0d, countMatchAlgorithm.getBundle(firstPattern).multiplicity, 0.01d); assertEquals(0.0d, countMatchAlgorithm.getBundle(secondPattern).multiplicity, 0.01d); assertEquals(0.0d, countMatchAlgorithm.getBundle(thirdPattern).multiplicity, 0.01d); assertEquals(thirdPattern, countMatchAlgorithm.bundles.get(0).traversal); assertEquals(secondPattern, countMatchAlgorithm.bundles.get(1).traversal); assertEquals(firstPattern, countMatchAlgorithm.bundles.get(2).traversal); countMatchAlgorithm.recordStart(EmptyTraverser.instance(), thirdPattern); countMatchAlgorithm.recordStart(EmptyTraverser.instance(), thirdPattern); countMatchAlgorithm.recordStart(EmptyTraverser.instance(), thirdPattern); countMatchAlgorithm.recordEnd(EmptyTraverser.instance(), thirdPattern); countMatchAlgorithm.recordEnd(EmptyTraverser.instance(), thirdPattern); countMatchAlgorithm.recordEnd(EmptyTraverser.instance(), thirdPattern); countMatchAlgorithm.recordEnd(EmptyTraverser.instance(), thirdPattern); countMatchAlgorithm.recordEnd(EmptyTraverser.instance(), thirdPattern); countMatchAlgorithm.recordEnd(EmptyTraverser.instance(), thirdPattern); assertEquals(1l, countMatchAlgorithm.getBundle(firstPattern).startsCount); assertEquals(0l, countMatchAlgorithm.getBundle(secondPattern).startsCount); assertEquals(3l, countMatchAlgorithm.getBundle(thirdPattern).startsCount); assertEquals(1l, countMatchAlgorithm.getBundle(firstPattern).endsCount); assertEquals(0l, countMatchAlgorithm.getBundle(secondPattern).endsCount); assertEquals(6l, countMatchAlgorithm.getBundle(thirdPattern).endsCount); assertEquals(1.0d, countMatchAlgorithm.getBundle(firstPattern).multiplicity, 0.01d); assertEquals(0.0d, countMatchAlgorithm.getBundle(secondPattern).multiplicity, 0.01d); assertEquals(2.0d, countMatchAlgorithm.getBundle(thirdPattern).multiplicity, 0.01d); assertEquals(thirdPattern, countMatchAlgorithm.bundles.get(0).traversal); assertEquals(secondPattern, countMatchAlgorithm.bundles.get(1).traversal); assertEquals(firstPattern, countMatchAlgorithm.bundles.get(2).traversal); countMatchAlgorithm.recordStart(EmptyTraverser.instance(), secondPattern); countMatchAlgorithm.recordStart(EmptyTraverser.instance(), secondPattern); countMatchAlgorithm.recordStart(EmptyTraverser.instance(), secondPattern); 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); assertEquals(1l, countMatchAlgorithm.getBundle(firstPattern).startsCount); assertEquals(4l, countMatchAlgorithm.getBundle(secondPattern).startsCount); assertEquals(3l, countMatchAlgorithm.getBundle(thirdPattern).startsCount); assertEquals(1l, countMatchAlgorithm.getBundle(firstPattern).endsCount); assertEquals(6l, countMatchAlgorithm.getBundle(secondPattern).endsCount); assertEquals(6l, countMatchAlgorithm.getBundle(thirdPattern).endsCount); assertEquals(1.0d, countMatchAlgorithm.getBundle(firstPattern).multiplicity, 0.01d); assertEquals(1.5d, countMatchAlgorithm.getBundle(secondPattern).multiplicity, 0.01d); assertEquals(2.0d, countMatchAlgorithm.getBundle(thirdPattern).multiplicity, 0.01d); assertEquals(thirdPattern, countMatchAlgorithm.bundles.get(0).traversal); assertEquals(firstPattern, countMatchAlgorithm.bundles.get(1).traversal); assertEquals(secondPattern, countMatchAlgorithm.bundles.get(2).traversal); }
@Test public void testComputerAwareCountMatchAlgorithm() { final Consumer doNothing = s -> { }; final Traversal.Admin<?, ?> traversal = __.match( as("a").sideEffect(doNothing).as("b"), as("b").sideEffect(doNothing).as("c"), as("a").sideEffect(doNothing).as("d"), as("c").sideEffect(doNothing).as("e"), as("c").sideEffect(doNothing).as("f")) .asAdmin(); traversal.applyStrategies(); 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); 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); countMatchAlgorithm.recordStart(EmptyTraverser.instance(), thirdPattern); countMatchAlgorithm.recordEnd(EmptyTraverser.instance(), thirdPattern); countMatchAlgorithm.recordEnd(EmptyTraverser.instance(), thirdPattern); countMatchAlgorithm.recordEnd(EmptyTraverser.instance(), thirdPattern); 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")); }
@Test public void shouldCalculateStartLabelCorrectly() { Traversal.Admin<?, ?> traversal = match( where(and( as("a").out("created").as("b"), as("b").in("created").count().is(eq(3)))), as("a").both().as("b"), where(as("b").in())).asAdmin(); assertEquals("a", MatchStep.Helper.computeStartLabel(((MatchStep<?, ?>) traversal.getStartStep()).getGlobalChildren())); traversal = match( where("a", P.neq("c")), as("a").out("created").as("b"), or( as("a").out("knows").has("name", "vadas"), as("a").in("knows").and().as("a").has(T.label, "person") ), as("b").in("created").as("c"), as("b").in("created").count().is(P.gt(1))).asAdmin(); assertEquals("a", MatchStep.Helper.computeStartLabel(((MatchStep<?, ?>) traversal.getStartStep()).getGlobalChildren())); }