Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.StructureIdentification/StructIdProblemInjectorView.cs @ 293

Last change on this file since 293 was 274, checked in by gkronber, 16 years ago

addition changeset (r273). added a warning dialog showing what was the problem while parsing if strict parsing fails.

File size: 4.7 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.Text;
28using System.Windows.Forms;
29using HeuristicLab.Core;
30using HeuristicLab.Data;
31using HeuristicLab.Operators;
32using HeuristicLab.DataAnalysis;
33
34namespace HeuristicLab.StructureIdentification {
35  public partial class StructIdProblemInjectorView : ViewBase {
36    public StructIdProblemInjector StructIdProblemInjector {
37      get { return (StructIdProblemInjector)Item; }
38      set { base.Item = value; }
39    }
40
41    public StructIdProblemInjectorView() {
42      InitializeComponent();
43    }
44    public StructIdProblemInjectorView(StructIdProblemInjector structIdProblemInjector)
45      : this() {
46      StructIdProblemInjector = structIdProblemInjector;
47    }
48
49    protected override void RemoveItemEvents() {
50      operatorBaseVariableInfosView.Operator = null;
51      operatorBaseDescriptionView.Operator = null;
52      base.RemoveItemEvents();
53    }
54    protected override void AddItemEvents() {
55      base.AddItemEvents();
56      operatorBaseVariableInfosView.Operator = StructIdProblemInjector;
57      operatorBaseDescriptionView.Operator = StructIdProblemInjector;
58    }
59
60    protected override void UpdateControls() {
61      base.UpdateControls();
62      if (StructIdProblemInjector == null) {
63        importInstanceButton.Enabled = false;
64      } else {
65        Dataset dataset = (Dataset)StructIdProblemInjector.GetVariable("Dataset").Value;
66        datasetView.Dataset = dataset;
67        importInstanceButton.Enabled = true;
68      }
69    }
70
71    private void importInstanceButton_Click(object sender, EventArgs e) {
72      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
73        DatasetParser parser = new DatasetParser();
74        bool success = false;
75        try {
76          try {
77            parser.Import(openFileDialog.FileName, true);
78            success = true;
79          } catch(DataFormatException ex) {
80            ShowWarningMessageBox(ex);
81            // not possible to parse strictly => try to parse non-strict
82            parser.Import(openFileDialog.FileName, false);
83            success = true;
84          }
85        } catch(DataFormatException ex) {
86          // if the non-strict parsing also failed then show the exception
87          ShowErrorMessageBox(ex);
88        }
89        if (success) {
90          Dataset dataset = (Dataset)StructIdProblemInjector.GetVariable("Dataset").Value;
91          dataset.Rows = parser.TrainingSamplesEnd - parser.TrainingSamplesStart;
92          dataset.Columns = parser.Columns;
93          dataset.VariableNames = parser.VariableNames;
94          dataset.Name = parser.ProblemName;
95          dataset.Samples = new double[dataset.Rows * dataset.Columns];
96          Array.Copy(parser.Samples, dataset.Samples, dataset.Columns * dataset.Rows);
97
98          ((IntData)StructIdProblemInjector.GetVariable("TargetVariable").Value).Data = parser.TargetVariable;
99          Refresh();
100        }
101      }
102    }
103
104    private void ShowWarningMessageBox(Exception ex) {
105      MessageBox.Show(ex.Message,
106                      "Warning - " + ex.GetType().Name,
107                      MessageBoxButtons.OK,
108                      MessageBoxIcon.Warning);
109    }
110    private void ShowErrorMessageBox(Exception ex) {
111      MessageBox.Show(BuildErrorMessage(ex),
112                      "Error - " + ex.GetType().Name,
113                      MessageBoxButtons.OK,
114                      MessageBoxIcon.Error);
115    }
116    private string BuildErrorMessage(Exception ex) {
117      StringBuilder sb = new StringBuilder();
118      sb.Append("Sorry, but something went wrong!\n\n" + ex.Message + "\n\n" + ex.StackTrace);
119
120      while(ex.InnerException != null) {
121        ex = ex.InnerException;
122        sb.Append("\n\n-----\n\n" + ex.Message + "\n\n" + ex.StackTrace);
123      }
124      return sb.ToString();
125    }
126  }
127}
Note: See TracBrowser for help on using the repository browser.