Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Views/3.4/ExperimentManager/HiveExperimentView.cs @ 6006

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

#1233

  • changed relationship between Job and HiveExperiment. There is no more HiveExperiment.RootJobId, instead there is Job.HiveExperimentId.
  • One HiveExperiment can now have multiple Experiments.
  • TreeView supports multiple root nodes
  • HiveEngine creates a HiveExperiment for each set of jobs, so jobs cannot be without an parent experiment anymore (no more loose jobs)
  • updated ExperimentManager binaries
File size: 14.6 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.ComponentModel;
24using System.Linq;
25using System.Threading;
26using System.Windows.Forms;
27using HeuristicLab.Collections;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.Core.Views;
31using HeuristicLab.MainForm;
32using HeuristicLab.PluginInfrastructure;
33
34namespace HeuristicLab.Clients.Hive.Views {
35  /// <summary>
36  /// The base class for visual representations of items.
37  /// </summary>
38  [View("Hive Experiment View")]
39  [Content(typeof(HiveExperiment), true)]
40  public sealed partial class HiveExperimentView : ItemView {
41    private ProgressView progressView;
42
43    public new HiveExperiment Content {
44      get { return (HiveExperiment)base.Content; }
45      set { base.Content = value; }
46    }
47
48    /// <summary>
49    /// Initializes a new instance of <see cref="ItemBaseView"/>.
50    /// </summary>
51    public HiveExperimentView() {
52      InitializeComponent();
53    }
54
55    protected override void DeregisterContentEvents() {
56      Content.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
57      Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
58      Content.RefreshAutomaticallyChanged -= new EventHandler(Content_RefreshAutomaticallyChanged);
59      Content.HiveJobsChanged -= new EventHandler(Content_HiveJobChanged);
60      Content.IsProgressingChanged -= new EventHandler(Content_IsProgressingChanged);
61      Content.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(Content_PropertyChanged);
62      base.DeregisterContentEvents();
63    }
64
65    protected override void RegisterContentEvents() {
66      base.RegisterContentEvents();
67      Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
68      Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
69      Content.RefreshAutomaticallyChanged += new EventHandler(Content_RefreshAutomaticallyChanged);
70      Content.HiveJobsChanged += new EventHandler(Content_HiveJobChanged);
71      Content.IsProgressingChanged += new EventHandler(Content_IsProgressingChanged);
72      Content.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(Content_PropertyChanged);
73    }
74
75    protected override void OnContentChanged() {
76      base.OnContentChanged();
77      if (Content == null) {
78        nameTextBox.Text = string.Empty;
79        executionTimeTextBox.Text = string.Empty;
80        resourceNamesTextBox.Text = string.Empty;
81        useLocalPluginsCheckBox.Checked = false;
82        logView.Content = null;
83        //includeJobsCheckBox.Checked = false;
84        refreshAutomaticallyCheckBox.Checked = false;
85        jobsTextBox.Text = "0";
86        calculatingTextBox.Text = "0";
87        finishedTextBox.Text = "0";
88      } else {
89        nameTextBox.Text = Content.Name;
90        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
91        resourceNamesTextBox.Text = Content.ResourceNames;
92        useLocalPluginsCheckBox.Checked = Content.UseLocalPlugins;
93        //includeJobsCheckBox.Checked = Content.IncludeJobs;
94        refreshAutomaticallyCheckBox.Checked = Content.RefreshAutomatically;
95        jobsTextBox.Text = Content.JobCount.ToString();
96        calculatingTextBox.Text = Content.CalculatingCount.ToString();
97        finishedTextBox.Text = Content.FinishedCount.ToString();
98      }
99      Content_HiveJobChanged(this, EventArgs.Empty);
100      Content_IsProgressingChanged(this, EventArgs.Empty);
101      SetEnabledStateOfControls();
102    }
103
104    protected override void SetEnabledStateOfControls() {
105      base.SetEnabledStateOfControls();
106      executionTimeTextBox.Enabled = Content != null;
107      experimentNamedItemView.ReadOnly = true;
108      jobsTextBox.ReadOnly = true;
109      calculatingTextBox.ReadOnly = true;
110      finishedTextBox.ReadOnly = true;
111
112      if (Content != null) {
113        bool alreadyUploaded = Content.Id != Guid.Empty;
114        bool jobsLoaded = Content.HiveJobs != null && Content.HiveJobs.All(x => x.Job.Id != Guid.Empty);
115
116        this.nameTextBox.ReadOnly = Content.ExecutionState != ExecutionState.Prepared || alreadyUploaded;
117        this.resourceNamesTextBox.ReadOnly = Content.ExecutionState != ExecutionState.Prepared || alreadyUploaded;
118        this.jobsTreeView.ReadOnly = Content.ExecutionState != ExecutionState.Prepared || alreadyUploaded;
119        this.useLocalPluginsCheckBox.Enabled = !(Content.ExecutionState != ExecutionState.Prepared || alreadyUploaded);
120        this.refreshAutomaticallyCheckBox.Enabled = alreadyUploaded && jobsLoaded && Content.ExecutionState == ExecutionState.Started;
121        this.viewExperimentButton.Enabled = Content.GetExperiment(0) != null;
122        this.openExperimentButton.Enabled = !alreadyUploaded && Content.ExecutionState == ExecutionState.Prepared;
123        this.newExperimentButton.Enabled = !alreadyUploaded && Content.ExecutionState == ExecutionState.Prepared;
124        this.refreshButton.Enabled = alreadyUploaded;
125
126        this.Locked = Content.ExecutionState == ExecutionState.Started;
127      }
128      SetEnabledStateOfExecutableButtons();
129    }
130
131    protected override void OnClosed(FormClosedEventArgs e) {
132      if (Content != null) {
133        if (Content.RefreshAutomatically)
134          Content.StopResultPolling();
135      }
136      base.OnClosed(e);
137    }
138
139    #region Content Events
140    private void HiveJobs_ItemsAdded_Removed_Reset(object sender, CollectionItemsChangedEventArgs<HiveJob> e) {
141      if (InvokeRequired)
142        Invoke(new CollectionItemsChangedEventHandler<HiveJob>(HiveJobs_ItemsAdded_Removed_Reset), sender, e);
143      else {
144        SetEnabledStateOfControls();
145      }
146    }
147    private void Content_ExecutionStateChanged(object sender, EventArgs e) {
148      if (InvokeRequired)
149        Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
150      else
151        SetEnabledStateOfControls();
152    }
153    private void Content_Prepared(object sender, EventArgs e) {
154      if (InvokeRequired)
155        Invoke(new EventHandler(Content_Prepared), sender, e);
156      else {
157        nameTextBox.Enabled = true;
158        Locked = false;
159        SetEnabledStateOfControls();
160      }
161    }
162    private void Content_Started(object sender, EventArgs e) {
163      if (InvokeRequired)
164        Invoke(new EventHandler(Content_Started), sender, e);
165      else {
166        nameTextBox.Enabled = false;
167        SetEnabledStateOfControls();
168      }
169    }
170    private void Content_Paused(object sender, EventArgs e) {
171      if (InvokeRequired)
172        Invoke(new EventHandler(Content_Paused), sender, e);
173      else {
174        nameTextBox.Enabled = true;
175        SetEnabledStateOfControls();
176      }
177    }
178    private void Content_Stopped(object sender, EventArgs e) {
179      if (InvokeRequired)
180        Invoke(new EventHandler(Content_Stopped), sender, e);
181      else {
182        nameTextBox.Enabled = true;
183        Locked = false;
184        SetEnabledStateOfControls();
185      }
186    }
187    private void Content_ExecutionTimeChanged(object sender, EventArgs e) {
188      if (InvokeRequired)
189        Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
190      else
191        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
192    }
193    private void Content_RefreshAutomaticallyChanged(object sender, EventArgs e) {
194      if (InvokeRequired)
195        Invoke(new EventHandler(Content_RefreshAutomaticallyChanged), sender, e);
196      else {
197        refreshAutomaticallyCheckBox.Checked = Content.RefreshAutomatically;
198        SetEnabledStateOfControls();
199      }
200    }
201    private void Content_HiveJobChanged(object sender, EventArgs e) {
202      if (InvokeRequired)
203        Invoke(new EventHandler(Content_HiveJobChanged), sender, e);
204      else {
205        if (Content != null && Content.HiveJobs != null) {
206          jobsTreeView.Content = Content.HiveJobs;
207          experimentNamedItemView.Content = Content.GetExperiment(0);
208          RegisterHiveJobEvents();
209        } else {
210          jobsTreeView.Content = null;
211          experimentNamedItemView.Content = null;
212        }
213        SetEnabledStateOfControls();
214      }
215    }
216
217    private void RegisterHiveJobEvents() {
218      Content.HiveJobs.ItemsAdded += new Collections.CollectionItemsChangedEventHandler<HiveJob>(HiveJobs_ItemsAdded_Removed_Reset);
219      Content.HiveJobs.ItemsRemoved += new Collections.CollectionItemsChangedEventHandler<HiveJob>(HiveJobs_ItemsAdded_Removed_Reset);
220      Content.HiveJobs.CollectionReset += new Collections.CollectionItemsChangedEventHandler<HiveJob>(HiveJobs_ItemsAdded_Removed_Reset);
221    }
222    private void DeregisterHiveJobEvents() {
223      Content.HiveJobs.ItemsAdded -= new Collections.CollectionItemsChangedEventHandler<HiveJob>(HiveJobs_ItemsAdded_Removed_Reset);
224      Content.HiveJobs.ItemsRemoved -= new Collections.CollectionItemsChangedEventHandler<HiveJob>(HiveJobs_ItemsAdded_Removed_Reset);
225      Content.HiveJobs.CollectionReset -= new Collections.CollectionItemsChangedEventHandler<HiveJob>(HiveJobs_ItemsAdded_Removed_Reset);
226    }
227
228    private void Content_PropertyChanged(object sender, PropertyChangedEventArgs e) {
229      if (InvokeRequired)
230        Invoke(new PropertyChangedEventHandler(Content_PropertyChanged), sender, e);
231      else {
232        jobsTextBox.Text = Content.JobCount.ToString();
233        calculatingTextBox.Text = Content.CalculatingCount.ToString();
234        finishedTextBox.Text = Content.FinishedCount.ToString();
235      }
236    }
237    #endregion
238
239    #region Control events
240    private void startButton_Click(object sender, EventArgs e) {
241      ExperimentManagerClient.StartExperiment(new Action<Exception>((Exception ex) => ErrorHandling.ShowErrorDialog(this, "Start failed.", ex)), Content);
242    }
243    private void pauseButton_Click(object sender, EventArgs e) {
244      ExperimentManagerClient.PauseExperiment(Content);
245    }
246    private void stopButton_Click(object sender, EventArgs e) {
247      ExperimentManagerClient.StopExperiment(Content);
248    }
249    private void resetButton_Click(object sender, EventArgs e) { }
250
251    private void nameTextBox_Validated(object sender, EventArgs e) {
252      if (Content.Name != nameTextBox.Text)
253        Content.Name = nameTextBox.Text;
254    }
255
256    private void resourceNamesTextBox_Validated(object sender, EventArgs e) {
257      if (Content.ResourceNames != resourceNamesTextBox.Text)
258        Content.ResourceNames = resourceNamesTextBox.Text;
259    }
260
261    private void newExperimentButton_Click(object sender, EventArgs e) {
262      Content.SetExperiment(new HeuristicLab.Optimization.Experiment());
263    }
264
265    private void openExperimentButton_Click(object sender, EventArgs e) {
266      OpenFileDialog openFileDialog = new OpenFileDialog();
267      openFileDialog.Title = "Open Experimenter";
268      openFileDialog.FileName = "Item";
269      openFileDialog.Multiselect = false;
270      openFileDialog.DefaultExt = "hl";
271      openFileDialog.Filter = "HeuristicLab Files|*.hl|All Files|*.*";
272
273      if (openFileDialog.ShowDialog() == DialogResult.OK) {
274        Content.SetExperiment((HeuristicLab.Optimization.Experiment)ContentManager.Load(openFileDialog.FileName));
275      }
276    }
277
278    private void showExperimentButton_Click(object sender, EventArgs e) {
279      MainFormManager.MainForm.ShowContent(Content.GetExperiment(0));
280    }
281
282    private void viewExperimentButton_Click(object sender, EventArgs e) {
283      MainFormManager.MainForm.ShowContent(Content.GetExperiment(0));
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.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, (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.GetExperiment(0) != null && Content.ExecutionState == ExecutionState.Prepared;
319        pauseButton.Enabled = Content.ExecutionState == ExecutionState.Started;
320        stopButton.Enabled = Content.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.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.Progress);
342      }
343      progressView.Progress = Content.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  }
356}
Note: See TracBrowser for help on using the repository browser.