Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Views/3.4/ExperimentManager/RefreshableHiveExperimentView.cs @ 6219

Last change on this file since 6219 was 6219, checked in by cneumuel, 14 years ago

#1233

  • improved exception handling for hive experiments
File size: 16.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.Linq;
24using System.Threading;
25using System.Windows.Forms;
26using HeuristicLab.Clients.Hive.ExperimentManager;
27using HeuristicLab.Collections;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.Core.Views;
31using HeuristicLab.MainForm;
32using HeuristicLab.Optimization;
33using HeuristicLab.PluginInfrastructure;
34
35namespace HeuristicLab.Clients.Hive.Views.ExperimentManager {
36  /// <summary>
37  /// The base class for visual representations of items.
38  /// </summary>
39  [View("Hive Experiment View")]
40  [Content(typeof(RefreshableHiveExperiment), true)]
41  public sealed partial class RefreshableHiveExperimentView : ItemView {
42    private ProgressView progressView;
43
44    public new RefreshableHiveExperiment Content {
45      get { return (RefreshableHiveExperiment)base.Content; }
46      set { base.Content = value; }
47    }
48
49    /// <summary>
50    /// Initializes a new instance of <see cref="ItemBaseView"/>.
51    /// </summary>
52    public RefreshableHiveExperimentView() {
53      InitializeComponent();
54    }
55
56    protected override void RegisterContentEvents() {
57      base.RegisterContentEvents();
58      Content.RefreshAutomaticallyChanged += new EventHandler(Content_RefreshAutomaticallyChanged);
59      Content.HiveExperimentChanged += new EventHandler(Content_HiveExperimentChanged);
60      Content.IsControllableChanged += new EventHandler(Content_IsControllableChanged);
61      Content.JobStatisticsChanged += new EventHandler(Content_JobStatisticsChanged);
62      Content.ExceptionOccured += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccured);
63    }
64
65    protected override void DeregisterContentEvents() {
66      Content.RefreshAutomaticallyChanged -= new EventHandler(Content_RefreshAutomaticallyChanged);
67      Content.HiveExperimentChanged -= new EventHandler(Content_HiveExperimentChanged);
68      Content.IsControllableChanged -= new EventHandler(Content_IsControllableChanged);
69      Content.JobStatisticsChanged -= new EventHandler(Content_JobStatisticsChanged);
70      Content.ExceptionOccured -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccured);
71      base.DeregisterContentEvents();
72    }
73
74    private void RegisterHiveExperimentEvents() {
75      Content.HiveExperiment.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
76      Content.HiveExperiment.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
77      Content.HiveExperiment.HiveJobsChanged += new EventHandler(Content_HiveJobChanged);
78      Content.HiveExperiment.IsProgressingChanged += new EventHandler(Content_IsProgressingChanged);
79    }
80    private void DeregisterHiveExperimentEvents() {
81      Content.HiveExperiment.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
82      Content.HiveExperiment.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
83      Content.HiveExperiment.HiveJobsChanged -= new EventHandler(Content_HiveJobChanged);
84      Content.HiveExperiment.IsProgressingChanged -= new EventHandler(Content_IsProgressingChanged);
85    }
86
87    private void RegisterHiveJobEvents() {
88      Content.HiveExperiment.HiveJobs.ItemsAdded += new Collections.CollectionItemsChangedEventHandler<HiveJob>(HiveJobs_ItemsAdded_Removed_Reset);
89      Content.HiveExperiment.HiveJobs.ItemsRemoved += new Collections.CollectionItemsChangedEventHandler<HiveJob>(HiveJobs_ItemsAdded_Removed_Reset);
90      Content.HiveExperiment.HiveJobs.CollectionReset += new Collections.CollectionItemsChangedEventHandler<HiveJob>(HiveJobs_ItemsAdded_Removed_Reset);
91    }
92    private void DeregisterHiveJobEvents() {
93      Content.HiveExperiment.HiveJobs.ItemsAdded -= new Collections.CollectionItemsChangedEventHandler<HiveJob>(HiveJobs_ItemsAdded_Removed_Reset);
94      Content.HiveExperiment.HiveJobs.ItemsRemoved -= new Collections.CollectionItemsChangedEventHandler<HiveJob>(HiveJobs_ItemsAdded_Removed_Reset);
95      Content.HiveExperiment.HiveJobs.CollectionReset -= new Collections.CollectionItemsChangedEventHandler<HiveJob>(HiveJobs_ItemsAdded_Removed_Reset);
96    }
97
98    protected override void OnContentChanged() {
99      base.OnContentChanged();
100      if (Content == null) {
101        nameTextBox.Text = string.Empty;
102        executionTimeTextBox.Text = string.Empty;
103        resourceNamesTextBox.Text = string.Empty;
104        useLocalPluginsCheckBox.Checked = false;
105        logView.Content = null;
106        //includeJobsCheckBox.Checked = false;
107        refreshAutomaticallyCheckBox.Checked = false;
108        logView.Content = null;
109      } else {
110        nameTextBox.Text = Content.HiveExperiment.Name;
111        executionTimeTextBox.Text = Content.HiveExperiment.ExecutionTime.ToString();
112        resourceNamesTextBox.Text = Content.HiveExperiment.ResourceNames;
113        useLocalPluginsCheckBox.Checked = Content.HiveExperiment.UseLocalPlugins;
114        //includeJobsCheckBox.Checked = Content.IncludeJobs;
115        refreshAutomaticallyCheckBox.Checked = Content.RefreshAutomatically;
116        logView.Content = Content.Log;
117      }
118      Content_JobStatisticsChanged(this, EventArgs.Empty);
119      Content_HiveExperimentChanged(this, EventArgs.Empty);
120      Content_HiveJobChanged(this, EventArgs.Empty);
121      Content_IsProgressingChanged(this, EventArgs.Empty);
122      SetEnabledStateOfControls();
123    }
124
125    protected override void SetEnabledStateOfControls() {
126      base.SetEnabledStateOfControls();
127      executionTimeTextBox.Enabled = Content != null;
128      jobsTextBox.ReadOnly = true;
129      calculatingTextBox.ReadOnly = true;
130      finishedTextBox.ReadOnly = true;
131
132      if (Content != null) {
133        bool alreadyUploaded = Content.Id != Guid.Empty;
134        bool jobsLoaded = Content.HiveExperiment.HiveJobs != null && Content.HiveExperiment.HiveJobs.All(x => x.Job.Id != Guid.Empty);
135
136        this.nameTextBox.ReadOnly =                 !Content.IsControllable || Content.HiveExperiment.ExecutionState != ExecutionState.Prepared || alreadyUploaded;
137        this.resourceNamesTextBox.ReadOnly =        !Content.IsControllable || Content.HiveExperiment.ExecutionState != ExecutionState.Prepared || alreadyUploaded;
138        this.jobsTreeView.ReadOnly =                !Content.IsControllable || Content.HiveExperiment.ExecutionState != ExecutionState.Prepared || alreadyUploaded;
139
140        this.useLocalPluginsCheckBox.Enabled =      Content.IsControllable && !(Content.HiveExperiment.ExecutionState != ExecutionState.Prepared || alreadyUploaded);
141        this.refreshAutomaticallyCheckBox.Enabled = Content.IsControllable && alreadyUploaded && jobsLoaded && Content.HiveExperiment.ExecutionState == ExecutionState.Started;
142        this.refreshButton.Enabled =                Content.IsControllable && alreadyUploaded;
143        this.Locked =                               !Content.IsControllable || Content.HiveExperiment.ExecutionState == ExecutionState.Started;
144      }
145      SetEnabledStateOfExecutableButtons();
146    }
147
148    protected override void OnClosed(FormClosedEventArgs e) {
149      if (Content != null) {
150        if (Content.RefreshAutomatically)
151          Content.StopResultPolling();
152      }
153      base.OnClosed(e);
154    }
155
156    #region Content Events
157    private void HiveJobs_ItemsAdded_Removed_Reset(object sender, CollectionItemsChangedEventArgs<HiveJob> e) {
158      if (InvokeRequired)
159        Invoke(new CollectionItemsChangedEventHandler<HiveJob>(HiveJobs_ItemsAdded_Removed_Reset), sender, e);
160      else {
161        SetEnabledStateOfControls();
162      }
163    }
164    private void Content_ExecutionStateChanged(object sender, EventArgs e) {
165      if (InvokeRequired)
166        Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
167      else
168        SetEnabledStateOfControls();
169    }
170    private void Content_Prepared(object sender, EventArgs e) {
171      if (InvokeRequired)
172        Invoke(new EventHandler(Content_Prepared), sender, e);
173      else {
174        nameTextBox.Enabled = true;
175        Locked = false;
176        SetEnabledStateOfControls();
177      }
178    }
179    private void Content_Started(object sender, EventArgs e) {
180      if (InvokeRequired)
181        Invoke(new EventHandler(Content_Started), sender, e);
182      else {
183        nameTextBox.Enabled = false;
184        SetEnabledStateOfControls();
185      }
186    }
187    private void Content_Paused(object sender, EventArgs e) {
188      if (InvokeRequired)
189        Invoke(new EventHandler(Content_Paused), sender, e);
190      else {
191        nameTextBox.Enabled = true;
192        SetEnabledStateOfControls();
193      }
194    }
195    private void Content_Stopped(object sender, EventArgs e) {
196      if (InvokeRequired)
197        Invoke(new EventHandler(Content_Stopped), sender, e);
198      else {
199        nameTextBox.Enabled = true;
200        Locked = false;
201        SetEnabledStateOfControls();
202      }
203    }
204    private void Content_ExecutionTimeChanged(object sender, EventArgs e) {
205      if (InvokeRequired)
206        Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
207      else
208        executionTimeTextBox.Text = Content.HiveExperiment.ExecutionTime.ToString();
209    }
210    private void Content_RefreshAutomaticallyChanged(object sender, EventArgs e) {
211      if (InvokeRequired)
212        Invoke(new EventHandler(Content_RefreshAutomaticallyChanged), sender, e);
213      else {
214        refreshAutomaticallyCheckBox.Checked = Content.RefreshAutomatically;
215        SetEnabledStateOfControls();
216      }
217    }
218    private void Content_HiveJobChanged(object sender, EventArgs e) {
219      if (InvokeRequired)
220        Invoke(new EventHandler(Content_HiveJobChanged), sender, e);
221      else {
222        if (Content != null && Content.HiveExperiment.HiveJobs != null) {
223          jobsTreeView.Content = Content.HiveExperiment.HiveJobs;
224          RegisterHiveJobEvents();
225        } else {
226          jobsTreeView.Content = null;
227        }
228        SetEnabledStateOfControls();
229      }
230    }
231
232    private void Content_HiveExperimentChanged(object sender, EventArgs e) {
233      if (Content.HiveExperiment != null) {
234        RegisterHiveExperimentEvents();
235        Content_IsProgressingChanged(sender, e);
236      }
237    }
238    private void Content_IsControllableChanged(object sender, EventArgs e) {
239      SetEnabledStateOfControls();
240    }
241    private void Content_JobStatisticsChanged(object sender, EventArgs e) {
242      if (InvokeRequired)
243        Invoke(new EventHandler(Content_JobStatisticsChanged), sender, e);
244      else {
245        if (Content != null) {
246          jobsTextBox.Text = Content.HiveExperiment.JobCount.ToString();
247          calculatingTextBox.Text = Content.HiveExperiment.CalculatingCount.ToString();
248          finishedTextBox.Text = Content.HiveExperiment.FinishedCount.ToString();
249        } else {
250          jobsTextBox.Text = "0";
251          calculatingTextBox.Text = "0";
252          finishedTextBox.Text = "0";
253        }
254      }
255    }
256    private void Content_ExceptionOccured(object sender, EventArgs<Exception> e) {
257      if (Content.IsControllable) {
258        // show error dialog only if Controllable (otherwise it should continue trying without an error dialog (e.g. HiveEngine))
259        ErrorHandling.ShowErrorDialog(this, e.Value);
260      }
261    }
262    #endregion
263
264    #region Control events
265    private void startButton_Click(object sender, EventArgs e) {
266      ExperimentManagerClient.StartExperiment((Exception ex) => ErrorHandling.ShowErrorDialog(this, "Start failed.", ex), Content);
267    }
268    private void pauseButton_Click(object sender, EventArgs e) {
269      ExperimentManagerClient.PauseExperiment(Content.HiveExperiment);
270    }
271    private void stopButton_Click(object sender, EventArgs e) {
272      ExperimentManagerClient.StopExperiment(Content.HiveExperiment);
273    }
274    private void resetButton_Click(object sender, EventArgs e) { }
275
276    private void nameTextBox_Validated(object sender, EventArgs e) {
277      if (Content.HiveExperiment.Name != nameTextBox.Text)
278        Content.HiveExperiment.Name = nameTextBox.Text;
279    }
280
281    private void resourceNamesTextBox_Validated(object sender, EventArgs e) {
282      if (Content.HiveExperiment.ResourceNames != resourceNamesTextBox.Text)
283        Content.HiveExperiment.ResourceNames = resourceNamesTextBox.Text;
284    }
285
286    private void includeJobsCheckBox_CheckedChanged(object sender, EventArgs e) {
287      //if (Content != null) Content.IncludeJobs = includeJobsCheckBox.Checked;
288    }
289
290    private void refreshAutomaticallyCheckBox_CheckedChanged(object sender, EventArgs e) {
291      if (Content != null) {
292        Content.RefreshAutomatically = refreshAutomaticallyCheckBox.Checked;
293      }
294    }
295
296    private void useLocalPluginsCheckBox_CheckedChanged(object sender, EventArgs e) {
297      if (Content != null) Content.HiveExperiment.UseLocalPlugins = useLocalPluginsCheckBox.Checked;
298    }
299
300    private void refreshButton_Click(object sender, EventArgs e) {
301      var invoker = new Action<HiveExperiment>(ExperimentManagerClient.LoadExperiment);
302      invoker.BeginInvoke(Content.HiveExperiment, (ar) => {
303        try {
304          invoker.EndInvoke(ar);
305        }
306        catch (Exception ex) {
307          ThreadPool.QueueUserWorkItem(delegate(object exception) { ErrorHandling.ShowErrorDialog(this, (Exception)exception); }, ex);
308        }
309      }, null);
310    }
311    #endregion
312
313    #region Helpers
314    private void SetEnabledStateOfExecutableButtons() {
315      if (Content == null) {
316        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
317      } else {
318        startButton.Enabled = Content.IsControllable && Content.HiveExperiment.HiveJobs != null && Content.HiveExperiment.HiveJobs.Count > 0 && Content.HiveExperiment.ExecutionState == ExecutionState.Prepared;
319        pauseButton.Enabled = Content.IsControllable && Content.HiveExperiment.ExecutionState == ExecutionState.Started;
320        stopButton.Enabled = Content.IsControllable && Content.HiveExperiment.ExecutionState == ExecutionState.Started;
321        resetButton.Enabled = false;
322      }
323    }
324    #endregion
325
326    #region Progress reporting
327    private void Content_IsProgressingChanged(object sender, EventArgs e) {
328      if (this.InvokeRequired) {
329        Invoke(new EventHandler(Content_IsProgressingChanged), sender, e);
330      } else {
331        if (Content != null && Content.HiveExperiment.IsProgressing) {
332          SetProgressView();
333        } else {
334          FinishProgressView();
335        }
336      }
337    }
338
339    private void SetProgressView() {
340      if (progressView == null) {
341        progressView = new ProgressView(this, Content.HiveExperiment.Progress);
342      }
343      progressView.Progress = Content.HiveExperiment.Progress;
344    }
345
346    private void FinishProgressView() {
347      if (progressView != null) {
348        progressView.Finish();
349        progressView = null;
350        SetEnabledStateOfControls();
351      }
352    }
353    #endregion
354
355    #region Drag & Drop
356    private void jobsTreeView_DragOver(object sender, DragEventArgs e) {
357      jobsTreeView_DragEnter(sender, e);
358    }
359
360    private void jobsTreeView_DragEnter(object sender, DragEventArgs e) {
361      e.Effect = DragDropEffects.None;
362      var obj = e.Data.GetData(Constants.DragDropDataFormat);
363      if (obj is IOptimizer) {
364        e.Effect = DragDropEffects.Copy;
365      }
366    }
367
368    private void jobsTreeView_DragDrop(object sender, DragEventArgs e) {
369      if (e.Effect != DragDropEffects.None) {
370        var obj = e.Data.GetData(Constants.DragDropDataFormat);
371
372        var optimizer = obj as IOptimizer;
373        if (optimizer != null) {
374          Content.HiveExperiment.HiveJobs.Add(new OptimizerHiveJob(optimizer));
375        }
376      }
377    }
378    #endregion
379  }
380}
Note: See TracBrowser for help on using the repository browser.