Skip to content

Commit cd78e57

Browse files
committed
new format
1 parent d54dd8e commit cd78e57

File tree

4 files changed

+53
-45
lines changed

4 files changed

+53
-45
lines changed

SimpleStateMachineLibrary/Data/Data.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public Data OnChange(Action<Data, object, object> actionOnChange)
2424

2525
return this;
2626
}
27+
2728
protected internal Data(StateMachine stateMachine, string nameData, object valueData = null) : base(stateMachine, nameData)
2829
{
2930
Value = valueData;

SimpleStateMachineLibrary/SimpleStateMachineLibrary.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,10 @@
44
<TargetFramework>netstandard2.1</TargetFramework>
55
</PropertyGroup>
66

7+
<ItemGroup>
8+
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.2" />
9+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.1.2" />
10+
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.1.2" />
11+
</ItemGroup>
12+
713
</Project>

SimpleStateMachineLibrary/StateMachines/StateMachine.cs

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ public partial class StateMachine
1313

1414
private Dictionary<string, Data> _data = new Dictionary<string, Data>();
1515

16-
public State CurrentState { get; internal set; }
16+
public State CurrentState { get; private set; }
1717

18-
public Transition CurrentTransition{ get; internal set; }
18+
public State PreviousState { get; private set; }
1919

20-
public State StartState { get; protected set; }
20+
public Transition CurrentTransition{ get; private set; }
2121

22-
private Action<State, State> _onChangeState;
22+
public State StartState { get; private set; }
2323

2424
public StateMachine OnChangeState(Action<State, State> actionOnChangeState)
2525
{
@@ -43,41 +43,22 @@ public StateMachine(string xDocumentPath)
4343
FromXDocument(this, xDocumentPath);
4444
}
4545

46-
4746
private Transition _nextTransition { get; set; }
4847

4948
private Dictionary<string, object> _currentParameters { get; set; }
5049

5150
private Dictionary<string, object> _nextParameters { get; set; }
5251

53-
private State _beforeState { get; set; }
54-
55-
56-
private void CheckStartState()
57-
{
58-
if (StartState != null)
59-
{
60-
throw new ArgumentException(String.Format("Start state already set. It's {0} ", StartState.Name));
61-
}
62-
}
63-
64-
private void CheckCurrentTransition()
65-
{
66-
if (CurrentTransition == null)
67-
{
68-
throw new ArgumentException(String.Format("State with name \"{0}\" doesn't invoke transition", CurrentState.Name));
69-
}
70-
}
52+
private Action<State, State> _onChangeState;
7153

7254
public State SetStartState(State state)
7355
{
74-
CheckStartState();
75-
return StartState = state;
56+
StartState = state;
57+
return state;
7658
}
7759

7860
public State SetStartState(string stateName)
7961
{
80-
CheckStartState();
8162
StartState = State(stateName);
8263
return StartState;
8364
}
@@ -94,31 +75,49 @@ public void InvokeTransitionWithParameters(string nameTransition, Dictionary<str
9475
_nextParameters = parameters;
9576
}
9677

97-
public void Start(Dictionary<string, object> startParameters = null)
78+
private StateMachine InvokeTransition()
9879
{
80+
//Mark nextParameters as current
81+
_currentParameters = _nextParameters;
82+
_nextParameters = null;
83+
84+
//Mark nextTransition as current
85+
CurrentTransition = _nextTransition;
86+
_nextTransition = null;
87+
88+
//Mark currentState as previous
89+
PreviousState = CurrentState;
90+
CurrentState = null;
91+
92+
93+
CurrentTransition.Invoke(_currentParameters);
94+
CurrentState = CurrentTransition.StateTo;
95+
CurrentTransition = null;
96+
97+
return this;
98+
}
99+
100+
private StateMachine ChangeState()
101+
{
102+
CurrentState.Entry(_currentParameters);
103+
_onChangeState?.Invoke(PreviousState, CurrentState);
104+
CurrentState.Exit(_currentParameters);
105+
106+
return this;
107+
}
108+
109+
public void Start(Dictionary<string, object> startParameters = null)
110+
{
99111
CurrentState = StartState;
100-
CurrentState.Entry(startParameters);
101-
_onChangeState?.Invoke(null, CurrentState);
102-
CurrentState.Exit(startParameters);
112+
_currentParameters = startParameters;
103113

114+
ChangeState();
104115

105116
while (_nextTransition != null)
106117
{
107-
_currentParameters = _nextParameters;
108-
_nextParameters = null;
109-
110-
CurrentTransition = _nextTransition;
111-
_nextTransition = null;
112-
113-
_beforeState = CurrentState;
114-
CurrentState = null;
115-
CurrentTransition.Invoke(_currentParameters);
116-
CurrentState = CurrentTransition.StateTo;
117-
CurrentTransition = null;
118+
InvokeTransition();
118119

119-
CurrentState.Entry(_currentParameters);
120-
_onChangeState?.Invoke(_beforeState, CurrentState);
121-
CurrentState.Exit(_currentParameters);
120+
ChangeState();
122121
}
123122

124123
}

SimpleStateMachineLibrary/Transitions/Transition.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ public Transition OnInvoke(Action<Transition, Dictionary<string, object>> action
3636
return this;
3737
}
3838

39-
internal void Invoke(Dictionary<string, object> parameters)
39+
internal Transition Invoke(Dictionary<string, object> parameters)
4040
{
4141
_onInvoke?.Invoke (this, parameters);
42+
43+
return this;
4244
}
4345

4446

0 commit comments

Comments
 (0)