Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5046 was 4790, checked in by abeham, 14 years ago

#1258

  • Added detection if a certain link in the chain implements INotifyPropertyChanged (still missing -> fire only on a change to the "right" property)
  • Added optional parameter LambdaExpression in the binding
  • Changed cloning behavior of binding -> bindings have to be cloned only after the clone is fully constructed
File size: 5.5 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;
26using System.Linq.Expressions;
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    protected List<IItemBinding> parameterBindingList;
37    public List<IItemBinding> ParameterBindingList {
38      get { return parameterBindingList; }
39    }
40    [Storable]
41    private ParameterCollection parameters;
42    protected ParameterCollection Parameters {
43      get { return parameters; }
44    }
45    private ReadOnlyKeyedItemCollection<string, IParameter> readOnlyParameters;
46    IKeyedItemCollection<string, IParameter> IParameterizedItem.Parameters {
47      get {
48        if (readOnlyParameters == null) readOnlyParameters = parameters.AsReadOnly();
49        return readOnlyParameters;
50      }
51    }
52
53    [StorableConstructor]
54    protected ParameterizedNamedItem(bool deserializing) : base(deserializing) { }
55    protected ParameterizedNamedItem(ParameterizedNamedItem original, Cloner cloner)
56      : base(original, cloner) {
57      parameters = cloner.Clone(original.parameters);
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 AddBinding(string targetPath, string sourcePath, LambdaExpression func) {
114      ItemBinding binding = new ItemBinding(this, targetPath, this, sourcePath, func);
115      parameterBindingList.Add(binding);
116      binding.Bind();
117    }
118
119    protected virtual void AddSourceBinding(IDeepCloneable target, string targetPath, string sourcePath) {
120      ItemBinding binding = new ItemBinding(target, targetPath, this, sourcePath);
121      parameterBindingList.Add(binding);
122      binding.Bind();
123    }
124
125    protected virtual void AddTargetBinding(string targetPath, IDeepCloneable source, string sourcePath) {
126      ItemBinding binding = new ItemBinding(this, targetPath, source, sourcePath);
127      parameterBindingList.Add(binding);
128      binding.Bind();
129    }
130
131    [StorableHook(HookType.AfterDeserialization)]
132    private void AfterDeserialization() {
133      //BackwardsCompatibility3.3
134      #region Remove this code when going to 3.4
135      if (parameterBindingList == null)
136        parameterBindingList = new List<IItemBinding>();
137      #endregion
138    }
139  }
140}
Note: See TracBrowser for help on using the repository browser.