source: trunk/sources/HeuristicLab.Optimization.Views/3.3/AlgorithmView.cs @ 3226

Last change on this file since 3226 was 3226, checked in by swagner, 12 years ago

Implemented first version of algorithm batch processing (#947).

File size: 10.9 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("Algorithm View")]
35  [Content(typeof(Algorithm), true)]
36  [Content(typeof(IAlgorithm), false)]
37  public partial class AlgorithmView : NamedItemView {
38    private TypeSelectorDialog problemTypeSelectorDialog;
39    private int executionTimeCounter;
40
41    public new IAlgorithm Content {
42      get { return (IAlgorithm)base.Content; }
43      set { base.Content = value; }
44    }
45
46    /// <summary>
47    /// Initializes a new instance of <see cref="ItemBaseView"/>.
48    /// </summary>
49    public AlgorithmView() {
50      InitializeComponent();
51    }
52    /// <summary>
53    /// Intializes a new instance of <see cref="ItemBaseView"/> with the given <paramref name="item"/>.
54    /// </summary>
55    /// <param name="item">The item that should be displayed.</param>
56    public AlgorithmView(IAlgorithm content)
57      : this() {
58      Content = content;
59    }
60
61    protected override void OnInitialized(EventArgs e) {
62      // Set order of tab pages according to z order.
63      // NOTE: This is required due to a bug in the VS designer.
64      List<Control> tabPages = new List<Control>();
65      for (int i = 0; i < tabControl.Controls.Count; i++) {
66        tabPages.Add(tabControl.Controls[i]);
67      }
68      tabControl.Controls.Clear();
69      foreach (Control control in tabPages)
70        tabControl.Controls.Add(control);
71
72      base.OnInitialized(e);
73    }
74
75    protected override void DeregisterContentEvents() {
76      Content.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
77      Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
78      Content.Prepared -= new EventHandler(Content_Prepared);
79      Content.ProblemChanged -= new EventHandler(Content_ProblemChanged);
80      Content.Started -= new EventHandler(Content_Started);
81      Content.Stopped -= new EventHandler(Content_Stopped);
82      base.DeregisterContentEvents();
83    }
84    protected override void RegisterContentEvents() {
85      base.RegisterContentEvents();
86      Content.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
87      Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
88      Content.Prepared += new EventHandler(Content_Prepared);
89      Content.ProblemChanged += new EventHandler(Content_ProblemChanged);
90      Content.Started += new EventHandler(Content_Started);
91      Content.Stopped += new EventHandler(Content_Stopped);
92    }
93
94    protected override void OnContentChanged() {
95      base.OnContentChanged();
96      stopButton.Enabled = false;
97      if (Content == null) {
98        parameterCollectionView.Content = null;
99        problemViewHost.Content = null;
100        resultsView.Content = null;
101        tabControl.Enabled = false;
102        startButton.Enabled = resetButton.Enabled = false;
103        executionTimeTextBox.Text = "-";
104        executionTimeTextBox.Enabled = false;
105      } else {
106        parameterCollectionView.Content = Content.Parameters;
107        saveProblemButton.Enabled = Content.Problem != null;
108        problemViewHost.ViewType = null;
109        problemViewHost.Content = Content.Problem;
110        resultsView.Content = Content.Results.AsReadOnly();
111        tabControl.Enabled = true;
112        startButton.Enabled = !Content.Finished;
113        resetButton.Enabled = true;
114        UpdateExecutionTimeTextBox();
115        executionTimeTextBox.Enabled = true;
116      }
117    }
118
119    protected override void OnClosed(FormClosedEventArgs e) {
120      if (Content != null) Content.Stop();
121      base.OnClosed(e);
122    }
123
124    #region Content Events
125    protected virtual void Content_Prepared(object sender, EventArgs e) {
126      if (InvokeRequired)
127        Invoke(new EventHandler(Content_Prepared), sender, e);
128      else {
129        resultsView.Content = Content.Results.AsReadOnly();
130        startButton.Enabled = !Content.Finished;
131        UpdateExecutionTimeTextBox();
132      }
133    }
134    protected virtual void Content_ProblemChanged(object sender, EventArgs e) {
135      if (InvokeRequired)
136        Invoke(new EventHandler(Content_ProblemChanged), sender, e);
137      else {
138        problemViewHost.ViewType = null;
139        problemViewHost.Content = Content.Problem;
140        saveProblemButton.Enabled = Content.Problem != null;
141      }
142    }
143    protected virtual void Content_Started(object sender, EventArgs e) {
144      executionTimeCounter = 0;
145      if (InvokeRequired)
146        Invoke(new EventHandler(Content_Started), sender, e);
147      else {
148        SaveEnabled = false;
149        parameterCollectionView.Enabled = false;
150        newProblemButton.Enabled = openProblemButton.Enabled = saveProblemButton.Enabled = false;
151        problemViewHost.Enabled = false;
152        resultsView.Enabled = false;
153        startButton.Enabled = false;
154        stopButton.Enabled = true;
155        resetButton.Enabled = false;
156        UpdateExecutionTimeTextBox();
157      }
158    }
159    protected virtual void Content_Stopped(object sender, EventArgs e) {
160      if (InvokeRequired)
161        Invoke(new EventHandler(Content_Stopped), sender, e);
162      else {
163        SaveEnabled = true;
164        parameterCollectionView.Enabled = true;
165        newProblemButton.Enabled = openProblemButton.Enabled = saveProblemButton.Enabled = true;
166        problemViewHost.Enabled = true;
167        resultsView.Enabled = true;
168        startButton.Enabled = !Content.Finished;
169        stopButton.Enabled = false;
170        resetButton.Enabled = true;
171        UpdateExecutionTimeTextBox();
172      }
173    }
174    protected virtual void Content_ExecutionTimeChanged(object sender, EventArgs e) {
175      executionTimeCounter++;
176      if ((executionTimeCounter == 100) || !Content.Running) {
177        executionTimeCounter = 0;
178        UpdateExecutionTimeTextBox();
179      }
180    }
181    protected virtual void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
182      if (InvokeRequired)
183        Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
184      else
185        Auxiliary.ShowErrorMessageBox(e.Value);
186    }
187    #endregion
188
189    #region Button events
190    protected virtual void newProblemButton_Click(object sender, EventArgs e) {
191      if (problemTypeSelectorDialog == null) {
192        problemTypeSelectorDialog = new TypeSelectorDialog();
193        problemTypeSelectorDialog.Caption = "Select Problem";
194        problemTypeSelectorDialog.TypeSelector.Configure(Content.ProblemType, false, false);
195      }
196      if (problemTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
197        Content.Problem = (IProblem)problemTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
198      }
199    }
200    protected virtual void openProblemButton_Click(object sender, EventArgs e) {
201      openFileDialog.Title = "Open Problem";
202      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
203        this.Cursor = Cursors.AppStarting;
204        newProblemButton.Enabled = openProblemButton.Enabled = saveProblemButton.Enabled = false;
205        problemViewHost.Enabled = false;
206
207        var call = new Func<string, object>(XmlParser.Deserialize);
208        call.BeginInvoke(openFileDialog.FileName, delegate(IAsyncResult a) {
209          IProblem problem = null;
210          try {
211            problem = call.EndInvoke(a) as IProblem;
212          } catch (Exception ex) {
213            Auxiliary.ShowErrorMessageBox(ex);
214          }
215          Invoke(new Action(delegate() {
216            if (problem == null)
217              MessageBox.Show(this, "The selected file does not contain a problem.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error);
218            else if (!Content.ProblemType.IsInstanceOfType(problem))
219              MessageBox.Show(this, "The selected file contains a problem type which is not supported by this algorithm.", "Invalid Problem Type", MessageBoxButtons.OK, MessageBoxIcon.Error);
220            else
221              Content.Problem = problem;
222            problemViewHost.Enabled = true;
223            newProblemButton.Enabled = openProblemButton.Enabled = saveProblemButton.Enabled = true;
224            this.Cursor = Cursors.Default;
225          }));
226        }, null);
227      }
228    }
229    protected virtual void saveProblemButton_Click(object sender, EventArgs e) {
230      saveFileDialog.Title = "Save Problem";
231      if (saveFileDialog.ShowDialog(this) == DialogResult.OK) {
232        this.Cursor = Cursors.AppStarting;
233        newProblemButton.Enabled = openProblemButton.Enabled = saveProblemButton.Enabled = false;
234        problemViewHost.Enabled = false;
235
236        var call = new Action<IProblem, string, int>(XmlGenerator.Serialize);
237        int compression = 9;
238        if (saveFileDialog.FilterIndex == 1) compression = 0;
239        call.BeginInvoke(Content.Problem, saveFileDialog.FileName, compression, delegate(IAsyncResult a) {
240          try {
241            call.EndInvoke(a);
242          }
243          catch (Exception ex) {
244            Auxiliary.ShowErrorMessageBox(ex);
245          }
246          Invoke(new Action(delegate() {
247            problemViewHost.Enabled = true;
248            newProblemButton.Enabled = openProblemButton.Enabled = saveProblemButton.Enabled = true;
249            this.Cursor = Cursors.Default;
250          }));
251        }, null);
252      }
253    }
254    protected virtual void startButton_Click(object sender, EventArgs e) {
255      Content.Start();
256    }
257    protected virtual void stopButton_Click(object sender, EventArgs e) {
258      Content.Stop();
259    }
260    protected virtual void resetButton_Click(object sender, EventArgs e) {
261      Content.Prepare();
262    }
263    #endregion
264
265    #region Helpers
266    protected virtual void UpdateExecutionTimeTextBox() {
267      if (InvokeRequired)
268        Invoke(new Action(UpdateExecutionTimeTextBox));
269      else
270        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
271    }
272    #endregion
273  }
274}
Note: See TracBrowser for help on using the repository browser.