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