Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Analysis/3.3/DataVisualization/DataTableValuesCollector.cs @ 12344

Last change on this file since 12344 was 7259, checked in by swagner, 13 years ago

Updated year of copyrights to 2012 (#1716)

File size: 5.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Operators;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Analysis {
32  /// <summary>
33  /// An operator which collects the actual values of parameters and adds them to a table of data values.
34  /// </summary>
35  [Item("DataTableValuesCollector", "An operator which collects the actual values of parameters and adds them to a table of data values.")]
36  [StorableClass]
37  public class DataTableValuesCollector : ValuesCollector {
38    public ValueLookupParameter<DataTable> DataTableParameter {
39      get { return (ValueLookupParameter<DataTable>)Parameters["DataTable"]; }
40    }
41    private IFixedValueParameter<BoolValue> StartIndexZeroParameter {
42      get { return (FixedValueParameter<BoolValue>)Parameters["StartIndexZero"]; }
43    }
44
45    public bool StartIndexZero {
46      get { return StartIndexZeroParameter.Value.Value; }
47      set { StartIndexZeroParameter.Value.Value = value; }
48    }
49
50    #region Storing & Cloning
51    [StorableConstructor]
52    protected DataTableValuesCollector(bool deserializing) : base(deserializing) { }
53    protected DataTableValuesCollector(DataTableValuesCollector original, Cloner cloner) : base(original, cloner) { }
54    public override IDeepCloneable Clone(Cloner cloner) {
55      return new DataTableValuesCollector(this, cloner);
56    }
57    #endregion
58    public DataTableValuesCollector()
59      : base() {
60      Parameters.Add(new ValueLookupParameter<DataTable>("DataTable", "The table of data values where the collected values should be stored."));
61      Parameters.Add(new FixedValueParameter<BoolValue>("StartIndexZero", "True, if the collected data values should start with index 0, otherwise false.", new BoolValue(true), false));
62      StartIndexZeroParameter.Hidden = true;
63    }
64
65    [StorableHook(HookType.AfterDeserialization)]
66    private void AfterDeserialization() {
67      // BackwardsCompatibility3.3
68      #region Backwards compatible code (remove with 3.4)
69      if (!Parameters.ContainsKey("StartIndexZero")) {
70        Parameters.Add(new FixedValueParameter<BoolValue>("StartIndexZero", "True, if the collected data values should start with index 0, otherwise false.", new BoolValue(true), false));
71        StartIndexZeroParameter.Hidden = true;
72      }
73      #endregion
74    }
75
76    public override IOperation Apply() {
77      DataTable table = DataTableParameter.ActualValue;
78      if (table == null) {
79        table = new DataTable(DataTableParameter.ActualName);
80        DataTableParameter.ActualValue = table;
81      }
82
83      foreach (IParameter param in CollectedValues) {
84        ILookupParameter lookupParam = param as ILookupParameter;
85        string name = lookupParam != null ? lookupParam.TranslatedName : param.Name;
86
87        if (param.ActualValue is DoubleValue) {
88          AddValue(table, (param.ActualValue as DoubleValue).Value, name, param.Description);
89        } else if (param.ActualValue is IntValue) {
90          AddValue(table, (param.ActualValue as IntValue).Value, name, param.Description);
91        } else if (param.ActualValue is IEnumerable<DoubleValue>) {
92          IEnumerable<DoubleValue> values = (IEnumerable<DoubleValue>)param.ActualValue;
93          if (values.Count() <= 1) {
94            foreach (DoubleValue data in values)
95              AddValue(table, data != null ? data.Value : double.NaN, name, param.Description);
96          } else {
97            int counter = 1;
98            foreach (DoubleValue data in values) {
99              AddValue(table, data != null ? data.Value : double.NaN, name + " " + counter.ToString(), param.Description);
100              counter++;
101            }
102          }
103        } else if (param.ActualValue is IEnumerable<IntValue>) {
104          IEnumerable<IntValue> values = (IEnumerable<IntValue>)param.ActualValue;
105          if (values.Count() <= 1) {
106            foreach (IntValue data in values)
107              AddValue(table, data != null ? data.Value : double.NaN, name, param.Description);
108          } else {
109            int counter = 1;
110            foreach (IntValue data in values) {
111              AddValue(table, data != null ? data.Value : double.NaN, name + " " + counter.ToString(), param.Description);
112              counter++;
113            }
114          }
115        } else {
116          AddValue(table, double.NaN, name, param.Description);
117        }
118      }
119      return base.Apply();
120    }
121
122    private void AddValue(DataTable table, double data, string name, string description) {
123      DataRow row;
124      table.Rows.TryGetValue(name, out row);
125      if (row == null) {
126        row = new DataRow(name, description);
127        row.VisualProperties.StartIndexZero = StartIndexZero;
128        row.Values.Add(data);
129        table.Rows.Add(row);
130      } else {
131        row.Values.Add(data);
132      }
133    }
134  }
135}
Note: See TracBrowser for help on using the repository browser.