Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Clients.OKB.Views/3.3/Query/Views/QueryView.cs @ 17945

Last change on this file since 17945 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

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