22using System . Xml . Linq ;
33using System ;
44using SimpleStateMachineLibrary . Helpers ;
5+ using System . Data . Common ;
6+ using System . Linq ;
7+ using Microsoft . Extensions . Logging . Abstractions ;
8+ using Microsoft . Extensions . Logging ;
59
610namespace SimpleStateMachineLibrary
711{
812 public partial class StateMachine
913 {
14+
1015 private Dictionary < string , State > _states = new Dictionary < string , State > ( ) ;
1116
1217 private Dictionary < string , Transition > _transitions = new Dictionary < string , Transition > ( ) ;
1318
1419 private Dictionary < string , Data > _data = new Dictionary < string , Data > ( ) ;
1520
21+ internal ILogger _logger ;
22+
23+ public ILogger SetILogger ( ILogger logger )
24+ {
25+ _logger = logger ?? NullLogger . Instance ;
26+
27+ return logger ;
28+ }
29+
1630 public State CurrentState { get ; private set ; }
1731
1832 public State PreviousState { get ; private set ; }
@@ -21,62 +35,78 @@ public partial class StateMachine
2135
2236 public State StartState { get ; private set ; }
2337
24- public StateMachine OnChangeState ( Action < State , State > actionOnChangeState )
25- {
26- _onChangeState += actionOnChangeState ;
27-
28- return this ;
29- }
3038
31- public StateMachine ( )
39+ public StateMachine ( ILogger logger = null )
3240 {
33-
41+ SetILogger ( logger ) ;
3442 }
3543
36- public StateMachine ( XDocument xDocument )
44+ public StateMachine ( XDocument xDocument , ILogger logger = null )
3745 {
46+ SetILogger ( logger ) ;
3847 FromXDocument ( this , xDocument ) ;
48+
3949 }
4050
41- public StateMachine ( string xDocumentPath )
51+ public StateMachine ( string xDocumentPath , ILogger logger = null )
4252 {
53+ SetILogger ( logger ) ;
4354 FromXDocument ( this , xDocumentPath ) ;
4455 }
4556
46- private Transition _nextTransition { get ; set ; }
4757
48- private Dictionary < string , object > _currentParameters { get ; set ; }
58+ internal Transition _nextTransition ;
4959
50- private Dictionary < string , object > _nextParameters { get ; set ; }
60+ internal Dictionary < string , object > _currentParameters ;
5161
52- private Action < State , State > _onChangeState ;
62+ internal Dictionary < string , object > _nextParameters ;
63+
64+ internal Action < State , State > _onChangeState ;
65+
66+
67+ public StateMachine OnChangeState ( Action < State , State > actionOnChangeState )
68+ {
69+ _onChangeState += actionOnChangeState ;
70+
71+ return this ;
72+ }
5373
5474 public State SetStartState ( State state )
5575 {
5676 StartState = state ;
5777 return state ;
5878 }
59-
79+
6080 public State SetStartState ( string stateName )
6181 {
6282 StartState = State ( stateName ) ;
6383 return StartState ;
6484 }
6585
66- public void InvokeTransition ( string nameTransition )
86+ public InvokeParameters InvokeTransition ( string nameTransition )
6787 {
6888 _nextTransition = Check . GetElement ( _transitions , nameTransition , true ) ;
89+
90+ if ( _nextTransition . StateTo != CurrentState )
91+ {
92+ throw new ArgumentException ( message : $ "{ nameTransition } not available from { CurrentState ? . Name } " ) ;
93+ }
94+
95+ return new InvokeParameters ( this ) ;
6996 }
7097
71- public void InvokeTransitionWithParameters ( string nameTransition , Dictionary < string , object > parameters )
98+ public InvokeParameters InvokeTransitionWithParameters ( string nameTransition , Dictionary < string , object > parameters )
7299 {
73100 InvokeTransition ( nameTransition ) ;
74101
75102 _nextParameters = parameters ;
103+
104+ return new InvokeParameters ( this ) ;
76105 }
77106
78107 private StateMachine InvokeTransition ( )
79108 {
109+
80110 //Mark nextParameters as current
81111 _currentParameters = _nextParameters ;
82112 _nextParameters = null ;
@@ -108,6 +138,11 @@ private StateMachine ChangeState()
108138
109139 public void Start ( Dictionary < string , object > startParameters = null )
110140 {
141+ if ( StartState == null )
142+ {
143+ throw new NullReferenceException ( message : "Start state don't set" ) ;
144+ }
145+
111146 CurrentState = StartState ;
112147 _currentParameters = startParameters ;
113148
0 commit comments