[5073] | 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 |
|
---|
| 22 | using System;
|
---|
[5304] | 23 | using System.IO;
|
---|
[5317] | 24 | using System.Threading;
|
---|
| 25 | using System.Threading.Tasks;
|
---|
[5073] | 26 | using System.Windows.Forms;
|
---|
[5304] | 27 | using HeuristicLab.Core;
|
---|
[5073] | 28 | using HeuristicLab.MainForm;
|
---|
| 29 | using HeuristicLab.MainForm.WindowsForms;
|
---|
[5304] | 30 | using HeuristicLab.Optimization;
|
---|
| 31 | using HeuristicLab.Persistence.Default.Xml;
|
---|
[5073] | 32 |
|
---|
[5606] | 33 | namespace HeuristicLab.Clients.OKB.Query {
|
---|
[5295] | 34 | [View("OKB Query")]
|
---|
[5073] | 35 | [Content(typeof(OKBClient), false)]
|
---|
[5286] | 36 | public sealed partial class QueryView : HeuristicLab.MainForm.WindowsForms.View {
|
---|
[5317] | 37 | private CancellationTokenSource cancellationTokenSource;
|
---|
[5304] | 38 | private CombinedFilterView combinedFilterView;
|
---|
[5295] | 39 |
|
---|
[5073] | 40 | public QueryView() {
|
---|
| 41 | InitializeComponent();
|
---|
| 42 | }
|
---|
| 43 |
|
---|
[5286] | 44 | protected override void OnInitialized(EventArgs e) {
|
---|
| 45 | base.OnInitialized(e);
|
---|
[5317] | 46 | LoadFiltersAsync();
|
---|
[5073] | 47 | }
|
---|
| 48 |
|
---|
[5295] | 49 |
|
---|
| 50 | protected override void SetEnabledStateOfControls() {
|
---|
| 51 | base.SetEnabledStateOfControls();
|
---|
[5304] | 52 | resultsGroupBox.Enabled = combinedFilterView != null;
|
---|
[5295] | 53 | }
|
---|
| 54 |
|
---|
[5317] | 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) {
|
---|
[5304] | 85 | bool deserialize = deserializeBlobsCheckBox.Checked;
|
---|
| 86 |
|
---|
[5317] | 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;
|
---|
[5304] | 96 |
|
---|
[5317] | 97 | Task task = Task.Factory.StartNew(() => {
|
---|
| 98 | var ids = OKBClient.Instance.GetQueryResultIds(combinedFilterView.Content);
|
---|
| 99 | int idsCount = ids.Count();
|
---|
[5304] | 100 |
|
---|
[5317] | 101 | Invoke(new Action(() => {
|
---|
| 102 | resultsInfoLabel.Text = "Loaded 0 of " + idsCount.ToString() + " Results ...";
|
---|
| 103 | resultsProgressBar.Maximum = idsCount;
|
---|
| 104 | }));
|
---|
[5304] | 105 |
|
---|
[5317] | 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 | }
|
---|
[5304] | 130 |
|
---|
[5317] | 131 | void runs_CollectionReset(object sender, Collections.CollectionItemsChangedEventArgs<IRun> e) {
|
---|
[5073] | 132 | }
|
---|
[5317] | 133 | #endregion
|
---|
[5073] | 134 |
|
---|
[5317] | 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 |
|
---|
[5304] | 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;
|
---|
[5073] | 153 | }
|
---|
[5295] | 154 |
|
---|
[5304] | 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;
|
---|
[5295] | 180 | }
|
---|
[5073] | 181 | }
|
---|
| 182 | }
|
---|