Class Episodes
java.lang.Object
org.machanism.machai.gw.processor.Episodes
Maintains an ordered collection of act episode prompts and provides execution
helpers that support several playback strategies.
Supported functionality:
- Holds an ordered list of episode prompts, each optionally starting with a
markdown heading (
"# Name") that names the episode. - Executes episodes in their natural order via
regularOrder(Integer, BiFunction), honoring repeat requests (RepeatEpisodeException) and jump/redirect requests (MoveToEpisodeException). - Executes an explicitly selected subset of episodes in the requested order
via
requestedOrder(BiFunction). - Resolves episode indices either by 1-based ID or by heading name when a
MoveToEpisodeExceptionrequests a jump to a named episode. - Exposes act/episode metadata for reporting via
getActInformation(int).
Example: regular order execution
Episodes episodes = new Episodes(actProcessor);
episodes.setName("demo-act");
episodes.setEpisodes(List.of(
"# Introduction\nWelcome to the show!",
"# Recap\nLast time on our show..."));
episodes.regularOrder(1, (id, prompt) -> executor.run(id, prompt));
Example: executing only a selected subset
episodes.setSelectedEpisodes(List.of(2));
if (!episodes.isRegularOrder()) {
episodes.requestedOrder((id, prompt) -> executor.run(id, prompt));
}
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate ActProcessorProcessor that receives the result produced by each completed episode.Ordered list of act episode prompts to execute.private static final StringPrefix that identifies a first-level Markdown episode heading.private static final org.slf4j.LoggerLogger for documentation input processing events.private StringLogical act name associated with the episodes.Explicitly selected 1-based episode identifiers. -
Constructor Summary
ConstructorsConstructorDescriptionEpisodes(ActProcessor actProcessor) Creates an episode collection whose execution results are recorded by the supplied processor. -
Method Summary
Modifier and TypeMethodDescriptionprivate booleanexecuteEpisode(int episodeId, int iteration, BiFunction<Integer, String, String> func) Runs one iteration of an episode and records its result when completed.private voidexecuteEpisodeWithRepeats(int episodeId, BiFunction<Integer, String, String> func) Executes an episode repeatedly until its callback completes without asking for another iteration.private IntegerexecuteRegularEpisodes(int startEpisodeId, BiFunction<Integer, String, String> func) Executes consecutive episodes until completion or a move request changes the next episode to execute.getActInformation(int episodeId) Builds metadata describing every configured episode and the current episode.getEpisodeId(Integer requestedEpisodeId, MoveToEpisodeException exception) Resolves the next episode index from a move request exception.private intgetEpisodeIdByName(String episodeName) Finds the 1-based identifier of the episode with the supplied heading.private StringgetEpisodeName(int episodeId) Extracts an episode's first-level Markdown heading, excluding optional YAML-style front matter.Returns the ordered episode prompts.getName()Returns the logical name associated with this act.booleanDetermines whether all episodes should execute in their natural order.private voidlogEpisodeHeader(int episodeId, int iteration, String msg) Logs a visual boundary around an episode execution when episode or iteration information is useful.private voidLogs a nonblank execution result using the standard output prefix.voidregularOrder(Integer startEpisodeId, BiFunction<Integer, String, String> func) Executes episodes in regular order starting from the supplied 1-based index while honoring repeat and move requests.intrequestedOrder(BiFunction<Integer, String, String> func) Executes only the explicitly selected episodes in their requested order.voidsetEpisodes(List<String> episodes) Replaces the ordered prompts available for execution.voidAssigns the logical name associated with this act.voidsetSelectedEpisodes(List<Integer> selectedEpisodeIds) Sets the list of explicitly requested episode identifiers.intsize()Returns the number of configured episode prompts.
-
Field Details
-
HEADER_MARKER
Prefix that identifies a first-level Markdown episode heading.- See Also:
-
logger
private static final org.slf4j.Logger loggerLogger for documentation input processing events. -
episodePrompts
Ordered list of act episode prompts to execute. -
selectedEpisodes
Explicitly selected 1-based episode identifiers. -
name
Logical act name associated with the episodes. -
actProcessor
Processor that receives the result produced by each completed episode.
-
-
Constructor Details
-
Episodes
Creates an episode collection whose execution results are recorded by the supplied processor.- Parameters:
actProcessor- processor that receives completed episode results
-
-
Method Details
-
setSelectedEpisodes
Sets the list of explicitly requested episode identifiers.- Parameters:
selectedEpisodeIds- 1-based episode identifiers to execute- Throws:
IllegalArgumentException- if any identifier is outside the available episode range
-
getEpisodeIdByName
Finds the 1-based identifier of the episode with the supplied heading.- Parameters:
episodeName- heading name to locate- Returns:
- the matching 1-based episode identifier
- Throws:
EpisodeNotFoundException- if no episode has the requested heading
-
getEpisodeName
Extracts an episode's first-level Markdown heading, excluding optional YAML-style front matter.- Parameters:
episodeId- 1-based identifier of the episode to inspect- Returns:
- the normalized heading text, or
nullwhen no heading exists - Throws:
IndexOutOfBoundsException- if the identifier does not address an episode
-
regularOrder
Executes episodes in regular order starting from the supplied 1-based index while honoring repeat and move requests.- Parameters:
startEpisodeId- starting 1-based episode indexfunc- callback used to execute an episode- Throws:
IndexOutOfBoundsException- if a requested episode index is invalid
-
executeRegularEpisodes
Executes consecutive episodes until completion or a move request changes the next episode to execute.- Parameters:
startEpisodeId- 1-based identifier at which execution beginsfunc- callback used to execute each episode- Returns:
- the requested destination after a move, or
nullon completion - Throws:
IndexOutOfBoundsException- if an episode identifier is invalidEpisodeNotFoundException- if a named move destination does not exist
-
requestedOrder
Executes only the explicitly selected episodes in their requested order.- Parameters:
func- callback used to execute an episode- Returns:
- the last processed episode identifier, or
0when none are selected - Throws:
IndexOutOfBoundsException- if a selected episode identifier is invalid
-
executeEpisodeWithRepeats
Executes an episode repeatedly until its callback completes without asking for another iteration.- Parameters:
episodeId- 1-based identifier of the episode to executefunc- callback used to execute the episode- Throws:
IndexOutOfBoundsException- if the identifier does not address an episode
-
executeEpisode
private boolean executeEpisode(int episodeId, int iteration, BiFunction<Integer, String, String> func) Runs one iteration of an episode and records its result when completed.- Parameters:
episodeId- 1-based identifier of the episode to executeiteration- current execution iteration, starting at1func- callback used to execute the episode- Returns:
truewhen the iteration completed, orfalsewhen it requested a repeat- Throws:
IndexOutOfBoundsException- if the identifier does not address an episode
-
logResult
Logs a nonblank execution result using the standard output prefix.- Parameters:
perform- result returned by an episode callback
-
getEpisodeId
Resolves the next episode index from a move request exception.- Parameters:
requestedEpisodeId- current fallback episode indexexception- exception describing the requested move- Returns:
- resolved 1-based episode index
- Throws:
EpisodeNotFoundException- if the requested episode name does not exist
-
logEpisodeHeader
Logs a visual boundary around an episode execution when episode or iteration information is useful.- Parameters:
episodeId- 1-based identifier of the episode being loggediteration- current execution iterationmsg- boundary label, such asStartorEnd- Throws:
IndexOutOfBoundsException- if the identifier does not address an episode
-
setEpisodes
Replaces the ordered prompts available for execution.- Parameters:
episodes- ordered list of episode prompts
-
getEpisodes
Returns the ordered episode prompts.- Returns:
- ordered list of episode prompts
-
isRegularOrder
public boolean isRegularOrder()Determines whether all episodes should execute in their natural order.- Returns:
trueif no explicit episode selection exists; otherwisefalse
-
size
public int size()Returns the number of configured episode prompts.- Returns:
- number of configured episodes
-
getActInformation
Builds metadata describing every configured episode and the current episode.- Parameters:
episodeId- 1-based identifier of the current episode- Returns:
- map containing episode metadata and the current episode identifier
-
getName
Returns the logical name associated with this act.- Returns:
- act name, or
nullwhen no name has been assigned
-
setName
Assigns the logical name associated with this act.- Parameters:
name- act name to assign
-