Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CEDMA-Refactoring-Ticket419/HeuristicLab.CEDMA.Core/ProblemView.cs @ 1003

Last change on this file since 1003 was 1003, checked in by gkronber, 15 years ago

worked on CEDMA console and problem importer. #419 (Refactor CEDMA plugins)

File size: 4.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.Collections.Generic;
24using System.ComponentModel;
25using System.Drawing;
26using System.Data;
27using System.Linq;
28using System.Text;
29using System.Windows.Forms;
30using HeuristicLab.Core;
31using HeuristicLab.DataAnalysis;
32
33namespace HeuristicLab.CEDMA.Core {
34  public partial class ProblemView : ViewBase {
35    private Problem problem;
36
37    public ProblemView(Problem problem) {
38      this.problem = problem;
39      InitializeComponent();
40    }
41
42    protected override void UpdateControls() {
43      base.UpdateControls();
44      // TASK update text-boxes and datasetview
45    }
46
47    private void importButton_Click(object sender, EventArgs e) {
48      if(openFileDialog.ShowDialog(this) == DialogResult.OK) {
49        DatasetParser parser = new DatasetParser();
50        bool success = false;
51        try {
52          try {
53            parser.Import(openFileDialog.FileName, true);
54            success = true;
55          } catch(DataFormatException ex) {
56            ShowWarningMessageBox(ex);
57            // not possible to parse strictly => clear and try to parse non-strict
58            parser.Reset();
59            parser.Import(openFileDialog.FileName, false);
60            success = true;
61          }
62        } catch(DataFormatException ex) {
63          // if the non-strict parsing also failed then show the exception
64          ShowErrorMessageBox(ex);
65        }
66        if(success) {
67          Dataset dataset = (Dataset)problem.DataSet;
68          dataset.Rows = parser.Rows;
69          dataset.Columns = parser.Columns;
70          dataset.VariableNames = parser.VariableNames;
71          dataset.Name = parser.ProblemName;
72          dataset.Samples = new double[dataset.Rows * dataset.Columns];
73          Array.Copy(parser.Samples, dataset.Samples, dataset.Columns * dataset.Rows);
74          problem.TrainingSamplesStart = parser.TrainingSamplesStart;
75          problem.ValidationSamplesEnd = parser.TrainingSamplesStart;
76          problem.TrainingSamplesEnd = parser.TrainingSamplesEnd;
77          problem.ValidationSamplesStart = parser.ValidationSamplesStart;
78          problem.ValidationSamplesEnd = parser.ValidationSamplesEnd;
79          problem.TestSamplesStart = parser.TestSamplesStart;
80          problem.TestSamplesEnd = parser.TestSamplesEnd;
81          problem.AllowedTargetVariables.Add(parser.TargetVariable);
82
83          List<int> nonInputVariables = parser.NonInputVariables;
84          for(int i = 0; i < dataset.Columns; i++) {
85            if(!nonInputVariables.Contains(i)) problem.AllowedInputVariables.Add(i);
86          }
87          Refresh();
88        }
89      }
90
91    }
92    private void ShowWarningMessageBox(Exception ex) {
93      MessageBox.Show(ex.Message,
94                      "Warning - " + ex.GetType().Name,
95                      MessageBoxButtons.OK,
96                      MessageBoxIcon.Warning);
97    }
98    private void ShowErrorMessageBox(Exception ex) {
99      MessageBox.Show(BuildErrorMessage(ex),
100                      "Error - " + ex.GetType().Name,
101                      MessageBoxButtons.OK,
102                      MessageBoxIcon.Error);
103    }
104    private string BuildErrorMessage(Exception ex) {
105      StringBuilder sb = new StringBuilder();
106      sb.Append("Sorry, but something went wrong!\n\n" + ex.Message + "\n\n" + ex.StackTrace);
107
108      while(ex.InnerException != null) {
109        ex = ex.InnerException;
110        sb.Append("\n\n-----\n\n" + ex.Message + "\n\n" + ex.StackTrace);
111      }
112      return sb.ToString();
113    }
114  }
115}
Note: See TracBrowser for help on using the repository browser.