[8055] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9456] | 3 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[8055] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Drawing;
|
---|
| 25 | using System.Linq;
|
---|
| 26 | using System.Threading.Tasks;
|
---|
| 27 | using System.Windows.Forms;
|
---|
| 28 | using HeuristicLab.Clients.Access;
|
---|
| 29 | using HeuristicLab.Core;
|
---|
| 30 | using HeuristicLab.Core.Views;
|
---|
| 31 | using HeuristicLab.MainForm;
|
---|
| 32 | using HeuristicLab.MainForm.WindowsForms;
|
---|
| 33 | using HeuristicLab.Optimization;
|
---|
| 34 |
|
---|
| 35 | namespace 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; }
|
---|
[8165] | 41 | set { base.Content = value; }
|
---|
[8055] | 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;
|
---|
[8181] | 56 | private ProgressView progressView;
|
---|
[8165] | 57 | private Progress progress;
|
---|
[8055] | 58 |
|
---|
| 59 | public OKBExperimentUploadView() {
|
---|
| 60 | InitializeComponent();
|
---|
[9893] | 61 | progress = new Progress();
|
---|
[8055] | 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);
|
---|
[8165] | 95 | progressView = new ProgressView(this, progress);
|
---|
[8055] | 96 | }
|
---|
| 97 |
|
---|
| 98 | protected override void DeregisterContentEvents() {
|
---|
| 99 | RunCreationClient.Instance.Refreshing -= new EventHandler(RunCreationClient_Refreshing);
|
---|
| 100 | RunCreationClient.Instance.Refreshed -= new EventHandler(RunCreationClient_Refreshed);
|
---|
[8165] | 101 | if (progressView != null) {
|
---|
| 102 | progressView.Dispose();
|
---|
| 103 | progressView = null;
|
---|
| 104 | }
|
---|
[8055] | 105 | base.DeregisterContentEvents();
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | private void DisplayError(Exception ex) {
|
---|
| 109 | PluginInfrastructure.ErrorHandling.ShowErrorDialog("An error occured while retrieving algorithm and problem information from the OKB.", ex);
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | private void DisplayRuns(RunCollection runs) {
|
---|
| 113 | if (RunCreationClient.Instance.Algorithms == null || RunCreationClient.Instance.Algorithms.Count() == 0) {
|
---|
[8158] | 114 | Action a = new Action(delegate {
|
---|
| 115 | RunCreationClient.Instance.Refresh();
|
---|
| 116 | CreateUI(runs);
|
---|
| 117 | });
|
---|
| 118 |
|
---|
| 119 | Task.Factory.StartNew(a).ContinueWith((t) => { DisplayError(t.Exception); }, TaskContinuationOptions.OnlyOnFaulted);
|
---|
[8055] | 120 | } else {
|
---|
| 121 | CreateUI(runs);
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | private void CreateUI(RunCollection runs) {
|
---|
[8158] | 126 | if (InvokeRequired) {
|
---|
| 127 | Invoke(new Action<RunCollection>(CreateUI), runs);
|
---|
| 128 | } else {
|
---|
| 129 | if (problems.Count == 0)
|
---|
| 130 | problems.AddRange(RunCreationClient.Instance.Problems);
|
---|
| 131 | if (algorithms.Count == 0)
|
---|
| 132 | algorithms.AddRange(RunCreationClient.Instance.Algorithms);
|
---|
[8055] | 133 |
|
---|
[8158] | 134 | IItem algorithmType;
|
---|
| 135 | IItem problemType;
|
---|
| 136 | IItem algorithmName;
|
---|
| 137 | IItem problemName;
|
---|
[8055] | 138 |
|
---|
[8158] | 139 | DataGridViewComboBoxColumn cmbAlgorithm = dataGridView.Columns[algorithmColumnIndex] as DataGridViewComboBoxColumn;
|
---|
| 140 | cmbAlgorithm.DataSource = algorithms;
|
---|
| 141 | cmbAlgorithm.DisplayMember = "Name";
|
---|
[8055] | 142 |
|
---|
[8158] | 143 | DataGridViewComboBoxColumn cmbProblem = dataGridView.Columns[problemColumnIndex] as DataGridViewComboBoxColumn;
|
---|
| 144 | cmbProblem.DataSource = problems;
|
---|
| 145 | cmbProblem.DisplayMember = "Name";
|
---|
[8055] | 146 |
|
---|
[8158] | 147 | foreach (IRun run in runs) {
|
---|
| 148 | int idx = dataGridView.Rows.Add(run.Name);
|
---|
| 149 | DataGridViewRow curRow = dataGridView.Rows[idx];
|
---|
| 150 | curRow.Tag = run;
|
---|
[8055] | 151 |
|
---|
[8158] | 152 | if (run.Parameters.TryGetValue(algorithmTypeParameterName, out algorithmType)) {
|
---|
| 153 | HeuristicLab.Data.StringValue algStr = algorithmType as HeuristicLab.Data.StringValue;
|
---|
| 154 | if (algStr != null) {
|
---|
| 155 | curRow.Cells[1].Value = algStr;
|
---|
| 156 | }
|
---|
[8055] | 157 | }
|
---|
| 158 |
|
---|
[8158] | 159 | if (run.Parameters.TryGetValue(algorithmNameParameterName, out algorithmName)) {
|
---|
| 160 | HeuristicLab.Data.StringValue algStr = algorithmName as HeuristicLab.Data.StringValue;
|
---|
| 161 | if (algStr != null) {
|
---|
| 162 | curRow.Cells[2].Value = algStr;
|
---|
| 163 | }
|
---|
[8055] | 164 | }
|
---|
| 165 |
|
---|
[8158] | 166 | if (run.Parameters.TryGetValue(problemTypeParameterName, out problemType)) {
|
---|
| 167 | HeuristicLab.Data.StringValue prbStr = problemType as HeuristicLab.Data.StringValue;
|
---|
| 168 | if (prbStr != null) {
|
---|
| 169 | curRow.Cells[4].Value = prbStr;
|
---|
| 170 | }
|
---|
[8055] | 171 | }
|
---|
| 172 |
|
---|
[8158] | 173 | if (run.Parameters.TryGetValue(problemNameParameterName, out problemName)) {
|
---|
| 174 | HeuristicLab.Data.StringValue prbStr = problemName as HeuristicLab.Data.StringValue;
|
---|
| 175 | if (prbStr != null) {
|
---|
| 176 | curRow.Cells[5].Value = prbStr;
|
---|
| 177 | }
|
---|
[8055] | 178 | }
|
---|
| 179 | }
|
---|
| 180 | }
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | private void ClearRuns() {
|
---|
[8158] | 184 | if (InvokeRequired) {
|
---|
| 185 | Invoke(new Action(ClearRuns));
|
---|
| 186 | } else {
|
---|
| 187 | dataGridView.Rows.Clear();
|
---|
| 188 | runs.Clear();
|
---|
| 189 | }
|
---|
[8055] | 190 | }
|
---|
| 191 |
|
---|
| 192 | private void RunCreationClient_Refreshing(object sender, EventArgs e) {
|
---|
| 193 | if (InvokeRequired) {
|
---|
| 194 | Invoke(new EventHandler(RunCreationClient_Refreshing), sender, e);
|
---|
| 195 | } else {
|
---|
[8145] | 196 | progress.Status = "Refreshing algorithms and problems...";
|
---|
[8165] | 197 | progress.ProgressState = ProgressState.Started;
|
---|
[8055] | 198 | }
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | private void RunCreationClient_Refreshed(object sender, EventArgs e) {
|
---|
| 202 | if (InvokeRequired) {
|
---|
| 203 | Invoke(new EventHandler(RunCreationClient_Refreshed), sender, e);
|
---|
| 204 | } else {
|
---|
[8165] | 205 | progress.Finish();
|
---|
[8055] | 206 | SetEnabledStateOfControls();
|
---|
| 207 | }
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | private void btnUpload_Click(object sender, EventArgs e) {
|
---|
| 211 | var task = System.Threading.Tasks.Task.Factory.StartNew(UploadAsync);
|
---|
| 212 | task.ContinueWith((t) => {
|
---|
[8165] | 213 | progress.Finish();
|
---|
[8055] | 214 | PluginInfrastructure.ErrorHandling.ShowErrorDialog("An exception occured while uploading the runs to the OKB.", t.Exception);
|
---|
| 215 | }, TaskContinuationOptions.OnlyOnFaulted);
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 | private void UploadAsync() {
|
---|
[8145] | 219 | progress.Status = "Uploading runs to OKB...";
|
---|
| 220 | progress.ProgressValue = 0;
|
---|
[8181] | 221 | progress.ProgressState = ProgressState.Started;
|
---|
[8055] | 222 | double count = dataGridView.Rows.Count;
|
---|
| 223 | int i = 0;
|
---|
[8181] | 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 | }
|
---|
[8055] | 230 |
|
---|
[8181] | 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;
|
---|
[8055] | 235 | }
|
---|
[8186] | 236 | progress.Finish();
|
---|
[8158] | 237 | ClearRuns();
|
---|
[8055] | 238 | }
|
---|
| 239 |
|
---|
| 240 | private void dataGridView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
|
---|
| 241 | if (e.Button == System.Windows.Forms.MouseButtons.Right && dataGridView[e.ColumnIndex, e.RowIndex].Value != null) {
|
---|
| 242 | string curVal = dataGridView[e.ColumnIndex, e.RowIndex].Value.ToString();
|
---|
| 243 | selectedAlgorithm = algorithms.Where(x => x.Name == curVal).FirstOrDefault();
|
---|
| 244 | selectedProblem = problems.Where(x => x.Name == curVal).FirstOrDefault();
|
---|
| 245 |
|
---|
| 246 | if (selectedAlgorithm != null || selectedProblem != null) {
|
---|
| 247 | Point pos = this.PointToClient(Cursor.Position);
|
---|
| 248 | contextMenu.Show(this, pos);
|
---|
| 249 | }
|
---|
| 250 | }
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 | private void setColumnToThisValueToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 254 | if (selectedAlgorithm != null) {
|
---|
| 255 | for (int i = 0; i < dataGridView.Rows.Count; i++) {
|
---|
| 256 | var row = dataGridView.Rows[i];
|
---|
| 257 | row.Cells[algorithmColumnIndex].Value = selectedAlgorithm.Name;
|
---|
| 258 | }
|
---|
| 259 | } else if (selectedProblem != null) {
|
---|
| 260 | for (int i = 0; i < dataGridView.Rows.Count; i++) {
|
---|
| 261 | var row = dataGridView.Rows[i];
|
---|
| 262 | row.Cells[problemColumnIndex].Value = selectedProblem.Name;
|
---|
| 263 | }
|
---|
| 264 | }
|
---|
| 265 | selectedAlgorithm = null;
|
---|
| 266 | selectedProblem = null;
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | private void dataGridView_DragEnter(object sender, DragEventArgs e) {
|
---|
| 270 | e.Effect = DragDropEffects.None;
|
---|
| 271 | if (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IRun
|
---|
| 272 | || e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IOptimizer) {
|
---|
| 273 | if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link; // ALT key
|
---|
| 274 | else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move; // SHIFT key
|
---|
| 275 | else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
|
---|
| 276 | else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
|
---|
| 277 | else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
|
---|
| 278 | }
|
---|
| 279 | }
|
---|
| 280 |
|
---|
| 281 | private void dataGridView_DragDrop(object sender, DragEventArgs e) {
|
---|
| 282 | if (e.Effect != DragDropEffects.None) {
|
---|
| 283 | IItem optimizer = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IItem;
|
---|
| 284 | if (e.Effect.HasFlag(DragDropEffects.Copy)) optimizer = (IItem)optimizer.Clone();
|
---|
| 285 | AddRuns(optimizer);
|
---|
| 286 | }
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | private void clearButton_Click(object sender, EventArgs e) {
|
---|
| 290 | ClearRuns();
|
---|
| 291 | }
|
---|
| 292 | }
|
---|
| 293 | }
|
---|