Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization.Views/3.3/BatchRunView.cs @ 3260

Last change on this file since 3260 was 3260, checked in by swagner, 14 years ago

Continued work on algorithm batch processing (#947).

File size: 10.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Windows.Forms;
25using HeuristicLab.Common;
26using HeuristicLab.Core.Views;
27using HeuristicLab.MainForm;
28using HeuristicLab.Persistence.Default.Xml;
29
30namespace HeuristicLab.Optimization.Views {
31  /// <summary>
32  /// The base class for visual representations of items.
33  /// </summary>
34  [View("BatchRun View")]
35  [Content(typeof(BatchRun), true)]
36  public sealed partial class BatchRunView : NamedItemView {
37    private TypeSelectorDialog algorithmTypeSelectorDialog;
38
39    public new BatchRun Content {
40      get { return (BatchRun)base.Content; }
41      set { base.Content = value; }
42    }
43
44    /// <summary>
45    /// Initializes a new instance of <see cref="ItemBaseView"/>.
46    /// </summary>
47    public BatchRunView() {
48      InitializeComponent();
49    }
50    /// <summary>
51    /// Intializes a new instance of <see cref="ItemBaseView"/> with the given <paramref name="item"/>.
52    /// </summary>
53    /// <param name="item">The item that should be displayed.</param>
54    public BatchRunView(BatchRun content)
55      : this() {
56      Content = content;
57    }
58
59    protected override void DeregisterContentEvents() {
60      Content.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
61      Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
62      Content.Prepared -= new EventHandler(Content_Prepared);
63      Content.AlgorithmChanged -= new EventHandler(Content_AlgorithmChanged);
64      Content.Started -= new EventHandler(Content_Started);
65      Content.Stopped -= new EventHandler(Content_Stopped);
66      Content.RepetitionsChanged -= new EventHandler(Content_RepetitionsChanged);
67      base.DeregisterContentEvents();
68    }
69    protected override void RegisterContentEvents() {
70      base.RegisterContentEvents();
71      Content.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
72      Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
73      Content.Prepared += new EventHandler(Content_Prepared);
74      Content.AlgorithmChanged += new EventHandler(Content_AlgorithmChanged);
75      Content.Started += new EventHandler(Content_Started);
76      Content.Stopped += new EventHandler(Content_Stopped);
77      Content.RepetitionsChanged += new EventHandler(Content_RepetitionsChanged);
78    }
79
80    protected override void OnContentChanged() {
81      base.OnContentChanged();
82      stopButton.Enabled = false;
83      if (Content == null) {
84        repetitionsNumericUpDown.Value = 1;
85        repetitionsNumericUpDown.Enabled = false;
86        algorithmViewHost.Content = null;
87        runsView.Content = null;
88        tabControl.Enabled = false;
89        startButton.Enabled = resetButton.Enabled = false;
90        executionTimeTextBox.Text = "-";
91        executionTimeTextBox.Enabled = false;
92      } else {
93        repetitionsNumericUpDown.Value = Content.Repetitions;
94        repetitionsNumericUpDown.Enabled = true;
95        saveAlgorithmButton.Enabled = Content.Algorithm != null;
96        algorithmViewHost.ViewType = null;
97        algorithmViewHost.Content = Content.Algorithm;
98        runsView.Content = Content.Runs;
99        tabControl.Enabled = true;
100        startButton.Enabled = !Content.Finished;
101        resetButton.Enabled = true;
102        UpdateExecutionTimeTextBox();
103        executionTimeTextBox.Enabled = true;
104      }
105    }
106
107    protected override void OnClosed(FormClosedEventArgs e) {
108      if (Content != null) Content.Stop();
109      base.OnClosed(e);
110    }
111
112    #region Content Events
113    private void Content_AlgorithmChanged(object sender, EventArgs e) {
114      if (InvokeRequired)
115        Invoke(new EventHandler(Content_AlgorithmChanged), sender, e);
116      else {
117        algorithmViewHost.ViewType = null;
118        algorithmViewHost.Content = Content.Algorithm;
119        saveAlgorithmButton.Enabled = Content.Algorithm != null;
120      }
121    }
122    private void Content_Prepared(object sender, EventArgs e) {
123      if (InvokeRequired)
124        Invoke(new EventHandler(Content_Prepared), sender, e);
125      else {
126        startButton.Enabled = !Content.Finished;
127        UpdateExecutionTimeTextBox();
128      }
129    }
130    private void Content_Started(object sender, EventArgs e) {
131      if (InvokeRequired)
132        Invoke(new EventHandler(Content_Started), sender, e);
133      else {
134        SaveEnabled = false;
135        repetitionsNumericUpDown.Enabled = false;
136        newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = saveAlgorithmButton.Enabled = false;
137        startButton.Enabled = false;
138        stopButton.Enabled = true;
139        resetButton.Enabled = false;
140        UpdateExecutionTimeTextBox();
141      }
142    }
143    private void Content_Stopped(object sender, EventArgs e) {
144      if (InvokeRequired)
145        Invoke(new EventHandler(Content_Stopped), sender, e);
146      else {
147        SaveEnabled = true;
148        repetitionsNumericUpDown.Enabled = true;
149        newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = saveAlgorithmButton.Enabled = true;
150        startButton.Enabled = !Content.Finished;
151        stopButton.Enabled = false;
152        resetButton.Enabled = true;
153        UpdateExecutionTimeTextBox();
154      }
155    }
156    private void Content_ExecutionTimeChanged(object sender, EventArgs e) {
157      UpdateExecutionTimeTextBox();
158    }
159    private void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
160      if (InvokeRequired)
161        Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
162      else
163        Auxiliary.ShowErrorMessageBox(e.Value);
164    }
165    private void Content_RepetitionsChanged(object sender, EventArgs e) {
166      if (InvokeRequired)
167        Invoke(new EventHandler(Content_RepetitionsChanged), sender, e);
168      else {
169        repetitionsNumericUpDown.Value = Content.Repetitions;
170        startButton.Enabled = !Content.Finished;
171      }
172    }
173    #endregion
174
175    #region Control events
176    private void repetitionsNumericUpDown_ValueChanged(object sender, EventArgs e) {
177      Content.Repetitions = (int)repetitionsNumericUpDown.Value;
178    }
179    private void newAlgorithmButton_Click(object sender, EventArgs e) {
180      if (algorithmTypeSelectorDialog == null) {
181        algorithmTypeSelectorDialog = new TypeSelectorDialog();
182        algorithmTypeSelectorDialog.Caption = "Select Algorithm";
183        algorithmTypeSelectorDialog.TypeSelector.Configure(typeof(IAlgorithm), false, false);
184      }
185      if (algorithmTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
186        Content.Algorithm = (IAlgorithm)algorithmTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
187      }
188    }
189    private void openAlgorithmButton_Click(object sender, EventArgs e) {
190      openFileDialog.Title = "Open Algorithm";
191      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
192        this.Cursor = Cursors.AppStarting;
193        newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = saveAlgorithmButton.Enabled = false;
194        algorithmViewHost.Enabled = false;
195
196        var call = new Func<string, object>(XmlParser.Deserialize);
197        call.BeginInvoke(openFileDialog.FileName, delegate(IAsyncResult a) {
198          IAlgorithm algorithm = null;
199          try {
200            algorithm = call.EndInvoke(a) as IAlgorithm;
201          } catch (Exception ex) {
202            Auxiliary.ShowErrorMessageBox(ex);
203          }
204          Invoke(new Action(delegate() {
205            if (algorithm == null)
206              MessageBox.Show(this, "The selected file does not contain an algorithm.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error);
207            else
208              Content.Algorithm = algorithm;
209            algorithmViewHost.Enabled = true;
210            newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = saveAlgorithmButton.Enabled = true;
211            this.Cursor = Cursors.Default;
212          }));
213        }, null);
214      }
215    }
216    private void saveAlgorithmButton_Click(object sender, EventArgs e) {
217      saveFileDialog.Title = "Save Algorithm";
218      if (saveFileDialog.ShowDialog(this) == DialogResult.OK) {
219        this.Cursor = Cursors.AppStarting;
220        newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = saveAlgorithmButton.Enabled = false;
221        algorithmViewHost.Enabled = false;
222
223        var call = new Action<IAlgorithm, string, int>(XmlGenerator.Serialize);
224        int compression = 9;
225        if (saveFileDialog.FilterIndex == 1) compression = 0;
226        call.BeginInvoke(Content.Algorithm, saveFileDialog.FileName, compression, delegate(IAsyncResult a) {
227          try {
228            call.EndInvoke(a);
229          }
230          catch (Exception ex) {
231            Auxiliary.ShowErrorMessageBox(ex);
232          }
233          Invoke(new Action(delegate() {
234            algorithmViewHost.Enabled = true;
235            newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = saveAlgorithmButton.Enabled = true;
236            this.Cursor = Cursors.Default;
237          }));
238        }, null);
239      }
240    }
241    private void startButton_Click(object sender, EventArgs e) {
242      Content.Start();
243    }
244    private void stopButton_Click(object sender, EventArgs e) {
245      Content.Stop();
246    }
247    private void resetButton_Click(object sender, EventArgs e) {
248      Content.Prepare();
249    }
250    #endregion
251
252    #region Helpers
253    private void UpdateExecutionTimeTextBox() {
254      if (InvokeRequired)
255        Invoke(new Action(UpdateExecutionTimeTextBox));
256      else
257        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
258    }
259    #endregion
260  }
261}
Note: See TracBrowser for help on using the repository browser.