Free cookie consent management tool by TermsFeed Policy Generator

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

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

Continued work on algorithm batch processing (#947).

File size: 3.8 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.Collections;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26
27namespace HeuristicLab.Core {
28  /// <summary>
29  /// A base class for items which have a name and contain parameters.
30  /// </summary>
31  [Item("ParameterizedNamedItem", "A base class for items which have a name and contain parameters.")]
32  [StorableClass]
33  public abstract class ParameterizedNamedItem : NamedItem, IParameterizedNamedItem {
34    [Storable]
35    private ParameterCollection parameters;
36    protected ParameterCollection Parameters {
37      get { return parameters; }
38    }
39    private ReadOnlyObservableKeyedCollection<string, IParameter> readOnlyParameters;
40    IObservableKeyedCollection<string, IParameter> IParameterizedItem.Parameters {
41      get {
42        if (readOnlyParameters == null) readOnlyParameters = parameters.AsReadOnly();
43        return readOnlyParameters;
44      }
45    }
46
47    protected ParameterizedNamedItem()
48      : base() {
49      name = ItemName;
50      description = ItemDescription;
51      parameters = new ParameterCollection();
52      readOnlyParameters = null;
53    }
54    protected ParameterizedNamedItem(string name)
55      : base(name) {
56      description = ItemDescription;
57      parameters = new ParameterCollection();
58      readOnlyParameters = null;
59    }
60    protected ParameterizedNamedItem(string name, ParameterCollection parameters)
61      : base(name) {
62      description = ItemDescription;
63      this.parameters = parameters;
64      readOnlyParameters = null;
65    }
66    protected ParameterizedNamedItem(string name, string description)
67      : base(name, description) {
68      parameters = new ParameterCollection();
69      readOnlyParameters = null;
70    }
71    protected ParameterizedNamedItem(string name, string description, ParameterCollection parameters)
72      : base(name, description) {
73      this.parameters = parameters;
74      readOnlyParameters = null;
75    }
76    [StorableConstructor]
77    protected ParameterizedNamedItem(bool deserializing) : base(deserializing) { }
78
79    public override IDeepCloneable Clone(Cloner cloner) {
80      ParameterizedNamedItem clone = (ParameterizedNamedItem)base.Clone(cloner);
81      clone.parameters = (ParameterCollection)cloner.Clone(parameters);
82      clone.readOnlyParameters = null;
83      return clone;
84    }
85
86    public virtual void CollectParameterValues(IDictionary<string, IItem> values) {
87      foreach (IValueParameter param in parameters.OfType<IValueParameter>()) {
88        values.Add(param.Name, param.Value);
89        if (param.Value is IParameterizedItem) {
90          Dictionary<string, IItem> children = new Dictionary<string, IItem>();
91          ((IParameterizedItem)param.Value).CollectParameterValues(children);
92          foreach (string key in children.Keys)
93            values.Add(param.Name + "." + key, children[key]);
94        }
95      }
96    }
97  }
98}
Note: See TracBrowser for help on using the repository browser.