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