StagesFactory { public Stages<K, V> make(final Pattern<K, V> pattern) { if( pattern == null) throw new NullPointerException("Cannot make null pattern"); final List<Stage<K, V>> sequence = new ArrayList<>(); Stage<K, V> successorStage = new Stage<>(nextStageId(), "$final", Stage.StateType.FINAL); sequence.add(successorStage); Pattern<K, V> successorPattern = null; Pattern<K, V> currentPattern = pattern; while( currentPattern.getAncestor() != null) { final List<Stage<K, V>> stages = buildStages( Stage.StateType.NORMAL, currentPattern, successorStage, successorPattern); sequence.addAll(stages); successorStage = stages.get(stages.size() - 1); successorPattern = currentPattern; currentPattern = currentPattern.getAncestor(); } sequence.addAll(buildStages(Stage.StateType.BEGIN, currentPattern, successorStage, successorPattern)); return new Stages<>(sequence); }  Stages<K, V> make(final Pattern<K, V> pattern);  }
@Test(expected = StagesFactory.InvalidPatternException.class) public void testInvalidPatternGivenAFinalOneOrMoreStage() { Pattern<Object, String> pattern = new QueryBuilder<Object, String>() .select() .oneOrMore() .where(TestMatcher.isEqualTo("N/A")) .build(); StagesFactory<Object, String> factory = new StagesFactory<>(); factory.make(pattern); }
@Test(expected = StagesFactory.InvalidPatternException.class) public void testInvalidPatternGivenAFinalOptionalStage() { Pattern<Object, String> pattern = new QueryBuilder<Object, String>() .select() .optional() .where(TestMatcher.isEqualTo("N/A")) .build(); StagesFactory<Object, String> factory = new StagesFactory<>(); factory.make(pattern); }
@Test public void testPatternWithSingleStage() { Pattern<String, Integer> pattern = new QueryBuilder<String, Integer>() .select(STAGE_1).where(TestMatcher.isEqualTo(0)).build(); StagesFactory<String, Integer> factory = new StagesFactory<>(); Stages<String, Integer> stages = factory.make(pattern); Assert.assertEquals(2, stages.getAllStages().size()); final Stage<String, Integer> stage0 = stages.getAllStages().get(0); Assert.assertEquals(Stage.StateType.FINAL, stage0.getType()); Assert.assertEquals(0, stage0.getEdges().size()); final Stage<String, Integer> stage1 = stages.getAllStages().get(1); Assert.assertEquals(Stage.StateType.BEGIN, stage1.getType()); Assert.assertEquals(1, stage1.getEdges().size()); Assert.assertEquals(true, stage1.getEdges().get(0).is(EdgeOperation.BEGIN)); Assert.assertEquals(stage0, stage1.getEdges().get(0).getTarget()); Assert.assertEquals(STAGE_1, stage1.getName()); }
@Test public void testPatternWithMultipleStages() { Pattern<String, Integer> pattern = new QueryBuilder<String, Integer>() .select(STAGE_1).where(TestMatcher.isEqualTo(0)) .then() .select(STAGE_2).where((event) -> event.value() % 2 == 0) .then() .select(STAGE_3).where(TestMatcher.isGreaterThan(100)) .build(); StagesFactory<String, Integer> factory = new StagesFactory<>(); Stages<String, Integer> patternStages = factory.make(pattern); List<Stage<String, Integer>> stages = patternStages.getAllStages(); Assert.assertEquals(4, stages.size()); Assert.assertEquals(Stage.StateType.FINAL, stages.get(0).getType()); Assert.assertEquals(Stage.StateType.NORMAL, stages.get(1).getType()); Assert.assertEquals(STAGE_3, stages.get(1).getName()); Assert.assertEquals(Stage.StateType.NORMAL, stages.get(2).getType()); Assert.assertEquals(STAGE_2, stages.get(2).getName()); Assert.assertEquals(Stage.StateType.BEGIN, stages.get(3).getType()); Assert.assertEquals(STAGE_1, stages.get(3).getName()); }
@Test public void testPatternWithMultipleStagesAndOneOrMore() { Pattern<String, Integer> pattern = new QueryBuilder<String, Integer>() .select(STAGE_1).where((event) -> event.value() == 0) .then() .select(STAGE_2).oneOrMore().where((event) -> event.value() % 2 == 0) .then() .select(STAGE_3).where((event) -> event.value() > 100) .build(); StagesFactory<String, Integer> factory = new StagesFactory<>(); Stages<String, Integer> patternStages = factory.make(pattern); List<Stage<String, Integer>> stages = patternStages.getAllStages(); Assert.assertEquals(5, stages.size()); final Stage<String, Integer> stage0 = stages.get(0); Assert.assertEquals(Stage.StateType.FINAL, stage0.getType()); final Stage<String, Integer> stage3 = stages.get(1); Assert.assertEquals(Stage.StateType.NORMAL, stage3.getType()); Assert.assertEquals(STAGE_3, stage3.getName()); Assert.assertEquals(EdgeOperation.BEGIN, stage3.getEdges().get(0).getOperation()); Assert.assertEquals(stage0.getName(), stage3.getEdges().get(0).getTarget().getName()); final Stage<String, Integer> stage2 = stages.get(2); Assert.assertEquals(Stage.StateType.NORMAL, stage2.getType()); Assert.assertEquals(STAGE_2, stage2.getName()); Assert.assertEquals(EdgeOperation.TAKE, stage2.getEdges().get(0).getOperation()); Assert.assertEquals(stage3.getName(), stage2.getEdges().get(0).getTarget().getName()); Assert.assertEquals(EdgeOperation.PROCEED, stage2.getEdges().get(1).getOperation()); Assert.assertEquals(stage3.getName(), stage2.getEdges().get(1).getTarget().getName()); final Stage<String, Integer> internalStage2 = stages.get(3); Assert.assertEquals(Stage.StateType.NORMAL, internalStage2.getType()); Assert.assertEquals(STAGE_2, internalStage2.getName()); Assert.assertEquals(EdgeOperation.BEGIN, internalStage2.getEdges().get(0).getOperation()); final Stage<String, Integer> stage1 = stages.get(4); Assert.assertEquals(Stage.StateType.BEGIN, stage1.getType()); Assert.assertEquals(STAGE_1, stage1.getName()); }