#region License Information /* HeuristicLab * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.IO; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using HeuristicLab.Core; using HeuristicLab.MainForm; using HeuristicLab.MainForm.WindowsForms; using HeuristicLab.Optimization; using HeuristicLab.Persistence.Default.Xml; namespace HeuristicLab.Clients.OKB.Query { [View("OKB Query")] [Content(typeof(OKBClient), false)] public sealed partial class QueryView : HeuristicLab.MainForm.WindowsForms.View { private CancellationTokenSource cancellationTokenSource; private CombinedFilterView combinedFilterView; public QueryView() { InitializeComponent(); } protected override void OnInitialized(EventArgs e) { base.OnInitialized(e); LoadFiltersAsync(); } protected override void SetEnabledStateOfControls() { base.SetEnabledStateOfControls(); resultsGroupBox.Enabled = combinedFilterView != null; } #region Load Filters private void LoadFiltersAsync() { Cursor = Cursors.AppStarting; filtersInfoPanel.Visible = true; splitContainer.Enabled = false; Task task = Task.Factory.StartNew(() => { return OKBClient.Instance.GetFilters().OfType().Where(x => x.Operation == BooleanOperation.And).FirstOrDefault(); }); task.ContinueWith(t => { CombinedFilter filter = t.Result; Invoke(new Action(() => { if (filter != null) { combinedFilterView = (CombinedFilterView)MainFormManager.CreateView(typeof(CombinedFilterView)); combinedFilterView.Content = (CombinedFilter)filter.Clone(); Control control = (Control)combinedFilterView; control.Dock = DockStyle.Fill; filterPanel.Controls.Add(control); } filtersInfoPanel.Visible = false; splitContainer.Enabled = true; this.Cursor = Cursors.Default; SetEnabledStateOfControls(); })); }); } #endregion #region Load Results private void LoadResultsAsync(int batchSize) { bool deserialize = deserializeBlobsCheckBox.Checked; Cursor = Cursors.AppStarting; resultsInfoLabel.Text = "Loading Results ..."; resultsProgressBar.Value = 0; resultsProgressBar.Step = batchSize; abortButton.Enabled = true; resultsInfoPanel.Visible = true; splitContainer.Enabled = false; cancellationTokenSource = new CancellationTokenSource(); CancellationToken cancellationToken = cancellationTokenSource.Token; Task task = Task.Factory.StartNew(() => { var ids = OKBClient.Instance.GetQueryResultIds(combinedFilterView.Content); int idsCount = ids.Count(); Invoke(new Action(() => { resultsInfoLabel.Text = "Loaded 0 of " + idsCount.ToString() + " Results ..."; resultsProgressBar.Maximum = idsCount; })); RunCollection runs = new RunCollection(); runs.CollectionReset += new Collections.CollectionItemsChangedEventHandler(runs_CollectionReset); runCollectionView.Content = runs; while (ids.Count() > 0) { cancellationToken.ThrowIfCancellationRequested(); runs.AddRange(OKBClient.Instance.GetQueryResults(ids.Take(batchSize)).Select(x => ConvertToOptimizationRun(x, deserialize))); ids = ids.Skip(batchSize); Invoke(new Action(() => { resultsInfoLabel.Text = "Loaded " + runs.Count + " of " + idsCount.ToString() + " Results ..."; resultsProgressBar.PerformStep(); })); } }, cancellationToken); task.ContinueWith(t => { Invoke(new Action(() => { cancellationTokenSource.Dispose(); cancellationTokenSource = null; resultsInfoPanel.Visible = false; splitContainer.Enabled = true; this.Cursor = Cursors.Default; SetEnabledStateOfControls(); })); }); } void runs_CollectionReset(object sender, Collections.CollectionItemsChangedEventArgs e) { } #endregion private void refreshResultsButton_Click(object sender, EventArgs e) { LoadResultsAsync(10); } private void abortButton_Click(object sender, EventArgs e) { if (cancellationTokenSource != null) cancellationTokenSource.Cancel(); abortButton.Enabled = false; } private Optimization.IRun ConvertToOptimizationRun(QueryResult queryResult, bool deserialize) { Optimization.Run run = new Optimization.Run(); foreach (QueryValue value in queryResult.AlgorithmParameters) run.Parameters.Add(value.Name, ConvertToItem(value, deserialize)); foreach (QueryValue value in queryResult.ProblemParameters) run.Parameters.Add(value.Name, ConvertToItem(value, deserialize)); foreach (QueryValue value in queryResult.Results) run.Results.Add(value.Name, ConvertToItem(value, deserialize)); return run; } private IItem ConvertToItem(QueryValue value, bool deserialize) { if (value is QueryBlobValue) { if (deserialize) { IItem item = null; using (MemoryStream stream = new MemoryStream(((QueryBlobValue)value).Value)) { try { item = XmlParser.Deserialize(stream); } catch (Exception) { } stream.Close(); } return item != null ? item : new StringValue(((QueryBlobValue)value).DataTypeName); } else { return new StringValue(((QueryBlobValue)value).DataTypeName); } } else if (value is QueryBoolValue) { return new BoolValue(((QueryBoolValue)value).Value); } else if (value is QueryFloatValue) { return new DoubleValue(((QueryFloatValue)value).Value); } else if (value is QueryIntValue) { return new IntValue((int)((QueryIntValue)value).Value); } else if (value is QueryStringValue) { return new StringValue(((QueryStringValue)value).Value); } return null; } } }