Free cookie consent management tool by TermsFeed Policy Generator

source: branches/TerminationCriteria/HeuristicLab.Optimization.Views/3.3/RunCollectionViews/RunCollectionChartAggregationView.cs @ 12809

Last change on this file since 12809 was 12809, checked in by pfleck, 9 years ago

#2027 Merged trunk changes.

File size: 10.2 KB
RevLine 
[7980]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 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
22using System;
23using System.Collections.Generic;
[11344]24using System.ComponentModel;
[7980]25using System.Linq;
26using HeuristicLab.Analysis;
27using HeuristicLab.Collections;
28using HeuristicLab.Core.Views;
29using HeuristicLab.MainForm;
30
31namespace 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
[12809]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();
[12809]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() {
[12809]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) {
[12809]77      if (suppressUpdates) return;
[7980]78      if (InvokeRequired) {
79        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded), sender, e);
80        return;
81      }
[12809]82      UpdateDataTableComboBox();
83      UpdateDataRowComboBox();
[7980]84      AddRuns(e.Items);
85    }
86    private void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
[12809]87      if (suppressUpdates) return;
[7980]88      if (InvokeRequired) {
89        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved), sender, e);
90        return;
91      }
[12809]92      UpdateDataTableComboBox();
93      UpdateDataRowComboBox();
[7980]94      RemoveRuns(e.Items);
95    }
96    private void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
[12809]97      if (suppressUpdates) return;
[7980]98      if (InvokeRequired) {
99        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset), sender, e);
100        return;
101      }
[12809]102      UpdateDataTableComboBox();
103      UpdateDataRowComboBox();
[7980]104      RemoveRuns(e.OldItems);
105      AddRuns(e.Items);
106    }
[8738]107    private void Content_AlgorithmNameChanged(object sender, EventArgs e) {
108      if (InvokeRequired)
109        Invoke(new EventHandler(Content_AlgorithmNameChanged), sender, e);
110      else UpdateCaption();
111    }
[7980]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;
[12809]118      if (!suppressUpdates) {
119        UpdateDataTableComboBox();
120        UpdateDataRowComboBox();
121        UpdateRuns(Content);
122      }
[7980]123    }
124
125    private void RegisterRunEvents(IRun run) {
[11344]126      run.PropertyChanged += run_PropertyChanged;
[7980]127    }
128    private void DeregisterRunEvents(IRun run) {
[11344]129      run.PropertyChanged -= run_PropertyChanged;
[7980]130    }
[11344]131    private void run_PropertyChanged(object sender, PropertyChangedEventArgs e) {
[7980]132      if (suppressUpdates) return;
[11344]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")
[12809]138          UpdateRuns(new[] { run });
[11344]139      }
[7980]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
[8738]150      UpdateCaption();
[7980]151      if (Content != null) {
152        UpdateDataTableComboBox();
153      }
154    }
155
156    private void RebuildCombinedDataTable() {
157      RemoveRuns(Content);
[8108]158      rowNumber = 0;
[7980]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    }
[8108]170
[7980]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
[12809]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          }
[7980]191        }
192      }
[12809]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));
[7980]196    }
197
198    private IEnumerable<DataRow> ExtractDataRowsFromRun(IRun run) {
[8108]199      var resultName = (string)dataTableComboBox.SelectedItem;
[12809]200      if (string.IsNullOrEmpty(resultName)) yield break;
201
[8108]202      var rowName = (string)dataRowComboBox.SelectedItem;
[7980]203      if (!run.Results.ContainsKey(resultName)) yield break;
204
[8108]205      var dataTable = (DataTable)run.Results[resultName];
[7980]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() {
[12809]219      string selectedItem = (string)dataTableComboBox.SelectedItem;
220
[7986]221      dataTableComboBox.Items.Clear();
[7980]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);
[12809]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      }
[7980]233    }
234
[8738]235    private void UpdateCaption() {
[9910]236      Caption = Content != null ? Content.OptimizerName + " Chart Aggregation" : ViewAttribute.GetViewName(GetType());
[8738]237    }
238
[7980]239    private void UpdateDataRowComboBox() {
[12809]240      string selectedItem = (string)dataRowComboBox.SelectedItem;
241
[7986]242      dataRowComboBox.Items.Clear();
[8108]243      var resultName = (string)dataTableComboBox.SelectedItem;
[12809]244      if (resultName == null) return;
245
[7980]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);
[12809]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      }
[7980]260    }
261
[12809]262    private void dataTableComboBox_SelectedIndexChanged(object sender, EventArgs e) {
[7980]263      UpdateDataRowComboBox();
264    }
[12809]265    private void dataRowComboBox_SelectedIndexChanged(object sender, EventArgs e) {
266      if (suppressUpdates) return;
[8925]267      RebuildCombinedDataTable();
[7980]268    }
269  }
270}
Note: See TracBrowser for help on using the repository browser.