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