Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKB (trunk integration)/HeuristicLab.Clients.OKB/3.3/Query/Views/QueryView.cs @ 7331

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

#1174

  • switched to new build style for branches and improved config merging
  • updated year in license headers and assembly infos
  • updated version in plugin files and assembly infos
File size: 8.3 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.IO;
24using System.Linq;
25using System.Threading;
26using System.Threading.Tasks;
27using System.Windows.Forms;
28using HeuristicLab.Core;
29using HeuristicLab.MainForm;
30using HeuristicLab.MainForm.WindowsForms;
31using HeuristicLab.Optimization;
32using HeuristicLab.Persistence.Default.Xml;
33using HeuristicLab.PluginInfrastructure;
34
35namespace HeuristicLab.Clients.OKB.Query {
36  [View("OKB Query")]
37  [Content(typeof(QueryClient), true)]
38  public sealed partial class QueryView : AsynchronousContentView {
39    private CancellationTokenSource cancellationTokenSource;
40    private CombinedFilterView combinedFilterView;
41
42    public new QueryClient Content {
43      get { return (QueryClient)base.Content; }
44      set { base.Content = value; }
45    }
46
47    public QueryView() {
48      InitializeComponent();
49    }
50
51    protected override void DeregisterContentEvents() {
52      Content.Refreshing -= new EventHandler(Content_Refreshing);
53      Content.Refreshed -= new EventHandler(Content_Refreshed);
54      base.DeregisterContentEvents();
55    }
56
57    protected override void RegisterContentEvents() {
58      base.RegisterContentEvents();
59      Content.Refreshing += new EventHandler(Content_Refreshing);
60      Content.Refreshed += new EventHandler(Content_Refreshed);
61    }
62
63    protected override void OnContentChanged() {
64      base.OnContentChanged();
65      CreateFilterView();
66      runCollectionView.Content = null;
67    }
68
69    protected override void SetEnabledStateOfControls() {
70      base.SetEnabledStateOfControls();
71      resultsGroupBox.Enabled = combinedFilterView != null;
72    }
73
74    #region Load Results
75    private void LoadResultsAsync(int batchSize) {
76      bool includeBinaryValues = includeBinaryValuesCheckBox.Checked;
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          runs.AddRange(QueryClient.Instance.GetRuns(ids.Take(batchSize), includeBinaryValues).Select(x => ConvertToOptimizationRun(x)));
102          ids = ids.Skip(batchSize);
103          Invoke(new Action(() => {
104            resultsInfoLabel.Text = "Loaded " + runs.Count + " of " + idsCount.ToString() + " Results ...";
105            resultsProgressBar.PerformStep();
106          }));
107        }
108      }, cancellationToken);
109      task.ContinueWith(t => {
110        Invoke(new Action(() => {
111          cancellationTokenSource.Dispose();
112          cancellationTokenSource = null;
113          resultsInfoPanel.Visible = false;
114          splitContainer.Enabled = true;
115          this.Cursor = Cursors.Default;
116          SetEnabledStateOfControls();
117          try {
118            t.Wait();
119          }
120          catch (AggregateException ex) {
121            try {
122              ex.Flatten().Handle(x => x is OperationCanceledException);
123            }
124            catch (AggregateException remaining) {
125              if (remaining.InnerExceptions.Count == 1) ErrorHandling.ShowErrorDialog(this, "Refresh results failed.", remaining.InnerExceptions[0]);
126              else ErrorHandling.ShowErrorDialog(this, "Refresh results failed.", remaining);
127            }
128          }
129        }));
130      });
131    }
132    #endregion
133
134    private void Content_Refreshing(object sender, EventArgs e) {
135      if (InvokeRequired) {
136        Invoke(new EventHandler(Content_Refreshing), sender, e);
137      } else {
138        Cursor = Cursors.AppStarting;
139        filtersInfoPanel.Visible = true;
140        splitContainer.Enabled = false;
141      }
142    }
143    private void Content_Refreshed(object sender, EventArgs e) {
144      if (InvokeRequired) {
145        Invoke(new EventHandler(Content_Refreshed), sender, e);
146      } else {
147        CreateFilterView();
148        filtersInfoPanel.Visible = false;
149        splitContainer.Enabled = true;
150        Cursor = Cursors.Default;
151        SetEnabledStateOfControls();
152      }
153    }
154
155    private void refreshFiltersButton_Click(object sender, EventArgs e) {
156      Content.RefreshAsync(new Action<Exception>((Exception ex) => ErrorHandling.ShowErrorDialog(this, "Refresh failed.", ex)));
157    }
158
159    private void refreshResultsButton_Click(object sender, EventArgs e) {
160      LoadResultsAsync(10);
161    }
162
163    private void abortButton_Click(object sender, EventArgs e) {
164      if (cancellationTokenSource != null) cancellationTokenSource.Cancel();
165      abortButton.Enabled = false;
166    }
167
168    private void CreateFilterView() {
169      combinedFilterView = null;
170      filterPanel.Controls.Clear();
171      if ((Content != null) && (Content.Filters != null)) {
172        CombinedFilter filter = Content.Filters.OfType<CombinedFilter>().Where(x => x.Operation == BooleanOperation.And).FirstOrDefault();
173        if (filter != null) {
174          combinedFilterView = (CombinedFilterView)MainFormManager.CreateView(typeof(CombinedFilterView));
175          combinedFilterView.Content = (CombinedFilter)filter.Clone();
176          Control control = (Control)combinedFilterView;
177          control.Dock = DockStyle.Fill;
178          filterPanel.Controls.Add(control);
179        }
180      }
181    }
182
183    private Optimization.IRun ConvertToOptimizationRun(Run run) {
184      Optimization.Run optRun = new Optimization.Run();
185      foreach (Value value in run.ParameterValues)
186        optRun.Parameters.Add(value.Name, ConvertToItem(value));
187      foreach (Value value in run.ResultValues)
188        optRun.Results.Add(value.Name, ConvertToItem(value));
189      return optRun;
190    }
191
192    private IItem ConvertToItem(Value value) {
193      if (value is BinaryValue) {
194        IItem item = null;
195        BinaryValue binaryValue = (BinaryValue)value;
196        if (binaryValue.Value != null) {
197          using (MemoryStream stream = new MemoryStream(binaryValue.Value)) {
198            try {
199              item = XmlParser.Deserialize<IItem>(stream);
200            }
201            catch (Exception) { }
202            stream.Close();
203          }
204        }
205        return item != null ? item : new Data.StringValue(value.DataType.Name);
206      } else if (value is BoolValue) {
207        return new Data.BoolValue(((BoolValue)value).Value);
208      } else if (value is FloatValue) {
209        return new Data.DoubleValue(((FloatValue)value).Value);
210      } else if (value is DoubleValue) {
211        return new Data.DoubleValue(((DoubleValue)value).Value);
212      } else if (value is IntValue) {
213        return new Data.IntValue((int)((IntValue)value).Value);
214      } else if (value is LongValue) {
215        return new Data.IntValue((int)((LongValue)value).Value);
216      } else if (value is StringValue) {
217        return new Data.StringValue(((StringValue)value).Value);
218      }
219      return null;
220    }
221  }
222}
Note: See TracBrowser for help on using the repository browser.