Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Clients.OKB.Views/3.3/RunCreation/Views/OKBExperimentUploadView.cs @ 12440

Last change on this file since 12440 was 12009, checked in by ascheibe, 10 years ago

#2212 updated copyright year

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