1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 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();
|
---|
83 | UpdateDataRowComboBox();
|
---|
84 | AddRuns(e.Items);
|
---|
85 | }
|
---|
86 | private void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
|
---|
87 | if (suppressUpdates) return;
|
---|
88 | if (InvokeRequired) {
|
---|
89 | Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved), sender, e);
|
---|
90 | return;
|
---|
91 | }
|
---|
92 | UpdateDataTableComboBox();
|
---|
93 | UpdateDataRowComboBox();
|
---|
94 | RemoveRuns(e.Items);
|
---|
95 | }
|
---|
96 | private void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
|
---|
97 | if (suppressUpdates) return;
|
---|
98 | if (InvokeRequired) {
|
---|
99 | Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset), sender, e);
|
---|
100 | return;
|
---|
101 | }
|
---|
102 | UpdateDataTableComboBox();
|
---|
103 | UpdateDataRowComboBox();
|
---|
104 | RemoveRuns(e.OldItems);
|
---|
105 | AddRuns(e.Items);
|
---|
106 | }
|
---|
107 | private void Content_AlgorithmNameChanged(object sender, EventArgs e) {
|
---|
108 | if (InvokeRequired)
|
---|
109 | Invoke(new EventHandler(Content_AlgorithmNameChanged), sender, e);
|
---|
110 | else UpdateCaption();
|
---|
111 | }
|
---|
112 | private void Content_UpdateOfRunsInProgressChanged(object sender, EventArgs e) {
|
---|
113 | if (InvokeRequired) {
|
---|
114 | Invoke(new EventHandler(Content_UpdateOfRunsInProgressChanged), sender, e);
|
---|
115 | return;
|
---|
116 | }
|
---|
117 | suppressUpdates = Content.UpdateOfRunsInProgress;
|
---|
118 | if (!suppressUpdates) {
|
---|
119 | UpdateDataTableComboBox();
|
---|
120 | UpdateDataRowComboBox();
|
---|
121 | UpdateRuns(Content);
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | private void RegisterRunEvents(IRun run) {
|
---|
126 | run.PropertyChanged += run_PropertyChanged;
|
---|
127 | }
|
---|
128 | private void DeregisterRunEvents(IRun run) {
|
---|
129 | run.PropertyChanged -= run_PropertyChanged;
|
---|
130 | }
|
---|
131 | private void run_PropertyChanged(object sender, PropertyChangedEventArgs e) {
|
---|
132 | if (suppressUpdates) return;
|
---|
133 | if (InvokeRequired) {
|
---|
134 | Invoke((Action<object, PropertyChangedEventArgs>)run_PropertyChanged, sender, e);
|
---|
135 | } else {
|
---|
136 | var run = (IRun)sender;
|
---|
137 | if (e.PropertyName == "Color" || e.PropertyName == "Visible")
|
---|
138 | UpdateRuns(new[] { run });
|
---|
139 | }
|
---|
140 | }
|
---|
141 | #endregion
|
---|
142 |
|
---|
143 | protected override void OnContentChanged() {
|
---|
144 | base.OnContentChanged();
|
---|
145 | dataTableComboBox.Items.Clear();
|
---|
146 | dataRowComboBox.Items.Clear();
|
---|
147 | combinedDataTable.Rows.Clear();
|
---|
148 | runMapping.Clear();
|
---|
149 |
|
---|
150 | UpdateCaption();
|
---|
151 | if (Content != null) {
|
---|
152 | UpdateDataTableComboBox();
|
---|
153 | }
|
---|
154 | }
|
---|
155 |
|
---|
156 | private void RebuildCombinedDataTable() {
|
---|
157 | RemoveRuns(Content);
|
---|
158 | rowNumber = 0;
|
---|
159 | AddRuns(Content);
|
---|
160 | }
|
---|
161 |
|
---|
162 | private void AddRuns(IEnumerable<IRun> runs) {
|
---|
163 | foreach (var run in runs) {
|
---|
164 | runMapping[run] = ExtractDataRowsFromRun(run).ToList();
|
---|
165 | RegisterRunEvents(run);
|
---|
166 | }
|
---|
167 | var dataRows = runs.Where(r => r.Visible && runMapping.ContainsKey(r)).SelectMany(r => runMapping[r]);
|
---|
168 | combinedDataTable.Rows.AddRange(dataRows);
|
---|
169 | }
|
---|
170 |
|
---|
171 | private void RemoveRuns(IEnumerable<IRun> runs) {
|
---|
172 | var dataRows = runs.Where(r => runMapping.ContainsKey(r)).SelectMany(r => runMapping[r]).ToList();
|
---|
173 | foreach (var run in runs) {
|
---|
174 | if (!runMapping.ContainsKey(run)) continue;
|
---|
175 | runMapping.Remove(run);
|
---|
176 | DeregisterRunEvents(run);
|
---|
177 | }
|
---|
178 | combinedDataTable.Rows.RemoveRange(dataRows);
|
---|
179 | }
|
---|
180 |
|
---|
181 | private void UpdateRuns(IEnumerable<IRun> runs) {
|
---|
182 | foreach (var run in runs) {
|
---|
183 | //update color
|
---|
184 | if (!runMapping.ContainsKey(run)) {
|
---|
185 | runMapping[run] = ExtractDataRowsFromRun(run).ToList();
|
---|
186 | RegisterRunEvents(run);
|
---|
187 | } else {
|
---|
188 | foreach (var dataRow in runMapping[run]) {
|
---|
189 | dataRow.VisualProperties.Color = run.Color;
|
---|
190 | }
|
---|
191 | }
|
---|
192 | }
|
---|
193 | //update visibility - remove and add all rows to keep the same order as before
|
---|
194 | combinedDataTable.Rows.Clear();
|
---|
195 | combinedDataTable.Rows.AddRange(runMapping.Where(mapping => mapping.Key.Visible).SelectMany(mapping => mapping.Value));
|
---|
196 | }
|
---|
197 |
|
---|
198 | private IEnumerable<DataRow> ExtractDataRowsFromRun(IRun run) {
|
---|
199 | var resultName = (string)dataTableComboBox.SelectedItem;
|
---|
200 | if (string.IsNullOrEmpty(resultName)) yield break;
|
---|
201 |
|
---|
202 | var rowName = (string)dataRowComboBox.SelectedItem;
|
---|
203 | if (!run.Results.ContainsKey(resultName)) yield break;
|
---|
204 |
|
---|
205 | var dataTable = (DataTable)run.Results[resultName];
|
---|
206 | foreach (var dataRow in dataTable.Rows) {
|
---|
207 | if (dataRow.Name != rowName && rowName != AllDataRows) continue;
|
---|
208 | rowNumber++;
|
---|
209 | var clonedRow = (DataRow)dataRow.Clone();
|
---|
210 | //row names must be unique -> add incremented number to the row name
|
---|
211 | clonedRow.Name = run.Name + "." + dataRow.Name + rowNumber;
|
---|
212 | clonedRow.VisualProperties.DisplayName = run.Name + "." + dataRow.Name;
|
---|
213 | clonedRow.VisualProperties.Color = run.Color;
|
---|
214 | yield return clonedRow;
|
---|
215 | }
|
---|
216 | }
|
---|
217 |
|
---|
218 | private void UpdateDataTableComboBox() {
|
---|
219 | string selectedItem = (string)dataTableComboBox.SelectedItem;
|
---|
220 |
|
---|
221 | dataTableComboBox.Items.Clear();
|
---|
222 | var dataTables = (from run in Content
|
---|
223 | from result in run.Results
|
---|
224 | where result.Value is DataTable
|
---|
225 | select result.Key).Distinct().ToArray();
|
---|
226 |
|
---|
227 | dataTableComboBox.Items.AddRange(dataTables);
|
---|
228 | if (selectedItem != null && dataTableComboBox.Items.Contains(selectedItem)) {
|
---|
229 | dataTableComboBox.SelectedItem = selectedItem;
|
---|
230 | } else if (dataTableComboBox.Items.Count > 0) {
|
---|
231 | dataTableComboBox.SelectedItem = dataTableComboBox.Items[0];
|
---|
232 | }
|
---|
233 | }
|
---|
234 |
|
---|
235 | private void UpdateCaption() {
|
---|
236 | Caption = Content != null ? Content.OptimizerName + " Chart Aggregation" : ViewAttribute.GetViewName(GetType());
|
---|
237 | }
|
---|
238 |
|
---|
239 | private void UpdateDataRowComboBox() {
|
---|
240 | string selectedItem = (string)dataRowComboBox.SelectedItem;
|
---|
241 |
|
---|
242 | dataRowComboBox.Items.Clear();
|
---|
243 | var resultName = (string)dataTableComboBox.SelectedItem;
|
---|
244 | if (resultName == null) return;
|
---|
245 |
|
---|
246 | var dataTables = from run in Content
|
---|
247 | where run.Results.ContainsKey(resultName)
|
---|
248 | select run.Results[resultName] as DataTable;
|
---|
249 | var rowNames = (from dataTable in dataTables
|
---|
250 | from row in dataTable.Rows
|
---|
251 | select row.Name).Distinct().ToArray();
|
---|
252 |
|
---|
253 | dataRowComboBox.Items.AddRange(rowNames);
|
---|
254 | dataRowComboBox.Items.Add(AllDataRows);
|
---|
255 | if (selectedItem != null && dataRowComboBox.Items.Contains(selectedItem)) {
|
---|
256 | dataRowComboBox.SelectedItem = selectedItem;
|
---|
257 | } else if (dataRowComboBox.Items.Count > 0) {
|
---|
258 | dataRowComboBox.SelectedItem = dataRowComboBox.Items[0];
|
---|
259 | }
|
---|
260 | }
|
---|
261 |
|
---|
262 | private void dataTableComboBox_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
263 | UpdateDataRowComboBox();
|
---|
264 | }
|
---|
265 | private void dataRowComboBox_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
266 | if (suppressUpdates) return;
|
---|
267 | RebuildCombinedDataTable();
|
---|
268 | }
|
---|
269 | }
|
---|
270 | }
|
---|