Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKB (trunk integration)/HeuristicLab.Clients.OKB/3.3/RunCreation/Views/OKBExperimentUploadView.cs @ 7584

Last change on this file since 7584 was 7584, checked in by ascheibe, 12 years ago

#1174 added displaying of progress when uploading runs

File size: 6.1 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Threading.Tasks;
26using System.Windows.Forms;
27using HeuristicLab.Clients.Access;
28using HeuristicLab.Core.Views;
29using HeuristicLab.MainForm;
30using HeuristicLab.MainForm.WindowsForms;
31using HeuristicLab.Optimization;
32
33namespace HeuristicLab.Clients.OKB.RunCreation {
34  [View("OKBExperimentUpload View")]
35  [Content(typeof(Experiment), false)]
36  public partial class OKBExperimentUploadView : ItemView {
37    public new Experiment Content {
38      get { return (Experiment)base.Content; }
39      set { base.Content = value; }
40    }
41
42    private ProgressView progressView;
43    List<OKBRunConfigSelectionView> runConfigViews = new List<OKBRunConfigSelectionView>();
44    private List<Problem> problems = new List<Problem>();
45    private List<Algorithm> algorithms = new List<Algorithm>();
46
47    public OKBExperimentUploadView() {
48      InitializeComponent();
49      tableLayoutPanel.RowCount = 0;
50    }
51
52    protected override void OnContentChanged() {
53      base.OnContentChanged();
54      if (Content == null) {
55        ClearRuns();
56      } else {
57        DisplayRuns();
58      }
59    }
60
61    protected override void RegisterContentEvents() {
62      base.RegisterContentEvents();
63      RunCreationClient.Instance.Refreshing += new EventHandler(RunCreationClient_Refreshing);
64      RunCreationClient.Instance.Refreshed += new EventHandler(RunCreationClient_Refreshed);
65    }
66
67    protected override void DeregisterContentEvents() {
68      RunCreationClient.Instance.Refreshing -= new EventHandler(RunCreationClient_Refreshing);
69      RunCreationClient.Instance.Refreshed -= new EventHandler(RunCreationClient_Refreshed);
70      base.DeregisterContentEvents();
71    }
72
73    private void DisplayError(Exception ex) {
74      PluginInfrastructure.ErrorHandling.ShowErrorDialog("An error occured while retrieving algorithm and problem information from the OKB.", ex);
75    }
76
77    private void DisplayRuns() {
78      if (RunCreationClient.Instance.Algorithms == null || RunCreationClient.Instance.Algorithms.Count() == 0) {
79        RunCreationClient.Instance.RefreshAsync(DisplayError);
80      } else {
81        CreateUI();
82      }
83    }
84
85    private void CreateUI() {
86      problems.AddRange(RunCreationClient.Instance.Problems);
87      algorithms.AddRange(RunCreationClient.Instance.Algorithms);
88
89      foreach (var run in Content.Runs) {
90        OKBRunConfigSelectionView orcsv = new OKBRunConfigSelectionView(run, algorithms, problems);
91        runConfigViews.Add(orcsv);
92        tableLayoutPanel.Controls.Add(orcsv);
93      }
94    }
95
96    private void ClearRuns() {
97      foreach (var runConfigView in runConfigViews) {
98        runConfigView.Hide();
99        tableLayoutPanel.Controls.Remove(runConfigView);
100        runConfigView.Dispose();
101      }
102      runConfigViews.Clear();
103    }
104
105    private void RunCreationClient_Refreshing(object sender, EventArgs e) {
106      if (InvokeRequired) {
107        Invoke(new EventHandler(RunCreationClient_Refreshing), sender, e);
108      } else {
109        IProgress prog = new Progress();
110        prog.Status = "Refreshing algorithms and problems...";
111        SetProgressView(prog);
112      }
113    }
114
115    private void RunCreationClient_Refreshed(object sender, EventArgs e) {
116      if (InvokeRequired) {
117        Invoke(new EventHandler(RunCreationClient_Refreshed), sender, e);
118      } else {
119        FinishProgressView();
120
121        CreateUI();
122        SetEnabledStateOfControls();
123      }
124    }
125
126    private void btnUpload_Click(object sender, EventArgs e) {
127      var task = System.Threading.Tasks.Task.Factory.StartNew(UploadAsync);
128      task.ContinueWith((t) => {
129        FinishProgressView();
130        PluginInfrastructure.ErrorHandling.ShowErrorDialog("An exception occured while uploading the runs to the OKB.", t.Exception);
131      }, TaskContinuationOptions.OnlyOnFaulted);
132    }
133
134    private void UploadAsync() {
135      IProgress prog = new Progress();
136      prog.Status = "Uploading runs to OKB...";
137      prog.ProgressValue = 0;
138      double count = runConfigViews.Count(x => x.UploadToOKB());
139      int i = 0;
140
141      SetProgressView(prog);
142      foreach (var runConfigView in runConfigViews) {
143        if (runConfigView.UploadToOKB()) {
144          OKBRun run = new OKBRun(runConfigView.GetSelectedAlgorithm().Id, runConfigView.GetSelectedProblem().Id, runConfigView.ExperimentRun, UserInformation.Instance.User.Id);
145          run.Store();
146          i++;
147          prog.ProgressValue = ((double)i) / count;
148        }
149      }
150      FinishProgressView();
151    }
152
153    private void SetProgressView(IProgress progress) {
154      if (InvokeRequired) {
155        Invoke(new Action<IProgress>(SetProgressView), progress);
156      } else {
157        if (progressView == null) {
158          progressView = new ProgressView(this, progress);
159        } else {
160          progressView.Progress = progress;
161        }
162      }
163    }
164
165    private void FinishProgressView() {
166      if (InvokeRequired) {
167        Invoke(new Action(FinishProgressView));
168      } else {
169        if (progressView != null) {
170          progressView.Finish();
171          progressView = null;
172          SetEnabledStateOfControls();
173        }
174      }
175    }
176  }
177}
Note: See TracBrowser for help on using the repository browser.