1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2013 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 |
|
---|
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; }
|
---|
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 | private ProgressView progressView;
|
---|
57 | private Progress progress;
|
---|
58 |
|
---|
59 | public OKBExperimentUploadView() {
|
---|
60 | InitializeComponent();
|
---|
61 | progress = new Progress();
|
---|
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);
|
---|
95 | progressView = new ProgressView(this, progress);
|
---|
96 | }
|
---|
97 |
|
---|
98 | protected override void DeregisterContentEvents() {
|
---|
99 | RunCreationClient.Instance.Refreshing -= new EventHandler(RunCreationClient_Refreshing);
|
---|
100 | RunCreationClient.Instance.Refreshed -= new EventHandler(RunCreationClient_Refreshed);
|
---|
101 | if (progressView != null) {
|
---|
102 | progressView.Dispose();
|
---|
103 | progressView = null;
|
---|
104 | }
|
---|
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) {
|
---|
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);
|
---|
120 | } else {
|
---|
121 | CreateUI(runs);
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | private void CreateUI(RunCollection runs) {
|
---|
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);
|
---|
133 |
|
---|
134 | IItem algorithmType;
|
---|
135 | IItem problemType;
|
---|
136 | IItem algorithmName;
|
---|
137 | IItem problemName;
|
---|
138 |
|
---|
139 | DataGridViewComboBoxColumn cmbAlgorithm = dataGridView.Columns[algorithmColumnIndex] as DataGridViewComboBoxColumn;
|
---|
140 | cmbAlgorithm.DataSource = algorithms;
|
---|
141 | cmbAlgorithm.DisplayMember = "Name";
|
---|
142 |
|
---|
143 | DataGridViewComboBoxColumn cmbProblem = dataGridView.Columns[problemColumnIndex] as DataGridViewComboBoxColumn;
|
---|
144 | cmbProblem.DataSource = problems;
|
---|
145 | cmbProblem.DisplayMember = "Name";
|
---|
146 |
|
---|
147 | foreach (IRun run in runs) {
|
---|
148 | int idx = dataGridView.Rows.Add(run.Name);
|
---|
149 | DataGridViewRow curRow = dataGridView.Rows[idx];
|
---|
150 | curRow.Tag = run;
|
---|
151 |
|
---|
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 | }
|
---|
157 | }
|
---|
158 |
|
---|
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 | }
|
---|
164 | }
|
---|
165 |
|
---|
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 | }
|
---|
171 | }
|
---|
172 |
|
---|
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 | }
|
---|
178 | }
|
---|
179 | }
|
---|
180 | }
|
---|
181 | }
|
---|
182 |
|
---|
183 | private void ClearRuns() {
|
---|
184 | if (InvokeRequired) {
|
---|
185 | Invoke(new Action(ClearRuns));
|
---|
186 | } else {
|
---|
187 | dataGridView.Rows.Clear();
|
---|
188 | runs.Clear();
|
---|
189 | }
|
---|
190 | }
|
---|
191 |
|
---|
192 | private void RunCreationClient_Refreshing(object sender, EventArgs e) {
|
---|
193 | if (InvokeRequired) {
|
---|
194 | Invoke(new EventHandler(RunCreationClient_Refreshing), sender, e);
|
---|
195 | } else {
|
---|
196 | progress.Status = "Refreshing algorithms and problems...";
|
---|
197 | progress.ProgressState = ProgressState.Started;
|
---|
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 {
|
---|
205 | progress.Finish();
|
---|
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) => {
|
---|
213 | progress.Finish();
|
---|
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() {
|
---|
219 | progress.Status = "Uploading runs to OKB...";
|
---|
220 | progress.ProgressValue = 0;
|
---|
221 | progress.ProgressState = ProgressState.Started;
|
---|
222 | double count = dataGridView.Rows.Count;
|
---|
223 | int i = 0;
|
---|
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 | }
|
---|
230 |
|
---|
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;
|
---|
235 | }
|
---|
236 | progress.Finish();
|
---|
237 | ClearRuns();
|
---|
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 | }
|
---|