Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1174 removed RunCollection as content from the OKBExperimentUploadView as this led to an InvalidCastException

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