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 |
|
---|
22 | using System.Collections.Generic;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
26 |
|
---|
27 | namespace 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<IParameterBinding> parameterBindingList;
|
---|
36 | // eventuelly this should be made public and exposed in a view so that the algorithm designer can add bindings in the GUI
|
---|
37 | protected List<IParameterBinding> 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 | parameterBindingList = original.parameterBindingList.Select(x => cloner.Clone(x)).ToList();
|
---|
59 | readOnlyParameters = null;
|
---|
60 | }
|
---|
61 | protected ParameterizedNamedItem()
|
---|
62 | : base() {
|
---|
63 | name = ItemName;
|
---|
64 | description = ItemDescription;
|
---|
65 | parameterBindingList = new List<IParameterBinding>();
|
---|
66 | parameters = new ParameterCollection();
|
---|
67 | readOnlyParameters = null;
|
---|
68 | }
|
---|
69 | protected ParameterizedNamedItem(string name)
|
---|
70 | : base(name) {
|
---|
71 | description = ItemDescription;
|
---|
72 | parameterBindingList = new List<IParameterBinding>();
|
---|
73 | parameters = new ParameterCollection();
|
---|
74 | readOnlyParameters = null;
|
---|
75 | }
|
---|
76 | protected ParameterizedNamedItem(string name, ParameterCollection parameters)
|
---|
77 | : base(name) {
|
---|
78 | description = ItemDescription;
|
---|
79 | parameterBindingList = new List<IParameterBinding>();
|
---|
80 | this.parameters = parameters;
|
---|
81 | readOnlyParameters = null;
|
---|
82 | }
|
---|
83 | protected ParameterizedNamedItem(string name, string description)
|
---|
84 | : base(name, description) {
|
---|
85 | parameterBindingList = new List<IParameterBinding>();
|
---|
86 | parameters = new ParameterCollection();
|
---|
87 | readOnlyParameters = null;
|
---|
88 | }
|
---|
89 | protected ParameterizedNamedItem(string name, string description, ParameterCollection parameters)
|
---|
90 | : base(name, description) {
|
---|
91 | parameterBindingList = new List<IParameterBinding>();
|
---|
92 | this.parameters = parameters;
|
---|
93 | readOnlyParameters = null;
|
---|
94 | }
|
---|
95 |
|
---|
96 | public virtual void CollectParameterValues(IDictionary<string, IItem> values) {
|
---|
97 | foreach (IValueParameter param in parameters.OfType<IValueParameter>()) {
|
---|
98 | if (param.GetsCollected && param.Value != null) values.Add(param.Name, param.Value);
|
---|
99 | if (param.Value is IParameterizedItem) {
|
---|
100 | Dictionary<string, IItem> children = new Dictionary<string, IItem>();
|
---|
101 | ((IParameterizedItem)param.Value).CollectParameterValues(children);
|
---|
102 | foreach (string key in children.Keys)
|
---|
103 | values.Add(param.Name + "." + key, children[key]);
|
---|
104 | }
|
---|
105 | }
|
---|
106 | }
|
---|
107 | }
|
---|
108 | }
|
---|