Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKB (trunk integration)/HeuristicLab.Clients.OKB/3.3/RunCreation/Views/OKBExperimentUploadView.cs @ 7844

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

#1174 worked on upload view

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