Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ChangeDatasetOfRegressionModel/HeuristicLab.Problems.DataAnalysis.Views/3.4/Solution Views/RegressionSolutionView.cs @ 7487

Last change on this file since 7487 was 7487, checked in by sforsten, 12 years ago

#1758: changes according to mkommend's reviewing comments have been made and catch blocks now only catch specific exceptions

File size: 4.8 KB
RevLine 
[5829]1#region License Information
2/* HeuristicLab
[7259]3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[5829]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
[7338]22using System;
23using System.Collections.Generic;
[7347]24using System.Linq;
25using System.Text;
[5829]26using System.Windows.Forms;
[6666]27using HeuristicLab.Core;
[5829]28using HeuristicLab.MainForm;
[7487]29using HeuristicLab.Persistence.Core;
[7338]30using HeuristicLab.Persistence.Default.Xml;
31using HeuristicLab.PluginInfrastructure;
[7487]32using ICSharpCode.SharpZipLib.Zip;
[5829]33
34namespace HeuristicLab.Problems.DataAnalysis.Views {
[5834]35  [View("RegressionSolution View")]
[6652]36  [Content(typeof(RegressionSolutionBase), false)]
[5829]37  public partial class RegressionSolutionView : DataAnalysisSolutionView {
38    public RegressionSolutionView() {
39      InitializeComponent();
40    }
41
[6597]42    public new RegressionSolutionBase Content {
43      get { return (RegressionSolutionBase)base.Content; }
[5829]44      set { base.Content = value; }
45    }
[6653]46
[7399]47    protected override void SetEnabledStateOfControls() {
48      base.SetEnabledStateOfControls();
[7468]49      importButton.Enabled = !Locked && !ReadOnly && Content != null;
[7399]50    }
51
[6653]52    #region drag and drop
53    protected override void itemsListView_DragEnter(object sender, DragEventArgs e) {
54      validDragOperation = false;
[6666]55      if (ReadOnly) return;
56
57      var dropData = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
58      if (dropData is RegressionProblemData) validDragOperation = true;
59      else if (dropData is IValueParameter) {
60        var param = (IValueParameter)dropData;
61        if (param.Value is RegressionProblemData) validDragOperation = true;
[6653]62      }
63    }
64    #endregion
[7338]65
[7487]66    protected void importButton_Click(object sender, EventArgs e) {
[7468]67      if (openFileDialog.ShowDialog(this) != DialogResult.OK) return;
[7487]68      ImportProblemData(openFileDialog.FileName);
69    }
[7347]70
[7487]71    protected void ImportProblemData(string filename) {
[7468]72      object hlFile = null;
73      try {
[7487]74        hlFile = XmlParser.Deserialize(filename);
[7468]75      }
[7487]76      catch (PersistenceException ex) {
[7468]77        ErrorHandling.ShowErrorDialog(this, ex);
78        return;
79      }
[7487]80      catch (ZipException ex) {
81        ErrorHandling.ShowErrorDialog(this, ex);
82        return;
83      }
[7338]84
[7468]85      IRegressionProblemData problemData = null;
86      if (hlFile is IRegressionProblemData) {
87        problemData = (IRegressionProblemData)hlFile;
88      } else if (hlFile is IRegressionProblem) {
89        problemData = ((IRegressionProblem)hlFile).ProblemData;
90      } else if (hlFile is IRegressionSolution) {
91        problemData = ((IRegressionSolution)hlFile).ProblemData;
92      }
[7338]93
[7468]94      if (problemData == null) {
[7487]95        ErrorHandling.ShowErrorDialog(this, new NullReferenceException("The problem data is null." + Environment.NewLine
96                                                                     + "The .hl-file is no RegressionProblemData or RegressionProblem."));
[7468]97        return;
98      }
[7399]99
[7487]100      if (CheckCompatibilityOfProblemData(Content.ProblemData, problemData))
101        Content.ProblemData = problemData;
102    }
103
104    protected virtual bool CheckCompatibilityOfProblemData(IRegressionProblemData iRegressionProblemData, IRegressionProblemData problemData) {
[7468]105      StringBuilder message = new StringBuilder();
106      if (!problemData.TargetVariable.Equals(Content.ProblemData.TargetVariable))
107        message.AppendLine("The target variables are not matching. Old target variable: '"
108                         + Content.ProblemData.TargetVariable
109                         + "'. New targetvariable: '" + problemData.TargetVariable + "'");
[7399]110
[7468]111      List<string> variables = problemData.InputVariables.Select(x => x.Value).ToList();
112      foreach (var item in Content.ProblemData.InputVariables.CheckedItems) {
113        if (!variables.Contains(item.Value.Value))
114          message.AppendLine("Input variable '" + item.Value.Value + "' is not in the new problem data.");
[7338]115      }
[7468]116
[7487]117      if (message.Length != 0) {
118        ErrorHandling.ShowErrorDialog(this, new InvalidOperationException(message.ToString()));
119        return false;
120      }
121      return true;
[7338]122    }
[5829]123  }
124}
Note: See TracBrowser for help on using the repository browser.