Skip to content

Commit 8786c59

Browse files
committed
new structure
1 parent 4ac6ed2 commit 8786c59

18 files changed

+212
-367
lines changed

SimpleStateMachineLibrary.sln

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 16
44
VisualStudioVersion = 16.0.29806.167
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleStateMachineLibrary", "SimpleStateMachineLibrary\SimpleStateMachineLibrary.csproj", "{047A9390-803D-4964-B512-546D19B62066}"
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimpleStateMachineLibrary", "SimpleStateMachineLibrary\SimpleStateMachineLibrary.csproj", "{047A9390-803D-4964-B512-546D19B62066}"
77
EndProject
8-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{7EE69DED-8322-4467-89E3-692CC55F2503}"
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{F508DC28-A157-49D4-8C65-791FFD6AF719}"
99
EndProject
1010
Global
1111
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -17,10 +17,10 @@ Global
1717
{047A9390-803D-4964-B512-546D19B62066}.Debug|Any CPU.Build.0 = Debug|Any CPU
1818
{047A9390-803D-4964-B512-546D19B62066}.Release|Any CPU.ActiveCfg = Release|Any CPU
1919
{047A9390-803D-4964-B512-546D19B62066}.Release|Any CPU.Build.0 = Release|Any CPU
20-
{7EE69DED-8322-4467-89E3-692CC55F2503}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21-
{7EE69DED-8322-4467-89E3-692CC55F2503}.Debug|Any CPU.Build.0 = Debug|Any CPU
22-
{7EE69DED-8322-4467-89E3-692CC55F2503}.Release|Any CPU.ActiveCfg = Release|Any CPU
23-
{7EE69DED-8322-4467-89E3-692CC55F2503}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{F508DC28-A157-49D4-8C65-791FFD6AF719}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{F508DC28-A157-49D4-8C65-791FFD6AF719}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{F508DC28-A157-49D4-8C65-791FFD6AF719}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{F508DC28-A157-49D4-8C65-791FFD6AF719}.Release|Any CPU.Build.0 = Release|Any CPU
2424
EndGlobalSection
2525
GlobalSection(SolutionProperties) = preSolution
2626
HideSolutionNode = FALSE

SimpleStateMachineLibrary/Data/Data.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,30 @@
11
using SimpleStateMachineLibrary.Helpers;
2+
using System;
23

34
namespace SimpleStateMachineLibrary
45
{
5-
public partial class Data : NamedObject
6+
public partial class Data : NamedObject<Data>
67
{
7-
public object Value { get; set; }
8+
private object _value;
89

9-
public Data(StateMachine stateMachine, string nameData, object valueData = null) : base(stateMachine, nameData)
10+
public object Value
11+
{ get { return _value; }
12+
set
13+
{
14+
_onChange?.Invoke(this, value, value);
15+
_value = value;
16+
}
17+
}
18+
19+
private Action<Data, object, object> _onChange;
20+
21+
public Data OnChange(Action<Data, object, object> actionOnChange)
22+
{
23+
_onChange += actionOnChange;
24+
25+
return this;
26+
}
27+
protected internal Data(StateMachine stateMachine, string nameData, object valueData = null) : base(stateMachine, nameData)
1028
{
1129
Value = valueData;
1230
}

SimpleStateMachineLibrary/Data/DataWorkWithXML.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,6 @@ public static Data FromXElement(StateMachine stateMachine, XElement data)
3232
string Value = data.Attribute("Value")?.Value;
3333
return stateMachine.AddData(Name, Value);
3434
}
35+
3536
}
3637
}
Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34

45
namespace SimpleStateMachineLibrary.Helpers
56
{
@@ -15,18 +16,19 @@ public static string Name(string name)
1516
public static TObject Object<TObject>(TObject objectRequested)
1617
{
1718
if (Equals(objectRequested, default(TObject)))
18-
throw new ArgumentNullException(String.Format("Object of type {0} must be not null", typeof(TObject).ToString()));
19+
throw new ArgumentNullException(String.Format("Object of type \"{0}\" must be not null", typeof(TObject).Name));
1920
return objectRequested;
2021
}
2122

22-
public static TObject NamedObject<TObject>(TObject objectRequested) where TObject : NamedObject
23+
public static TObject NamedObject<TObject>(TObject objectRequested) where TObject : NamedObject<TObject>
2324
{
2425
Check.Object(objectRequested);
2526
Check.Name(objectRequested.Name);
2627
return objectRequested;
2728
}
2829

29-
private static bool _Contains<TObject>(Dictionary<string, TObject> dictionary, string nameObject, bool needContains, bool exeption) where TObject : NamedObject
30+
31+
private static bool _Contains<TObject>(Dictionary<string, TObject> dictionary, string nameObject, bool needContains, bool exeption) where TObject : NamedObject<TObject>
3032
{
3133
dictionary = Check.Object(dictionary);
3234
nameObject = Check.Name(nameObject);
@@ -35,13 +37,13 @@ private static bool _Contains<TObject>(Dictionary<string, TObject> dictionary, s
3537
return true;
3638
if (exeption)
3739
if (needContains)
38-
throw new KeyNotFoundException(String.Format("Element with name {0} is not found", nameObject));
40+
throw new KeyNotFoundException(String.Format("Element with name \"{0}\" is not found", nameObject));
3941
else
40-
throw new ArgumentException(String.Format("Element with name {0} already exists", nameObject));
42+
throw new ArgumentException(String.Format("Element with name \"{0}\" already exists", nameObject));
4143
return false;
4244
}
4345

44-
private static bool _Contains<TObject>(Dictionary<string, TObject> dictionary, TObject objectRequested, bool needContains, bool exeption) where TObject : NamedObject
46+
private static bool _Contains<TObject>(Dictionary<string, TObject> dictionary, TObject objectRequested, bool needContains, bool exeption) where TObject : NamedObject<TObject>
4547
{
4648
dictionary = Check.Object(dictionary);
4749
objectRequested = Check.Object(objectRequested);
@@ -51,36 +53,36 @@ private static bool _Contains<TObject>(Dictionary<string, TObject> dictionary, T
5153

5254
if (exeption)
5355
if (needContains)
54-
throw new KeyNotFoundException(String.Format("Element of type {0} not found", typeof(TObject).ToString()));
56+
throw new KeyNotFoundException(String.Format("Element of type \"{0}\" not found", typeof(TObject).Name));
5557
else
56-
throw new ArgumentException(String.Format("Element of type {0} already exists", typeof(TObject).ToString()));
58+
throw new ArgumentException(String.Format("Element of type \"{0}\" already exists", typeof(TObject).Name));
5759
return false;
5860
}
5961

60-
61-
public static bool Contains<TObject>(Dictionary<string, TObject> dictionary, string nameObject, bool exeption = true) where TObject : NamedObject
62+
63+
public static bool Contains<TObject>(Dictionary<string, TObject> dictionary, string nameObject, bool exeption = true) where TObject : NamedObject<TObject>
6264
{
6365
return _Contains(dictionary, nameObject, true, exeption);
6466
}
6567

66-
public static bool Contains<TObject>(Dictionary<string, TObject> dictionary, TObject objectRequested, bool exeption = true) where TObject : NamedObject
68+
public static bool Contains<TObject>(Dictionary<string, TObject> dictionary, TObject objectRequested, bool exeption = true) where TObject : NamedObject<TObject>
6769
{
6870
return _Contains(dictionary, objectRequested, true, exeption);
6971
}
7072

71-
72-
public static bool NotContains<TObject>(Dictionary<string, TObject> dictionary, string nameObject, bool exeption = true) where TObject : NamedObject
73+
74+
public static bool NotContains<TObject>(Dictionary<string, TObject> dictionary, string nameObject, bool exeption = true) where TObject : NamedObject<TObject>
7375
{
7476
return _Contains(dictionary, nameObject, false, exeption);
7577
}
7678

77-
public static bool NotContains<TObject>(Dictionary<string, TObject> dictionary, TObject objectRequested, bool exeption = true) where TObject : NamedObject
79+
public static bool NotContains<TObject>(Dictionary<string, TObject> dictionary, TObject objectRequested, bool exeption = true) where TObject : NamedObject<TObject>
7880
{
7981
return _Contains(dictionary, objectRequested, false, exeption);
8082
}
8183

82-
83-
public static TObject Remove<TObject>(Dictionary<string, TObject> dictionary, string nameObject, bool exeption = true) where TObject : NamedObject
84+
85+
public static TObject Remove<TObject>(Dictionary<string, TObject> dictionary, string nameObject, bool exeption = true) where TObject : NamedObject<TObject>
8486
{
8587
dictionary = Check.Object(dictionary);
8688
nameObject = Check.Name(nameObject);
@@ -91,7 +93,7 @@ public static TObject Remove<TObject>(Dictionary<string, TObject> dictionary, st
9193
if (removedObj == default(TObject))
9294
{
9395
if (exeption)
94-
throw new KeyNotFoundException(String.Format("Element with name {0} is not deleted because not found. ", nameObject));
96+
throw new KeyNotFoundException(String.Format("Element with name \"{0}\" is not deleted because not found. ", nameObject));
9597
else
9698
return default(TObject);
9799
}
@@ -100,7 +102,7 @@ public static TObject Remove<TObject>(Dictionary<string, TObject> dictionary, st
100102
return removedObj;
101103
}
102104

103-
public static TObject Remove<TObject>(Dictionary<string, TObject> dictionary, TObject obj, bool exeption = true) where TObject : NamedObject
105+
public static TObject Remove<TObject>(Dictionary<string, TObject> dictionary, TObject obj, bool exeption = true) where TObject : NamedObject<TObject>
104106
{
105107
dictionary = Check.Object(dictionary);
106108
obj = Check.NamedObject(obj);
@@ -111,7 +113,7 @@ public static TObject Remove<TObject>(Dictionary<string, TObject> dictionary, TO
111113
if (removedObj == default(TObject))
112114
{
113115
if (exeption)
114-
throw new KeyNotFoundException(String.Format("Element with name {0} is not deleted because not found. ", obj.Name));
116+
throw new KeyNotFoundException(String.Format("Element with name \"{0}\" is not deleted because not found. ", obj.Name));
115117
else
116118
return default(TObject);
117119
}
@@ -121,34 +123,17 @@ public static TObject Remove<TObject>(Dictionary<string, TObject> dictionary, TO
121123
}
122124

123125

124-
public static TObject GetElement<TObject>(Dictionary<string, TObject> dictionary, string nameObject, bool exeption = true) where TObject : NamedObject
126+
public static TObject GetElement<TObject>(Dictionary<string, TObject> dictionary, string nameObject, bool exeption = true) where TObject : NamedObject<TObject>
125127
{
126128
bool contains = Contains(dictionary, nameObject, exeption);
127129
return contains ? dictionary[nameObject] : default(TObject);
128130
}
129131

130-
public static TObject GetElement<TObject>(Dictionary<string, TObject> dictionary, TObject obj, bool exeption = true) where TObject : NamedObject
132+
public static TObject GetElement<TObject>(Dictionary<string, TObject> dictionary, TObject obj, bool exeption = true) where TObject : NamedObject<TObject>
131133
{
132134
bool contains = Contains(dictionary, obj, exeption);
133135
return contains ? obj : default(TObject);
134136
}
135137

136-
137-
public static TObject AddElement<TObject>(Dictionary<string, TObject> dictionary, TObject obj, bool exeption = true) where TObject : NamedObject
138-
{
139-
return AddElement(dictionary, obj?.Name, obj, exeption);
140-
}
141-
142-
public static TObject AddElement<TObject>(Dictionary<string, TObject> dictionary, string name, TObject obj, bool exeption = true) where TObject : NamedObject
143-
{
144-
obj = Check.NamedObject(obj);
145-
bool nonContains = NotContains(dictionary, name, exeption);
146-
147-
if (nonContains)
148-
return default(TObject);
149-
150-
dictionary.Add(name, obj);
151-
return obj;
152-
}
153138
}
154139
}
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
namespace SimpleStateMachineLibrary.Helpers
22
{
33

4-
public abstract class NamedObject
4+
public abstract class NamedObject<TObject>
55
{
66
public string Name { get; }
7+
78
public StateMachine StateMachine { get; }
89

9-
public NamedObject(StateMachine stateMachine, string nameObject)
10+
protected internal NamedObject(StateMachine stateMachine, string nameObject)
1011
{
11-
StateMachine = Check.Object(stateMachine);
12-
1312
Name = Check.Name(nameObject);
13+
StateMachine = Check.Object(stateMachine);
1414
}
15-
1615
}
1716
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netstandard2.0</TargetFramework>
4+
<TargetFramework>netstandard2.1</TargetFramework>
55
</PropertyGroup>
66

77
</Project>

SimpleStateMachineLibrary/StateMachines/StateMachine.cs

Lines changed: 22 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,16 @@ public partial class StateMachine
99
{
1010
private Dictionary<string, State> _states = new Dictionary<string, State>();
1111

12-
private Dictionary<string, Transition> _transitions = new Dictionary<string, Transition>();
12+
private Dictionary<string, Transition> _transitions = new Dictionary<string, Transition>();
1313

14-
private Dictionary<string, Data> _data = new Dictionary<string, Data>();
14+
private Dictionary<string, Data> _data = new Dictionary<string, Data>();
1515

1616
public State CurrentState { get; internal set; }
1717

1818
public Transition CurrentTransition{ get; internal set; }
1919

2020
public State StartState { get; protected set; }
2121

22-
public State EndState { get; protected set; }
23-
2422
public StateMachine()
2523
{
2624

@@ -53,14 +51,6 @@ private void CheckStartState()
5351
}
5452
}
5553

56-
private void CheckEndState()
57-
{
58-
if (EndState != null)
59-
{
60-
throw new ArgumentException(String.Format("End state already set. It's {0} ", StartState.Name));
61-
}
62-
}
63-
6454
private void CheckCurrentTransition()
6555
{
6656
if (CurrentTransition == null)
@@ -71,30 +61,16 @@ private void CheckCurrentTransition()
7161

7262
public State SetStartState(State state)
7363
{
74-
CheckStartState();
75-
StartState = State(Check.Object(state));
76-
return StartState;
64+
return SetStartState(state);
7765
}
7866

7967
public State SetStartState(string stateName)
8068
{
81-
CheckEndState();
82-
StartState = State(Check.Object(stateName));
69+
CheckStartState();
70+
StartState = State(stateName);
8371
return StartState;
8472
}
8573

86-
public State SetEndState(State state)
87-
{
88-
EndState = State(Check.Object(state));
89-
return EndState;
90-
}
91-
92-
public State SetEndState(string stateName)
93-
{
94-
EndState = State(Check.Object(stateName));
95-
return EndState;
96-
}
97-
9874
public void InvokeTransition(string nameTransition)
9975
{
10076
_nextTransition = Check.GetElement(_transitions, nameTransition, true);
@@ -112,23 +88,23 @@ public void Start(Dictionary<string, object> startParameters = null)
11288
CurrentState = StartState;
11389
CurrentState.Entry(startParameters);
11490
CurrentState.Exit(startParameters);
115-
while (CurrentState != EndState)
116-
{
117-
_currentParameters = _nextParameters;
118-
_nextParameters = null;
119-
120-
CurrentTransition = _nextTransition;
121-
_nextTransition = null;
122-
123-
CheckCurrentTransition();
124-
CurrentState = null;
125-
CurrentTransition.Invoke(_currentParameters);
126-
CurrentState = CurrentTransition.StateTo;
127-
CurrentTransition = null;
128-
129-
CurrentState.Entry(_currentParameters);
130-
CurrentState.Exit(_currentParameters);
131-
}
91+
//while (CurrentState != EndState)
92+
//{
93+
// _currentParameters = _nextParameters;
94+
// _nextParameters = null;
95+
96+
// CurrentTransition = _nextTransition;
97+
// _nextTransition = null;
98+
99+
// CheckCurrentTransition();
100+
// CurrentState = null;
101+
// CurrentTransition.Invoke(_currentParameters);
102+
// CurrentState = CurrentTransition.StateTo;
103+
// CurrentTransition = null;
104+
105+
// CurrentState.Entry(_currentParameters);
106+
// CurrentState.Exit(_currentParameters);
107+
//}
132108

133109
}
134110

0 commit comments

Comments
 (0)