Free cookie consent management tool by TermsFeed Policy Generator

source: branches/RuntimeOptimizer/HeuristicLab.Optimization.Views/3.3/TimeLimitRunView.cs @ 8961

Last change on this file since 8961 was 8961, checked in by abeham, 12 years ago

#1985: added ability to edit snapshot times (changed representation to a list of timespan instead of a list of double)

File size: 13.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Windows.Forms;
26using HeuristicLab.Collections;
27using HeuristicLab.Common;
28using HeuristicLab.Common.Resources;
29using HeuristicLab.Core;
30using HeuristicLab.Core.Views;
31using HeuristicLab.MainForm;
32using HeuristicLab.MainForm.WindowsForms;
33using HeuristicLab.PluginInfrastructure;
34
35namespace HeuristicLab.Optimization.Views {
36  [View("TimeLimit Run View")]
37  [Content(typeof(TimeLimitRun), IsDefaultView = true)]
38  public partial class TimeLimitRunView : IOptimizerView {
39    protected TypeSelectorDialog algorithmTypeSelectorDialog;
40    protected virtual bool SuppressEvents { get; set; }
41
42    public new TimeLimitRun Content {
43      get { return (TimeLimitRun)base.Content; }
44      set { base.Content = value; }
45    }
46
47    public TimeLimitRunView() {
48      InitializeComponent();
49      snapshotButton.Text = String.Empty;
50      snapshotButton.Image = VSImageLibrary.Breakpoint;
51    }
52
53    protected override void Dispose(bool disposing) {
54      if (disposing) {
55        if (algorithmTypeSelectorDialog != null) algorithmTypeSelectorDialog.Dispose();
56        if (components != null) components.Dispose();
57      }
58      base.Dispose(disposing);
59    }
60
61    protected override void DeregisterContentEvents() {
62      Content.PropertyChanged -= Content_PropertyChanged;
63      Content.SnapshotTimes.ItemsAdded -= Content_SnapshotTimes_Changed;
64      Content.SnapshotTimes.ItemsMoved -= Content_SnapshotTimes_Changed;
65      Content.SnapshotTimes.ItemsRemoved -= Content_SnapshotTimes_Changed;
66      Content.SnapshotTimes.ItemsReplaced -= Content_SnapshotTimes_Changed;
67      Content.SnapshotTimes.CollectionReset -= Content_SnapshotTimes_Changed;
68      base.DeregisterContentEvents();
69    }
70    protected override void RegisterContentEvents() {
71      base.RegisterContentEvents();
72      Content.PropertyChanged += Content_PropertyChanged;
73      Content.SnapshotTimes.ItemsAdded += Content_SnapshotTimes_Changed;
74      Content.SnapshotTimes.ItemsMoved += Content_SnapshotTimes_Changed;
75      Content.SnapshotTimes.ItemsRemoved += Content_SnapshotTimes_Changed;
76      Content.SnapshotTimes.ItemsReplaced += Content_SnapshotTimes_Changed;
77      Content.SnapshotTimes.CollectionReset += Content_SnapshotTimes_Changed;
78    }
79
80    protected override void OnContentChanged() {
81      base.OnContentChanged();
82      SuppressEvents = true;
83      try {
84        if (Content == null) {
85          timeLimitTextBox.Text = FormatTimeSpan(TimeSpan.FromSeconds(60));
86          snapshotsTextBox.Text = String.Empty;
87          storeAlgorithmInEachSnapshotCheckBox.Checked = false;
88          algorithmViewHost.Content = null;
89          snapshotsView.Content = null;
90          runsView.Content = null;
91        } else {
92          timeLimitTextBox.Text = FormatTimeSpan(Content.MaximumExecutionTime);
93          snapshotsTextBox.Text = String.Join(" ; ", Content.SnapshotTimes.Select(x => FormatTimeSpan(x, true)));
94          storeAlgorithmInEachSnapshotCheckBox.Checked = Content.StoreAlgorithmInEachSnapshot;
95          algorithmViewHost.Content = Content.Algorithm;
96          snapshotsView.Content = Content.Snapshots;
97          runsView.Content = Content.Runs;
98        }
99      } finally { SuppressEvents = false; }
100    }
101
102    protected override void SetEnabledStateOfControls() {
103      base.SetEnabledStateOfControls();
104      timeLimitTextBox.Enabled = Content != null && !ReadOnly;
105      snapshotsTextBox.Enabled = Content != null && !ReadOnly;
106      storeAlgorithmInEachSnapshotCheckBox.Enabled = Content != null && !ReadOnly;
107      newAlgorithmButton.Enabled = Content != null && !ReadOnly;
108      openAlgorithmButton.Enabled = Content != null && !ReadOnly;
109      algorithmViewHost.Enabled = Content != null;
110      snapshotsView.Enabled = Content != null;
111      runsView.Enabled = Content != null;
112    }
113
114    protected override void SetEnabledStateOfExecutableButtons() {
115      base.SetEnabledStateOfExecutableButtons();
116      snapshotButton.Enabled = Content != null && (Content.ExecutionState == ExecutionState.Started || Content.ExecutionState == ExecutionState.Paused);
117    }
118
119    protected override void OnClosed(FormClosedEventArgs e) {
120      if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) {
121        //The content must be stopped if no other view showing the content is available
122        var optimizers = MainFormManager.MainForm.Views.OfType<IContentView>().Where(v => v != this).Select(v => v.Content).OfType<IAlgorithm>();
123        if (!optimizers.Contains(Content.Algorithm)) {
124          var nestedOptimizers = optimizers.SelectMany(opt => opt.NestedOptimizers);
125          if (!nestedOptimizers.Contains(Content)) Content.Stop();
126        }
127      }
128      base.OnClosed(e);
129    }
130
131    #region Event Handlers
132    #region Content events
133    private void Content_PropertyChanged(object sender, PropertyChangedEventArgs e) {
134      switch (e.PropertyName) {
135        case "MaximumExecutionTime":
136          SuppressEvents = true;
137          try {
138            timeLimitTextBox.Text = FormatTimeSpan(Content.MaximumExecutionTime);
139          } finally { SuppressEvents = false; }
140          break;
141        case "SnapshotTimes":
142          SuppressEvents = true;
143          try {
144            snapshotsTextBox.Text = string.Join(" ; ", Content.SnapshotTimes.Select(x => FormatTimeSpan(x, true)));
145            Content.SnapshotTimes.ItemsAdded += Content_SnapshotTimes_Changed;
146            Content.SnapshotTimes.ItemsMoved += Content_SnapshotTimes_Changed;
147            Content.SnapshotTimes.ItemsRemoved += Content_SnapshotTimes_Changed;
148            Content.SnapshotTimes.ItemsReplaced += Content_SnapshotTimes_Changed;
149            Content.SnapshotTimes.CollectionReset += Content_SnapshotTimes_Changed;
150          } finally { SuppressEvents = false; }
151          break;
152        case "StoreAlgorithmInEachSnapshot":
153          SuppressEvents = true;
154          try {
155            storeAlgorithmInEachSnapshotCheckBox.Checked = Content.StoreAlgorithmInEachSnapshot;
156          } finally { SuppressEvents = false; }
157          break;
158        case "Algorithm":
159          SuppressEvents = true;
160          try {
161            algorithmViewHost.Content = Content.Algorithm;
162          } finally { SuppressEvents = false; }
163          break;
164        case "Snapshots":
165          SuppressEvents = true;
166          try {
167            snapshotsView.Content = Content.Snapshots;
168          } finally { SuppressEvents = false; }
169          break;
170        case "Runs":
171          SuppressEvents = true;
172          try {
173            runsView.Content = Content.Runs;
174          } finally { SuppressEvents = false; }
175          break;
176      }
177    }
178
179    private void Content_SnapshotTimes_Changed(object sender, EventArgs e) {
180      SuppressEvents = true;
181      try {
182        snapshotsTextBox.Text = string.Join(" ; ", Content.SnapshotTimes.Select(x => FormatTimeSpan(x, true)));
183      } finally { SuppressEvents = false; }
184    }
185    #endregion
186
187    #region Control events
188    private void timeLimitTextBox_Validating(object sender, CancelEventArgs e) {
189      if (SuppressEvents) return;
190      TimeSpan ts;
191      if (!TryGetTimeSpanFromFormat(timeLimitTextBox.Text, out ts)) {
192        e.Cancel = true;
193        errorProvider.SetError(timeLimitTextBox, "Please enter a valid time span, e.g. 60, 20s, 45 seconds, 3h, 1 hour, 4 d, 2 days");
194      } else {
195        Content.MaximumExecutionTime = ts;
196        e.Cancel = false;
197        errorProvider.SetError(timeLimitTextBox, String.Empty);
198      }
199    }
200
201    private void snapshotsTextBox_Validating(object sender, CancelEventArgs e) {
202      if (SuppressEvents) return;
203      e.Cancel = false;
204      errorProvider.SetError(timeLimitTextBox, String.Empty);
205
206      var tokens = snapshotsTextBox.Text.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
207
208      var snapshotTimes = new ObservableList<TimeSpan>();
209      foreach (var t in tokens) {
210        TimeSpan value;
211        if (!TryGetTimeSpanFromFormat(t, out value)) {
212          e.Cancel = true;
213          errorProvider.SetError(snapshotsTextBox, "Error parsing " + t + ", please provide a valid time span, e.g. 60, 20s, 45 seconds, 3h, 1 hour, 4 d, 2 days");
214          return;
215        } else {
216          snapshotTimes.Add(value);
217        }
218      }
219      Content.SnapshotTimes = snapshotTimes;
220    }
221
222    private void storeAlgorithmInEachSnapshotCheckBox_CheckedChanged(object sender, EventArgs e) {
223      if (SuppressEvents) return;
224      SuppressEvents = true;
225      try {
226        Content.StoreAlgorithmInEachSnapshot = storeAlgorithmInEachSnapshotCheckBox.Checked;
227      } finally { SuppressEvents = false; }
228    }
229
230    private void newAlgorithmButton_Click(object sender, EventArgs e) {
231      if (algorithmTypeSelectorDialog == null) {
232        algorithmTypeSelectorDialog = new TypeSelectorDialog { Caption = "Select Algorithm" };
233        algorithmTypeSelectorDialog.TypeSelector.Caption = "Available Algorithms";
234        algorithmTypeSelectorDialog.TypeSelector.Configure(typeof(IAlgorithm), false, true);
235      }
236      if (algorithmTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
237        try {
238          Content.Algorithm = (IAlgorithm)algorithmTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
239        } catch (Exception ex) {
240          ErrorHandling.ShowErrorDialog(this, ex);
241        }
242      }
243    }
244
245    private void openAlgorithmButton_Click(object sender, EventArgs e) {
246      openFileDialog.Title = "Open Algorithm";
247      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
248        newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = false;
249        algorithmViewHost.Enabled = false;
250
251        ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) {
252          try {
253            if (error != null) throw error;
254            var algorithm = content as IAlgorithm;
255            if (algorithm == null)
256              MessageBox.Show(this, "The selected file does not contain an algorithm.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error);
257            else
258              Content.Algorithm = algorithm;
259          } catch (Exception ex) {
260            ErrorHandling.ShowErrorDialog(this, ex);
261          } finally {
262            Invoke(new Action(delegate() {
263              algorithmViewHost.Enabled = true;
264              newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = true;
265            }));
266          }
267        });
268      }
269    }
270
271    private void algorithmTabPage_DragEnterOver(object sender, DragEventArgs e) {
272      e.Effect = DragDropEffects.None;
273      if (!ReadOnly && (e.Data.GetData(Constants.DragDropDataFormat) is IAlgorithm)) {
274        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
275        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
276        else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
277        else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
278        else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
279      }
280    }
281
282    private void algorithmTabPage_DragDrop(object sender, DragEventArgs e) {
283      if (e.Effect != DragDropEffects.None) {
284        var algorithm = e.Data.GetData(Constants.DragDropDataFormat) as IAlgorithm;
285        if (e.Effect.HasFlag(DragDropEffects.Copy)) algorithm = (IAlgorithm)algorithm.Clone();
286        Content.Algorithm = algorithm;
287      }
288    }
289
290    private void snapshotButton_Click(object sender, EventArgs e) {
291      Content.Snapshot();
292    }
293    #endregion
294    #endregion
295
296    private string FormatTimeSpan(TimeSpan ts, bool abbrevUnit = false) {
297      if (ts.TotalSeconds < 180) return ts.TotalSeconds + (abbrevUnit ? "s" : " seconds");
298      if (ts.TotalMinutes < 180) return ts.TotalMinutes + (abbrevUnit ? "min" : " minutes");
299      if (ts.TotalHours < 96) return ts.TotalHours + (abbrevUnit ? "h" : " hours");
300      return ts.TotalDays + (abbrevUnit ? "d" : " days");
301    }
302
303    private bool TryGetTimeSpanFromFormat(string text, out TimeSpan result) {
304      double value;
305      text = text.Trim();
306      var length = text.Length;
307      while (!double.TryParse(text.Substring(0, length), out value)) {
308        length--;
309        if (length <= 0) {
310          value = double.NaN;
311          break;
312        }
313      }
314      if (double.IsNaN(value)) {
315        result = TimeSpan.Zero;
316        return false;
317      } else {
318        var remaining = String.Empty;
319        if (length < text.Length) remaining = text.Substring(length, text.Length - length);
320        switch (remaining.Trim()) {
321          case "d":
322          case "day":
323          case "days": result = TimeSpan.FromDays(value); break;
324          case "h":
325          case "hour":
326          case "hours": result = TimeSpan.FromHours(value); break;
327          case "min":
328          case "minute":
329          case "minutes": result = TimeSpan.FromMinutes(value); break;
330          default: result = TimeSpan.FromSeconds(value); break;
331        }
332        return true;
333      }
334    }
335  }
336}
Note: See TracBrowser for help on using the repository browser.