Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2915-AbsoluteSymbol/HeuristicLab.Analysis/3.3/DataVisualization/IndexedDataRow.cs @ 16662

Last change on this file since 16662 was 16240, checked in by gkronber, 6 years ago

#2915: merged changes in the trunk up to current HEAD (r15951:16232) into the branch

File size: 5.3 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.ComponentModel;
25using System.Linq;
26using HeuristicLab.Collections;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Analysis {
32  [Item("IndexedDataRow", "A data row that contains a series of points.")]
33  [StorableClass]
34  public class IndexedDataRow<T> : NamedItem {
35
36    private DataRowVisualProperties visualProperties;
37    public DataRowVisualProperties VisualProperties {
38      get { return visualProperties; }
39      set {
40        if (visualProperties != value) {
41          if (value == null) throw new ArgumentNullException("VisualProperties");
42          if (visualProperties != null) visualProperties.PropertyChanged -= new PropertyChangedEventHandler(VisualProperties_PropertyChanged);
43          visualProperties = value;
44          visualProperties.PropertyChanged += new PropertyChangedEventHandler(VisualProperties_PropertyChanged);
45          OnVisualPropertiesChanged();
46        }
47      }
48    }
49    private ObservableList<Tuple<T, double>> values;
50    public ObservableList<Tuple<T, double>> Values {
51      get { return values; }
52    }
53
54    #region Persistence Properties
55    [Storable(Name = "visualProperties")]
56    private DataRowVisualProperties StorableVisualProperties {
57      get { return visualProperties; }
58      set { visualProperties = value; }
59    }
60    // BackwardsCompatibility3.3
61    #region Backwards compatible code, remove with 3.4
62    // tuples are stored inefficiently
63    [Storable(Name = "values", AllowOneWay = true)]
64    private IEnumerable<Tuple<T, double>> StorableValues {
65      set { values = new ObservableList<Tuple<T, double>>(value); }
66    }
67    #endregion
68    private T[] storableX;
69    [Storable(Name = "x")]
70    private T[] StorableX {
71      get { return Values.Select(x => x.Item1).ToArray(); }
72      set { storableX = value; }
73    }
74    private double[] storableY;
75    [Storable(Name = "y")]
76    private double[] StorableY {
77      get { return Values.Select(x => x.Item2).ToArray(); }
78      set { storableY = value; }
79    }
80    #endregion
81
82    [StorableConstructor]
83    protected IndexedDataRow(bool deserializing) : base(deserializing) { }
84    protected IndexedDataRow(IndexedDataRow<T> original, Cloner cloner)
85      : base(original, cloner) {
86      values = new ObservableList<Tuple<T, double>>(original.values.Select(x => Tuple.Create<T, double>(x.Item1, x.Item2)).ToList());
87      VisualProperties = cloner.Clone(original.visualProperties);
88    }
89    public IndexedDataRow() {
90      values = new ObservableList<Tuple<T, double>>();
91      VisualProperties = new DataRowVisualProperties();
92    }
93    public IndexedDataRow(string name)
94      : base(name) {
95      values = new ObservableList<Tuple<T, double>>();
96      VisualProperties = new DataRowVisualProperties(name);
97    }
98    public IndexedDataRow(string name, string description)
99      : base(name, description) {
100      values = new ObservableList<Tuple<T, double>>();
101      VisualProperties = new DataRowVisualProperties(name);
102    }
103    public IndexedDataRow(string name, string description, IEnumerable<Tuple<T, double>> values)
104      : base(name, description) {
105      this.values = new ObservableList<Tuple<T, double>>(values);
106      VisualProperties = new DataRowVisualProperties(name);
107    }
108
109    public override IDeepCloneable Clone(Cloner cloner) {
110      return new IndexedDataRow<T>(this, cloner);
111    }
112
113    public event EventHandler VisualPropertiesChanged;
114    protected virtual void OnVisualPropertiesChanged() {
115      EventHandler handler = VisualPropertiesChanged;
116      if (handler != null) handler(this, EventArgs.Empty);
117    }
118
119    private void VisualProperties_PropertyChanged(object sender, PropertyChangedEventArgs e) {
120      OnVisualPropertiesChanged();
121    }
122    protected override void OnNameChanged() {
123      base.OnNameChanged();
124      VisualProperties.DisplayName = Name;
125    }
126
127    [StorableHook(HookType.AfterDeserialization)]
128    private void AfterDeserialization() {
129      if (storableX != null && storableY != null) {
130        values = new ObservableList<Tuple<T, double>>(storableX.Zip(storableY, (x, y) => Tuple.Create(x, y)));
131        storableX = null;
132        storableY = null;
133      } else if (values == null) throw new InvalidOperationException("Deserialization problem with IndexedDataRow.");
134    }
135  }
136}
Note: See TracBrowser for help on using the repository browser.