Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/3.3/ParameterizedNamedItem.cs @ 7695

Last change on this file since 7695 was 7579, checked in by abeham, 13 years ago

#1695
I solved this issue now. I found that CollectParameterValues was too monolithic in that you have to overwrite and re-implement the same method again if you wanted to change just a detail (in that case that operators are stored by their name). So I split CollectParameterValues into two separate logical parts:

  • CollectParameterValues is iterating over the parameters
  • GetCollectedValues decides what values are collected from the given parameter value

Algorithm and Problem now overwrite only GetCollectedValues, but reuse the implementation of the base class in that they only filter the values. When they see an IOperator they will instead convert it to its name. Using IEnumerable and yield I think that's a nice solution.

File size: 4.1 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;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Core {
29  /// <summary>
30  /// A base class for items which have a name and contain parameters.
31  /// </summary>
32  [Item("ParameterizedNamedItem", "A base class for items which have a name and contain parameters.")]
33  [StorableClass]
34  public abstract class ParameterizedNamedItem : NamedItem, IParameterizedNamedItem {
35    [Storable]
36    private ParameterCollection parameters;
37    protected ParameterCollection Parameters {
38      get { return parameters; }
39    }
40    private ReadOnlyKeyedItemCollection<string, IParameter> readOnlyParameters;
41    IKeyedItemCollection<string, IParameter> IParameterizedItem.Parameters {
42      get {
43        if (readOnlyParameters == null) readOnlyParameters = parameters.AsReadOnly();
44        return readOnlyParameters;
45      }
46    }
47
48    [StorableConstructor]
49    protected ParameterizedNamedItem(bool deserializing) : base(deserializing) { }
50    protected ParameterizedNamedItem(ParameterizedNamedItem original, Cloner cloner)
51      : base(original, cloner) {
52      parameters = cloner.Clone(original.parameters);
53      readOnlyParameters = null;
54    }
55    protected ParameterizedNamedItem()
56      : base() {
57      name = ItemName;
58      description = ItemDescription;
59      parameters = new ParameterCollection();
60      readOnlyParameters = null;
61    }
62    protected ParameterizedNamedItem(string name)
63      : base(name) {
64      description = ItemDescription;
65      parameters = new ParameterCollection();
66      readOnlyParameters = null;
67    }
68    protected ParameterizedNamedItem(string name, ParameterCollection parameters)
69      : base(name) {
70      description = ItemDescription;
71      this.parameters = parameters;
72      readOnlyParameters = null;
73    }
74    protected ParameterizedNamedItem(string name, string description)
75      : base(name, description) {
76      parameters = new ParameterCollection();
77      readOnlyParameters = null;
78    }
79    protected ParameterizedNamedItem(string name, string description, ParameterCollection parameters)
80      : base(name, description) {
81      this.parameters = parameters;
82      readOnlyParameters = null;
83    }
84
85    public virtual void CollectParameterValues(IDictionary<string, IItem> values) {
86      foreach (IValueParameter param in parameters.OfType<IValueParameter>()) {
87        if (param.GetsCollected) {
88          var children = GetCollectedValues(param.Value);
89          foreach (var c in children) {
90            if (String.IsNullOrEmpty(c.Key))
91              values.Add(param.Name, c.Value);
92            else values.Add(param.Name + "." + c.Key, c.Value);
93          }
94        }
95      }
96    }
97
98    protected virtual IEnumerable<KeyValuePair<string, IItem>> GetCollectedValues(IItem value) {
99      if (value == null) yield break;
100      yield return new KeyValuePair<string, IItem>(String.Empty, value);
101      var parameterizedItem = value as IParameterizedItem;
102      if (parameterizedItem != null) {
103        var children = new Dictionary<string, IItem>();
104        parameterizedItem.CollectParameterValues(children);
105        foreach (var child in children) yield return child;
106      }
107    }
108  }
109}
Note: See TracBrowser for help on using the repository browser.