[5073] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[7331] | 3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[5073] | 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;
|
---|
[5611] | 24 | using System.Linq;
|
---|
[5317] | 25 | using System.Threading;
|
---|
| 26 | using System.Threading.Tasks;
|
---|
[5073] | 27 | using System.Windows.Forms;
|
---|
[5304] | 28 | using HeuristicLab.Core;
|
---|
[5073] | 29 | using HeuristicLab.MainForm;
|
---|
| 30 | using HeuristicLab.MainForm.WindowsForms;
|
---|
[5304] | 31 | using HeuristicLab.Optimization;
|
---|
| 32 | using HeuristicLab.Persistence.Default.Xml;
|
---|
[5612] | 33 | using HeuristicLab.PluginInfrastructure;
|
---|
[5073] | 34 |
|
---|
[5606] | 35 | namespace HeuristicLab.Clients.OKB.Query {
|
---|
[5295] | 36 | [View("OKB Query")]
|
---|
[5611] | 37 | [Content(typeof(QueryClient), true)]
|
---|
| 38 | public sealed partial class QueryView : AsynchronousContentView {
|
---|
[5317] | 39 | private CancellationTokenSource cancellationTokenSource;
|
---|
[5304] | 40 | private CombinedFilterView combinedFilterView;
|
---|
[5295] | 41 |
|
---|
[5611] | 42 | public new QueryClient Content {
|
---|
| 43 | get { return (QueryClient)base.Content; }
|
---|
| 44 | set { base.Content = value; }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
[5073] | 47 | public QueryView() {
|
---|
| 48 | InitializeComponent();
|
---|
| 49 | }
|
---|
| 50 |
|
---|
[5611] | 51 | protected override void DeregisterContentEvents() {
|
---|
| 52 | Content.Refreshing -= new EventHandler(Content_Refreshing);
|
---|
| 53 | Content.Refreshed -= new EventHandler(Content_Refreshed);
|
---|
| 54 | base.DeregisterContentEvents();
|
---|
[5073] | 55 | }
|
---|
| 56 |
|
---|
[5611] | 57 | protected override void RegisterContentEvents() {
|
---|
| 58 | base.RegisterContentEvents();
|
---|
| 59 | Content.Refreshing += new EventHandler(Content_Refreshing);
|
---|
| 60 | Content.Refreshed += new EventHandler(Content_Refreshed);
|
---|
| 61 | }
|
---|
[5295] | 62 |
|
---|
[5611] | 63 | protected override void OnContentChanged() {
|
---|
| 64 | base.OnContentChanged();
|
---|
| 65 | CreateFilterView();
|
---|
| 66 | runCollectionView.Content = null;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[5295] | 69 | protected override void SetEnabledStateOfControls() {
|
---|
| 70 | base.SetEnabledStateOfControls();
|
---|
[5304] | 71 | resultsGroupBox.Enabled = combinedFilterView != null;
|
---|
[5295] | 72 | }
|
---|
| 73 |
|
---|
[5317] | 74 | #region Load Results
|
---|
| 75 | private void LoadResultsAsync(int batchSize) {
|
---|
[5612] | 76 | bool includeBinaryValues = includeBinaryValuesCheckBox.Checked;
|
---|
[5304] | 77 |
|
---|
[5317] | 78 | Cursor = Cursors.AppStarting;
|
---|
| 79 | resultsInfoLabel.Text = "Loading Results ...";
|
---|
| 80 | resultsProgressBar.Value = 0;
|
---|
| 81 | resultsProgressBar.Step = batchSize;
|
---|
| 82 | abortButton.Enabled = true;
|
---|
| 83 | resultsInfoPanel.Visible = true;
|
---|
| 84 | splitContainer.Enabled = false;
|
---|
| 85 | cancellationTokenSource = new CancellationTokenSource();
|
---|
| 86 | CancellationToken cancellationToken = cancellationTokenSource.Token;
|
---|
[5304] | 87 |
|
---|
[5317] | 88 | Task task = Task.Factory.StartNew(() => {
|
---|
[5611] | 89 | var ids = QueryClient.Instance.GetRunIds(combinedFilterView.Content);
|
---|
[5317] | 90 | int idsCount = ids.Count();
|
---|
[5304] | 91 |
|
---|
[5317] | 92 | Invoke(new Action(() => {
|
---|
| 93 | resultsInfoLabel.Text = "Loaded 0 of " + idsCount.ToString() + " Results ...";
|
---|
| 94 | resultsProgressBar.Maximum = idsCount;
|
---|
| 95 | }));
|
---|
[5304] | 96 |
|
---|
[5317] | 97 | RunCollection runs = new RunCollection();
|
---|
| 98 | runCollectionView.Content = runs;
|
---|
| 99 | while (ids.Count() > 0) {
|
---|
| 100 | cancellationToken.ThrowIfCancellationRequested();
|
---|
[5902] | 101 | runs.AddRange(QueryClient.Instance.GetRuns(ids.Take(batchSize), includeBinaryValues).Select(x => ConvertToOptimizationRun(x)));
|
---|
[5317] | 102 | ids = ids.Skip(batchSize);
|
---|
| 103 | Invoke(new Action(() => {
|
---|
| 104 | resultsInfoLabel.Text = "Loaded " + runs.Count + " of " + idsCount.ToString() + " Results ...";
|
---|
| 105 | resultsProgressBar.PerformStep();
|
---|
| 106 | }));
|
---|
| 107 | }
|
---|
| 108 | }, cancellationToken);
|
---|
| 109 | task.ContinueWith(t => {
|
---|
| 110 | Invoke(new Action(() => {
|
---|
| 111 | cancellationTokenSource.Dispose();
|
---|
| 112 | cancellationTokenSource = null;
|
---|
| 113 | resultsInfoPanel.Visible = false;
|
---|
| 114 | splitContainer.Enabled = true;
|
---|
| 115 | this.Cursor = Cursors.Default;
|
---|
| 116 | SetEnabledStateOfControls();
|
---|
[5674] | 117 | try {
|
---|
| 118 | t.Wait();
|
---|
| 119 | }
|
---|
| 120 | catch (AggregateException ex) {
|
---|
| 121 | try {
|
---|
| 122 | ex.Flatten().Handle(x => x is OperationCanceledException);
|
---|
| 123 | }
|
---|
| 124 | catch (AggregateException remaining) {
|
---|
| 125 | if (remaining.InnerExceptions.Count == 1) ErrorHandling.ShowErrorDialog(this, "Refresh results failed.", remaining.InnerExceptions[0]);
|
---|
| 126 | else ErrorHandling.ShowErrorDialog(this, "Refresh results failed.", remaining);
|
---|
| 127 | }
|
---|
| 128 | }
|
---|
[5317] | 129 | }));
|
---|
| 130 | });
|
---|
| 131 | }
|
---|
[5612] | 132 | #endregion
|
---|
[5304] | 133 |
|
---|
[5612] | 134 | private void Content_Refreshing(object sender, EventArgs e) {
|
---|
| 135 | if (InvokeRequired) {
|
---|
| 136 | Invoke(new EventHandler(Content_Refreshing), sender, e);
|
---|
| 137 | } else {
|
---|
| 138 | Cursor = Cursors.AppStarting;
|
---|
| 139 | filtersInfoPanel.Visible = true;
|
---|
| 140 | splitContainer.Enabled = false;
|
---|
| 141 | }
|
---|
[5073] | 142 | }
|
---|
[5612] | 143 | private void Content_Refreshed(object sender, EventArgs e) {
|
---|
| 144 | if (InvokeRequired) {
|
---|
| 145 | Invoke(new EventHandler(Content_Refreshed), sender, e);
|
---|
| 146 | } else {
|
---|
| 147 | CreateFilterView();
|
---|
| 148 | filtersInfoPanel.Visible = false;
|
---|
| 149 | splitContainer.Enabled = true;
|
---|
| 150 | Cursor = Cursors.Default;
|
---|
| 151 | SetEnabledStateOfControls();
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
[5073] | 154 |
|
---|
[5612] | 155 | private void refreshFiltersButton_Click(object sender, EventArgs e) {
|
---|
| 156 | Content.RefreshAsync(new Action<Exception>((Exception ex) => ErrorHandling.ShowErrorDialog(this, "Refresh failed.", ex)));
|
---|
| 157 | }
|
---|
| 158 |
|
---|
[5317] | 159 | private void refreshResultsButton_Click(object sender, EventArgs e) {
|
---|
| 160 | LoadResultsAsync(10);
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | private void abortButton_Click(object sender, EventArgs e) {
|
---|
| 164 | if (cancellationTokenSource != null) cancellationTokenSource.Cancel();
|
---|
| 165 | abortButton.Enabled = false;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
[5611] | 168 | private void CreateFilterView() {
|
---|
| 169 | combinedFilterView = null;
|
---|
| 170 | filterPanel.Controls.Clear();
|
---|
| 171 | if ((Content != null) && (Content.Filters != null)) {
|
---|
| 172 | CombinedFilter filter = Content.Filters.OfType<CombinedFilter>().Where(x => x.Operation == BooleanOperation.And).FirstOrDefault();
|
---|
| 173 | if (filter != null) {
|
---|
| 174 | combinedFilterView = (CombinedFilterView)MainFormManager.CreateView(typeof(CombinedFilterView));
|
---|
| 175 | combinedFilterView.Content = (CombinedFilter)filter.Clone();
|
---|
| 176 | Control control = (Control)combinedFilterView;
|
---|
| 177 | control.Dock = DockStyle.Fill;
|
---|
| 178 | filterPanel.Controls.Add(control);
|
---|
| 179 | }
|
---|
| 180 | }
|
---|
[5073] | 181 | }
|
---|
[5295] | 182 |
|
---|
[5902] | 183 | private Optimization.IRun ConvertToOptimizationRun(Run run) {
|
---|
[5611] | 184 | Optimization.Run optRun = new Optimization.Run();
|
---|
| 185 | foreach (Value value in run.ParameterValues)
|
---|
[5902] | 186 | optRun.Parameters.Add(value.Name, ConvertToItem(value));
|
---|
[5611] | 187 | foreach (Value value in run.ResultValues)
|
---|
[5902] | 188 | optRun.Results.Add(value.Name, ConvertToItem(value));
|
---|
[5611] | 189 | return optRun;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
[5902] | 192 | private IItem ConvertToItem(Value value) {
|
---|
[5611] | 193 | if (value is BinaryValue) {
|
---|
[5902] | 194 | IItem item = null;
|
---|
| 195 | BinaryValue binaryValue = (BinaryValue)value;
|
---|
| 196 | if (binaryValue.Value != null) {
|
---|
| 197 | using (MemoryStream stream = new MemoryStream(binaryValue.Value)) {
|
---|
[5304] | 198 | try {
|
---|
| 199 | item = XmlParser.Deserialize<IItem>(stream);
|
---|
| 200 | }
|
---|
| 201 | catch (Exception) { }
|
---|
| 202 | stream.Close();
|
---|
| 203 | }
|
---|
| 204 | }
|
---|
[5902] | 205 | return item != null ? item : new Data.StringValue(value.DataType.Name);
|
---|
[5611] | 206 | } else if (value is BoolValue) {
|
---|
| 207 | return new Data.BoolValue(((BoolValue)value).Value);
|
---|
| 208 | } else if (value is FloatValue) {
|
---|
| 209 | return new Data.DoubleValue(((FloatValue)value).Value);
|
---|
[7573] | 210 | } else if (value is PercentValue) {
|
---|
| 211 | return new Data.PercentValue(((PercentValue)value).Value);
|
---|
[5611] | 212 | } else if (value is DoubleValue) {
|
---|
[5674] | 213 | return new Data.DoubleValue(((DoubleValue)value).Value);
|
---|
[5611] | 214 | } else if (value is IntValue) {
|
---|
| 215 | return new Data.IntValue((int)((IntValue)value).Value);
|
---|
| 216 | } else if (value is LongValue) {
|
---|
[5674] | 217 | return new Data.IntValue((int)((LongValue)value).Value);
|
---|
[5611] | 218 | } else if (value is StringValue) {
|
---|
| 219 | return new Data.StringValue(((StringValue)value).Value);
|
---|
[5304] | 220 | }
|
---|
| 221 | return null;
|
---|
[5295] | 222 | }
|
---|
[5073] | 223 | }
|
---|
| 224 | }
|
---|