Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Analysis/3.3/DataTableValuesCollector.cs @ 4040

Last change on this file since 4040 was 3818, checked in by swagner, 15 years ago

Implemented reviewers' comments (#893)

  • enabled DataTableValuesCollector to collect int values
File size: 4.3 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;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Operators;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Analysis {
33  /// <summary>
34  /// An operator which collects the actual values of parameters and adds them to a table of data values.
35  /// </summary>
36  [Item("DataTableValuesCollector", "An operator which collects the actual values of parameters and adds them to a table of data values.")]
37  [StorableClass]
38  public class DataTableValuesCollector : ValuesCollector {
39    public ValueLookupParameter<DataTable> DataTableParameter {
40      get { return (ValueLookupParameter<DataTable>)Parameters["DataTable"]; }
41    }
42
43    public DataTableValuesCollector()
44      : base() {
45      Parameters.Add(new ValueLookupParameter<DataTable>("DataTable", "The table of data values where the collected values should be stored."));
46    }
47
48    public override IOperation Apply() {
49      DataTable table = DataTableParameter.ActualValue;
50      if (table == null) {
51        table = new DataTable(DataTableParameter.ActualName);
52        DataTableParameter.ActualValue = table;
53      }
54
55      foreach (IParameter param in CollectedValues) {
56        ILookupParameter lookupParam = param as ILookupParameter;
57        string name = lookupParam != null ? lookupParam.TranslatedName : param.Name;
58
59        if (param.ActualValue is DoubleValue) {
60          AddValue(table, (param.ActualValue as DoubleValue).Value, name, param.Description);
61        } else if (param.ActualValue is IntValue) {
62          AddValue(table, (param.ActualValue as IntValue).Value, name, param.Description);
63        } else if (param.ActualValue is IEnumerable<DoubleValue>) {
64          IEnumerable<DoubleValue> values = (IEnumerable<DoubleValue>)param.ActualValue;
65          if (values.Count() <= 1) {
66            foreach (DoubleValue data in values)
67              AddValue(table, data != null ? data.Value : double.NaN, name, param.Description);
68          } else {
69            int counter = 1;
70            foreach (DoubleValue data in values) {
71              AddValue(table, data != null ? data.Value : double.NaN, name + " " + counter.ToString(), param.Description);
72              counter++;
73            }
74          }
75        } else if (param.ActualValue is IEnumerable<IntValue>) {
76          IEnumerable<IntValue> values = (IEnumerable<IntValue>)param.ActualValue;
77          if (values.Count() <= 1) {
78            foreach (IntValue data in values)
79              AddValue(table, data != null ? data.Value : double.NaN, name, param.Description);
80          } else {
81            int counter = 1;
82            foreach (IntValue data in values) {
83              AddValue(table, data != null ? data.Value : double.NaN, name + " " + counter.ToString(), param.Description);
84              counter++;
85            }
86          }
87        } else {
88          AddValue(table, double.NaN, name, param.Description);
89        }
90      }
91      return base.Apply();
92    }
93
94    private void AddValue(DataTable table, double data, string name, string description) {
95      DataRow row;
96      table.Rows.TryGetValue(name, out row);
97      if (row == null) {
98        row = new DataRow(name, description);
99        row.Values.Add(data);
100        table.Rows.Add(row);
101      } else {
102        row.Values.Add(data);
103      }
104    }
105  }
106}
Note: See TracBrowser for help on using the repository browser.