MplProcessMaterializer extends ProcessCommandsHelper { public @Nullable CommandChain visitProcess(MplProgram program, MplProcess process) { if (process.getType() == INLINE) { return null; } List<ChainPart> chainParts = new CopyScope().copy(process.getChainParts()); List<ChainLink> result = new ArrayList<>(chainParts.size()); boolean containsSkip = containsHighlevelSkip(chainParts); String name = process.getName(); if (name != null) { if (process.isRepeating()) { if (options.hasOption(TRANSMITTER)) { result.add(new MplSkip()); } if (chainParts.isEmpty()) { chainParts.add(new MplCommand("", process.getSource())); } ChainPart first = chainParts.get(0); try { if (containsSkip) { first.setMode(IMPULSE); } else { first.setMode(REPEAT); } first.setNeedsRedstone(true); } catch (IllegalModifierException ex) { throw new IllegalStateException(ex.getMessage(), ex); } } else { result.addAll(newJumpDestination(false)); } } else if (options.hasOption(TRANSMITTER)) { result.add(new MplSkip()); } MplProcessAstVisitor visitor = new MplProcessAstVisitor(compilerContext, newVisitorContext(program)); for (ChainPart chainPart : chainParts) { result.addAll(chainPart.accept(visitor)); } if (process.isRepeating() && containsSkip) { result.addAll(getRestartBackref(result.get(0), false)); } if (!process.isRepeating() && name != null && !"install".equals(name) && !"uninstall".equals(name)) { result.addAll(visitor.visitIgnoringWarnings(new MplNotify(name, process.getSource()))); } resolveAllTargetedThisInserts(result); return new CommandChain(name, result, process.getTags()); } MplProcessMaterializer(MplCompilerContext compilerContext); ChainContainer materialize(MplProgram program); @Nullable CommandChain visitProcess(MplProgram program, MplProcess process);  }
@Test public void test_the_first_command_of_a_repeat_process_is_repeating() { List<MplCommand> mplCommands = listOf(several(), $MplCommand() .withConditional($oneOf(UNCONDITIONAL, CONDITIONAL))); MplProcess process = some($MplProcess() .withRepeating(true) .withChainParts(mplCommands)); CommandChain result = visitProcess(process); Iterator<ChainLink> act = result.getCommands().iterator(); if (context.getOptions().hasOption(TRANSMITTER)) { assertThatNext(act).isSkip(); } assertThatNext(act).hasMode(REPEAT).doesNeedRedstone(); }
@Test public void test_an_impulse_process_ends_with_notify() throws Exception { List<MplCommand> mplCommands = listOf(several(), $MplCommand() .withConditional($oneOf(UNCONDITIONAL, CONDITIONAL))); MplProcess process = some($MplProcess() .withRepeating(false) .withChainParts(mplCommands)); CommandChain result = visitProcess(process); Iterator<ChainLink> act = result.getCommands().iterator(); assertThatNext(act).isJumpDestination().isNotInternal(); Iterator<MplCommand> exp = mplCommands.iterator(); assertMatches(act, exp); assertThatNext(act).isUnconditionalNotify(process.getName()).isNotInternal(); assertThat(act).isEmpty(); assertThat(exp).isEmpty(); }
@Test public void test_a_process_with_tags_results_in_a_chain_with_tags() throws Exception { List<String> tags = some($listOf(several(), $String())); MplProcess process = some($MplProcess().withTags(tags)); CommandChain result = visitProcess(process); assertThat(result.getTags()).containsExactlyElementsOf(tags); }
@Test public void test_an_inline_process_is_ignored() throws Exception { MplProcess process = some($MplProcess().withType(INLINE)); CommandChain result = visitProcess(process); assertThat(result).isNull(); }
@Test public void test_a_repeat_process_uses_a_repeat_command_block() { MplCommand first = some($MplCommand().withConditional(UNCONDITIONAL)); MplCommand second = some($MplCommand().withConditional($oneOf(UNCONDITIONAL, CONDITIONAL))); MplProcess process = some($MplProcess() .withRepeating(true) .withChainParts(listOf(first, second))); CommandChain result = visitProcess(process); Iterator<ChainLink> it = result.getCommands().iterator(); if (context.getOptions().hasOption(TRANSMITTER)) assertThat(it.next()).isSkip().isNotInternal(); assertThat(it.next()).hasMinecraftCommand(first.getMinecraftCommand()).hasModifiers(REPEAT); assertThat(it.next()).matches(second); assertThat(it).isEmpty(); }
@Test public void test_invert_modifier_referenziert_den_richtigen_mode() { MplCommand first = some($MplCommand().withConditional(UNCONDITIONAL)); MplCommand second = some($MplCommand().withConditional(INVERT)); MplProcess process = some($MplProcess() .withRepeating(false) .withChainParts(listOf(first, second))); CommandChain result = visitProcess(process); Iterator<ChainLink> it = result.getCommands().iterator(); assertThatNext(it).isNotInternal().isJumpDestination(); assertThat(it.next()).matches(first); assertThat(it.next()).isInvertingCommandFor(first.getMode()); assertThat(it.next()).matches(second); assertThatNext(it).isNotInternal().isUnconditionalNotify(process.getName()); }
@Test public void test_Der_erste_invert_in_einem_repeating_process_referenziert_einen_repeating_command_block() { MplCommand first = some($MplCommand().withConditional(UNCONDITIONAL)); MplCommand second = some($MplCommand().withConditional(INVERT)); MplProcess process = some($MplProcess() .withRepeating(true) .withChainParts(listOf(first, second))); CommandChain result = visitProcess(process); Iterator<ChainLink> it = result.getCommands().iterator(); if (context.getOptions().hasOption(TRANSMITTER)) assertThat(it.next()).isSkip().isNotInternal(); assertThat(it.next()).hasMinecraftCommand(first.getMinecraftCommand()).hasModifiers(REPEAT); assertThat(it.next()).isInvertingCommandFor(REPEAT); assertThat(it.next()).matches(second); assertThat(it).isEmpty(); }
@Test public void test_If_with_one_Else_in_a_repeat_Process_results_in_an_Invert_which_references_a_Repeating_Command() { MplCommand else1 = some($MplCommand() .withConditional(UNCONDITIONAL)); MplIf mplIf = some($MplIf() .withNot(false) .withElseParts(listOf(else1))); MplProcess process = some($MplProcess() .withRepeating(true) .withChainParts(listOf(mplIf))); CommandChain result = visitProcess(process); Iterator<ChainLink> it = result.getCommands().iterator(); if (context.getOptions().hasOption(TRANSMITTER)) assertThat(it.next()).isSkip().isNotInternal(); assertThat(it.next()).isNotInternal().hasCommandParts(mplIf.getCondition()) .hasModifiers(REPEAT); assertThat(it.next()).isInvertingCommandFor(REPEAT); assertThat(it.next()).matchesAsConditional(else1); assertThat(it).isEmpty(); }
@Test public void test_If_not_with_one_Then_in_a_repeat_Process_results_in_an_Invert_which_references_a_Repeating_Command() { MplCommand then1 = some($MplCommand().withConditional(UNCONDITIONAL)); MplIf mplIf = some($MplIf() .withNot(true) .withThenParts(listOf(then1))); MplProcess process = some($MplProcess() .withRepeating(true) .withChainParts(listOf(mplIf))); CommandChain result = visitProcess(process); Iterator<ChainLink> it = result.getCommands().iterator(); if (context.getOptions().hasOption(TRANSMITTER)) assertThat(it.next()).isSkip().isNotInternal(); assertThat(it.next()).isNotInternal().hasCommandParts(mplIf.getCondition()) .hasModifiers(REPEAT); assertThat(it.next()).isInvertingCommandFor(REPEAT); assertThat(it.next()).matchesAsConditional(then1); assertThat(it).isEmpty(); }