Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Core/3.3/ParameterizedNamedItem.cs @ 16755

Last change on this file since 16755 was 15584, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers on stable

File size: 4.1 KB
RevLine 
[2845]1#region License Information
2/* HeuristicLab
[15584]3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2845]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
[7579]22using System;
[3260]23using System.Collections.Generic;
24using System.Linq;
[3376]25using HeuristicLab.Common;
[2845]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.")]
[3017]33  [StorableClass]
[2845]34  public abstract class ParameterizedNamedItem : NamedItem, IParameterizedNamedItem {
[3280]35    [Storable]
[2845]36    private ParameterCollection parameters;
37    protected ParameterCollection Parameters {
38      get { return parameters; }
39    }
[3393]40    private ReadOnlyKeyedItemCollection<string, IParameter> readOnlyParameters;
41    IKeyedItemCollection<string, IParameter> IParameterizedItem.Parameters {
[2845]42      get {
43        if (readOnlyParameters == null) readOnlyParameters = parameters.AsReadOnly();
44        return readOnlyParameters;
45      }
46    }
47
[4722]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    }
[2851]55    protected ParameterizedNamedItem()
56      : base() {
57      name = ItemName;
58      description = ItemDescription;
[3280]59      parameters = new ParameterCollection();
[2845]60      readOnlyParameters = null;
61    }
62    protected ParameterizedNamedItem(string name)
63      : base(name) {
[2851]64      description = ItemDescription;
[3280]65      parameters = new ParameterCollection();
[2845]66      readOnlyParameters = null;
67    }
68    protected ParameterizedNamedItem(string name, ParameterCollection parameters)
69      : base(name) {
[2851]70      description = ItemDescription;
[3280]71      this.parameters = parameters;
[2845]72      readOnlyParameters = null;
73    }
74    protected ParameterizedNamedItem(string name, string description)
75      : base(name, description) {
[3280]76      parameters = new ParameterCollection();
[2845]77      readOnlyParameters = null;
78    }
79    protected ParameterizedNamedItem(string name, string description, ParameterCollection parameters)
80      : base(name, description) {
[3280]81      this.parameters = parameters;
[2845]82      readOnlyParameters = null;
83    }
84
[3260]85    public virtual void CollectParameterValues(IDictionary<string, IItem> values) {
86      foreach (IValueParameter param in parameters.OfType<IValueParameter>()) {
[7706]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);
[3260]92        }
93      }
94    }
[7579]95
[7706]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;
[7579]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    }
[2845]106  }
107}
Note: See TracBrowser for help on using the repository browser.