Free cookie consent management tool by TermsFeed Policy Generator

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

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

Worked on OKB (#1174)

File size: 7.0 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.Data;
30using HeuristicLab.MainForm;
31using HeuristicLab.MainForm.WindowsForms;
32using HeuristicLab.Optimization;
33using HeuristicLab.Persistence.Default.Xml;
34
35namespace HeuristicLab.Clients.OKB {
36  [View("OKB Query")]
37  [Content(typeof(OKBClient), false)]
38  public sealed partial class QueryView : HeuristicLab.MainForm.WindowsForms.View {
39    private CancellationTokenSource cancellationTokenSource;
40    private CombinedFilterView combinedFilterView;
41
42    public QueryView() {
43      InitializeComponent();
44    }
45
46    protected override void OnInitialized(EventArgs e) {
47      base.OnInitialized(e);
48      LoadFiltersAsync();
49    }
50
51
52    protected override void SetEnabledStateOfControls() {
53      base.SetEnabledStateOfControls();
54      resultsGroupBox.Enabled = combinedFilterView != null;
55    }
56
57    #region Load Filters
58    private void LoadFiltersAsync() {
59      Cursor = Cursors.AppStarting;
60      filtersInfoPanel.Visible = true;
61      splitContainer.Enabled = false;
62
63      Task<CombinedFilter> task = Task.Factory.StartNew<CombinedFilter>(() => {
64        return OKBClient.Instance.GetFilters().OfType<CombinedFilter>().Where(x => x.Operation == BooleanOperation.And).FirstOrDefault();
65      });
66      task.ContinueWith(t => {
67        CombinedFilter filter = t.Result;
68        Invoke(new Action(() => {
69          if (filter != null) {
70            combinedFilterView = (CombinedFilterView)MainFormManager.CreateView(typeof(CombinedFilterView));
71            combinedFilterView.Content = (CombinedFilter)filter.Clone();
72            Control control = (Control)combinedFilterView;
73            control.Dock = DockStyle.Fill;
74            filterPanel.Controls.Add(control);
75          }
76          filtersInfoPanel.Visible = false;
77          splitContainer.Enabled = true;
78          this.Cursor = Cursors.Default;
79          SetEnabledStateOfControls();
80        }));
81      });
82    }
83    #endregion
84
85    #region Load Results
86    private void LoadResultsAsync(int batchSize) {
87      bool deserialize = deserializeBlobsCheckBox.Checked;
88
89      Cursor = Cursors.AppStarting;
90      resultsInfoLabel.Text = "Loading Results ...";
91      resultsProgressBar.Value = 0;
92      resultsProgressBar.Step = batchSize;
93      abortButton.Enabled = true;
94      resultsInfoPanel.Visible = true;
95      splitContainer.Enabled = false;
96      cancellationTokenSource = new CancellationTokenSource();
97      CancellationToken cancellationToken = cancellationTokenSource.Token;
98
99      Task task = Task.Factory.StartNew(() => {
100        var ids = OKBClient.Instance.GetQueryResultIds(combinedFilterView.Content);
101        int idsCount = ids.Count();
102
103        Invoke(new Action(() => {
104          resultsInfoLabel.Text = "Loaded 0 of " + idsCount.ToString() + " Results ...";
105          resultsProgressBar.Maximum = idsCount;
106        }));
107
108        RunCollection runs = new RunCollection();
109        runs.CollectionReset += new Collections.CollectionItemsChangedEventHandler<IRun>(runs_CollectionReset);
110        runCollectionView.Content = runs;
111        while (ids.Count() > 0) {
112          cancellationToken.ThrowIfCancellationRequested();
113          runs.AddRange(OKBClient.Instance.GetQueryResults(ids.Take(batchSize)).Select(x => ConvertToOptimizationRun(x, deserialize)));
114          ids = ids.Skip(batchSize);
115          Invoke(new Action(() => {
116            resultsInfoLabel.Text = "Loaded " + runs.Count + " of " + idsCount.ToString() + " Results ...";
117            resultsProgressBar.PerformStep();
118          }));
119        }
120      }, cancellationToken);
121      task.ContinueWith(t => {
122        Invoke(new Action(() => {
123          cancellationTokenSource.Dispose();
124          cancellationTokenSource = null;
125          resultsInfoPanel.Visible = false;
126          splitContainer.Enabled = true;
127          this.Cursor = Cursors.Default;
128          SetEnabledStateOfControls();
129        }));
130      });
131    }
132
133    void runs_CollectionReset(object sender, Collections.CollectionItemsChangedEventArgs<IRun> e) {
134    }
135    #endregion
136
137    private void refreshResultsButton_Click(object sender, EventArgs e) {
138      LoadResultsAsync(10);
139    }
140
141    private void abortButton_Click(object sender, EventArgs e) {
142      if (cancellationTokenSource != null) cancellationTokenSource.Cancel();
143      abortButton.Enabled = false;
144    }
145
146    private Optimization.IRun ConvertToOptimizationRun(QueryResult queryResult, bool deserialize) {
147      Optimization.Run run = new Optimization.Run();
148      foreach (QueryValue value in queryResult.AlgorithmParameters)
149        run.Parameters.Add(value.Name, ConvertToItem(value, deserialize));
150      foreach (QueryValue value in queryResult.ProblemParameters)
151        run.Parameters.Add(value.Name, ConvertToItem(value, deserialize));
152      foreach (QueryValue value in queryResult.Results)
153        run.Results.Add(value.Name, ConvertToItem(value, deserialize));
154      return run;
155    }
156
157    private IItem ConvertToItem(QueryValue value, bool deserialize) {
158      if (value is QueryBlobValue) {
159        if (deserialize) {
160          IItem item = null;
161          using (MemoryStream stream = new MemoryStream(((QueryBlobValue)value).Value)) {
162            try {
163              item = XmlParser.Deserialize<IItem>(stream);
164            }
165            catch (Exception) { }
166            stream.Close();
167          }
168          return item != null ? item : new StringValue(((QueryBlobValue)value).DataTypeName);
169        } else {
170          return new StringValue(((QueryBlobValue)value).DataTypeName);
171        }
172      } else if (value is QueryBoolValue) {
173        return new BoolValue(((QueryBoolValue)value).Value);
174      } else if (value is QueryFloatValue) {
175        return new DoubleValue(((QueryFloatValue)value).Value);
176      } else if (value is QueryIntValue) {
177        return new IntValue((int)((QueryIntValue)value).Value);
178      } else if (value is QueryStringValue) {
179        return new StringValue(((QueryStringValue)value).Value);
180      }
181      return null;
182    }
183  }
184}
Note: See TracBrowser for help on using the repository browser.