Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.StructureIdentification/StructIdProblemInjectorView.cs @ 13365

Last change on this file since 13365 was 495, checked in by gkronber, 16 years ago

fixed a problem that caused an exception when a dataset was imported after one of the local variables had been removed.

File size: 6.0 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      : base() {
43      InitializeComponent();
44    }
45    public StructIdProblemInjectorView(StructIdProblemInjector structIdProblemInjector)
46      : this() {
47      StructIdProblemInjector = structIdProblemInjector;
48      variablesView.Operator = structIdProblemInjector;
49      variablesView.Update();
50    }
51
52    protected override void RemoveItemEvents() {
53      operatorBaseVariableInfosView.Operator = null;
54      operatorBaseDescriptionView.Operator = null;
55      base.RemoveItemEvents();
56    }
57    protected override void AddItemEvents() {
58      base.AddItemEvents();
59      operatorBaseVariableInfosView.Operator = StructIdProblemInjector;
60      operatorBaseDescriptionView.Operator = StructIdProblemInjector;
61    }
62
63    protected override void UpdateControls() {
64      base.UpdateControls();
65      if(StructIdProblemInjector == null) {
66        importInstanceButton.Enabled = false;
67      } else {
68        Dataset dataset = (Dataset)StructIdProblemInjector.GetVariable("Dataset").Value;
69        datasetView.Dataset = dataset;
70        importInstanceButton.Enabled = true;
71      }
72    }
73
74    private void importInstanceButton_Click(object sender, EventArgs e) {
75      if(openFileDialog.ShowDialog(this) == DialogResult.OK) {
76        DatasetParser parser = new DatasetParser();
77        bool success = false;
78        try {
79          try {
80            parser.Import(openFileDialog.FileName, true);
81            success = true;
82          } catch(DataFormatException ex) {
83            ShowWarningMessageBox(ex);
84            // not possible to parse strictly => clear and try to parse non-strict
85            parser.Reset();
86            parser.Import(openFileDialog.FileName, false);
87            success = true;
88          }
89        } catch(DataFormatException ex) {
90          // if the non-strict parsing also failed then show the exception
91          ShowErrorMessageBox(ex);
92        }
93        if(success) {
94          Dataset dataset = (Dataset)StructIdProblemInjector.GetVariable("Dataset").Value;
95          dataset.Rows = parser.Rows;
96          dataset.Columns = parser.Columns;
97          dataset.VariableNames = parser.VariableNames;
98          dataset.Name = parser.ProblemName;
99          dataset.Samples = new double[dataset.Rows * dataset.Columns];
100          Array.Copy(parser.Samples, dataset.Samples, dataset.Columns * dataset.Rows);
101          TrySetVariable("TrainingSamplesStart", parser.TrainingSamplesStart);
102          TrySetVariable("TrainingSamplesStart", parser.TrainingSamplesStart);
103          TrySetVariable("TrainingSamplesEnd", parser.TrainingSamplesEnd);
104          TrySetVariable("ValidationSamplesStart", parser.ValidationSamplesStart);
105          TrySetVariable("ValidationSamplesEnd", parser.ValidationSamplesEnd);
106          TrySetVariable("TestSamplesStart", parser.TestSamplesStart);
107          TrySetVariable("TestSamplesEnd", parser.TestSamplesEnd);
108          TrySetVariable("TargetVariable", parser.TargetVariable);
109
110          IVariable var = StructIdProblemInjector.GetVariable("AllowedFeatures");
111          if(var != null) {
112            ItemList<IntData> allowedFeatures = (ItemList<IntData>)var.Value;
113            allowedFeatures.Clear();
114            List<int> nonInputVariables = parser.NonInputVariables;
115            for(int i = 0; i < dataset.Columns; i++) {
116              if(!nonInputVariables.Contains(i)) allowedFeatures.Add(new IntData(i));
117            }
118          }
119          Refresh();
120        }
121      }
122    }
123
124    private void TrySetVariable(string name, int value) {
125      IVariable var = StructIdProblemInjector.GetVariable(name);
126      if(var != null) {
127        ((IntData)var.Value).Data = value;
128      }
129    }
130
131    private void ShowWarningMessageBox(Exception ex) {
132      MessageBox.Show(ex.Message,
133                      "Warning - " + ex.GetType().Name,
134                      MessageBoxButtons.OK,
135                      MessageBoxIcon.Warning);
136    }
137    private void ShowErrorMessageBox(Exception ex) {
138      MessageBox.Show(BuildErrorMessage(ex),
139                      "Error - " + ex.GetType().Name,
140                      MessageBoxButtons.OK,
141                      MessageBoxIcon.Error);
142    }
143    private string BuildErrorMessage(Exception ex) {
144      StringBuilder sb = new StringBuilder();
145      sb.Append("Sorry, but something went wrong!\n\n" + ex.Message + "\n\n" + ex.StackTrace);
146
147      while(ex.InnerException != null) {
148        ex = ex.InnerException;
149        sb.Append("\n\n-----\n\n" + ex.Message + "\n\n" + ex.StackTrace);
150      }
151      return sb.ToString();
152    }
153  }
154}
Note: See TracBrowser for help on using the repository browser.