Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Analysis/3.3/DataVisualization/DataTableValuesCollector.cs @ 16811

Last change on this file since 16811 was 16565, checked in by gkronber, 6 years ago

#2520: merged changes from PersistenceOverhaul branch (r16451:16564) into trunk

File size: 5.8 KB
RevLine 
[2908]1#region License Information
2/* HeuristicLab
[16565]3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2908]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
[3489]22using System.Collections.Generic;
[3690]23using System.Linq;
[4722]24using HeuristicLab.Common;
[2908]25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Operators;
28using HeuristicLab.Parameters;
[16565]29using HEAL.Attic;
[2908]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.")]
[16565]36  [StorableType("B3409421-D7F0-4C79-B8F8-B7E77E7C554F")]
[2908]37  public class DataTableValuesCollector : ValuesCollector {
38    public ValueLookupParameter<DataTable> DataTableParameter {
39      get { return (ValueLookupParameter<DataTable>)Parameters["DataTable"]; }
40    }
[5877]41    private IFixedValueParameter<BoolValue> StartIndexZeroParameter {
42      get { return (FixedValueParameter<BoolValue>)Parameters["StartIndexZero"]; }
43    }
[2908]44
[5877]45    public bool StartIndexZero {
46      get { return StartIndexZeroParameter.Value.Value; }
47      set { StartIndexZeroParameter.Value.Value = value; }
48    }
49
[4722]50    #region Storing & Cloning
51    [StorableConstructor]
[16565]52    protected DataTableValuesCollector(StorableConstructorFlag _) : base(_) { }
[4722]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
[2908]58    public DataTableValuesCollector()
59      : base() {
60      Parameters.Add(new ValueLookupParameter<DataTable>("DataTable", "The table of data values where the collected values should be stored."));
[5877]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;
[2908]63    }
64
[5877]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
[2908]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) {
[3687]84        ILookupParameter lookupParam = param as ILookupParameter;
85        string name = lookupParam != null ? lookupParam.TranslatedName : param.Name;
86
[3479]87        if (param.ActualValue is DoubleValue) {
[3687]88          AddValue(table, (param.ActualValue as DoubleValue).Value, name, param.Description);
[3818]89        } else if (param.ActualValue is IntValue) {
90          AddValue(table, (param.ActualValue as IntValue).Value, name, param.Description);
[3489]91        } else if (param.ActualValue is IEnumerable<DoubleValue>) {
[3690]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            }
[3479]102          }
[3818]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          }
[2908]115        } else {
[3687]116          AddValue(table, double.NaN, name, param.Description);
[2908]117        }
118      }
119      return base.Apply();
120    }
[3489]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);
[5877]127        row.VisualProperties.StartIndexZero = StartIndexZero;
[3489]128        row.Values.Add(data);
129        table.Rows.Add(row);
130      } else {
131        row.Values.Add(data);
132      }
133    }
[2908]134  }
135}
Note: See TracBrowser for help on using the repository browser.