Skip to content

Commit 7c00f89

Browse files
committed
save for data
1 parent 5e0e5d8 commit 7c00f89

File tree

6 files changed

+33
-16
lines changed

6 files changed

+33
-16
lines changed

SimpleStateMachineLibrary/Data/DataWorkWithXML.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public static XElement ToXElement(Data data)
1717

1818
public XElement ToXElement()
1919
{
20-
XElement element = new XElement("Transition");
20+
XElement element = new XElement("Data");
2121
element.Add(new XAttribute("Name", this.Name));
2222
element.Add(new XAttribute("Value", this.Value.ToString()));
2323
return element;

SimpleStateMachineLibrary/Helpers/Check.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ public static string Name(string name)
1616
public static TObject Object<TObject>(TObject objectRequested)
1717
{
1818
if (Equals(objectRequested, default(TObject)))
19-
throw new ArgumentNullException(String.Format("Object of type \"{0}\" must be not null", typeof(TObject).Name));
19+
throw new ArgumentNullException(message: String.Format("Object of type \"{0}\" must be not null", typeof(TObject).Name), paramName: typeof(TObject).Name);
20+
2021
return objectRequested;
2122
}
2223

SimpleStateMachineLibrary/StateMachines/StateMachineWorkWithData.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
using SimpleStateMachineLibrary.Helpers;
5+
using System.Xml.Linq;
56

67
namespace SimpleStateMachineLibrary
78
{
@@ -61,10 +62,10 @@ internal Data AddData(Data data, bool exeption)
6162
return _AddData(nameData, valueData, false);
6263
}
6364

64-
//public Data AddData(XElement xElement)
65-
//{
66-
// return StateMachines.Data.FromXElement(this, xElement);
67-
//}
65+
public Data AddData(XElement xElement)
66+
{
67+
return SimpleStateMachineLibrary.Data.FromXElement(this, Check.Object(xElement));
68+
}
6869

6970
private Data _DeleteData(Data data, bool exeption)
7071
{

SimpleStateMachineLibrary/StateMachines/StateMachineWorkWithXML.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,13 @@ public static XDocument ToXDocument(StateMachine stateMachine, string nameFile)
3737
transitions.Add(transition.Value.ToXElement());
3838
}
3939

40+
XElement datas = new XElement("Data");
41+
stateMachineXElement.Add(datas);
4042

43+
foreach (var data in stateMachine._data)
44+
{
45+
datas.Add(data.Value.ToXElement());
46+
}
4147

4248
xDocument.Save(nameFile);
4349
return xDocument;
@@ -51,17 +57,20 @@ public XDocument ToXDocument(string nameFile)
5157
public static StateMachine FromXDocument(StateMachine stateMachine, XDocument xDocument)
5258
{
5359
XElement stateMachineXElement = Check.Object(xDocument).Element("StateMachine");
60+
stateMachineXElement = Check.Object(stateMachineXElement);
5461
var States = stateMachineXElement.Element("States")?.Elements()?.ToList();
55-
States.ForEach(x => stateMachine.AddState(x));
62+
States?.ForEach(x => stateMachine.AddState(x));
5663
var startState = stateMachineXElement.Element("StartState");
5764
string nameStartState = startState?.Attribute("Name").Value;
58-
5965
if (!string.IsNullOrEmpty(nameStartState))
6066
stateMachine.SetStartState(nameStartState);
6167

6268
var Transitions = stateMachineXElement.Element("Transitions")?.Elements()?.ToList();
63-
Transitions.ForEach(x => stateMachine.AddTransition(x));
64-
69+
Transitions?.ForEach(x => stateMachine.AddTransition(x));
70+
71+
var Datas = stateMachineXElement.Element("Data")?.Elements()?.ToList();
72+
Datas?.ForEach(x => stateMachine.AddData(x));
73+
6574
return stateMachine;
6675
}
6776

SimpleStateMachineLibrary/States/StateWorkWithXML.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,19 @@ public static XElement ToXElement(State state)
1414
return element;
1515
}
1616

17-
public static State FromXElement(StateMachine stateMachine, XElement state)
18-
{
19-
string Name = state.Attribute("Name")?.Value;
20-
return stateMachine.AddState(Name);
21-
}
22-
2317
public XElement ToXElement()
2418
{
2519
XElement element = new XElement("State");
2620
element.Add(new XAttribute("Name", this.Name));
2721
return element;
2822
}
23+
24+
public static State FromXElement(StateMachine stateMachine, XElement state)
25+
{
26+
string Name = state.Attribute("Name")?.Value;
27+
return stateMachine.AddState(Name);
28+
}
29+
30+
2931
}
3032
}

Tests/StateMachineUnitTests.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ public void StateMachineFromCode()
4646

4747
Assert.AreEqual(stateMachine.CurrentState.Name, "State3");
4848

49+
stateMachine.AddData("number1", 55);
50+
stateMachine.AddData("name1", "Roman");
51+
stateMachine.AddData("result1", 1001.0005);
52+
4953
stateMachine.ToXDocument("text.xml");
5054
}
5155

0 commit comments

Comments
 (0)