Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Analysis/3.3/DataTableValuesCollector.cs @ 8959

Last change on this file since 8959 was 4722, checked in by swagner, 14 years ago

Merged cloning refactoring branch back into trunk (#922)

File size: 4.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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
42    #region Storing & Cloning
43    [StorableConstructor]
44    protected DataTableValuesCollector(bool deserializing) : base(deserializing) { }
45    protected DataTableValuesCollector(DataTableValuesCollector original, Cloner cloner) : base(original, cloner) { }
46    public override IDeepCloneable Clone(Cloner cloner) {
47      return new DataTableValuesCollector(this, cloner);
48    }
49    #endregion
50    public DataTableValuesCollector()
51      : base() {
52      Parameters.Add(new ValueLookupParameter<DataTable>("DataTable", "The table of data values where the collected values should be stored."));
53    }
54
55    public override IOperation Apply() {
56      DataTable table = DataTableParameter.ActualValue;
57      if (table == null) {
58        table = new DataTable(DataTableParameter.ActualName);
59        DataTableParameter.ActualValue = table;
60      }
61
62      foreach (IParameter param in CollectedValues) {
63        ILookupParameter lookupParam = param as ILookupParameter;
64        string name = lookupParam != null ? lookupParam.TranslatedName : param.Name;
65
66        if (param.ActualValue is DoubleValue) {
67          AddValue(table, (param.ActualValue as DoubleValue).Value, name, param.Description);
68        } else if (param.ActualValue is IntValue) {
69          AddValue(table, (param.ActualValue as IntValue).Value, name, param.Description);
70        } else if (param.ActualValue is IEnumerable<DoubleValue>) {
71          IEnumerable<DoubleValue> values = (IEnumerable<DoubleValue>)param.ActualValue;
72          if (values.Count() <= 1) {
73            foreach (DoubleValue data in values)
74              AddValue(table, data != null ? data.Value : double.NaN, name, param.Description);
75          } else {
76            int counter = 1;
77            foreach (DoubleValue data in values) {
78              AddValue(table, data != null ? data.Value : double.NaN, name + " " + counter.ToString(), param.Description);
79              counter++;
80            }
81          }
82        } else if (param.ActualValue is IEnumerable<IntValue>) {
83          IEnumerable<IntValue> values = (IEnumerable<IntValue>)param.ActualValue;
84          if (values.Count() <= 1) {
85            foreach (IntValue data in values)
86              AddValue(table, data != null ? data.Value : double.NaN, name, param.Description);
87          } else {
88            int counter = 1;
89            foreach (IntValue data in values) {
90              AddValue(table, data != null ? data.Value : double.NaN, name + " " + counter.ToString(), param.Description);
91              counter++;
92            }
93          }
94        } else {
95          AddValue(table, double.NaN, name, param.Description);
96        }
97      }
98      return base.Apply();
99    }
100
101    private void AddValue(DataTable table, double data, string name, string description) {
102      DataRow row;
103      table.Rows.TryGetValue(name, out row);
104      if (row == null) {
105        row = new DataRow(name, description);
106        row.Values.Add(data);
107        table.Rows.Add(row);
108      } else {
109        row.Values.Add(data);
110      }
111    }
112  }
113}
Note: See TracBrowser for help on using the repository browser.