[7980] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15584] | 3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[7980] | 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;
|
---|
| 23 | using System.Collections.Generic;
|
---|
[11522] | 24 | using System.ComponentModel;
|
---|
[7980] | 25 | using System.Linq;
|
---|
| 26 | using HeuristicLab.Analysis;
|
---|
| 27 | using HeuristicLab.Collections;
|
---|
| 28 | using HeuristicLab.Core.Views;
|
---|
| 29 | using HeuristicLab.MainForm;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Optimization.Views {
|
---|
[9910] | 32 | [View("Chart Aggregation")]
|
---|
[7980] | 33 | [Content(typeof(RunCollection), false)]
|
---|
[9910] | 34 | public partial class RunCollectionChartAggregationView : ItemView {
|
---|
[7980] | 35 | private const string AllDataRows = "All DataRows";
|
---|
| 36 |
|
---|
| 37 | public new RunCollection Content {
|
---|
| 38 | get { return (RunCollection)base.Content; }
|
---|
| 39 | set { base.Content = value; }
|
---|
| 40 | }
|
---|
| 41 |
|
---|
[12725] | 42 | private int rowNumber;
|
---|
[7980] | 43 | private bool suppressUpdates;
|
---|
| 44 | private readonly Dictionary<IRun, IEnumerable<DataRow>> runMapping;
|
---|
| 45 | private readonly DataTable combinedDataTable;
|
---|
| 46 | public DataTable CombinedDataTable {
|
---|
| 47 | get { return combinedDataTable; }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
[9910] | 50 | public RunCollectionChartAggregationView() {
|
---|
[8108] | 51 | InitializeComponent();
|
---|
| 52 | runMapping = new Dictionary<IRun, IEnumerable<DataRow>>();
|
---|
| 53 | combinedDataTable = new DataTable("Combined DataTable", "A data table containing data rows from multiple runs.");
|
---|
| 54 | viewHost.Content = combinedDataTable;
|
---|
| 55 | suppressUpdates = false;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
[7980] | 58 | #region Content events
|
---|
| 59 | protected override void RegisterContentEvents() {
|
---|
| 60 | base.RegisterContentEvents();
|
---|
[12725] | 61 | Content.ItemsAdded += Content_ItemsAdded;
|
---|
| 62 | Content.ItemsRemoved += Content_ItemsRemoved;
|
---|
| 63 | Content.CollectionReset += Content_CollectionReset;
|
---|
| 64 | Content.UpdateOfRunsInProgressChanged += Content_UpdateOfRunsInProgressChanged;
|
---|
| 65 | Content.OptimizerNameChanged += Content_AlgorithmNameChanged;
|
---|
[7980] | 66 | }
|
---|
| 67 | protected override void DeregisterContentEvents() {
|
---|
[12725] | 68 | Content.ItemsAdded -= Content_ItemsAdded;
|
---|
| 69 | Content.ItemsRemoved -= Content_ItemsRemoved;
|
---|
| 70 | Content.CollectionReset -= Content_CollectionReset;
|
---|
| 71 | Content.UpdateOfRunsInProgressChanged -= Content_UpdateOfRunsInProgressChanged;
|
---|
| 72 | Content.OptimizerNameChanged -= Content_AlgorithmNameChanged;
|
---|
[7980] | 73 | base.DeregisterContentEvents();
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | private void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) {
|
---|
[12725] | 77 | if (suppressUpdates) return;
|
---|
[7980] | 78 | if (InvokeRequired) {
|
---|
| 79 | Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded), sender, e);
|
---|
| 80 | return;
|
---|
| 81 | }
|
---|
[13199] | 82 | UpdateDataTableComboBox(); // will trigger AddRuns
|
---|
[7980] | 83 | }
|
---|
| 84 | private void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
|
---|
[12725] | 85 | if (suppressUpdates) return;
|
---|
[7980] | 86 | if (InvokeRequired) {
|
---|
| 87 | Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved), sender, e);
|
---|
| 88 | return;
|
---|
| 89 | }
|
---|
[13199] | 90 | RemoveRuns(e.Items);
|
---|
[12725] | 91 | UpdateDataTableComboBox();
|
---|
| 92 | UpdateDataRowComboBox();
|
---|
[13199] | 93 | RebuildCombinedDataTable();
|
---|
[7980] | 94 | }
|
---|
| 95 | private void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
|
---|
[12725] | 96 | if (suppressUpdates) return;
|
---|
[7980] | 97 | if (InvokeRequired) {
|
---|
| 98 | Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset), sender, e);
|
---|
| 99 | return;
|
---|
| 100 | }
|
---|
[13199] | 101 | RemoveRuns(e.OldItems);
|
---|
[12725] | 102 | UpdateDataTableComboBox();
|
---|
| 103 | UpdateDataRowComboBox();
|
---|
[13199] | 104 | RebuildCombinedDataTable();
|
---|
[7980] | 105 | }
|
---|
[8738] | 106 | private void Content_AlgorithmNameChanged(object sender, EventArgs e) {
|
---|
| 107 | if (InvokeRequired)
|
---|
| 108 | Invoke(new EventHandler(Content_AlgorithmNameChanged), sender, e);
|
---|
| 109 | else UpdateCaption();
|
---|
| 110 | }
|
---|
[7980] | 111 | private void Content_UpdateOfRunsInProgressChanged(object sender, EventArgs e) {
|
---|
| 112 | if (InvokeRequired) {
|
---|
| 113 | Invoke(new EventHandler(Content_UpdateOfRunsInProgressChanged), sender, e);
|
---|
| 114 | return;
|
---|
| 115 | }
|
---|
| 116 | suppressUpdates = Content.UpdateOfRunsInProgress;
|
---|
[12725] | 117 | if (!suppressUpdates) {
|
---|
[13199] | 118 | foreach (var run in runMapping)
|
---|
| 119 | DeregisterRunEvents(run.Key);
|
---|
| 120 | runMapping.Clear();
|
---|
| 121 | combinedDataTable.Rows.Clear();
|
---|
[12725] | 122 | UpdateDataTableComboBox();
|
---|
| 123 | }
|
---|
[7980] | 124 | }
|
---|
| 125 |
|
---|
| 126 | private void RegisterRunEvents(IRun run) {
|
---|
[11522] | 127 | run.PropertyChanged += run_PropertyChanged;
|
---|
[7980] | 128 | }
|
---|
| 129 | private void DeregisterRunEvents(IRun run) {
|
---|
[11522] | 130 | run.PropertyChanged -= run_PropertyChanged;
|
---|
[7980] | 131 | }
|
---|
[11522] | 132 | private void run_PropertyChanged(object sender, PropertyChangedEventArgs e) {
|
---|
[7980] | 133 | if (suppressUpdates) return;
|
---|
[11522] | 134 | if (InvokeRequired) {
|
---|
| 135 | Invoke((Action<object, PropertyChangedEventArgs>)run_PropertyChanged, sender, e);
|
---|
| 136 | } else {
|
---|
| 137 | var run = (IRun)sender;
|
---|
| 138 | if (e.PropertyName == "Color" || e.PropertyName == "Visible")
|
---|
[12725] | 139 | UpdateRuns(new[] { run });
|
---|
[11522] | 140 | }
|
---|
[7980] | 141 | }
|
---|
| 142 | #endregion
|
---|
| 143 |
|
---|
| 144 | protected override void OnContentChanged() {
|
---|
| 145 | base.OnContentChanged();
|
---|
| 146 | dataTableComboBox.Items.Clear();
|
---|
| 147 | dataRowComboBox.Items.Clear();
|
---|
| 148 | combinedDataTable.Rows.Clear();
|
---|
| 149 | runMapping.Clear();
|
---|
| 150 |
|
---|
[8738] | 151 | UpdateCaption();
|
---|
[7980] | 152 | if (Content != null) {
|
---|
| 153 | UpdateDataTableComboBox();
|
---|
| 154 | }
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | private void RebuildCombinedDataTable() {
|
---|
| 158 | RemoveRuns(Content);
|
---|
[8108] | 159 | rowNumber = 0;
|
---|
[7980] | 160 | AddRuns(Content);
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | private void AddRuns(IEnumerable<IRun> runs) {
|
---|
| 164 | foreach (var run in runs) {
|
---|
| 165 | runMapping[run] = ExtractDataRowsFromRun(run).ToList();
|
---|
| 166 | RegisterRunEvents(run);
|
---|
| 167 | }
|
---|
| 168 | var dataRows = runs.Where(r => r.Visible && runMapping.ContainsKey(r)).SelectMany(r => runMapping[r]);
|
---|
| 169 | combinedDataTable.Rows.AddRange(dataRows);
|
---|
| 170 | }
|
---|
[8108] | 171 |
|
---|
[7980] | 172 | private void RemoveRuns(IEnumerable<IRun> runs) {
|
---|
| 173 | var dataRows = runs.Where(r => runMapping.ContainsKey(r)).SelectMany(r => runMapping[r]).ToList();
|
---|
| 174 | foreach (var run in runs) {
|
---|
| 175 | if (!runMapping.ContainsKey(run)) continue;
|
---|
| 176 | runMapping.Remove(run);
|
---|
| 177 | DeregisterRunEvents(run);
|
---|
| 178 | }
|
---|
| 179 | combinedDataTable.Rows.RemoveRange(dataRows);
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | private void UpdateRuns(IEnumerable<IRun> runs) {
|
---|
| 183 | foreach (var run in runs) {
|
---|
| 184 | //update color
|
---|
[12725] | 185 | if (!runMapping.ContainsKey(run)) {
|
---|
| 186 | runMapping[run] = ExtractDataRowsFromRun(run).ToList();
|
---|
| 187 | RegisterRunEvents(run);
|
---|
| 188 | } else {
|
---|
| 189 | foreach (var dataRow in runMapping[run]) {
|
---|
| 190 | dataRow.VisualProperties.Color = run.Color;
|
---|
| 191 | }
|
---|
[7980] | 192 | }
|
---|
| 193 | }
|
---|
[12725] | 194 | //update visibility - remove and add all rows to keep the same order as before
|
---|
| 195 | combinedDataTable.Rows.Clear();
|
---|
| 196 | combinedDataTable.Rows.AddRange(runMapping.Where(mapping => mapping.Key.Visible).SelectMany(mapping => mapping.Value));
|
---|
[7980] | 197 | }
|
---|
| 198 |
|
---|
| 199 | private IEnumerable<DataRow> ExtractDataRowsFromRun(IRun run) {
|
---|
[8108] | 200 | var resultName = (string)dataTableComboBox.SelectedItem;
|
---|
[12725] | 201 | if (string.IsNullOrEmpty(resultName)) yield break;
|
---|
| 202 |
|
---|
[8108] | 203 | var rowName = (string)dataRowComboBox.SelectedItem;
|
---|
[7980] | 204 | if (!run.Results.ContainsKey(resultName)) yield break;
|
---|
| 205 |
|
---|
[8108] | 206 | var dataTable = (DataTable)run.Results[resultName];
|
---|
[7980] | 207 | foreach (var dataRow in dataTable.Rows) {
|
---|
| 208 | if (dataRow.Name != rowName && rowName != AllDataRows) continue;
|
---|
| 209 | rowNumber++;
|
---|
| 210 | var clonedRow = (DataRow)dataRow.Clone();
|
---|
| 211 | //row names must be unique -> add incremented number to the row name
|
---|
| 212 | clonedRow.Name = run.Name + "." + dataRow.Name + rowNumber;
|
---|
| 213 | clonedRow.VisualProperties.DisplayName = run.Name + "." + dataRow.Name;
|
---|
| 214 | clonedRow.VisualProperties.Color = run.Color;
|
---|
| 215 | yield return clonedRow;
|
---|
| 216 | }
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | private void UpdateDataTableComboBox() {
|
---|
[12725] | 220 | string selectedItem = (string)dataTableComboBox.SelectedItem;
|
---|
| 221 |
|
---|
[7986] | 222 | dataTableComboBox.Items.Clear();
|
---|
[7980] | 223 | var dataTables = (from run in Content
|
---|
| 224 | from result in run.Results
|
---|
| 225 | where result.Value is DataTable
|
---|
| 226 | select result.Key).Distinct().ToArray();
|
---|
| 227 |
|
---|
| 228 | dataTableComboBox.Items.AddRange(dataTables);
|
---|
[12725] | 229 | if (selectedItem != null && dataTableComboBox.Items.Contains(selectedItem)) {
|
---|
| 230 | dataTableComboBox.SelectedItem = selectedItem;
|
---|
| 231 | } else if (dataTableComboBox.Items.Count > 0) {
|
---|
| 232 | dataTableComboBox.SelectedItem = dataTableComboBox.Items[0];
|
---|
| 233 | }
|
---|
[7980] | 234 | }
|
---|
| 235 |
|
---|
[8738] | 236 | private void UpdateCaption() {
|
---|
[9910] | 237 | Caption = Content != null ? Content.OptimizerName + " Chart Aggregation" : ViewAttribute.GetViewName(GetType());
|
---|
[8738] | 238 | }
|
---|
| 239 |
|
---|
[7980] | 240 | private void UpdateDataRowComboBox() {
|
---|
[12725] | 241 | string selectedItem = (string)dataRowComboBox.SelectedItem;
|
---|
| 242 |
|
---|
[7986] | 243 | dataRowComboBox.Items.Clear();
|
---|
[8108] | 244 | var resultName = (string)dataTableComboBox.SelectedItem;
|
---|
[12725] | 245 | if (resultName == null) return;
|
---|
| 246 |
|
---|
[7980] | 247 | var dataTables = from run in Content
|
---|
| 248 | where run.Results.ContainsKey(resultName)
|
---|
| 249 | select run.Results[resultName] as DataTable;
|
---|
| 250 | var rowNames = (from dataTable in dataTables
|
---|
| 251 | from row in dataTable.Rows
|
---|
| 252 | select row.Name).Distinct().ToArray();
|
---|
| 253 |
|
---|
| 254 | dataRowComboBox.Items.AddRange(rowNames);
|
---|
| 255 | dataRowComboBox.Items.Add(AllDataRows);
|
---|
[12725] | 256 | if (selectedItem != null && dataRowComboBox.Items.Contains(selectedItem)) {
|
---|
| 257 | dataRowComboBox.SelectedItem = selectedItem;
|
---|
| 258 | } else if (dataRowComboBox.Items.Count > 0) {
|
---|
| 259 | dataRowComboBox.SelectedItem = dataRowComboBox.Items[0];
|
---|
| 260 | }
|
---|
[7980] | 261 | }
|
---|
| 262 |
|
---|
[12725] | 263 | private void dataTableComboBox_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
[7980] | 264 | UpdateDataRowComboBox();
|
---|
| 265 | }
|
---|
[12725] | 266 | private void dataRowComboBox_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
| 267 | if (suppressUpdates) return;
|
---|
[8925] | 268 | RebuildCombinedDataTable();
|
---|
[7980] | 269 | }
|
---|
| 270 | }
|
---|
| 271 | }
|
---|