[8055] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15584] | 3 | * Copyright (C) 2002-2018 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.MainForm;
|
---|
| 31 | using HeuristicLab.Optimization;
|
---|
[13195] | 32 | using View = HeuristicLab.MainForm.WindowsForms.View;
|
---|
[8055] | 33 |
|
---|
| 34 | namespace HeuristicLab.Clients.OKB.RunCreation {
|
---|
| 35 | [View("OKBExperimentUpload View")]
|
---|
[13195] | 36 | public partial class OKBExperimentUploadView : View {
|
---|
[8055] | 37 |
|
---|
[13195] | 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";
|
---|
[8055] | 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();
|
---|
[13195] | 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;
|
---|
[8055] | 59 | }
|
---|
| 60 |
|
---|
[13195] | 61 | private void DisposeSpecific() {
|
---|
| 62 | RunCreationClient.Instance.Refreshing -= RunCreationClient_Refreshing;
|
---|
| 63 | RunCreationClient.Instance.Refreshed -= RunCreationClient_Refreshed;
|
---|
[8055] | 64 | }
|
---|
| 65 |
|
---|
[13195] | 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; }
|
---|
[8055] | 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 | }
|
---|
[13195] | 91 | SetEnabledStateOfControls();
|
---|
[8055] | 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) {
|
---|
[8158] | 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);
|
---|
[8055] | 106 | } else {
|
---|
| 107 | CreateUI(runs);
|
---|
| 108 | }
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | private void CreateUI(RunCollection runs) {
|
---|
[13195] | 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);
|
---|
[8055] | 117 |
|
---|
[13195] | 118 | IItem algorithmType;
|
---|
| 119 | IItem problemType;
|
---|
| 120 | IItem algorithmName;
|
---|
| 121 | IItem problemName;
|
---|
[8055] | 122 |
|
---|
[13195] | 123 | OKBAlgorithmColumn.DataSource = algorithms;
|
---|
| 124 | OKBProblemColumn.DataSource = problems;
|
---|
[8055] | 125 |
|
---|
[13195] | 126 | foreach (IRun run in runs) {
|
---|
| 127 | int idx = dataGridView.Rows.Add(run.Name);
|
---|
| 128 | DataGridViewRow curRow = dataGridView.Rows[idx];
|
---|
| 129 | curRow.Tag = run;
|
---|
[8055] | 130 |
|
---|
[13195] | 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 | }
|
---|
[8055] | 138 |
|
---|
[13195] | 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;
|
---|
[8055] | 143 | }
|
---|
[13195] | 144 | }
|
---|
[8055] | 145 |
|
---|
[13195] | 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;
|
---|
[8055] | 152 | }
|
---|
[13195] | 153 | }
|
---|
[8055] | 154 |
|
---|
[13195] | 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;
|
---|
[8055] | 159 | }
|
---|
[13195] | 160 | }
|
---|
[8055] | 161 |
|
---|
[13195] | 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;
|
---|
[8055] | 166 | }
|
---|
| 167 | }
|
---|
[13195] | 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;
|
---|
[8055] | 177 | }
|
---|
| 178 | }
|
---|
| 179 |
|
---|
[13195] | 180 | public void ClearRuns() {
|
---|
| 181 | if (InvokeRequired) { Invoke((Action)ClearRuns); return; }
|
---|
| 182 | dataGridView.Rows.Clear();
|
---|
| 183 | runs.Clear();
|
---|
| 184 | SetEnabledStateOfControls();
|
---|
[8055] | 185 | }
|
---|
| 186 |
|
---|
| 187 | private void RunCreationClient_Refreshing(object sender, EventArgs e) {
|
---|
[13195] | 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();
|
---|
[8055] | 193 | }
|
---|
| 194 |
|
---|
| 195 | private void RunCreationClient_Refreshed(object sender, EventArgs e) {
|
---|
[13195] | 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();
|
---|
[8055] | 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) => {
|
---|
[9933] | 205 | MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().RemoveOperationProgressFromView(this);
|
---|
[8055] | 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() {
|
---|
[9933] | 211 | var message = "Uploading runs to OKB...";
|
---|
| 212 | IProgress progress = MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().AddOperationProgressToView(this, message);
|
---|
[8055] | 213 | double count = dataGridView.Rows.Count;
|
---|
| 214 | int i = 0;
|
---|
[8181] | 215 | foreach (DataGridViewRow row in dataGridView.Rows) {
|
---|
[13195] | 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());
|
---|
[8181] | 220 | if (selectedAlgorithm == null || selectedProblem == null) {
|
---|
| 221 | throw new ArgumentException("Can't retrieve the algorithm/problem to upload");
|
---|
| 222 | }
|
---|
[8055] | 223 |
|
---|
[8181] | 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;
|
---|
[8055] | 227 | }
|
---|
[9933] | 228 | MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().RemoveOperationProgressFromView(this);
|
---|
[8158] | 229 | ClearRuns();
|
---|
[8055] | 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();
|
---|
[13195] | 235 | selectedAlgorithm = algorithms.FirstOrDefault(x => x.Name == curVal);
|
---|
| 236 | selectedProblem = problems.FirstOrDefault(x => x.Name == curVal);
|
---|
[8055] | 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];
|
---|
[13195] | 249 | row.Cells[OKBAlgorithmColumn.Name].Value = selectedAlgorithm.Name;
|
---|
[8055] | 250 | }
|
---|
| 251 | } else if (selectedProblem != null) {
|
---|
| 252 | for (int i = 0; i < dataGridView.Rows.Count; i++) {
|
---|
| 253 | var row = dataGridView.Rows[i];
|
---|
[13195] | 254 | row.Cells[OKBProblemColumn.Name].Value = selectedProblem.Name;
|
---|
[8055] | 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 | }
|
---|