Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Clients.OKB.Views/3.3/RunCreation/Views/OKBExperimentUploadView.cs @ 8160

Last change on this file since 8160 was 8160, checked in by ascheibe, 12 years ago

#1762 removed unnecessary SetEnabledStateOfControls calls

File size: 11.8 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.Collections.Generic;
24using System.Drawing;
25using System.Linq;
26using System.Threading.Tasks;
27using System.Windows.Forms;
28using HeuristicLab.Clients.Access;
29using HeuristicLab.Core;
30using HeuristicLab.Core.Views;
31using HeuristicLab.MainForm;
32using HeuristicLab.MainForm.WindowsForms;
33using HeuristicLab.Optimization;
34
35namespace HeuristicLab.Clients.OKB.RunCreation {
36  [View("OKBExperimentUpload View")]
37  [Content(typeof(IOptimizer), false)]
38  public partial class OKBExperimentUploadView : ItemView {
39    public new IOptimizer Content {
40      get { return (IOptimizer)base.Content; }
41      set {
42        base.Content = value;
43      }
44    }
45
46    private const string algorithmTypeParameterName = "Algorithm Type";
47    private const string problemTypeParameterName = "Problem Type";
48    private const string algorithmNameParameterName = "Algorithm Name";
49    private const string problemNameParameterName = "Problem Name";
50    private const int algorithmColumnIndex = 3;
51    private const int problemColumnIndex = 6;
52
53    private List<IRun> runs = new List<IRun>();
54    private List<Problem> problems = new List<Problem>();
55    private List<Algorithm> algorithms = new List<Algorithm>();
56    Algorithm selectedAlgorithm = null;
57    Problem selectedProblem = null;
58    private ProgressView progressView;
59    private IProgress progress;
60
61    public OKBExperimentUploadView() {
62      InitializeComponent();
63      progressView = new ProgressView(this);
64    }
65
66    protected override void OnContentChanged() {
67      base.OnContentChanged();
68      if (Content == null) {
69        ClearRuns();
70      } else {
71        AddRuns(Content);
72      }
73    }
74
75    private void AddRuns(IItem item) {
76      if (item is Experiment) {
77        runs.AddRange((item as Experiment).Runs);
78        DisplayRuns((item as Experiment).Runs);
79      } else if (item is RunCollection) {
80        runs.AddRange((item as RunCollection));
81        DisplayRuns((item as RunCollection));
82      } else if (item is IOptimizer) {
83        runs.AddRange((item as IOptimizer).Runs);
84        DisplayRuns((item as IOptimizer).Runs);
85      } else if (item is IRun) {
86        runs.Add(item as IRun);
87        RunCollection tmp = new RunCollection();
88        tmp.Add(item as IRun);
89        DisplayRuns(tmp);
90      }
91    }
92
93    protected override void RegisterContentEvents() {
94      base.RegisterContentEvents();
95      RunCreationClient.Instance.Refreshing += new EventHandler(RunCreationClient_Refreshing);
96      RunCreationClient.Instance.Refreshed += new EventHandler(RunCreationClient_Refreshed);
97    }
98
99    protected override void DeregisterContentEvents() {
100      RunCreationClient.Instance.Refreshing -= new EventHandler(RunCreationClient_Refreshing);
101      RunCreationClient.Instance.Refreshed -= new EventHandler(RunCreationClient_Refreshed);
102      base.DeregisterContentEvents();
103    }
104
105    private void DisplayError(Exception ex) {
106      PluginInfrastructure.ErrorHandling.ShowErrorDialog("An error occured while retrieving algorithm and problem information from the OKB.", ex);
107    }
108
109    private void DisplayRuns(RunCollection runs) {
110      if (RunCreationClient.Instance.Algorithms == null || RunCreationClient.Instance.Algorithms.Count() == 0) {
111        Action a = new Action(delegate {
112          RunCreationClient.Instance.Refresh();
113          CreateUI(runs);
114        });
115
116        Task.Factory.StartNew(a).ContinueWith((t) => { DisplayError(t.Exception); }, TaskContinuationOptions.OnlyOnFaulted);
117      } else {
118        CreateUI(runs);
119      }
120    }
121
122    private void CreateUI(RunCollection runs) {
123      if (InvokeRequired) {
124        Invoke(new Action<RunCollection>(CreateUI), runs);
125      } else {
126        if (problems.Count == 0)
127          problems.AddRange(RunCreationClient.Instance.Problems);
128        if (algorithms.Count == 0)
129          algorithms.AddRange(RunCreationClient.Instance.Algorithms);
130
131        IItem algorithmType;
132        IItem problemType;
133        IItem algorithmName;
134        IItem problemName;
135
136        DataGridViewComboBoxColumn cmbAlgorithm = dataGridView.Columns[algorithmColumnIndex] as DataGridViewComboBoxColumn;
137        cmbAlgorithm.DataSource = algorithms;
138        cmbAlgorithm.DisplayMember = "Name";
139
140        DataGridViewComboBoxColumn cmbProblem = dataGridView.Columns[problemColumnIndex] as DataGridViewComboBoxColumn;
141        cmbProblem.DataSource = problems;
142        cmbProblem.DisplayMember = "Name";
143
144        foreach (IRun run in runs) {
145          int idx = dataGridView.Rows.Add(run.Name);
146          DataGridViewRow curRow = dataGridView.Rows[idx];
147          curRow.Tag = run;
148
149          if (run.Parameters.TryGetValue(algorithmTypeParameterName, out algorithmType)) {
150            HeuristicLab.Data.StringValue algStr = algorithmType as HeuristicLab.Data.StringValue;
151            if (algStr != null) {
152              curRow.Cells[1].Value = algStr;
153            }
154          }
155
156          if (run.Parameters.TryGetValue(algorithmNameParameterName, out algorithmName)) {
157            HeuristicLab.Data.StringValue algStr = algorithmName as HeuristicLab.Data.StringValue;
158            if (algStr != null) {
159              curRow.Cells[2].Value = algStr;
160            }
161          }
162
163          if (run.Parameters.TryGetValue(problemTypeParameterName, out problemType)) {
164            HeuristicLab.Data.StringValue prbStr = problemType as HeuristicLab.Data.StringValue;
165            if (prbStr != null) {
166              curRow.Cells[4].Value = prbStr;
167            }
168          }
169
170          if (run.Parameters.TryGetValue(problemNameParameterName, out problemName)) {
171            HeuristicLab.Data.StringValue prbStr = problemName as HeuristicLab.Data.StringValue;
172            if (prbStr != null) {
173              curRow.Cells[5].Value = prbStr;
174            }
175          }
176        }
177      }
178    }
179
180    private void ClearRuns() {
181      if (InvokeRequired) {
182        Invoke(new Action(ClearRuns));
183      } else {
184        dataGridView.Rows.Clear();
185        runs.Clear();
186      }
187    }
188
189    private void RunCreationClient_Refreshing(object sender, EventArgs e) {
190      if (InvokeRequired) {
191        Invoke(new EventHandler(RunCreationClient_Refreshing), sender, e);
192      } else {
193        progress = new Progress();
194        progress.Status = "Refreshing algorithms and problems...";
195        SetProgressView(progress);
196      }
197    }
198
199    private void RunCreationClient_Refreshed(object sender, EventArgs e) {
200      if (InvokeRequired) {
201        Invoke(new EventHandler(RunCreationClient_Refreshed), sender, e);
202      } else {
203        FinishProgressView();
204        SetEnabledStateOfControls();
205      }
206    }
207
208    private void btnUpload_Click(object sender, EventArgs e) {
209      var task = System.Threading.Tasks.Task.Factory.StartNew(UploadAsync);
210      task.ContinueWith((t) => {
211        FinishProgressView();
212        PluginInfrastructure.ErrorHandling.ShowErrorDialog("An exception occured while uploading the runs to the OKB.", t.Exception);
213      }, TaskContinuationOptions.OnlyOnFaulted);
214    }
215
216    private void UploadAsync() {
217      progress = new Progress();
218      progress.Status = "Uploading runs to OKB...";
219      progress.ProgressValue = 0;
220      double count = dataGridView.Rows.Count;
221      int i = 0;
222
223      SetProgressView(progress);
224      foreach (DataGridViewRow row in dataGridView.Rows) {
225        selectedAlgorithm = algorithms.Where(x => x.Name == row.Cells[algorithmColumnIndex].Value.ToString()).FirstOrDefault();
226        selectedProblem = problems.Where(x => x.Name == row.Cells[problemColumnIndex].Value.ToString()).FirstOrDefault();
227        if (selectedAlgorithm == null || selectedProblem == null) {
228          throw new ArgumentException("Can't retrieve the algorithm/problem to upload");
229        }
230
231        OKBRun run = new OKBRun(selectedAlgorithm.Id, selectedProblem.Id, row.Tag as IRun, UserInformation.Instance.User.Id);
232        run.Store();
233        i++;
234        progress.ProgressValue = ((double)i) / count;
235      }
236      FinishProgressView();
237      ClearRuns();
238    }
239
240    private void SetProgressView(IProgress progress) {
241      if (InvokeRequired) {
242        Invoke(new Action<IProgress>(SetProgressView), progress);
243      } else {
244        progressView.Progress = progress;
245      }
246    }
247
248    private void FinishProgressView() {
249      if (InvokeRequired) {
250        Invoke(new Action(FinishProgressView));
251      } else {
252        progress.Finish();
253      }
254    }
255
256    private void dataGridView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
257      if (e.Button == System.Windows.Forms.MouseButtons.Right && dataGridView[e.ColumnIndex, e.RowIndex].Value != null) {
258        string curVal = dataGridView[e.ColumnIndex, e.RowIndex].Value.ToString();
259        selectedAlgorithm = algorithms.Where(x => x.Name == curVal).FirstOrDefault();
260        selectedProblem = problems.Where(x => x.Name == curVal).FirstOrDefault();
261
262        if (selectedAlgorithm != null || selectedProblem != null) {
263          Point pos = this.PointToClient(Cursor.Position);
264          contextMenu.Show(this, pos);
265        }
266      }
267    }
268
269    private void setColumnToThisValueToolStripMenuItem_Click(object sender, EventArgs e) {
270      if (selectedAlgorithm != null) {
271        for (int i = 0; i < dataGridView.Rows.Count; i++) {
272          var row = dataGridView.Rows[i];
273          row.Cells[algorithmColumnIndex].Value = selectedAlgorithm.Name;
274        }
275      } else if (selectedProblem != null) {
276        for (int i = 0; i < dataGridView.Rows.Count; i++) {
277          var row = dataGridView.Rows[i];
278          row.Cells[problemColumnIndex].Value = selectedProblem.Name;
279        }
280      }
281      selectedAlgorithm = null;
282      selectedProblem = null;
283    }
284
285    private void dataGridView_DragEnter(object sender, DragEventArgs e) {
286      e.Effect = DragDropEffects.None;
287      if (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IRun
288        || e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IOptimizer) {
289        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
290        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
291        else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
292        else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
293        else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
294      }
295    }
296
297    private void dataGridView_DragDrop(object sender, DragEventArgs e) {
298      if (e.Effect != DragDropEffects.None) {
299        IItem optimizer = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IItem;
300        if (e.Effect.HasFlag(DragDropEffects.Copy)) optimizer = (IItem)optimizer.Clone();
301        AddRuns(optimizer);
302      }
303    }
304
305    private void clearButton_Click(object sender, EventArgs e) {
306      ClearRuns();
307    }
308  }
309}
Note: See TracBrowser for help on using the repository browser.