Free cookie consent management tool by TermsFeed Policy Generator

source: branches/RegressionBenchmarks/HeuristicLab.Problems.DataAnalysis.Views/3.4/DataAnalysisProblemView.cs @ 7729

Last change on this file since 7729 was 7309, checked in by sforsten, 13 years ago

#1752: -data set is exported exactly in the way it is stored in the Dataset class

File size: 3.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.IO;
24using System.Text;
25using System.Windows.Forms;
26using HeuristicLab.MainForm;
27using HeuristicLab.Optimization.Views;
28using HeuristicLab.PluginInfrastructure;
29
30namespace HeuristicLab.Problems.DataAnalysis.Views {
31  [View("DataAnalysisProblem View")]
32  [Content(typeof(IDataAnalysisProblem), true)]
33  public partial class DataAnalysisProblemView : ProblemView {
34
35    private BenchmarkGeneratorDialog problemDataSelectorDialog;
36
37    public DataAnalysisProblemView() {
38      InitializeComponent();
39    }
40
41    public new IDataAnalysisProblem Content {
42      get { return (IDataAnalysisProblem)base.Content; }
43      set { base.Content = value; }
44    }
45
46    protected override void SetEnabledStateOfControls() {
47      base.SetEnabledStateOfControls();
48      ImportButton.Enabled = !Locked && !ReadOnly && Content != null;
49      LoadButton.Enabled = !Locked && !ReadOnly && Content != null;
50      ExportButton.Enabled = !Locked && !ReadOnly && Content != null;
51    }
52
53    private void ImportButton_Click(object sender, System.EventArgs e) {
54      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
55        try {
56          Content.ImportProblemDataFromFile(openFileDialog.FileName);
57        }
58        catch (Exception ex) {
59          ErrorHandling.ShowErrorDialog(this, ex);
60        }
61      }
62    }
63
64    private void LoadButton_Click(object sender, EventArgs e) {
65      if (problemDataSelectorDialog == null) {
66        problemDataSelectorDialog = new BenchmarkGeneratorDialog(Content);
67      }
68      if (problemDataSelectorDialog.ShowDialog(this) == DialogResult.OK) {
69        try {
70          Content.ProblemData = problemDataSelectorDialog.ProblemData;
71        }
72        catch (Exception ex) {
73          ErrorHandling.ShowErrorDialog(this, ex);
74        }
75      }
76    }
77
78    private void ExportButton_Click(object sender, EventArgs e) {
79      if (saveFileDialog.ShowDialog(this) == DialogResult.OK) {
80        try {
81          ExportProblemData(Content.ProblemData.Dataset, saveFileDialog.FileName);
82        }
83        catch (Exception ex) {
84          ErrorHandling.ShowErrorDialog(this, ex);
85        }
86      }
87    }
88
89    private void ExportProblemData(Dataset dataset, string file) {
90      StringBuilder strBuilder = new StringBuilder();
91
92      foreach (var variable in dataset.VariableNames) {
93        strBuilder.Append(variable + ";");
94      }
95      strBuilder.Remove(strBuilder.Length - 1, 1);
96      strBuilder.AppendLine();
97
98      for (int i = 0; i < dataset.Rows; i++) {
99        for (int j = 0; j < dataset.Columns; j++) {
100          strBuilder.Append(dataset.GetValue(i, j) + ";");
101        }
102        strBuilder.Remove(strBuilder.Length - 1, 1);
103        strBuilder.AppendLine();
104      }
105
106      using (StreamWriter writer = new StreamWriter(file)) {
107        writer.Write(strBuilder);
108      }
109    }
110  }
111}
Note: See TracBrowser for help on using the repository browser.