Free cookie consent management tool by TermsFeed Policy Generator

source: branches/RefactorPluginInfrastructure-2522/HeuristicLab.Algorithms.DataAnalysis.Views/3.4/CrossValidationView.cs @ 13338

Last change on this file since 13338 was 13338, checked in by gkronber, 8 years ago

#2522:

  • moved UI components out of HeuristicLab.PluginInfrastructure -> HeuristicLab.PluginInfrastructure.UI
  • moved ErrorDialog to HeuristicLab.MainForm.WindowsForms
  • moved ErrorHandling (for building an error message string) to HeuristicLab.Common
  • Changed exception handlers in Views to use MainForm.ShowError()
  • Changed usages for ErrorDialog in non-UI components to throw exceptions instead.
File size: 16.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Windows.Forms;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Core.Views;
29using HeuristicLab.MainForm;
30using HeuristicLab.Optimization;
31using HeuristicLab.PluginInfrastructure;
32using HeuristicLab.Problems.DataAnalysis;
33
34namespace HeuristicLab.Algorithms.DataAnalysis.Views {
35  [View("CrossValidation View")]
36  [Content(typeof(CrossValidation), true)]
37  public sealed partial class CrossValidationView : NamedItemView {
38    private TypeSelectorDialog algorithmTypeSelectorDialog;
39    private TypeSelectorDialog problemTypeSelectorDialog;
40
41    public CrossValidationView() {
42      InitializeComponent();
43    }
44
45    public new CrossValidation Content {
46      get { return (CrossValidation)base.Content; }
47      set { base.Content = value; }
48    }
49
50    protected override void OnContentChanged() {
51      base.OnContentChanged();
52      if (Content == null) {
53        workersNumericUpDown.Value = 1;
54        foldsNumericUpDown.Value = 2;
55        samplesStartStringConvertibleValueView.Content = null;
56        samplesEndStringConvertibleValueView.Content = null;
57        algorithmNamedItemView.Content = null;
58        algorithmProblemViewHost.Content = null;
59        algorithmParameterCollectionView.Content = null;
60        resultCollectionView.Content = null;
61        runCollectionView.Content = null;
62        storeAlgorithmInEachRunCheckBox.Checked = true;
63      } else {
64        Locked = ReadOnly = Content.ExecutionState == ExecutionState.Started;
65        workersNumericUpDown.Value = Content.NumberOfWorkers.Value;
66        foldsNumericUpDown.Value = Content.Folds.Value;
67        samplesStartStringConvertibleValueView.Content = Content.SamplesStart;
68        samplesEndStringConvertibleValueView.Content = Content.SamplesEnd;
69        UpdateAlgorithmView();
70        UpdateProblemView();
71        runCollectionView.Content = Content.Runs;
72        algorithmParameterCollectionView.Content = ((IParameterizedNamedItem)Content).Parameters;
73        resultCollectionView.Content = Content.Results;
74        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
75        storeAlgorithmInEachRunCheckBox.Checked = Content.StoreAlgorithmInEachRun;
76      }
77    }
78
79    protected override void RegisterContentEvents() {
80      base.RegisterContentEvents();
81      Content.AlgorithmChanged += new EventHandler(Content_AlgorithmChanged);
82      Content.ProblemChanged += new EventHandler(Content_ProblemChanged);
83      Content.Folds.ValueChanged += new EventHandler(Content_Folds_ValueChanged);
84      Content.NumberOfWorkers.ValueChanged += new EventHandler(Content_NumberOfWorker_ValueChanged);
85
86      Content.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
87      Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
88      Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
89      Content.StoreAlgorithmInEachRunChanged += new EventHandler(Content_StoreAlgorithmInEachRunChanged);
90    }
91
92    protected override void DeregisterContentEvents() {
93      Content.AlgorithmChanged -= new EventHandler(Content_AlgorithmChanged);
94      Content.ProblemChanged -= new EventHandler(Content_ProblemChanged);
95      Content.Folds.ValueChanged -= new EventHandler(Content_Folds_ValueChanged);
96      Content.NumberOfWorkers.ValueChanged -= new EventHandler(Content_NumberOfWorker_ValueChanged);
97
98      Content.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
99      Content.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
100      Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
101      Content.StoreAlgorithmInEachRunChanged -= new EventHandler(Content_StoreAlgorithmInEachRunChanged);
102      base.DeregisterContentEvents();
103    }
104
105    protected override void OnClosed(FormClosedEventArgs e) {
106      if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) {
107        //The content must be stopped if no other view showing the content is available
108        var optimizers = MainFormManager.MainForm.Views.OfType<IContentView>().Where(v => v != this).Select(v => v.Content).OfType<IOptimizer>();
109        if (!optimizers.Contains(Content)) {
110          var nestedOptimizers = optimizers.SelectMany(opt => opt.NestedOptimizers);
111          if (!nestedOptimizers.Contains(Content)) Content.Stop();
112        }
113      }
114      base.OnClosed(e);
115    }
116
117    protected override void SetEnabledStateOfControls() {
118      if (InvokeRequired) Invoke((Action)SetEnabledStateOfControls);
119      else {
120        base.SetEnabledStateOfControls();
121        this.Enabled = Content != null;
122
123        if (Content != null) {
124          storeAlgorithmInEachRunCheckBox.Enabled = !ReadOnly;
125          openAlgorithmButton.Enabled = !ReadOnly;
126          newAlgorithmButton.Enabled = !ReadOnly;
127
128          algorithmNamedItemView.Enabled = Content.Algorithm != null && (Content.ExecutionState == ExecutionState.Prepared || Content.ExecutionState == ExecutionState.Stopped);
129          algorithmTabControl.Enabled = Content.Algorithm != null && (Content.ExecutionState == ExecutionState.Prepared || Content.ExecutionState == ExecutionState.Stopped);
130          foldsNumericUpDown.Enabled = Content.ExecutionState == ExecutionState.Prepared;
131          samplesStartStringConvertibleValueView.Enabled = Content.ExecutionState == ExecutionState.Prepared;
132          samplesEndStringConvertibleValueView.Enabled = Content.ExecutionState == ExecutionState.Prepared;
133          workersNumericUpDown.Enabled = (Content.ExecutionState == ExecutionState.Prepared) || (Content.ExecutionState == ExecutionState.Paused);
134
135          startButton.Enabled = (Content.ExecutionState == ExecutionState.Prepared) || (Content.ExecutionState == ExecutionState.Paused);
136          pauseButton.Enabled = Content.ExecutionState == ExecutionState.Started;
137          stopButton.Enabled = (Content.ExecutionState == ExecutionState.Started) || (Content.ExecutionState == ExecutionState.Paused);
138          resetButton.Enabled = Content.ExecutionState != ExecutionState.Started;
139        }
140      }
141    }
142
143    #region Content Events
144    private void Content_AlgorithmChanged(object sender, EventArgs e) {
145      UpdateAlgorithmView();
146      UpdateProblemView();
147      SetEnabledStateOfControls();
148    }
149    private void UpdateAlgorithmView() {
150      algorithmNamedItemView.Content = Content.Algorithm;
151      UpdateProblemView();
152    }
153
154    private void Content_ProblemChanged(object sender, EventArgs e) {
155      UpdateProblemView();
156      SetEnabledStateOfControls();
157    }
158    private void UpdateProblemView() {
159      algorithmProblemViewHost.Content = Content.Problem;
160    }
161
162    private void Content_Folds_ValueChanged(object sender, EventArgs e) {
163      foldsNumericUpDown.Value = Content.Folds.Value;
164    }
165    private void Content_NumberOfWorker_ValueChanged(object sender, EventArgs e) {
166      workersNumericUpDown.Value = Content.NumberOfWorkers.Value;
167    }
168
169    private void Content_ExecutionStateChanged(object sender, EventArgs e) {
170      if (InvokeRequired)
171        Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
172      else {
173        Locked = ReadOnly = Content.ExecutionState == ExecutionState.Started;
174        SetEnabledStateOfControls();
175      }
176    }
177
178    private void Content_ExecutionTimeChanged(object sender, EventArgs e) {
179      if (InvokeRequired)
180        Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
181      else
182        executionTimeTextBox.Text = Content == null ? "-" : Content.ExecutionTime.ToString();
183    }
184    private void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
185      if (InvokeRequired)
186        Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
187      else
188        MainFormManager.MainForm.ShowError(e.Value.Message, e.Value);
189    }
190    private void Content_StoreAlgorithmInEachRunChanged(object sender, EventArgs e) {
191      if (InvokeRequired)
192        Invoke(new EventHandler(Content_StoreAlgorithmInEachRunChanged), sender, e);
193      else
194        storeAlgorithmInEachRunCheckBox.Checked = Content.StoreAlgorithmInEachRun;
195    }
196    #endregion
197
198    #region GUI events
199    private void foldsNumericUpDown_Validated(object sender, EventArgs e) {
200      if (foldsNumericUpDown.Text == string.Empty)
201        foldsNumericUpDown.Text = foldsNumericUpDown.Value.ToString();
202    }
203    private void foldsNumericUpDown_ValueChanged(object sender, EventArgs e) {
204      if (Content != null)
205        Content.Folds.Value = (int)foldsNumericUpDown.Value;
206    }
207
208    private void workersNumericUpDown_Validated(object sender, EventArgs e) {
209      if (workersNumericUpDown.Text == string.Empty)
210        workersNumericUpDown.Text = workersNumericUpDown.Value.ToString();
211    }
212    private void workersNumericUpDown_ValueChanged(object sender, EventArgs e) {
213      if (Content != null)
214        Content.NumberOfWorkers.Value = (int)workersNumericUpDown.Value;
215    }
216
217    private void startButton_Click(object sender, EventArgs e) {
218      Content.Start();
219    }
220    private void pauseButton_Click(object sender, EventArgs e) {
221      Content.Pause();
222    }
223    private void stopButton_Click(object sender, EventArgs e) {
224      Content.Stop();
225    }
226    private void resetButton_Click(object sender, EventArgs e) {
227      Content.Prepare(false);
228    }
229
230    private void newAlgorithmButton_Click(object sender, EventArgs e) {
231      if (algorithmTypeSelectorDialog == null) {
232        algorithmTypeSelectorDialog = new TypeSelectorDialog();
233        algorithmTypeSelectorDialog.Caption = "Select Algorithm";
234        algorithmTypeSelectorDialog.TypeSelector.Caption = "Available Algorithms";
235        algorithmTypeSelectorDialog.TypeSelector.Configure(typeof(IAlgorithm), false, true);
236      }
237      if (algorithmTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
238        try {
239          Content.Algorithm = (IAlgorithm)algorithmTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
240        } catch (Exception ex) {
241          MainFormManager.MainForm.ShowError(ex.Message, ex);
242        }
243      }
244    }
245
246    private void openAlgorithmButton_Click(object sender, EventArgs e) {
247      openFileDialog.Title = "Open Algorithm";
248      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
249        algorithmTabControl.Enabled = false;
250
251        ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) {
252          try {
253            if (error != null) throw error;
254            IAlgorithm algorithm = content as IAlgorithm;
255            if (algorithm == null || !(algorithm.Problem is IDataAnalysisProblem))
256              MessageBox.Show(this, "The selected file does not contain an algorithm or the problem of the algorithm is not a DataAnalysisProblem.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error);
257            else
258              Content.Algorithm = algorithm;
259          } catch (Exception ex) {
260            MainFormManager.MainForm.ShowError(ex.Message, ex);
261          } finally {
262            Invoke(new Action(delegate() {
263              algorithmTabControl.Enabled = true;
264            }));
265          }
266        });
267      }
268    }
269
270    private void newProblemButton_Click(object sender, EventArgs e) {
271      if (problemTypeSelectorDialog == null) {
272        problemTypeSelectorDialog = new TypeSelectorDialog();
273        problemTypeSelectorDialog.Caption = "Select Problem";
274        problemTypeSelectorDialog.TypeSelector.Caption = "Available Problems";
275      }
276      problemTypeSelectorDialog.TypeSelector.Configure(new List<Type>() { Content.ProblemType, Content.Algorithm.ProblemType }, false, true, true);
277      if (problemTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
278        Content.Problem = (IDataAnalysisProblem)problemTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
279      }
280    }
281
282    private void openProblemButton_Click(object sender, EventArgs e) {
283      openFileDialog.Title = "Open Problem";
284      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
285        newProblemButton.Enabled = openProblemButton.Enabled = false;
286        algorithmProblemViewHost.Enabled = false;
287
288        ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) {
289          try {
290            if (error != null) throw error;
291            IDataAnalysisProblem problem = content as IDataAnalysisProblem;
292            if (problem == null && (Content.Algorithm.ProblemType.IsAssignableFrom(content.GetType())))
293              Invoke(new Action(() =>
294                MessageBox.Show(this, "The selected file does not contain a DataAnalysisProblem problem.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error)));
295            else
296              Content.Problem = problem;
297          } catch (Exception ex) {
298            Invoke(new Action(() => MainFormManager.MainForm.ShowError(ex.Message, ex)));
299          } finally {
300            Invoke(new Action(delegate() {
301              algorithmProblemViewHost.Enabled = true;
302              newProblemButton.Enabled = openProblemButton.Enabled = true;
303            }));
304          }
305        });
306      }
307    }
308
309    private void algorithmTabPage_DragEnterOver(object sender, DragEventArgs e) {
310      e.Effect = DragDropEffects.None;
311      IAlgorithm algorithm = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IAlgorithm;
312      if (!ReadOnly && algorithm != null &&
313        (algorithm.ProblemType != null || Content.ProblemType.IsAssignableFrom(algorithm.Problem.GetType()))) {
314        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
315        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
316        else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
317        else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
318        else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
319      }
320    }
321    private void algorithmTabPage_DragDrop(object sender, DragEventArgs e) {
322      if (e.Effect != DragDropEffects.None) {
323        IAlgorithm algorithm = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IAlgorithm;
324        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) algorithm = (IAlgorithm)algorithm.Clone();
325        Content.Algorithm = algorithm;
326      }
327    }
328
329    private void algorithmProblemTabPage_DragEnterOver(object sender, DragEventArgs e) {
330      e.Effect = DragDropEffects.None;
331      if (ReadOnly) return;
332      IProblem problem = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IProblem;
333      if (problem != null && Content.ProblemType.IsAssignableFrom(problem.GetType()) &&
334        Content.Algorithm.ProblemType.IsAssignableFrom(problem.GetType())) {
335        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
336        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
337        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
338        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
339        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
340      }
341    }
342    private void algorithmProblemTabPage_DragDrop(object sender, DragEventArgs e) {
343      if (e.Effect != DragDropEffects.None) {
344        IDataAnalysisProblem problem = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IDataAnalysisProblem;
345        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) problem = (IDataAnalysisProblem)problem.Clone();
346        Content.Problem = problem;
347      }
348    }
349
350    private void storeAlgorithmInEachRunCheckBox_CheckedChanged(object sender, EventArgs e) {
351      if (Content != null) Content.StoreAlgorithmInEachRun = storeAlgorithmInEachRunCheckBox.Checked;
352    }
353    #endregion
354
355  }
356}
Note: See TracBrowser for help on using the repository browser.