@@ -19,10 +19,10 @@ public partial class StateMachine
1919
2020 private Dictionary < string , Data > _data = new Dictionary < string , Data > ( ) ;
2121
22- internal string _currentState ;
22+ public string CurrentStateName { get ; private set ; }
2323
24- internal string _previousState ;
25- internal string _currentTransition { get ; private set ; }
24+ public string PreviousStateName { get ; private set ; }
25+ public string CurrentTransitionName { get ; private set ; }
2626
2727 internal string _nextTransition ;
2828 internal string _startState { get ; private set ; }
@@ -33,10 +33,9 @@ public partial class StateMachine
3333
3434 internal Action < State , State > _onChangeState ;
3535
36- public State CurrentState { get { return GetState ( _currentState ) ; } }
37- public Transition CurrentTransition { get { return GetTransition ( _currentTransition ) ; } }
38-
39-
36+ public State CurrentState { get { return _GetState ( CurrentStateName , out _ , true , false ) ; } }
37+ public Transition CurrentTransition { get { return _GetTransition ( CurrentTransitionName , out _ , true , false ) ; } }
38+ public State PreviousState { get { return _GetState ( PreviousStateName , out _ , true , false ) ; } }
4039
4140
4241
@@ -58,66 +57,69 @@ public StateMachine(ILogger logger=null)
5857
5958 public StateMachine ( XDocument xDocument , ILogger logger = null ) : this ( logger )
6059 {
61- FromXDocument ( this , xDocument ) ;
60+ _FromXDocument ( this , xDocument , true ) ;
6261
6362 }
6463
6564 public StateMachine ( string xDocumentPath , ILogger logger = null ) : this ( logger )
6665 {
67- FromXDocument ( this , xDocumentPath ) ;
66+ _FromXDocument ( this , xDocumentPath , true ) ;
6867 }
6968
7069 public StateMachine OnChangeState ( Action < State , State > actionOnChangeState )
7170 {
7271 _onChangeState += actionOnChangeState ;
73- _logger ? . LogDebug ( "Method \" {NameMethod}\" subscribe on change state State Machine" , actionOnChangeState . Method . Name ) ;
72+ _logger . LogDebug ( "Method \" {NameMethod}\" subscribe on change state for State Machine" , actionOnChangeState . Method . Name ) ;
7473 return this ;
7574 }
7675
7776 public State SetStartState ( State state )
7877 {
79- _startState = _StateExists ( state . Name , out _ , true ) ;
78+ _startState = _StateExists ( state . Name , out _ , true , false ) ;
8079
81- _logger ? . LogDebug ( "State \" {NameState}\" set as start" , state . Name ) ;
80+ _logger . LogDebug ( "State \" {NameState}\" set as start" , state . Name ) ;
8281
8382 return state ;
8483 }
8584
8685 public State SetStartState ( string stateName )
8786 {
88- State state = GetState ( stateName ) ;
87+ State state = _GetState ( stateName , out bool result , true , false ) ;
8988 _startState = state . Name ;
9089
91- _logger ? . LogDebug ( "State \" {NameState}\" set as start" , stateName ) ;
90+ if ( result )
91+ _logger . LogDebug ( "State \" {NameState}\" set as start" , stateName ) ;
9292
9393 return state ;
9494 }
9595
9696 public InvokeParameters InvokeTransition ( string nameTransition , Dictionary < string , object > parameters = null )
9797 {
98- _nextTransition = TransitionExists ( nameTransition , out _ ) ;
98+ _nextTransition = _TransitionExists ( nameTransition , out _ , true , false ) ;
9999
100- CheckBeforeInvoke ( this . _logger ) ;
100+ _CheckBeforeInvoke ( this . _logger , true ) ;
101101
102102 InvokeParameters invokeParameters = new InvokeParameters ( this ) ;
103103 if ( parameters != null )
104104 invokeParameters . AddParameters ( parameters ) ;
105105 return invokeParameters ;
106106 }
107107
108- private void CheckBeforeInvoke ( ILogger logger )
108+ internal void _CheckBeforeInvoke ( ILogger logger , bool withLog )
109109 {
110- Transition transition = GetTransition ( _nextTransition ) ;
111- if ( transition . StateFrom != _currentState )
110+ Transition transition = _GetTransition ( _nextTransition , out _ , true , false ) ;
111+ if ( transition . StateFrom != CurrentStateName )
112112 {
113- object [ ] args = { _nextTransition , _currentState } ;
113+ object [ ] args = { _nextTransition , CurrentStateName } ;
114114 string message = "Transition \" {0}\" not available from state \" {0}\" " ;
115115 var exception = new ArgumentException ( message : String . Format ( message , args ) ) ;
116- _logger ? . LogError ( exception , message , args ) ;
116+ _logger . LogError ( exception , message , args ) ;
117117
118118 throw exception ;
119119 }
120- _logger ? . LogDebug ( "Transition \" {NameTransition}\" set as next" , _nextTransition ) ;
120+
121+ if ( withLog )
122+ _logger . LogDebug ( "Transition \" {NameTransition}\" set as next" , _nextTransition ) ;
121123 }
122124
123125 public InvokeParameters InvokeTransition ( Transition transition , Dictionary < string , object > parameters = null )
@@ -126,84 +128,89 @@ public InvokeParameters InvokeTransition(Transition transition, Dictionary<strin
126128 }
127129
128130
129- private StateMachine InvokeTransition ( )
131+ internal StateMachine _InvokeTransition ( )
130132 {
131133
132134 //Mark nextParameters as current
133135 _currentParameters = _nextParameters ;
134136 _nextParameters = null ;
135137
136138 //Mark nextTransition as current
137- _currentTransition = _nextTransition ;
139+ CurrentTransitionName = _nextTransition ;
138140 _nextTransition = null ;
139141
140142 //Mark currentState as previous
141- _previousState = _currentState ;
142- _currentState = null ;
143+ PreviousStateName = CurrentStateName ;
144+ CurrentStateName = null ;
143145
144- Transition currentTransition = GetTransition ( _currentTransition ) ;
145- currentTransition . Invoking ( _currentParameters ) ;
146- _currentState = currentTransition . StateTo ;
147- _currentTransition = null ;
146+ Transition currentTransition = _GetTransition ( CurrentTransitionName , out _ , true , false ) ;
147+ currentTransition . _Invoking ( _currentParameters ) ;
148+ CurrentStateName = currentTransition . StateTo ;
149+ CurrentTransitionName = null ;
148150
149151 return this ;
150152 }
151153
152- private StateMachine ChangeState ( )
154+ internal StateMachine _ChangeState ( )
153155 {
154- State currentState = GetState ( _currentState ) ;
155- currentState . Entry ( _currentParameters ) ;
156+ State currentState = _GetState ( CurrentStateName , out bool result , true , false ) ;
157+ currentState . _Entry ( _currentParameters , true ) ;
156158 State previousState = null ;
157- object [ ] obj = { _previousState , _currentState } ;
159+ List < object > obj = new List < object > ( ) ;
158160 string message ;
159- if ( string . IsNullOrEmpty ( _previousState ) )
161+
162+ if ( string . IsNullOrEmpty ( PreviousStateName ) )
160163 {
161- message = "State \" {StateNew}\" was set" ;
164+ obj . Add ( CurrentStateName ) ;
165+ message = "State \" {StateNew}\" was set" ;
162166 }
163167 else
164168 {
169+ obj . Add ( PreviousStateName ) ;
170+ obj . Add ( CurrentStateName ) ;
165171 message = "State \" {StateOld}\" change on \" {StateNew}\" " ;
166- previousState = GetState ( _previousState ) ;
167- }
172+ previousState = _GetState ( PreviousStateName , out _ , true , false ) ;
173+ }
174+
168175 _onChangeState ? . Invoke ( previousState , currentState ) ;
169- _logger ? . LogDebug ( message , obj ) ;
170- currentState . Exit ( _currentParameters ) ;
176+ _logger . LogDebug ( message , obj . ToArray ( ) ) ;
177+ currentState . _Exit ( _currentParameters , true ) ;
171178
172179 return this ;
173180 }
174181
175- private void CheckStartState ( )
182+ internal void _CheckStartState ( )
176183 {
177184 string message ;
178185 if ( string . IsNullOrEmpty ( _startState ) )
179186 {
180187 message = "Start state not set" ;
181188 var exception = new NullReferenceException ( message : message ) ;
182- _logger ? . LogError ( exception , message ) ;
189+ _logger . LogError ( exception , message ) ;
183190 throw exception ;
184191 }
185- _startState = _StateExists ( _startState , out _ , true ) ;
186- _currentState = _startState ;
192+ _startState = _StateExists ( _startState , out _ , true , false ) ;
193+ CurrentStateName = _startState ;
187194 }
188195
189196 public void Start ( Dictionary < string , object > startParameters = null )
190197 {
191- CheckStartState ( ) ;
198+ _CheckStartState ( ) ;
192199
193- _logger ? . LogDebugAndInformation ( "Start work state machine" ) ;
200+ _logger . LogInformation ( "Start work state machine" ) ;
194201 _currentParameters = startParameters ;
195202
196- ChangeState ( ) ;
203+ _ChangeState ( ) ;
197204
198205 while ( _nextTransition != null )
199206 {
200- InvokeTransition ( ) ;
207+ _InvokeTransition ( ) ;
201208
202- ChangeState ( ) ;
209+ _ChangeState ( ) ;
203210 }
204- _logger ? . LogDebugAndInformation ( "End work state machine" ) ;
211+ _logger . LogInformation ( "End work state machine" ) ;
205212
206213 }
207-
214+
208215 }
209216}
0 commit comments