Free cookie consent management tool by TermsFeed Policy Generator

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

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

Worked on OKB (#1174)

File size: 7.8 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;
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    private void Content_Refreshing(object sender, EventArgs e) {
74      if (InvokeRequired) {
75        Invoke(new EventHandler(Content_Refreshing), sender, e);
76      } else {
77        Cursor = Cursors.AppStarting;
78        filtersInfoPanel.Visible = true;
79        splitContainer.Enabled = false;
80      }
81    }
82    private void Content_Refreshed(object sender, EventArgs e) {
83      if (InvokeRequired) {
84        Invoke(new EventHandler(Content_Refreshed), sender, e);
85      } else {
86        CreateFilterView();
87        filtersInfoPanel.Visible = false;
88        splitContainer.Enabled = true;
89        Cursor = Cursors.Default;
90        SetEnabledStateOfControls();
91      }
92    }
93
94    #region Load Results
95    private void LoadResultsAsync(int batchSize) {
96      bool deserialize = deserializeBlobsCheckBox.Checked;
97
98      Cursor = Cursors.AppStarting;
99      resultsInfoLabel.Text = "Loading Results ...";
100      resultsProgressBar.Value = 0;
101      resultsProgressBar.Step = batchSize;
102      abortButton.Enabled = true;
103      resultsInfoPanel.Visible = true;
104      splitContainer.Enabled = false;
105      cancellationTokenSource = new CancellationTokenSource();
106      CancellationToken cancellationToken = cancellationTokenSource.Token;
107
108      Task task = Task.Factory.StartNew(() => {
109        var ids = QueryClient.Instance.GetRunIds(combinedFilterView.Content);
110        int idsCount = ids.Count();
111
112        Invoke(new Action(() => {
113          resultsInfoLabel.Text = "Loaded 0 of " + idsCount.ToString() + " Results ...";
114          resultsProgressBar.Maximum = idsCount;
115        }));
116
117        RunCollection runs = new RunCollection();
118        runs.CollectionReset += new Collections.CollectionItemsChangedEventHandler<IRun>(runs_CollectionReset);
119        runCollectionView.Content = runs;
120        while (ids.Count() > 0) {
121          cancellationToken.ThrowIfCancellationRequested();
122          runs.AddRange(QueryClient.Instance.GetRuns(ids.Take(batchSize), true).Select(x => ConvertToOptimizationRun(x, deserialize)));
123          ids = ids.Skip(batchSize);
124          Invoke(new Action(() => {
125            resultsInfoLabel.Text = "Loaded " + runs.Count + " of " + idsCount.ToString() + " Results ...";
126            resultsProgressBar.PerformStep();
127          }));
128        }
129      }, cancellationToken);
130      task.ContinueWith(t => {
131        Invoke(new Action(() => {
132          cancellationTokenSource.Dispose();
133          cancellationTokenSource = null;
134          resultsInfoPanel.Visible = false;
135          splitContainer.Enabled = true;
136          this.Cursor = Cursors.Default;
137          SetEnabledStateOfControls();
138        }));
139      });
140    }
141
142    void runs_CollectionReset(object sender, Collections.CollectionItemsChangedEventArgs<IRun> e) {
143    }
144    #endregion
145
146    private void refreshResultsButton_Click(object sender, EventArgs e) {
147      LoadResultsAsync(10);
148    }
149
150    private void abortButton_Click(object sender, EventArgs e) {
151      if (cancellationTokenSource != null) cancellationTokenSource.Cancel();
152      abortButton.Enabled = false;
153    }
154
155    private void CreateFilterView() {
156      combinedFilterView = null;
157      filterPanel.Controls.Clear();
158      if ((Content != null) && (Content.Filters != null)) {
159        CombinedFilter filter = Content.Filters.OfType<CombinedFilter>().Where(x => x.Operation == BooleanOperation.And).FirstOrDefault();
160        if (filter != null) {
161          combinedFilterView = (CombinedFilterView)MainFormManager.CreateView(typeof(CombinedFilterView));
162          combinedFilterView.Content = (CombinedFilter)filter.Clone();
163          Control control = (Control)combinedFilterView;
164          control.Dock = DockStyle.Fill;
165          filterPanel.Controls.Add(control);
166        }
167      }
168    }
169
170    private Optimization.IRun ConvertToOptimizationRun(Run run, bool deserialize) {
171      Optimization.Run optRun = new Optimization.Run();
172      foreach (Value value in run.ParameterValues)
173        optRun.Parameters.Add(value.Name, ConvertToItem(value, deserialize));
174      foreach (Value value in run.ResultValues)
175        optRun.Results.Add(value.Name, ConvertToItem(value, deserialize));
176      return optRun;
177    }
178
179    private IItem ConvertToItem(Value value, bool deserialize) {
180      if (value is BinaryValue) {
181        if (deserialize) {
182          IItem item = null;
183          using (MemoryStream stream = new MemoryStream(((BinaryValue)value).Value)) {
184            try {
185              item = XmlParser.Deserialize<IItem>(stream);
186            }
187            catch (Exception) { }
188            stream.Close();
189          }
190          return item != null ? item : new Data.StringValue(value.DataType.Name);
191        } else {
192          return new Data.StringValue(value.DataType.Name);
193        }
194      } else if (value is BoolValue) {
195        return new Data.BoolValue(((BoolValue)value).Value);
196      } else if (value is FloatValue) {
197        return new Data.DoubleValue(((FloatValue)value).Value);
198      } else if (value is DoubleValue) {
199        return new Data.DoubleValue(((FloatValue)value).Value);
200      } else if (value is IntValue) {
201        return new Data.IntValue((int)((IntValue)value).Value);
202      } else if (value is LongValue) {
203        return new Data.IntValue((int)((IntValue)value).Value);
204      } else if (value is StringValue) {
205        return new Data.StringValue(((StringValue)value).Value);
206      }
207      return null;
208    }
209  }
210}
Note: See TracBrowser for help on using the repository browser.