1 | using HeuristicLab.BioBoost.Representation;
|
---|
2 | using HeuristicLab.Common;
|
---|
3 | using HeuristicLab.Core;
|
---|
4 | using HeuristicLab.Data;
|
---|
5 | using HeuristicLab.MainForm;
|
---|
6 | using HeuristicLab.Optimization;
|
---|
7 | using HeuristicLab.Optimizer;
|
---|
8 | using System;
|
---|
9 | using System.Collections.Generic;
|
---|
10 | using System.Globalization;
|
---|
11 | using System.IO;
|
---|
12 | using System.Linq;
|
---|
13 | using System.Text;
|
---|
14 | using System.Windows.Forms;
|
---|
15 | using MenuItem = HeuristicLab.MainForm.WindowsForms.MenuItem;
|
---|
16 |
|
---|
17 |
|
---|
18 | namespace HeuristicLab.BioBoost.Views {
|
---|
19 | internal sealed class ExportSolutionMenuItem : MenuItem, IOptimizerUserInterfaceItemProvider {
|
---|
20 | public override string Name {
|
---|
21 | get { return "Export CSV"; }
|
---|
22 | }
|
---|
23 |
|
---|
24 | public override IEnumerable<string> Structure {
|
---|
25 | get { return new string[] { "&File" }; }
|
---|
26 | }
|
---|
27 |
|
---|
28 | public override int Position {
|
---|
29 | get { return 1450; }
|
---|
30 | }
|
---|
31 |
|
---|
32 | protected override void OnToolStripItemSet(EventArgs e) {
|
---|
33 | ToolStripItem.Enabled = false;
|
---|
34 | }
|
---|
35 |
|
---|
36 | protected override void OnActiveViewChanged(object sender, EventArgs e) {
|
---|
37 | IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
|
---|
38 | if (activeView == null) {
|
---|
39 | ToolStripItem.Enabled = false;
|
---|
40 | return;
|
---|
41 | }
|
---|
42 |
|
---|
43 | var content = activeView.Content;
|
---|
44 | RunCollection runCollection = content as RunCollection;
|
---|
45 | var optimizer = content as IOptimizer;
|
---|
46 | if (runCollection == null && optimizer != null) {
|
---|
47 | runCollection = ((IOptimizer)content).Runs;
|
---|
48 | }
|
---|
49 |
|
---|
50 | var alg = content as IAlgorithm;
|
---|
51 | var solution = content as BioBoostCompoundSolution;
|
---|
52 |
|
---|
53 | ToolStripItem.Enabled =
|
---|
54 | // at least on run with a result || or the alg is paused and results contains a bioboost solution
|
---|
55 | (runCollection != null && runCollection.Any(run => run.Results.Any(p => p.Value is BioBoostCompoundSolution))) ||
|
---|
56 | (alg != null && alg.ExecutionState == ExecutionState.Paused && alg.Results.Any(r => r.Value is BioBoostCompoundSolution)) ||
|
---|
57 | (solution != null)
|
---|
58 | ;
|
---|
59 | }
|
---|
60 |
|
---|
61 | public override void Execute() {
|
---|
62 | var folderBrowserDialog = new FolderBrowserDialog();
|
---|
63 | folderBrowserDialog.Description = "Select the export directory.";
|
---|
64 | folderBrowserDialog.RootFolder = Environment.SpecialFolder.Desktop;
|
---|
65 | if (folderBrowserDialog.ShowDialog() == DialogResult.OK) {
|
---|
66 | var folderName = folderBrowserDialog.SelectedPath;
|
---|
67 | IContentView activeView = (IContentView)MainFormManager.MainForm.ActiveView;
|
---|
68 | var mainForm = (MainForm.WindowsForms.MainForm)MainFormManager.MainForm;
|
---|
69 | mainForm.AddOperationProgressToContent(activeView.Content, "Export solutions.");
|
---|
70 |
|
---|
71 | Action<IContentView> action = (view) => {
|
---|
72 | var i = 0;
|
---|
73 | // export the solutions of all runs
|
---|
74 | foreach (var run in view.Content.GetObjectGraphObjects(excludeStaticMembers: true).OfType<Run>()) {
|
---|
75 | var solution =
|
---|
76 | run.GetObjectGraphObjects(excludeStaticMembers: true).OfType<BioBoostCompoundSolution>().FirstOrDefault();
|
---|
77 | if (solution == null) continue;
|
---|
78 | ExportSolution(folderName, i++, run.Name, solution);
|
---|
79 | }
|
---|
80 | // export the solutions in the results of the paused algorithm
|
---|
81 | var alg = view.Content as IAlgorithm;
|
---|
82 | if (alg != null && alg.ExecutionState == ExecutionState.Paused) {
|
---|
83 | foreach (var solution in alg.Results.Select(r => r.Value).OfType<BioBoostCompoundSolution>()) {
|
---|
84 | ExportSolution(folderName, i++, alg.Name, solution);
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | // export the solution itself
|
---|
89 | var bioBoostSolution = view.Content as BioBoostCompoundSolution;
|
---|
90 | if (bioBoostSolution != null)
|
---|
91 | ExportSolution(folderName, i++, bioBoostSolution.Name, bioBoostSolution);
|
---|
92 | };
|
---|
93 |
|
---|
94 | action.BeginInvoke(activeView, delegate(IAsyncResult result) {
|
---|
95 | try {
|
---|
96 | action.EndInvoke(result);
|
---|
97 | } catch (SystemException e) {
|
---|
98 | var dialog = new HeuristicLab.PluginInfrastructure.ErrorDialog(e);
|
---|
99 | dialog.ShowDialog();
|
---|
100 | }
|
---|
101 | mainForm.RemoveOperationProgressFromContent(activeView.Content);
|
---|
102 | }, null);
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | private void ExportSolution(string folderName, int i, string name, BioBoostCompoundSolution solution) {
|
---|
107 | var csv = CreateCsvSolution(solution);
|
---|
108 | var filename = string.Format("{0}-{1}.csv", i, name);
|
---|
109 | // remove invalid characters
|
---|
110 | foreach (var ch in Path.GetInvalidFileNameChars()) {
|
---|
111 | filename = filename.Replace(ch, '_');
|
---|
112 |
|
---|
113 | }
|
---|
114 | var path = Path.Combine(folderName, filename);
|
---|
115 | if (path.Length >= 248) throw new PathTooLongException("The path is longer than 248 characters. Try to save your files in a different folder");
|
---|
116 | if (filename.Length >= 260) throw new PathTooLongException("The filename is longer than 260 characters. Please change the name of runs to something shorter.");
|
---|
117 | File.WriteAllText(path, csv);
|
---|
118 | }
|
---|
119 |
|
---|
120 | private string CreateCsvSolution(BioBoostCompoundSolution content) {
|
---|
121 | content.EvaluateAndUpdateAllResults();
|
---|
122 | var sb = new StringBuilder();
|
---|
123 | // header
|
---|
124 | sb.Append("NUTS_ID").Append(",");
|
---|
125 | sb.Append(string.Join(",", content.IntValues.Select(x => string.Format("\"{0}\"", x.Key)))).Append(",");
|
---|
126 | sb.Append(string.Join(",", content.IntValues.Select(x => string.Format("\"{0}-Region\"", x.Key)))).Append(",");
|
---|
127 | sb.Append(string.Join(",", content.DoubleValues.Select(x => string.Format("\"{0}\"", x.Key)))).Append(",");
|
---|
128 | sb.Append(string.Join(",", content.StringValues.Select(x => string.Format("\"{0}\"", x.Key))));
|
---|
129 | sb.Append(Environment.NewLine);
|
---|
130 | // rows
|
---|
131 | for (int i = 0; i < content.LocationNames.Length; i++) {
|
---|
132 | sb.Append(content.LocationNames[i]);
|
---|
133 | foreach (var kvp in content.IntValues) { sb.Append(",").Append(kvp.Value[i].ToString("D", CultureInfo.InvariantCulture)); }
|
---|
134 | foreach (var kvp in content.IntValues) { sb.Append(",").Append(kvp.Value[i] < 0 ? "" : content.LocationNames[kvp.Value[i]]); }
|
---|
135 | foreach (var kvp in content.DoubleValues) { sb.Append(",").Append(kvp.Value[i].ToString("G", CultureInfo.InvariantCulture)); }
|
---|
136 | foreach (var kvp in content.StringValues) { sb.Append(",").Append(kvp.Value[i]); }
|
---|
137 | sb.Append(Environment.NewLine);
|
---|
138 | }
|
---|
139 |
|
---|
140 | return sb.ToString();
|
---|
141 | }
|
---|
142 |
|
---|
143 | /*
|
---|
144 | private string CreateJsonSolution(BioBoostCompoundSolution content) {
|
---|
145 | var builder = new StringBuilder();
|
---|
146 | builder.Append("{ \"type\": \"FeatureCollection\", \n\"features\": [");
|
---|
147 | for (int i = 0; i < content.LocationNames.Length; i++) {
|
---|
148 | if (i > 0) builder.Append(",");
|
---|
149 | builder.Append("\n{ \"type\": \"Feature\", \"properties\": { \"NUTS_ID\": \"");
|
---|
150 | builder.Append(content.LocationNames[i]);
|
---|
151 | builder.Append("\"");
|
---|
152 | foreach (var kvp in content.IntValues) {
|
---|
153 | var target = content.LocationNames[i];
|
---|
154 | if (kvp.Value[i] >= 0) {
|
---|
155 | target = content.LocationNames[kvp.Value[i]];
|
---|
156 | }
|
---|
157 | builder.Append(",\n \"");
|
---|
158 | builder.Append(kvp.Key);
|
---|
159 | builder.Append("\": \"");
|
---|
160 | builder.Append(target);
|
---|
161 | builder.Append("\"");
|
---|
162 | }
|
---|
163 | foreach (var kvp in content.DoubleValues) {
|
---|
164 | builder.Append(",\n \"");
|
---|
165 | builder.Append(kvp.Key);
|
---|
166 | builder.Append("\": ");
|
---|
167 | builder.Append(kvp.Value[i].ToString("R", CultureInfo.InvariantCulture));
|
---|
168 | }
|
---|
169 | builder.Append("} }");
|
---|
170 | }
|
---|
171 | builder.Append("\n]\n}");
|
---|
172 |
|
---|
173 | return builder.ToString();
|
---|
174 | }
|
---|
175 | */
|
---|
176 | }
|
---|
177 | }
|
---|