Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5608 was 5606, checked in by swagner, 14 years ago

Worked on OKB (#1174)

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