Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14399 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 4.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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        var children = GetCollectedValues(param);
88        foreach (var c in children) {
89          if (String.IsNullOrEmpty(c.Key))
90            values.Add(param.Name, c.Value);
91          else values.Add(param.Name + "." + c.Key, c.Value);
92        }
93      }
94    }
95
96    protected virtual IEnumerable<KeyValuePair<string, IItem>> GetCollectedValues(IValueParameter param) {
97      if (param.Value == null) yield break;
98      if (param.GetsCollected) yield return new KeyValuePair<string, IItem>(String.Empty, param.Value);
99      var parameterizedItem = param.Value as IParameterizedItem;
100      if (parameterizedItem != null) {
101        var children = new Dictionary<string, IItem>();
102        parameterizedItem.CollectParameterValues(children);
103        foreach (var child in children) yield return child;
104      }
105    }
106  }
107}
Note: See TracBrowser for help on using the repository browser.