1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2018 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;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.ComponentModel;
|
---|
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 {
|
---|
32 | [View("Chart Aggregation")]
|
---|
33 | [Content(typeof(RunCollection), false)]
|
---|
34 | public partial class RunCollectionChartAggregationView : ItemView {
|
---|
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 |
|
---|
42 | private int rowNumber;
|
---|
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 |
|
---|
50 | public RunCollectionChartAggregationView() {
|
---|
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 |
|
---|
58 | #region Content events
|
---|
59 | protected override void RegisterContentEvents() {
|
---|
60 | base.RegisterContentEvents();
|
---|
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;
|
---|
66 | }
|
---|
67 | protected override void DeregisterContentEvents() {
|
---|
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;
|
---|
73 | base.DeregisterContentEvents();
|
---|
74 | }
|
---|
75 |
|
---|
76 | private void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) {
|
---|
77 | if (suppressUpdates) return;
|
---|
78 | if (InvokeRequired) {
|
---|
79 | Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded), sender, e);
|
---|
80 | return;
|
---|
81 | }
|
---|
82 | UpdateDataTableComboBox(); // will trigger AddRuns
|
---|
83 | }
|
---|
84 | private void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
|
---|
85 | if (suppressUpdates) return;
|
---|
86 | if (InvokeRequired) {
|
---|
87 | Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved), sender, e);
|
---|
88 | return;
|
---|
89 | }
|
---|
90 | RemoveRuns(e.Items);
|
---|
91 | UpdateDataTableComboBox();
|
---|
92 | UpdateDataRowComboBox();
|
---|
93 | RebuildCombinedDataTable();
|
---|
94 | }
|
---|
95 | private void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
|
---|
96 | if (suppressUpdates) return;
|
---|
97 | if (InvokeRequired) {
|
---|
98 | Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset), sender, e);
|
---|
99 | return;
|
---|
100 | }
|
---|
101 | RemoveRuns(e.OldItems);
|
---|
102 | UpdateDataTableComboBox();
|
---|
103 | UpdateDataRowComboBox();
|
---|
104 | RebuildCombinedDataTable();
|
---|
105 | }
|
---|
106 | private void Content_AlgorithmNameChanged(object sender, EventArgs e) {
|
---|
107 | if (InvokeRequired)
|
---|
108 | Invoke(new EventHandler(Content_AlgorithmNameChanged), sender, e);
|
---|
109 | else UpdateCaption();
|
---|
110 | }
|
---|
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;
|
---|
117 | if (!suppressUpdates) {
|
---|
118 | foreach (var run in runMapping)
|
---|
119 | DeregisterRunEvents(run.Key);
|
---|
120 | runMapping.Clear();
|
---|
121 | combinedDataTable.Rows.Clear();
|
---|
122 | UpdateDataTableComboBox();
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | private void RegisterRunEvents(IRun run) {
|
---|
127 | run.PropertyChanged += run_PropertyChanged;
|
---|
128 | }
|
---|
129 | private void DeregisterRunEvents(IRun run) {
|
---|
130 | run.PropertyChanged -= run_PropertyChanged;
|
---|
131 | }
|
---|
132 | private void run_PropertyChanged(object sender, PropertyChangedEventArgs e) {
|
---|
133 | if (suppressUpdates) return;
|
---|
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")
|
---|
139 | UpdateRuns(new[] { run });
|
---|
140 | }
|
---|
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 |
|
---|
151 | UpdateCaption();
|
---|
152 | if (Content != null) {
|
---|
153 | UpdateDataTableComboBox();
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | private void RebuildCombinedDataTable() {
|
---|
158 | RemoveRuns(Content);
|
---|
159 | rowNumber = 0;
|
---|
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 | }
|
---|
171 |
|
---|
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
|
---|
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 | }
|
---|
192 | }
|
---|
193 | }
|
---|
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));
|
---|
197 | }
|
---|
198 |
|
---|
199 | private IEnumerable<DataRow> ExtractDataRowsFromRun(IRun run) {
|
---|
200 | var resultName = (string)dataTableComboBox.SelectedItem;
|
---|
201 | if (string.IsNullOrEmpty(resultName)) yield break;
|
---|
202 |
|
---|
203 | var rowName = (string)dataRowComboBox.SelectedItem;
|
---|
204 | if (!run.Results.ContainsKey(resultName)) yield break;
|
---|
205 |
|
---|
206 | var dataTable = (DataTable)run.Results[resultName];
|
---|
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() {
|
---|
220 | string selectedItem = (string)dataTableComboBox.SelectedItem;
|
---|
221 |
|
---|
222 | dataTableComboBox.Items.Clear();
|
---|
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);
|
---|
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 | }
|
---|
234 | }
|
---|
235 |
|
---|
236 | private void UpdateCaption() {
|
---|
237 | Caption = Content != null ? Content.OptimizerName + " Chart Aggregation" : ViewAttribute.GetViewName(GetType());
|
---|
238 | }
|
---|
239 |
|
---|
240 | private void UpdateDataRowComboBox() {
|
---|
241 | string selectedItem = (string)dataRowComboBox.SelectedItem;
|
---|
242 |
|
---|
243 | dataRowComboBox.Items.Clear();
|
---|
244 | var resultName = (string)dataTableComboBox.SelectedItem;
|
---|
245 | if (resultName == null) return;
|
---|
246 |
|
---|
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);
|
---|
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 | }
|
---|
261 | }
|
---|
262 |
|
---|
263 | private void dataTableComboBox_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
264 | UpdateDataRowComboBox();
|
---|
265 | }
|
---|
266 | private void dataRowComboBox_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
267 | if (suppressUpdates) return;
|
---|
268 | RebuildCombinedDataTable();
|
---|
269 | }
|
---|
270 | }
|
---|
271 | }
|
---|