Skip to content

Commit

Permalink
Merge pull request #17 from maslankam/xml-fix
Browse files Browse the repository at this point in the history
Xml fix
  • Loading branch information
maslankam authored Dec 7, 2019
2 parents a88801e + 77d7af8 commit a8b0fb2
Show file tree
Hide file tree
Showing 17 changed files with 73 additions and 63 deletions.
4 changes: 2 additions & 2 deletions Model/CellularAutomaton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public CellularAutomaton(
ITransitionRule transition,
INeighbourhood neighbourhood,
IBoundaryCondition boundary,
int step)
int step, ISimulationExecutor executor)
{
if (transition == null ||
neighbourhood == null ||
Expand All @@ -78,7 +78,7 @@ public CellularAutomaton(
Space = space;
_lastStepSpace = space.Clone();

_executor = new SimulationExecutor(step);
_executor = executor;
}

public void NextStep()
Expand Down
2 changes: 1 addition & 1 deletion Model/CurvatureExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void NextState(CelluralSpace space, CelluralSpace lastSpace, ITransitionR
{
for (int j = 0; j < space.GetYLength(); j++)
{
// TODO: refactor
// TODO: refactor, injected arguments are not used !!
IBoundaryCondition boun = new AbsorbingBoundary();
INeighbourhood nei = new MooreNeighbourhood(new AbsorbingBoundary());
ITransitionRule rule = new RuleOne();
Expand Down
24 changes: 21 additions & 3 deletions MsmGrainGrowthGui/ApplicationState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class ApplicationState
public IBoundaryCondition boundary;
public bool isAutomatonGenerated;
public bool isSaved;
public ISimulationExecutor executor;

public ApplicationState(
CellularAutomaton automaton,
Expand All @@ -29,7 +30,8 @@ public ApplicationState(
INeighbourhood neighbourhood,
IBoundaryCondition boundary,
bool isGenerated,
bool isSaved
bool isSaved,
ISimulationExecutor executor
){
this.automaton = automaton;
this.spaceSize = spaceSize;
Expand All @@ -42,7 +44,8 @@ bool isSaved
this.boundary = boundary;
this.isAutomatonGenerated = isGenerated;
this.isSaved = isSaved;
}
this.executor = executor;
}


public static IBoundaryCondition GetBoundaryByName(string name)
Expand Down Expand Up @@ -77,11 +80,26 @@ public static ITransitionRule GetTransitionByName(string name)
{
switch(name)
{
case "Model.GrainGrowthRule":
case "Model.Transition.GrainGrowthRule":
return new GrainGrowthRule();
default: throw new ArgumentException();
}
}

public static ISimulationExecutor GetExecutorByName(string name, int step = 0)
{
switch (name)
{
case "Model.SimulationExecutor":

return new SimulationExecutor(step);
case "Model.CurvatureExecutor":
return new CurvatureExecutor(step);

default: throw new ArgumentException();
}
}

}

}
4 changes: 3 additions & 1 deletion MsmGrainGrowthGui/CelluralAutomatonViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ void OpenExecute()
_boundary = state.boundary;
IsGenerated = state.isAutomatonGenerated;
_isSaved = state.isSaved;
_executor = state.executor;

//IsGenerated = true;

Expand Down Expand Up @@ -434,7 +435,8 @@ void SaveAsExecute()
_neighbourhood,
_boundary,
_isAutomatonGenerated,
_isSaved
_isSaved,
_executor
);
var factory = new XmlFactory();
var doc = factory.GetXDocument(state);
Expand Down
14 changes: 0 additions & 14 deletions MsmGrainGrowthGui/GrainGrowthGui.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,11 @@
<UseWPF>true</UseWPF>
</PropertyGroup>

<ItemGroup>
<None Remove="icons\diskette.png" />
<None Remove="icons\excel.png" />
<None Remove="icons\folder.png" />
<None Remove="icons\picture.png" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Model\Model.csproj" />
<ProjectReference Include="..\Utility\Utility.csproj" />
</ItemGroup>

<ItemGroup>
<Resource Include="icons\diskette.png" />
<Resource Include="icons\excel.png" />
<Resource Include="icons\folder.png" />
<Resource Include="icons\picture.png" />
</ItemGroup>

<ItemGroup>
<Compile Update="Properties\Resources.Designer.cs">
<DesignTime>True</DesignTime>
Expand Down
4 changes: 0 additions & 4 deletions MsmGrainGrowthGui/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,23 @@
<ToolBar>
<Button Command="{Binding Open}">
<StackPanel Orientation="Horizontal">
<Image Source="/icons/folder.png" />
<TextBlock Margin="5,0">Open</TextBlock>
</StackPanel>
</Button>
<Button Padding="5" Command="{Binding SaveAs}">
<StackPanel Orientation="Horizontal">
<Image Source="/icons/diskette.png" />
<TextBlock Margin="5,0">Save As</TextBlock>
</StackPanel>
</Button>
</ToolBar>
<ToolBar>
<Button Command="{Binding ExportCsv}">
<StackPanel Orientation="Horizontal" >
<Image Source="/icons/excel.png" />
<TextBlock Margin="5,0">Export to CSV</TextBlock>
</StackPanel>
</Button>
<Button Command="{Binding ExportPng}">
<StackPanel Orientation="Horizontal" IsEnabled="False">
<Image Source="/icons/picture.png" />
<TextBlock Margin="5,0">Save image</TextBlock>
</StackPanel>
</Button>
Expand Down
6 changes: 5 additions & 1 deletion MsmGrainGrowthGui/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.ComponentModel;
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media.Imaging;


Expand Down Expand Up @@ -65,5 +67,7 @@ private void PropertyChangedHandler(object sender, PropertyChangedEventArgs e)
}
}



}
}
34 changes: 12 additions & 22 deletions MsmGrainGrowthGui/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions MsmGrainGrowthGui/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,4 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="diskette" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\icons\diskette.png;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
</root>
2 changes: 2 additions & 0 deletions MsmGrainGrowthGui/XmlFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ private static void AddVariables(ApplicationState state, XDocument doc)
widowVariables.Add(new XAttribute("Boundary", state.boundary.GetType()));
widowVariables.Add(new XAttribute("IsGenerated", state.isAutomatonGenerated));
widowVariables.Add(new XAttribute("IsSaved", state.isSaved));
widowVariables.Add(new XAttribute("Step", state.automaton.Step));
widowVariables.Add(new XAttribute("Executor", state.executor.GetType()));
doc.Root.Add(widowVariables);
}

Expand Down
13 changes: 11 additions & 2 deletions MsmGrainGrowthGui/XmlReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Xml.Linq;
using Model;
using System.Drawing;
using MsmGrainGrowthGui;

namespace GrainGrowthGui
{
Expand All @@ -25,6 +26,7 @@ public class XmlReader
private List<Grain> _grains;
private List<Inclusion> _inclusions;
private int _step; // TODO: Add step
private ISimulationExecutor _executor;


public ApplicationState Read(XDocument doc)
Expand Down Expand Up @@ -92,7 +94,8 @@ public ApplicationState Read(XDocument doc)
_transition,
_neighbourhood,
_boundary,
_step
_step,
_executor
);


Expand All @@ -108,7 +111,8 @@ public ApplicationState Read(XDocument doc)
_neighbourhood,
_boundary,
_isAutomatonGenerated,
_isSaved
_isSaved,
_executor
);
}

Expand Down Expand Up @@ -180,6 +184,11 @@ private void ReadWindowVariables(XDocument doc)

_isSaved = GetValueFromElement(doc, "IsSaved") == "true" ? true : false;
_isAutomatonGenerated = GetValueFromElement(doc, "IsGenerated") == "true" ? true : false;

_step = Convert.ToInt32(GetValueFromElement(doc, "Step"));

string executorName = (GetValueFromElement(doc, "Executor"));
_executor = ApplicationState.GetExecutorByName(executorName, _step);
}
catch (Exception e)
{
Expand Down
Binary file removed MsmGrainGrowthGui/icons/diskette.png
Binary file not shown.
Binary file removed MsmGrainGrowthGui/icons/excel.png
Binary file not shown.
Binary file removed MsmGrainGrowthGui/icons/folder.png
Binary file not shown.
Binary file removed MsmGrainGrowthGui/icons/picture.png
Binary file not shown.
11 changes: 7 additions & 4 deletions Test/GrainGrowthGui/XmlFactoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public class XmlFactoryTest
public void BasicTest()
{
#region expectedXml
string expected =
string expected =
@"<Document>
<WindowVariables SpaceSize=""3"" GrainsCount=""2"" InclusionsCount=""2"" MinRadius=""1"" MaxRadius=""1"" Transition=""Model.GrainGrowthRule"" Neighbourhood=""Model.VonNeumanNeighbourhood"" Boundary=""Model.AbsorbingBoundary"" IsGenerated=""false"" IsSaved=""false"" />
<WindowVariables SpaceSize=""3"" GrainsCount=""2"" InclusionsCount=""2"" MinRadius=""1"" MaxRadius=""1"" Transition=""Model.Transition.GrainGrowthRule"" Neighbourhood=""Model.VonNeumanNeighbourhood"" Boundary=""Model.AbsorbingBoundary"" IsGenerated=""false"" IsSaved=""false"" Step=""0"" Executor=""Model.SimulationExecutor"" />
<Grains>
<Grain Id=""0"" P=""0"" A=""1"" R=""2"" G=""3"" B=""4"" />
<Grain Id=""1"" P=""0"" A=""5"" R=""6"" G=""7"" B=""8"" />
Expand Down Expand Up @@ -61,6 +61,7 @@ public void BasicTest()
ITransitionRule transition = new GrainGrowthRule();
IBoundaryCondition boundary = new AbsorbingBoundary();
INeighbourhood neighbourhood = new VonNeumanNeighbourhood(boundary);
ISimulationExecutor executor = new SimulationExecutor();

var grains = new List<Grain>();
grains.Add(new Grain(0, 0, Color.FromArgb(1,2,3,4)));
Expand Down Expand Up @@ -90,7 +91,8 @@ public void BasicTest()
transition,
neighbourhood,
boundary,
step
step,
executor
);

var state = new ApplicationState(
Expand All @@ -104,7 +106,8 @@ public void BasicTest()
neighbourhood,
boundary,
isGenerated,
isSaved
isSaved,
executor
);

var document = factory.GetXDocument(state);
Expand Down
14 changes: 9 additions & 5 deletions Test/GrainGrowthGui/XmlReaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public void BasicTest()
Assert.Equal(expected.maxRadius, result.maxRadius);
Assert.Equal(expected.minRadius, result.minRadius);
Assert.Equal(expected.spaceSize, result.spaceSize);

Assert.Equal(expected.executor.GetType(), result.executor.GetType());

int i = 0;
foreach(var grain in expected.automaton.Grains)
{
Expand Down Expand Up @@ -96,6 +97,7 @@ private ApplicationState BuildMockState()
ITransitionRule transition = new GrainGrowthRule();
IBoundaryCondition boundary = new AbsorbingBoundary();
INeighbourhood neighbourhood = new VonNeumanNeighbourhood(boundary);
ISimulationExecutor executor = new SimulationExecutor();

var grains = new List<Grain>();
grains.Add(new Grain(0, 0, Color.FromArgb(1,2,3,4)));
Expand Down Expand Up @@ -125,7 +127,8 @@ private ApplicationState BuildMockState()
transition,
neighbourhood,
boundary,
step
step,
executor
);

return new ApplicationState(
Expand All @@ -139,15 +142,16 @@ private ApplicationState BuildMockState()
neighbourhood,
boundary,
isGenerated,
isSaved
isSaved,
executor
);
}

private XDocument BuildMockInput()
{
string xmlState =
string xmlState =
@"<Document>
<WindowVariables SpaceSize=""3"" GrainsCount=""2"" InclusionsCount=""2"" MinRadius=""1"" MaxRadius=""1"" Transition=""Model.GrainGrowthRule"" Neighbourhood=""Model.VonNeumanNeighbourhood"" Boundary=""Model.AbsorbingBoundary"" IsGenerated=""false"" IsSaved=""false"" />
<WindowVariables SpaceSize=""3"" GrainsCount=""2"" InclusionsCount=""2"" MinRadius=""1"" MaxRadius=""1"" Transition=""Model.Transition.GrainGrowthRule"" Neighbourhood=""Model.VonNeumanNeighbourhood"" Boundary=""Model.AbsorbingBoundary"" IsGenerated=""false"" IsSaved=""false"" Step=""0"" Executor=""Model.SimulationExecutor"" />
<Grains>
<Grain Id=""0"" P=""0"" A=""1"" R=""2"" G=""3"" B=""4"" />
<Grain Id=""1"" P=""0"" A=""5"" R=""6"" G=""7"" B=""8"" />
Expand Down

0 comments on commit a8b0fb2

Please sign in to comment.