Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5674 was 5674, checked in by swagner, 13 years ago

Worked on OKB (#1174)

File size: 8.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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      bool deserialize = deserializeBlobsCheckBox.Checked;
78
79      Cursor = Cursors.AppStarting;
80      resultsInfoLabel.Text = "Loading Results ...";
81      resultsProgressBar.Value = 0;
82      resultsProgressBar.Step = batchSize;
83      abortButton.Enabled = true;
84      resultsInfoPanel.Visible = true;
85      splitContainer.Enabled = false;
86      cancellationTokenSource = new CancellationTokenSource();
87      CancellationToken cancellationToken = cancellationTokenSource.Token;
88
89      Task task = Task.Factory.StartNew(() => {
90        var ids = QueryClient.Instance.GetRunIds(combinedFilterView.Content);
91        int idsCount = ids.Count();
92
93        Invoke(new Action(() => {
94          resultsInfoLabel.Text = "Loaded 0 of " + idsCount.ToString() + " Results ...";
95          resultsProgressBar.Maximum = idsCount;
96        }));
97
98        RunCollection runs = new RunCollection();
99        runCollectionView.Content = runs;
100        while (ids.Count() > 0) {
101          cancellationToken.ThrowIfCancellationRequested();
102          runs.AddRange(QueryClient.Instance.GetRuns(ids.Take(batchSize), includeBinaryValues).Select(x => ConvertToOptimizationRun(x, deserialize)));
103          ids = ids.Skip(batchSize);
104          Invoke(new Action(() => {
105            resultsInfoLabel.Text = "Loaded " + runs.Count + " of " + idsCount.ToString() + " Results ...";
106            resultsProgressBar.PerformStep();
107          }));
108        }
109      }, cancellationToken);
110      task.ContinueWith(t => {
111        Invoke(new Action(() => {
112          cancellationTokenSource.Dispose();
113          cancellationTokenSource = null;
114          resultsInfoPanel.Visible = false;
115          splitContainer.Enabled = true;
116          this.Cursor = Cursors.Default;
117          SetEnabledStateOfControls();
118          try {
119            t.Wait();
120          }
121          catch (AggregateException ex) {
122            try {
123              ex.Flatten().Handle(x => x is OperationCanceledException);
124            }
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        filtersInfoPanel.Visible = false;
150        splitContainer.Enabled = true;
151        Cursor = Cursors.Default;
152        SetEnabledStateOfControls();
153      }
154    }
155
156    private void refreshFiltersButton_Click(object sender, EventArgs e) {
157      Content.RefreshAsync(new Action<Exception>((Exception ex) => ErrorHandling.ShowErrorDialog(this, "Refresh failed.", ex)));
158    }
159
160    private void refreshResultsButton_Click(object sender, EventArgs e) {
161      LoadResultsAsync(10);
162    }
163
164    private void abortButton_Click(object sender, EventArgs e) {
165      if (cancellationTokenSource != null) cancellationTokenSource.Cancel();
166      abortButton.Enabled = false;
167    }
168
169    private void CreateFilterView() {
170      combinedFilterView = null;
171      filterPanel.Controls.Clear();
172      if ((Content != null) && (Content.Filters != null)) {
173        CombinedFilter filter = Content.Filters.OfType<CombinedFilter>().Where(x => x.Operation == BooleanOperation.And).FirstOrDefault();
174        if (filter != null) {
175          combinedFilterView = (CombinedFilterView)MainFormManager.CreateView(typeof(CombinedFilterView));
176          combinedFilterView.Content = (CombinedFilter)filter.Clone();
177          Control control = (Control)combinedFilterView;
178          control.Dock = DockStyle.Fill;
179          filterPanel.Controls.Add(control);
180        }
181      }
182    }
183
184    private Optimization.IRun ConvertToOptimizationRun(Run run, bool deserialize) {
185      Optimization.Run optRun = new Optimization.Run();
186      foreach (Value value in run.ParameterValues)
187        optRun.Parameters.Add(value.Name, ConvertToItem(value, deserialize));
188      foreach (Value value in run.ResultValues)
189        optRun.Results.Add(value.Name, ConvertToItem(value, deserialize));
190      return optRun;
191    }
192
193    private IItem ConvertToItem(Value value, bool deserialize) {
194      if (value is BinaryValue) {
195        if (deserialize) {
196          IItem item = null;
197          using (MemoryStream stream = new MemoryStream(((BinaryValue)value).Value)) {
198            try {
199              item = XmlParser.Deserialize<IItem>(stream);
200            }
201            catch (Exception) { }
202            stream.Close();
203          }
204          return item != null ? item : new Data.StringValue(value.DataType.Name);
205        } else {
206          return new Data.StringValue(value.DataType.Name);
207        }
208      } else if (value is BoolValue) {
209        return new Data.BoolValue(((BoolValue)value).Value);
210      } else if (value is FloatValue) {
211        return new Data.DoubleValue(((FloatValue)value).Value);
212      } else if (value is DoubleValue) {
213        return new Data.DoubleValue(((DoubleValue)value).Value);
214      } else if (value is IntValue) {
215        return new Data.IntValue((int)((IntValue)value).Value);
216      } else if (value is LongValue) {
217        return new Data.IntValue((int)((LongValue)value).Value);
218      } else if (value is StringValue) {
219        return new Data.StringValue(((StringValue)value).Value);
220      }
221      return null;
222    }
223  }
224}
Note: See TracBrowser for help on using the repository browser.