[2845] | 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 |
|
---|
[3260] | 22 | using System.Collections.Generic;
|
---|
| 23 | using System.Linq;
|
---|
[3376] | 24 | using HeuristicLab.Common;
|
---|
[2845] | 25 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[4790] | 26 | using System.Linq.Expressions;
|
---|
[2845] | 27 |
|
---|
| 28 | namespace 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]
|
---|
[4790] | 36 | protected List<IItemBinding> parameterBindingList;
|
---|
[4787] | 37 | public List<IItemBinding> ParameterBindingList {
|
---|
[4757] | 38 | get { return parameterBindingList; }
|
---|
| 39 | }
|
---|
| 40 | [Storable]
|
---|
[2845] | 41 | private ParameterCollection parameters;
|
---|
| 42 | protected ParameterCollection Parameters {
|
---|
| 43 | get { return parameters; }
|
---|
| 44 | }
|
---|
[3393] | 45 | private ReadOnlyKeyedItemCollection<string, IParameter> readOnlyParameters;
|
---|
| 46 | IKeyedItemCollection<string, IParameter> IParameterizedItem.Parameters {
|
---|
[2845] | 47 | get {
|
---|
| 48 | if (readOnlyParameters == null) readOnlyParameters = parameters.AsReadOnly();
|
---|
| 49 | return readOnlyParameters;
|
---|
| 50 | }
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[4722] | 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 | }
|
---|
[2851] | 60 | protected ParameterizedNamedItem()
|
---|
| 61 | : base() {
|
---|
| 62 | name = ItemName;
|
---|
| 63 | description = ItemDescription;
|
---|
[4787] | 64 | parameterBindingList = new List<IItemBinding>();
|
---|
[3280] | 65 | parameters = new ParameterCollection();
|
---|
[2845] | 66 | readOnlyParameters = null;
|
---|
| 67 | }
|
---|
| 68 | protected ParameterizedNamedItem(string name)
|
---|
| 69 | : base(name) {
|
---|
[2851] | 70 | description = ItemDescription;
|
---|
[4787] | 71 | parameterBindingList = new List<IItemBinding>();
|
---|
[3280] | 72 | parameters = new ParameterCollection();
|
---|
[2845] | 73 | readOnlyParameters = null;
|
---|
| 74 | }
|
---|
| 75 | protected ParameterizedNamedItem(string name, ParameterCollection parameters)
|
---|
| 76 | : base(name) {
|
---|
[2851] | 77 | description = ItemDescription;
|
---|
[4787] | 78 | parameterBindingList = new List<IItemBinding>();
|
---|
[3280] | 79 | this.parameters = parameters;
|
---|
[2845] | 80 | readOnlyParameters = null;
|
---|
| 81 | }
|
---|
| 82 | protected ParameterizedNamedItem(string name, string description)
|
---|
| 83 | : base(name, description) {
|
---|
[4787] | 84 | parameterBindingList = new List<IItemBinding>();
|
---|
[3280] | 85 | parameters = new ParameterCollection();
|
---|
[2845] | 86 | readOnlyParameters = null;
|
---|
| 87 | }
|
---|
| 88 | protected ParameterizedNamedItem(string name, string description, ParameterCollection parameters)
|
---|
| 89 | : base(name, description) {
|
---|
[4787] | 90 | parameterBindingList = new List<IItemBinding>();
|
---|
[3280] | 91 | this.parameters = parameters;
|
---|
[2845] | 92 | readOnlyParameters = null;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[3260] | 95 | public virtual void CollectParameterValues(IDictionary<string, IItem> values) {
|
---|
| 96 | foreach (IValueParameter param in parameters.OfType<IValueParameter>()) {
|
---|
[4332] | 97 | if (param.GetsCollected && param.Value != null) values.Add(param.Name, param.Value);
|
---|
[3260] | 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 | }
|
---|
[4770] | 106 |
|
---|
[4787] | 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 |
|
---|
[4790] | 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 |
|
---|
[4787] | 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 |
|
---|
[4770] | 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)
|
---|
[4787] | 136 | parameterBindingList = new List<IItemBinding>();
|
---|
[4770] | 137 | #endregion
|
---|
| 138 | }
|
---|
[2845] | 139 | }
|
---|
| 140 | }
|
---|