Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Core/3.3/ParameterizedNamedItem.cs @ 4787

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

#1258

  • Updated binding according to discussion
  • Added small test case for the TSP
File size: 5.3 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.Common;
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 List<IItemBinding> parameterBindingList;
36    public List<IItemBinding> ParameterBindingList {
37      get { return parameterBindingList; }
38    }
39    [Storable]
40    private ParameterCollection parameters;
41    protected ParameterCollection Parameters {
42      get { return parameters; }
43    }
44    private ReadOnlyKeyedItemCollection<string, IParameter> readOnlyParameters;
45    IKeyedItemCollection<string, IParameter> IParameterizedItem.Parameters {
46      get {
47        if (readOnlyParameters == null) readOnlyParameters = parameters.AsReadOnly();
48        return readOnlyParameters;
49      }
50    }
51
52    [StorableConstructor]
53    protected ParameterizedNamedItem(bool deserializing) : base(deserializing) { }
54    protected ParameterizedNamedItem(ParameterizedNamedItem original, Cloner cloner)
55      : base(original, cloner) {
56      parameters = cloner.Clone(original.parameters);
57      parameterBindingList = original.parameterBindingList.Select(x => cloner.Clone(x)).ToList();
58      readOnlyParameters = null;
59    }
60    protected ParameterizedNamedItem()
61      : base() {
62      name = ItemName;
63      description = ItemDescription;
64      parameterBindingList = new List<IItemBinding>();
65      parameters = new ParameterCollection();
66      readOnlyParameters = null;
67    }
68    protected ParameterizedNamedItem(string name)
69      : base(name) {
70      description = ItemDescription;
71      parameterBindingList = new List<IItemBinding>();
72      parameters = new ParameterCollection();
73      readOnlyParameters = null;
74    }
75    protected ParameterizedNamedItem(string name, ParameterCollection parameters)
76      : base(name) {
77      description = ItemDescription;
78      parameterBindingList = new List<IItemBinding>();
79      this.parameters = parameters;
80      readOnlyParameters = null;
81    }
82    protected ParameterizedNamedItem(string name, string description)
83      : base(name, description) {
84      parameterBindingList = new List<IItemBinding>();
85      parameters = new ParameterCollection();
86      readOnlyParameters = null;
87    }
88    protected ParameterizedNamedItem(string name, string description, ParameterCollection parameters)
89      : base(name, description) {
90      parameterBindingList = new List<IItemBinding>();
91      this.parameters = parameters;
92      readOnlyParameters = null;
93    }
94
95    public virtual void CollectParameterValues(IDictionary<string, IItem> values) {
96      foreach (IValueParameter param in parameters.OfType<IValueParameter>()) {
97        if (param.GetsCollected && param.Value != null) values.Add(param.Name, param.Value);
98        if (param.Value is IParameterizedItem) {
99          Dictionary<string, IItem> children = new Dictionary<string, IItem>();
100          ((IParameterizedItem)param.Value).CollectParameterValues(children);
101          foreach (string key in children.Keys)
102            values.Add(param.Name + "." + key, children[key]);
103        }
104      }
105    }
106
107    protected virtual void AddBinding(string targetPath, string sourcePath) {
108      ItemBinding binding = new ItemBinding(this, targetPath, this, sourcePath);
109      parameterBindingList.Add(binding);
110      binding.Bind();
111    }
112
113    protected virtual void AddSourceBinding(IDeepCloneable target, string targetPath, string sourcePath) {
114      ItemBinding binding = new ItemBinding(target, targetPath, this, sourcePath);
115      parameterBindingList.Add(binding);
116      binding.Bind();
117    }
118
119    protected virtual void AddTargetBinding(string targetPath, IDeepCloneable source, string sourcePath) {
120      ItemBinding binding = new ItemBinding(this, targetPath, source, sourcePath);
121      parameterBindingList.Add(binding);
122      binding.Bind();
123    }
124
125    [StorableHook(HookType.AfterDeserialization)]
126    private void AfterDeserialization() {
127      //BackwardsCompatibility3.3
128      #region Remove this code when going to 3.4
129      if (parameterBindingList == null)
130        parameterBindingList = new List<IItemBinding>();
131      #endregion
132    }
133  }
134}
Note: See TracBrowser for help on using the repository browser.