Nt.Automaton
1.3.1
dotnet add package Nt.Automaton --version 1.3.1
NuGet\Install-Package Nt.Automaton -Version 1.3.1
<PackageReference Include="Nt.Automaton" Version="1.3.1" />
<PackageVersion Include="Nt.Automaton" Version="1.3.1" />
<PackageReference Include="Nt.Automaton" />
paket add Nt.Automaton --version 1.3.1
#r "nuget: Nt.Automaton, 1.3.1"
#:package Nt.Automaton@1.3.1
#addin nuget:?package=Nt.Automaton&version=1.3.1
#tool nuget:?package=Nt.Automaton&version=1.3.1
Nt.Automaton
Introduction
Nt.Automaton is an automate containing a set of states and transitions between those states. Each state or transition can be linked to an action, so that any structure or information for the user can be processed from any sequences. The entire automaton project is made for a generic type. From string to complex structures, there are possibility to read almost any data.
Applications of this library include:
- Parsing structured text formats (used for example in compilers or reading datas from files)
- Updating user interfaces based on input or events
- Behavior of entities (like PNJ in a video game or other simulations)
- Any kind of workflow or any other state-based logic
Features
- Easily define states and transitions for an automate.
- Link actions to states and transitions.
- Iterate over a list of tokens and process them according to the defined automate.
- Easily extendable for custom behavior.
- Lightweight and efficient.
Installation
Install the NuGet package Nt.Automaton in your project from nuget.org:
dotnet add package Nt.Automaton --version 1.3.0
or add the following line in your project file:
<PackageReference Include="Nt.Automaton" Version="1.3.0" />
Using an automaton
Creating a token
The automaton functions by processing tokens that implement the IAutomatonToken interface.
Such interface only requires a Name property with a public getter, which represents the value of the token and can be of any type.
Example of the already implemented token:
public class AutomatonToken<T>(T name) : IAutomatonToken<T>
{
public T Name { get; } = name;
}
// you need to specify the type of the token, for example AutomatonToken<string>
The advantage of IAutomatonToken is that you can extend any existing class to implement it, and then transform them into automaton tokens (without having to create a new class for tokens).
Defining actions
When a state is reached or a transition is taken, an action can be executed. These actions can be whatever you prefer and are yours to implement.
An action must implement the IAction interface which requires a single method Perform(IAutomatonToken token) where token is the current token being processed by the automaton.
Example of a custom action implementation:
using Nt.Automaton.Actions;
public class MyAction : IAction<string> // or int, char...
{
public void Perform(IAutomatonToken<string> token)
{
// Your action logic here
}
}
Creating states and transitions
A state represents a specific condition or situation in the automate. The specificity of Nt.Automaton is the possibility to declare actions linked to states or transitions.
The base structure you will need is an instance of StateAutomaton, which contains states and transitions.
This state automaton is an implementation of IAutomaton with one current state at a time. See below for more automatons.
You'll need to define an initial state to initialize the automaton, and then you can add states and transitions as needed.
- Create a default state
using Nt.Automaton.States;
var defaultState = new State();
- Create an automaton with the default state
using Nt.Automaton;
var automaton = new StateAutomaton(defaultState);
- Add states and transitions
using Nt.Automaton.States;
// Define the tokens used in the automaton
var tokenA = new MyToken("A");
var tokenB = new MyToken("B");
// Define the actions that are triggered
var action = new MyAction();
// Create a new state
var stateA = new State();
// Create a new state with an action to trigger when entering the state
var stateB = new State().SetAction(new MyAction());
// Mark the state as a final node
var stateC = new State().SetFinal();
// Add a default state to transfer to when no transition is valid
stateA.SetDefault(new Transition(stateB));
// Add a default state with an action to trigger when transferring to the default state
stateB.SetDefault(new Transition(stateC).SetAction(action));
// Add a transition from stateA to stateB when the token read is "B"
stateA.AddTransition(new Transition(tokenB, stateB)));
// Add a transition from stateB to stateA with an action when the token read is "A"
stateB.AddTransition(new Transition(tokenA, stateA).SetAction(action));
Customising the automaton
A particularity of Nt.Automaton is that components are largely customisable, including the automatons. This project includes some implementations for quick use like StateAutomaton, but feel free to create your own implementations at any time by extending the IAutomaton interface.
Two implementations already exists:
Nt.Automaton.Automatons.StateAutomaton: Ideal for a single state automaton, where only one state is active at a time.Nt.Automaton.Automatons.StackAutomaton: Ideal for a single state automaton that must handle an historic of the states encountered
See the automatons documentation for more details.
Custom states
By extending the IState interface, it is possible to create other types of states than the default one State.
See the states documentation for more details.
Custom transitions
Similarly, you can declare your own transitions by extending the ITransition interface.
See the transitions documention for more details.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Nt.Automaton:
| Package | Downloads |
|---|---|
|
Nt.SyntaxParser
Library containing tools for parsing grammars and interpreting custom syntaxes. |
GitHub repositories
This package is not used by any popular GitHub repositories.
# Bug fixes
- Add author name in the project metadata
- Fix syntax issues in worflow