Skip to content

softwaretechnica/machinesy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Category overview screenshot

machinesy - Simple finite state machine in c#

Example: Car simualtion

First, we need to define the possible car states that we are interested to simulate. This can differ from case to case.

public enum CarState { Off, On, Start, Drive, Stop }

Second, we need to define events that will trigger certain state transitions. For this case, we have chosen the following ones.

public enum CarStateEvent { TurnOn, TurnOff, Acceleerate, Break }

The CarSimulator class is the acutal simulator that can be used to wrap up the state machine and privide a way to control the class states in a simple way.

  public class CarSimulator
  {
    private StateMachine<CarState, CarStateEvent> _stateMachine;

    public CarSimulator()
    {
        _stateMachine = new StateMachine<CarState, CarStateEvent>(CarState.Off); 
        LoadTransitions();
    }

    private void LoadTransitions()
    { 
        _stateMachine.AddTransition(CarState.Off, CarStateEvent.TurnOn, () => _stateMachine.CurrentState == CarState.Off, CarState.On, () => { });
        _stateMachine.AddTransition(CarState.Off, CarStateEvent.Break, () => _stateMachine.CurrentState == CarState.Off, CarState.Off, () => { });
        _stateMachine.AddTransition(CarState.On, CarStateEvent.Acceleerate, () => _stateMachine.CurrentState == CarState.On, CarState.Drive, () => { }); 
        _stateMachine.AddTransition(CarState.Drive, CarStateEvent.Break, () => _stateMachine.CurrentState == CarState.Drive, CarState.Stop, () => { });
        _stateMachine.AddTransition(CarState.On, CarStateEvent.TurnOff, () => _stateMachine.CurrentState == CarState.On, CarState.Off, () => { });
        _stateMachine.AddTransition(CarState.Stop, CarStateEvent.TurnOff, () => true, CarState.Off, () => { });
     }

        public void TurnOn()
        {
            _stateMachine.HandleEvent(CarStateEvent.TurnOn);
        }

        public void TurnOff()
        {
            _stateMachine.HandleEvent(CarStateEvent.TurnOff);
        }

        public void Accelerate()
        {
            _stateMachine.HandleEvent(CarStateEvent.Acceleerate);
        }

        public void Break()
        {
            _stateMachine.HandleEvent(CarStateEvent.Break);
        }

        public void Stop()
        {
            _stateMachine.HandleEvent(CarStateEvent.Break);
        }
    }

About

Machinesy is an attempt to provide a small robust state machine implementation. Machinesy should be easy to understand and to feed with states.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages