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