Discord Group for development / help
The original goal of StateNet was to create a simple way to define and control the flow through pages of an application. Since its inception the library has grown versatile with many usecases.
StateNet works by defining a network of states interlinked by the inputs that can be applied to them.
List all of the states your application needs and consider the relationship between them to determine the inputs for your system. Create a connection by assigning a weight to each input that can be applied to a state. There are three approaches to configure the network:
Using the built in network creation tool you can graphically create / modify networks saved as Json files.
- Create a class which derives from 'Network'
- Define each State as a property on the class
- Use attributes on the State properties to define the relationships between them
public class CustomNetwork : Network
{
[StartState("Start")]
[Connection("Left", "D1")]
[Connection("Right", "D2")]
public State StartTestState { get; set; }
[StateName("D1")]
[Connection("Next", "D1", "StateVisitCount(\"D2\") < 2 ? 1 : 0")]
[Connection("Next", "End", "StateVisitCount(\"D2\") >= 2 ? 1 : 0")]
public State Decision1TestState { get; set; }
[StateName("D2")]
[Connection("Next", "D1", "StateVisitCount(\"D2\") > 2 ? 1 : 0")]
[Connection("Next", "D2", "StateVisitCount(\"D2\") <= 2 ? 1 : 0")]
public State Decision2TestState { get; set; }
[StateName("End")]
public State EndTestState { get; set; }
}
IStateNetwork stateNetwork = new StateNetwork();
var networkEditor = new StateNetworkEditor(stateNetwork);
networkEditor.SetStart("ready");
networkEditor.Always("ready", "Play", "playing");
networkEditor.Always("ready", "Stop", "stopped");
networkEditor.Always("playing", "Pause", "paused");
networkEditor.Always("playing", "Stop", "stopped");
networkEditor.Always("paused", "Play", "playing");
networkEditor.Always("paused", "Stop", "stopped");
Pass the configured network to the engine through its constructor. Optionally subscribe to the engines events such as OnStarted, OnFinished, OnTransition or subscribe to specific State transitions. Control the flow through the network by calling Start(), Stop() and Apply(action)
//Create and configure the Network using your prefered method
var network = ...
//Create the Engine
var engine = new Engine(network);
//Subscribe to the engines events
engine.OnFinished += (sender) => { ... }
engine.OnTransition += (sender) => { ... }
engine.Subscribe(network["Playing"], () => { ... });
//Start the Engine
engine.Start();
//Apply actions to move through the states
engine.Apply("Play");
...
engine.Apply("Stop");
MIT License