Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2434_crossvalidation/HeuristicLab.Clients.OKB.Views/3.3/Query/Views/QueryView.cs

Last change on this file was 14029, checked in by gkronber, 8 years ago

#2434: merged trunk changes r12934:14026 from trunk to branch

File size: 7.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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;
24using System.Collections.Generic;
25using System.Linq;
26using System.Threading;
27using System.Threading.Tasks;
28using System.Windows.Forms;
29using HeuristicLab.MainForm;
30using HeuristicLab.MainForm.WindowsForms;
31using HeuristicLab.Optimization;
32using HeuristicLab.PluginInfrastructure;
33
34namespace HeuristicLab.Clients.OKB.Query {
35  [View("OKB Query")]
36  [Content(typeof(QueryClient), true)]
37  public sealed partial class QueryView : AsynchronousContentView {
38    private CancellationTokenSource cancellationTokenSource;
39    private CombinedFilterView combinedFilterView;
40
41    public new QueryClient Content {
42      get { return (QueryClient)base.Content; }
43      set { base.Content = value; }
44    }
45
46    public QueryView() {
47      InitializeComponent();
48    }
49
50    protected override void DeregisterContentEvents() {
51      Content.Refreshing -= new EventHandler(Content_Refreshing);
52      Content.Refreshed -= new EventHandler(Content_Refreshed);
53      base.DeregisterContentEvents();
54    }
55
56    protected override void RegisterContentEvents() {
57      base.RegisterContentEvents();
58      Content.Refreshing += new EventHandler(Content_Refreshing);
59      Content.Refreshed += new EventHandler(Content_Refreshed);
60    }
61
62    protected override void OnContentChanged() {
63      base.OnContentChanged();
64      CreateFilterView();
65      runCollectionView.Content = null;
66    }
67
68    protected override void SetEnabledStateOfControls() {
69      base.SetEnabledStateOfControls();
70      resultsGroupBox.Enabled = combinedFilterView != null;
71    }
72
73    #region Load Results
74    private void LoadResultsAsync(int batchSize) {
75      bool includeBinaryValues = includeBinaryValuesCheckBox.Checked;
76      IEnumerable<ValueName> valueNames = constraintsCheckedListBox.CheckedItems.Cast<ValueName>();
77
78      Cursor = Cursors.AppStarting;
79      resultsInfoLabel.Text = "Loading Results ...";
80      resultsProgressBar.Value = 0;
81      resultsProgressBar.Step = batchSize;
82      abortButton.Enabled = true;
83      resultsInfoPanel.Visible = true;
84      splitContainer.Enabled = false;
85      cancellationTokenSource = new CancellationTokenSource();
86      CancellationToken cancellationToken = cancellationTokenSource.Token;
87
88      Task task = Task.Factory.StartNew(() => {
89        var ids = QueryClient.Instance.GetRunIds(combinedFilterView.Content);
90        int idsCount = ids.Count();
91
92        Invoke(new Action(() => {
93          resultsInfoLabel.Text = "Loaded 0 of " + idsCount.ToString() + " Results ...";
94          resultsProgressBar.Maximum = idsCount;
95        }));
96
97        RunCollection runs = new RunCollection();
98        runCollectionView.Content = runs;
99        while (ids.Count() > 0) {
100          cancellationToken.ThrowIfCancellationRequested();
101          if (AllValueNamesChecked()) {
102            runs.AddRange(QueryClient.Instance.GetRuns(ids.Take(batchSize), includeBinaryValues).Select(x => QueryClient.Instance.ConvertToOptimizationRun(x)));
103          } else {
104            runs.AddRange(QueryClient.Instance.GetRunsWithValues(ids.Take(batchSize), includeBinaryValues, valueNames).Select(x => QueryClient.Instance.ConvertToOptimizationRun(x)));
105          }
106          ids = ids.Skip(batchSize);
107          Invoke(new Action(() => {
108            resultsInfoLabel.Text = "Loaded " + runs.Count + " of " + idsCount.ToString() + " Results ...";
109            resultsProgressBar.PerformStep();
110          }));
111        }
112      }, cancellationToken);
113      task.ContinueWith(t => {
114        Invoke(new Action(() => {
115          cancellationTokenSource.Dispose();
116          cancellationTokenSource = null;
117          resultsInfoPanel.Visible = false;
118          splitContainer.Enabled = true;
119          this.Cursor = Cursors.Default;
120          SetEnabledStateOfControls();
121          try {
122            t.Wait();
123          } catch (AggregateException ex) {
124            try {
125              ex.Flatten().Handle(x => x is OperationCanceledException);
126            } catch (AggregateException remaining) {
127              if (remaining.InnerExceptions.Count == 1) ErrorHandling.ShowErrorDialog(this, "Refresh results failed.", remaining.InnerExceptions[0]);
128              else ErrorHandling.ShowErrorDialog(this, "Refresh results failed.", remaining);
129            }
130          }
131        }));
132      });
133    }
134    #endregion
135
136    private void Content_Refreshing(object sender, EventArgs e) {
137      if (InvokeRequired) {
138        Invoke(new EventHandler(Content_Refreshing), sender, e);
139      } else {
140        Cursor = Cursors.AppStarting;
141        filtersInfoPanel.Visible = true;
142        splitContainer.Enabled = false;
143      }
144    }
145    private void Content_Refreshed(object sender, EventArgs e) {
146      if (InvokeRequired) {
147        Invoke(new EventHandler(Content_Refreshed), sender, e);
148      } else {
149        CreateFilterView();
150        CreateConstraintsView();
151        filtersInfoPanel.Visible = false;
152        splitContainer.Enabled = true;
153        Cursor = Cursors.Default;
154        SetEnabledStateOfControls();
155      }
156    }
157
158    private void refreshFiltersButton_Click(object sender, EventArgs e) {
159      Content.RefreshAsync(new Action<Exception>((Exception ex) => ErrorHandling.ShowErrorDialog(this, "Refresh failed.", ex)));
160    }
161
162    private void refreshResultsButton_Click(object sender, EventArgs e) {
163      LoadResultsAsync(10);
164    }
165
166    private void abortButton_Click(object sender, EventArgs e) {
167      if (cancellationTokenSource != null) cancellationTokenSource.Cancel();
168      abortButton.Enabled = false;
169    }
170
171    private void CreateFilterView() {
172      combinedFilterView = null;
173      filterPanel.Controls.Clear();
174      if ((Content != null) && (Content.Filters != null)) {
175        CombinedFilter filter = Content.Filters.OfType<CombinedFilter>().Where(x => x.Operation == BooleanOperation.And).FirstOrDefault();
176        if (filter != null) {
177          combinedFilterView = (CombinedFilterView)MainFormManager.CreateView(typeof(CombinedFilterView));
178          combinedFilterView.Content = (CombinedFilter)filter.Clone();
179          Control control = (Control)combinedFilterView;
180          control.Dock = DockStyle.Fill;
181          filterPanel.Controls.Add(control);
182        }
183      }
184    }
185
186    private void CreateConstraintsView() {
187      constraintsCheckedListBox.Items.Clear();
188      constraintsCheckedListBox.Items.AddRange(QueryClient.Instance.ValueNames.ToArray());
189      SetCheckedState(true);
190    }
191
192    private void SetCheckedState(bool val) {
193      for (int i = 0; i < constraintsCheckedListBox.Items.Count; i++) {
194        constraintsCheckedListBox.SetItemChecked(i, val);
195      }
196    }
197
198    private bool AllValueNamesChecked() {
199      return constraintsCheckedListBox.Items.Count == constraintsCheckedListBox.CheckedItems.Count;
200    }
201
202    private void selectAllButton_Click(object sender, EventArgs e) {
203      SetCheckedState(true);
204    }
205
206    private void deselectAllButton_Click(object sender, EventArgs e) {
207      SetCheckedState(false);
208    }
209  }
210}
Note: See TracBrowser for help on using the repository browser.