-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMainForm.cs
More file actions
338 lines (296 loc) · 13.6 KB
/
MainForm.cs
File metadata and controls
338 lines (296 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
using SimpleRoutingAnalyzer.Metrics;
using SimpleRoutingAnalyzer.RoutingAlgorithms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SimpleRoutingAnalyzer {
public partial class MainForm : Form {
private static readonly string Version = "0.0.0.0";
private Graph GraphStorage = null;
private Graph Graph {
get => GraphStorage;
set {
GraphStorage = value;
PreviewPanel.Graph = value;
}
}
private IRoutingAlgorithm AlgorithmStorage = null;
private IRoutingAlgorithm Algorithm {
get => AlgorithmStorage;
set {
GlobalMetadataTextBox.Text = value?.Metadata() ?? "";
AlgorithmStorage = value;
PreviewPanel.Algorithm = value;
}
}
private MessageQueue LogQueue = new MessageQueue();
private void LoadConfig(string path) {
Config config = JsonSerializer.Deserialize<Config>(File.ReadAllText(path));
Graph = config.Graph;
Algorithm = Config.StringToAlgorithm(config.Algorithm, Graph);
ConfigsTextBox.Lines = config.Configs;
MetricsArgumentsTextBox.Lines = config.Seeds;
NameTextBox.Text = config.Name;
AlgorithmComboBox.SelectedItem = Config.AlgorithmToString(Algorithm);
AdjencyTextBox.Text = Graph.ToString();
CoordinatesTextBox.Text = Graph.PointsToString();
GraphMetadataTextBox.Text = Graph.MetadataToString();
}
private void SaveConfig(string path) {
var config = new Config();
config.Graph = Graph;
config.Algorithm = Config.AlgorithmToString(Algorithm);
config.Configs = ConfigsTextBox.Lines;
config.Seeds = MetricsArgumentsTextBox.Lines;
config.Name = NameTextBox.Text;
File.WriteAllText(path,
JsonSerializer.Serialize(
config,
new JsonSerializerOptions() {
WriteIndented = true,
}
)
);
}
public MainForm() {
InitializeComponent();
MainSplitContainer.Panel2Collapsed = true;
Text += " " + Version;
GeneratorComboBox.Items.Add("Mesh");
GeneratorComboBox.Items.Add("Circulant");
GeneratorComboBox.SelectedIndex = 0;
AlgorithmComboBox.Items.Add("None");
AlgorithmComboBox.Items.Add(DijkstraRouting.Name);
AlgorithmComboBox.Items.Add(NoCahceDijkstraRouting.Name);
AlgorithmComboBox.Items.Add(GreedyPromotionRouting.Name);
AlgorithmComboBox.Items.Add(AdvancedGreedyPromotionRouting.Name);
AlgorithmComboBox.Items.Add(RestrictedGreedyPromotionRouting.Name);
AlgorithmComboBox.Items.Add(RestrictedGreedyPromotion2Routing.Name);
AlgorithmComboBox.Items.Add(BrutCoordsRouting.Name);
AlgorithmComboBox.Items.Add(HotPotatoRouting.Name);
AlgorithmComboBox.Items.Add(OFTRouting.Name);
AlgorithmComboBox.Items.Add(CirculantGreedyPromotionRouting.Name);
AlgorithmComboBox.SelectedIndex = 0;
ModeComboBox.SelectedIndex = 0;
try { LoadConfig("current_config.json"); } catch { }
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e) {
try { SaveConfig("current_config.json"); } catch (Exception ex) {
MessageBox.Show(
this,
ex.ToString(),
"Error saving current config.",
MessageBoxButtons.OK,
MessageBoxIcon.Error
);
}
}
private void GeneratorButton_Click(object sender, EventArgs e) {
Graph graph = null;
var splitter = new Regex("[^\\d]+");
var args = splitter.Split(ArgumentsTextBox.Text).Where(it => it != "").ToArray();
switch (GeneratorComboBox.SelectedIndex) {
case 0:
int w, h;
if (args.Length == 2 &&
int.TryParse(args[0], out w) &&
int.TryParse(args[1], out h))
graph = GraphGenerator.Mesh(w, h);
break;
case 1:
int n;
if (args.Length >= 1 && int.TryParse(args[0], out n)) {
int[] gens = new int[args.Length - 1];
bool no_error = true;
for (int i = 1; i < args.Length; i++)
if (!int.TryParse(args[i], out gens[i - 1]))
no_error = false;
if (no_error)
graph = GraphGenerator.Circulant(n, gens);
}
break;
}
if (graph == null) {
AdjencyTextBox.Text = "";
CoordinatesTextBox.Text = "";
GraphMetadataTextBox.Text = "";
MessageBox.Show(this,
"Can not parse arguments.",
"Generator arguments error.",
MessageBoxButtons.OK,
MessageBoxIcon.Error
);
} else {
AdjencyTextBox.Text = graph.ToString();
CoordinatesTextBox.Text = graph.PointsToString();
GraphMetadataTextBox.Text = graph.MetadataToString();
}
}
private void ApplyButton_Click(object sender, EventArgs e) {
Graph = Graph.Parse(AdjencyTextBox.Text);
Graph?.ParsePoints(CoordinatesTextBox.Text);
Graph?.ParseMetadata(GraphMetadataTextBox.Text);
Algorithm = Config.StringToAlgorithm(AlgorithmComboBox.SelectedItem as string, Graph);
}
private void ResetButton_Click(object sender, EventArgs e) {
AdjencyTextBox.Text = Graph.ToString();
CoordinatesTextBox.Text = Graph.PointsToString();
}
private void ApplyAlgorithmButton_Click(object sender, EventArgs e) {
Algorithm = Config.StringToAlgorithm(AlgorithmComboBox.SelectedItem as string, Graph);
}
private void ResetAlgorithmButton_Click(object sender, EventArgs e) {
AlgorithmComboBox.SelectedItem = Config.AlgorithmToString(Algorithm);
}
private void RunButton_Click(object sender, EventArgs e) {
LogTextBox.Clear();
ResultsDataGridView.Rows.Clear();
IMetrics metrics = null;
switch (ModeComboBox.SelectedIndex) {
case 0: metrics = new StressMetrics(Graph, Algorithm, (int)SeedNumeric.Value); break;
case 1: metrics = new HalfFaultMetrics(Graph, Algorithm, (int)SeedNumeric.Value, (int)IterationsNumeric.Value); break;
};
if (!MetricsBackgroundWorker.IsBusy) {
MetricsBackgroundWorker.RunWorkerAsync(metrics);
}
}
private void StopButton_Click(object sender, EventArgs e) {
if (MetricsBackgroundWorker.IsBusy && !MetricsBackgroundWorker.CancellationPending) {
MetricsBackgroundWorker.CancelAsync();
}
}
private void MetricsBackgroundWorker_DoWork(object sender, DoWorkEventArgs e) {
var metrics = e.Argument as IMetrics;
for (int i = 0; i < metrics.IterationsTotal; i++) {
metrics.Iterate();
var state = metrics.State();
state.Add("Iteration", $"{metrics.Iteration:000}");
state.Add("Progress", $"{(double)metrics.Iteration / (metrics.IterationsTotal - 1):f4}");
MetricsBackgroundWorker.ReportProgress(100 * i / metrics.IterationsTotal, state);
if (MetricsBackgroundWorker.CancellationPending) {
break;
}
}
}
private void MetricsBackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) {
PreviewPanel.Invalidate();
MetricsProgressBar.Value = e.ProgressPercentage;
var state = e.UserState as Dictionary<string, string>;
if (state != null) {
var row = new List<string>();
foreach (var item in state) {
var columns = ResultsDataGridView.Columns.Cast<DataGridViewColumn>().ToList();
int index = columns.FindIndex(column => column.HeaderText == item.Key);
if (index < 0) {
index = columns.Count;
ResultsDataGridView.Columns.Add(item.Key, item.Key);
}
while (row.Count <= index) {
row.Add(null);
}
row[index] = item.Value;
}
ResultsDataGridView.Rows.Add(row.ToArray());
}
}
private void MetricsBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
if (NameTextBox.Text != "") {
var dir = "results";
var path = $"{dir}/{NameTextBox.Text} [{SeedNumeric.Value}].csv";
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
ResultsDataGridView.ExportCSV(path);
}
MetricsProgressBar.Value = 0;
}
private void ToggleToolStripMenuItem_Click(object sender, EventArgs e) {
PreviewPanel.ToggleSelected();
}
private void SourceToolStripMenuItem_Click(object sender, EventArgs e) {
PreviewPanel.SourceSelected();
}
private void DestinationToolStripMenuItem_Click(object sender, EventArgs e) {
PreviewPanel.DestinationSelected();
}
private void ExportToolStripMenuItem_Click(object sender, EventArgs e) {
var bitmap = PreviewPanel.Export();
if (SaveExportDialog.ShowDialog(this) == DialogResult.OK) {
bitmap.Save(SaveExportDialog.FileName, ImageFormat.Png);
}
}
private void SaveToolStripMenuItem_Click(object sender, EventArgs e) {
try {
SaveConfig("current_config.json");
} catch { }
}
private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e) {
try {
if (SaveConfigDialog.ShowDialog(this) == DialogResult.OK)
SaveConfig(SaveConfigDialog.FileName);
} catch (Exception ex) {
MessageBox.Show(this, ex.ToString(), "Error: Can not save config.", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void OpenToolStripMenuItem_Click(object sender, EventArgs e) {
try {
if (OpenConfigDialog.ShowDialog(this) == DialogResult.OK)
LoadConfig(OpenConfigDialog.FileName);
} catch (Exception ex) {
MessageBox.Show(this, ex.ToString(), "Error: Can not save config.", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void MessageTimer_Tick(object sender, EventArgs e) {
string message;
while ((message = LogQueue.Get()) != null)
LogTextBox.AppendText(message + Environment.NewLine);
}
private void MetadataToolStripMenuItem_Click(object sender, EventArgs e) {
MetadataToolStripMenuItem.Checked = !MetadataToolStripMenuItem.Checked;
MainSplitContainer.Panel2Collapsed = !MetadataToolStripMenuItem.Checked;
}
private void DistancesToolStripMenuItem_Click(object sender, EventArgs e) {
DistancesToolStripMenuItem.Checked = !DistancesToolStripMenuItem.Checked;
PreviewPanel.ShowDistances = DistancesToolStripMenuItem.Checked;
}
private void PreviewPanel_SelectionChanged(object sender, EventArgs e) {
MetadataTextBox.Clear();
var builder = new StringBuilder();
foreach (int node in PreviewPanel.Selected) {
var metadata = Algorithm.Metadata(node);
builder.Append($"Node: {node} {{");
if (metadata != null) {
builder.Append(Environment.NewLine);
builder.Append(metadata);
builder.Append(Environment.NewLine);
}
builder.Append("};");
builder.Append(Environment.NewLine);
}
MetadataTextBox.Text = builder.ToString();
}
private static Random SeedGenerator = new Random();
private void RandomSeedButton_Click(object sender, EventArgs e) {
SeedNumeric.Value = SeedGenerator.Next();
}
private void DrawMetadataToolStripMenuItem_Click(object sender, EventArgs e) {
DrawMetadataToolStripMenuItem.Checked = !DrawMetadataToolStripMenuItem.Checked;
if (DrawMetadataToolStripMenuItem.Checked) {
var metadata = new string[Graph.Count];
for (int i = 0; i < Graph.Count; i++) {
metadata[i] = Algorithm?.Metadata(i) ?? "{none}";
}
PreviewPanel.Metadata = metadata;
} else PreviewPanel.Metadata = null;
}
}
}