Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12547 was 8032, checked in by sforsten, 13 years ago

#1758:

  • merged trunk r7330:8022 into branch
  • importButton shows a message, if clicked and it can't be used
File size: 4.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Linq;
25using System.Text;
26using System.Windows.Forms;
27using HeuristicLab.Core;
28using HeuristicLab.MainForm;
29using HeuristicLab.Persistence.Core;
30using HeuristicLab.Persistence.Default.Xml;
31using HeuristicLab.PluginInfrastructure;
32using ICSharpCode.SharpZipLib.Zip;
33
34namespace HeuristicLab.Problems.DataAnalysis.Views {
35  [View("RegressionSolution View")]
36  [Content(typeof(RegressionSolutionBase), false)]
37  public partial class RegressionSolutionView : DataAnalysisSolutionView {
38    public RegressionSolutionView() {
39      InitializeComponent();
40    }
41
42    public new RegressionSolutionBase Content {
43      get { return (RegressionSolutionBase)base.Content; }
44      set { base.Content = value; }
45    }
46
47    #region drag and drop
48    protected override void itemsListView_DragEnter(object sender, DragEventArgs e) {
49      validDragOperation = false;
50      if (ReadOnly) return;
51
52      var dropData = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
53      if (dropData is RegressionProblemData) validDragOperation = true;
54      else if (dropData is IValueParameter) {
55        var param = (IValueParameter)dropData;
56        if (param.Value is RegressionProblemData) validDragOperation = true;
57      }
58    }
59    #endregion
60
61    protected void importButton_Click(object sender, EventArgs e) {
62      if (Locked || ReadOnly || Content == null) {
63        MessageBox.Show("The Regression Solution is read-only or locked. Save the solution and open it again or use " +
64          "the 'Simplify' button to get a copy of the current solution. Import will then be enabled.");
65        return;
66      }
67      if (openFileDialog.ShowDialog(this) != DialogResult.OK) return;
68      ImportProblemData(openFileDialog.FileName);
69    }
70
71    protected void ImportProblemData(string filename) {
72      object hlFile = null;
73      try {
74        hlFile = XmlParser.Deserialize(filename);
75      }
76      catch (PersistenceException ex) {
77        ErrorHandling.ShowErrorDialog(this, ex);
78        return;
79      }
80      catch (ZipException ex) {
81        ErrorHandling.ShowErrorDialog(this, ex);
82        return;
83      }
84
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      }
93
94      if (problemData == null) {
95        ErrorHandling.ShowErrorDialog(this, new NullReferenceException("The problem data is null." + Environment.NewLine
96                                                                     + "The .hl-file is no RegressionProblemData or RegressionProblem."));
97        return;
98      }
99
100      if (CheckCompatibilityOfProblemData(Content.ProblemData, problemData))
101        Content.ProblemData = problemData;
102    }
103
104    protected virtual bool CheckCompatibilityOfProblemData(IRegressionProblemData iRegressionProblemData, IRegressionProblemData problemData) {
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 + "'");
110
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.");
115      }
116
117      if (message.Length != 0) {
118        ErrorHandling.ShowErrorDialog(this, new InvalidOperationException(message.ToString()));
119        return false;
120      }
121      return true;
122    }
123  }
124}
Note: See TracBrowser for help on using the repository browser.