Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3260 was 3260, checked in by swagner, 14 years ago

Continued work on algorithm batch processing (#947).

File size: 3.7 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    private ParameterCollection parameters;
35    [Storable]
36    protected ParameterCollection Parameters {
37      get { return parameters; }
38      private set {
39        parameters = value;
40        readOnlyParameters = null;
41      }
42    }
43    private ReadOnlyObservableKeyedCollection<string, IParameter> readOnlyParameters;
44    IObservableKeyedCollection<string, IParameter> IParameterizedItem.Parameters {
45      get {
46        if (readOnlyParameters == null) readOnlyParameters = parameters.AsReadOnly();
47        return readOnlyParameters;
48      }
49    }
50
51    protected ParameterizedNamedItem()
52      : base() {
53      name = ItemName;
54      description = ItemDescription;
55      Parameters = new ParameterCollection();
56      readOnlyParameters = null;
57    }
58    protected ParameterizedNamedItem(string name)
59      : base(name) {
60      description = ItemDescription;
61      Parameters = new ParameterCollection();
62      readOnlyParameters = null;
63    }
64    protected ParameterizedNamedItem(string name, ParameterCollection parameters)
65      : base(name) {
66      description = ItemDescription;
67      Parameters = parameters;
68      readOnlyParameters = null;
69    }
70    protected ParameterizedNamedItem(string name, string description)
71      : base(name, description) {
72      Parameters = new ParameterCollection();
73      readOnlyParameters = null;
74    }
75    protected ParameterizedNamedItem(string name, string description, ParameterCollection parameters)
76      : base(name, description) {
77      Parameters = parameters;
78      readOnlyParameters = null;
79    }
80
81    public override IDeepCloneable Clone(Cloner cloner) {
82      ParameterizedNamedItem clone = (ParameterizedNamedItem)base.Clone(cloner);
83      clone.Parameters = (ParameterCollection)cloner.Clone(parameters);
84      return clone;
85    }
86
87    public virtual void CollectParameterValues(IDictionary<string, IItem> values) {
88      foreach (IValueParameter param in parameters.OfType<IValueParameter>()) {
89        values.Add(param.Name, param.Value != null ? (IItem)param.Value.Clone() : null);
90        if (param.Value is IParameterizedItem) {
91          Dictionary<string, IItem> children = new Dictionary<string, IItem>();
92          ((IParameterizedItem)param.Value).CollectParameterValues(children);
93          foreach (string key in children.Keys)
94            values.Add(param.Name + "." + key, children[key]);
95        }
96      }
97    }
98  }
99}
Note: See TracBrowser for help on using the repository browser.