s_mach: A Scala open-source state machine library

Versions

  • Scala 2.11.0
  • SBT 0.13.1

Introduction

s_mach1 is an open-source Scala library designed to support creation of functional, composable, streamable and recoverable state machines. s_mach state machines can be composed together to form larger state machines or connected together to form a processing pipeline plan that can be run to produce a final value. s_mach state machines also support a generic error recovery system (by way of the Halted state) that may be used to report errors encountered during processing by any composed or connected s_mach state machine. Additionally, when an error is encountered, s_mach state machines may provide an optional recovery method for fixing or ignoring broken input. While running a composed or connected s_mach state machine, the client is provided with the error message and empowered with the decision of whether to proceed with processing despite any errors.

In s_mach, a state machine is defined as an object that has an initial State, s0:

A State is a class that represents the state of a state machine at some point in processing. The State class is never instantiated directly, instead one of the three basic sub-types are created:

  1. Continuation: represents the state of a state machine that requires more input to continue processing.
  2. Success: represents the state of a state machine that has completed processing and which resulted in a single value.
  3. Halted: represents the state of a state machine that has performed some processing but did not successfully complete that processing and which might be recoverable

Transitions

When input is applied to a Continuation, a Transition derived object is created which has the next state of the state machine and any output produced by processing the input by the Continuation:

The DoneTransition is a sub-type of Transition returned when EndOfInput (EOI) is applied to a Continuation state. A DoneTransition state can only return Done states: Halted or Success. A DoneTransition may also have overflow, input that was not consumed during processing.

Each of the basic State derived types has a corresponding Transition derived type:

Note
Transition derived objects are verbs and their State derived equivalents are nouns.

Type-parameters

All s_mach state machine classes ( StateMachine, State, Transition, etc) accept three type-parameters:

  1. I: The type of input consumed by the machine during processing (may be Unit)
  2. O: The type of output produced by the machine after each transition (may be Unit)
  3. A: The final value type produced by the machine once processing has completed successfully (also may be Unit)

StateMachine instances are always connected input to output, with the first state machine typically accepting Unit. Only the final value type of the last state machine is passed on when connecting StateMachine instances:

Type-Aliases

To make working with s_mach state machines easier, type-aliases are provided based on the state machine’s purpose:

An Enumerator produces output in chunks by stepping the enumerator:

An Iteratee consumes input in chunks to eventually yield a final single value:

A Transformer transforms input chunks into output chunks of the same or a different type:

A Plan to stream input from an Enumerator to an Iteratee by way of 0 or more Transformers and eventually produce a final single value:

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">