Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14676 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 12.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.MainForm;
31using HeuristicLab.Optimization;
32using View = HeuristicLab.MainForm.WindowsForms.View;
33
34namespace HeuristicLab.Clients.OKB.RunCreation {
35  [View("OKBExperimentUpload View")]
36  public partial class OKBExperimentUploadView : View {
37
38    private const string AlgorithmTypeParameterName = "Algorithm Type";
39    private const string ProblemTypeParameterName = "Problem Type";
40    private const string AlgorithmNameParameterName = "Algorithm Name";
41    private const string ProblemNameParameterName = "Problem Name";
42
43    private List<IRun> runs = new List<IRun>();
44    private List<Problem> problems = new List<Problem>();
45    private List<Algorithm> algorithms = new List<Algorithm>();
46    Algorithm selectedAlgorithm = null;
47    Problem selectedProblem = null;
48
49    public OKBExperimentUploadView() {
50      InitializeComponent();
51      OKBAlgorithmColumn.ValueType = typeof(Algorithm);
52      OKBAlgorithmColumn.ValueMember = "Name";
53      OKBAlgorithmColumn.DisplayMember = "Name";
54      OKBProblemColumn.ValueType = typeof(Problem);
55      OKBProblemColumn.ValueMember = "Name";
56      OKBProblemColumn.DisplayMember = "Name";
57      RunCreationClient.Instance.Refreshing += RunCreationClient_Refreshing;
58      RunCreationClient.Instance.Refreshed += RunCreationClient_Refreshed;
59    }
60
61    private void DisposeSpecific() {
62      RunCreationClient.Instance.Refreshing -= RunCreationClient_Refreshing;
63      RunCreationClient.Instance.Refreshed -= RunCreationClient_Refreshed;
64    }
65
66    private bool refreshing;
67
68    protected override void SetEnabledStateOfControls() {
69      if (InvokeRequired) { Invoke((Action)SetEnabledStateOfControls); return; }
70      base.SetEnabledStateOfControls();
71      btnUpload.Enabled = runs.Count > 0 && !refreshing;
72    }
73
74    public void AddRuns(IItem item) {
75      if (InvokeRequired) { Invoke((Action<IItem>)AddRuns, item); return; }
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      SetEnabledStateOfControls();
92    }
93
94    private void DisplayError(Exception ex) {
95      PluginInfrastructure.ErrorHandling.ShowErrorDialog("An error occured while retrieving algorithm and problem information from the OKB.", ex);
96    }
97
98    private void DisplayRuns(RunCollection runs) {
99      if (RunCreationClient.Instance.Algorithms == null || RunCreationClient.Instance.Algorithms.Count() == 0) {
100        Action a = new Action(delegate {
101          RunCreationClient.Instance.Refresh();
102          CreateUI(runs);
103        });
104
105        Task.Factory.StartNew(a).ContinueWith((t) => { DisplayError(t.Exception); }, TaskContinuationOptions.OnlyOnFaulted);
106      } else {
107        CreateUI(runs);
108      }
109    }
110
111    private void CreateUI(RunCollection runs) {
112      if (InvokeRequired) { Invoke((Action<RunCollection>)CreateUI, runs); return; }
113      if (problems.Count == 0)
114        problems.AddRange(RunCreationClient.Instance.Problems);
115      if (algorithms.Count == 0)
116        algorithms.AddRange(RunCreationClient.Instance.Algorithms);
117
118      IItem algorithmType;
119      IItem problemType;
120      IItem algorithmName;
121      IItem problemName;
122
123      OKBAlgorithmColumn.DataSource = algorithms;
124      OKBProblemColumn.DataSource = problems;
125
126      foreach (IRun run in runs) {
127        int idx = dataGridView.Rows.Add(run.Name);
128        DataGridViewRow curRow = dataGridView.Rows[idx];
129        curRow.Tag = run;
130
131        HeuristicLab.Data.StringValue algStr = null, algTypeStr = null, prbStr = null, prbTypeStr = null;
132        if (run.Parameters.TryGetValue(AlgorithmNameParameterName, out algorithmName)) {
133          algStr = algorithmName as HeuristicLab.Data.StringValue;
134          if (algStr != null) {
135            curRow.Cells[AlgorithmNameColumn.Name].Value = algStr;
136          }
137        }
138
139        if (run.Parameters.TryGetValue(AlgorithmTypeParameterName, out algorithmType)) {
140          algTypeStr = algorithmType as HeuristicLab.Data.StringValue;
141          if (algTypeStr != null) {
142            curRow.Cells[AlgorithmTypeColumn.Name].Value = algTypeStr;
143          }
144        }
145
146        var uploadOk = false;
147        if (algStr != null && algTypeStr != null) {
148          var alg = algorithms.FirstOrDefault(x => x.DataType.Name == algTypeStr.Value && x.Name == algStr.Value);
149          if (alg != null) {
150            curRow.Cells[OKBAlgorithmColumn.Name].Value = alg.Name;
151            uploadOk = true;
152          }
153        }
154
155        if (run.Parameters.TryGetValue(ProblemNameParameterName, out problemName)) {
156          prbStr = problemName as HeuristicLab.Data.StringValue;
157          if (prbStr != null) {
158            curRow.Cells[ProblemNameColumn.Name].Value = prbStr;
159          }
160        }
161
162        if (run.Parameters.TryGetValue(ProblemTypeParameterName, out problemType)) {
163          prbTypeStr = problemType as HeuristicLab.Data.StringValue;
164          if (prbTypeStr != null) {
165            curRow.Cells[ProblemTypeColumn.Name].Value = prbTypeStr;
166          }
167        }
168
169        if (prbStr != null && prbTypeStr != null) {
170          var prb = problems.FirstOrDefault(x => x.DataType.Name == prbTypeStr.Value && x.Name == prbStr.Value);
171          if (prb != null) {
172            curRow.Cells[OKBProblemColumn.Name].Value = prb.Name;
173          } else uploadOk = false;
174        }
175
176        curRow.Cells[UploadColumn.Name].Value = uploadOk;
177      }
178    }
179
180    public void ClearRuns() {
181      if (InvokeRequired) { Invoke((Action)ClearRuns); return; }
182      dataGridView.Rows.Clear();
183      runs.Clear();
184      SetEnabledStateOfControls();
185    }
186
187    private void RunCreationClient_Refreshing(object sender, EventArgs e) {
188      if (InvokeRequired) { Invoke((Action<object, EventArgs>)RunCreationClient_Refreshing, sender, e); return; }
189      var message = "Refreshing algorithms and problems...";
190      MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().AddOperationProgressToView(this, message);
191      refreshing = true;
192      SetEnabledStateOfControls();
193    }
194
195    private void RunCreationClient_Refreshed(object sender, EventArgs e) {
196      if (InvokeRequired) { Invoke((Action<object, EventArgs>)RunCreationClient_Refreshed, sender, e); return; }
197      MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().RemoveOperationProgressFromView(this);
198      refreshing = false;
199      SetEnabledStateOfControls();
200    }
201
202    private void btnUpload_Click(object sender, EventArgs e) {
203      var task = System.Threading.Tasks.Task.Factory.StartNew(UploadAsync);
204      task.ContinueWith((t) => {
205        MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().RemoveOperationProgressFromView(this);
206        PluginInfrastructure.ErrorHandling.ShowErrorDialog("An exception occured while uploading the runs to the OKB.", t.Exception);
207      }, TaskContinuationOptions.OnlyOnFaulted);
208    }
209
210    private void UploadAsync() {
211      var message = "Uploading runs to OKB...";
212      IProgress progress = MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().AddOperationProgressToView(this, message);
213      double count = dataGridView.Rows.Count;
214      int i = 0;
215      foreach (DataGridViewRow row in dataGridView.Rows) {
216        i++;
217        if (!Convert.ToBoolean(row.Cells[UploadColumn.Name].Value)) continue;
218        selectedAlgorithm = algorithms.FirstOrDefault(x => x.Name == row.Cells[OKBAlgorithmColumn.Name].Value.ToString());
219        selectedProblem = problems.FirstOrDefault(x => x.Name == row.Cells[OKBProblemColumn.Name].Value.ToString());
220        if (selectedAlgorithm == null || selectedProblem == null) {
221          throw new ArgumentException("Can't retrieve the algorithm/problem to upload");
222        }
223
224        OKBRun run = new OKBRun(selectedAlgorithm.Id, selectedProblem.Id, row.Tag as IRun, UserInformation.Instance.User.Id);
225        run.Store();
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.FirstOrDefault(x => x.Name == curVal);
236        selectedProblem = problems.FirstOrDefault(x => x.Name == curVal);
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[OKBAlgorithmColumn.Name].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[OKBProblemColumn.Name].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.