1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2014 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 HeuristicLab.Common;
|
---|
23 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
24 | using System;
|
---|
25 | using System.Collections.Generic;
|
---|
26 | using System.Linq;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Core.Networks {
|
---|
29 | [Item("ParameterizedPort", "Abstract base class for parameterized ports of network nodes.")]
|
---|
30 | [StorableClass]
|
---|
31 | public abstract class ParameterizedPort : Port, IParameterizedPort {
|
---|
32 | public override IEnumerable<INetworkItem> Children {
|
---|
33 | get { return base.Children.Concat(Parameters.AsEnumerable<INetworkItem>()); }
|
---|
34 | }
|
---|
35 |
|
---|
36 | [Storable]
|
---|
37 | private PortParameterCollection parameters;
|
---|
38 | protected PortParameterCollection Parameters {
|
---|
39 | get { return parameters; }
|
---|
40 | }
|
---|
41 | private ReadOnlyKeyedItemCollection<string, IPortParameter> readOnlyParameters;
|
---|
42 | IKeyedItemCollection<string, IPortParameter> IParameterizedPort.Parameters {
|
---|
43 | get {
|
---|
44 | if (readOnlyParameters == null) readOnlyParameters = parameters.AsReadOnly();
|
---|
45 | return readOnlyParameters;
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | [StorableConstructor]
|
---|
50 | protected ParameterizedPort(bool deserializing) : base(deserializing) { }
|
---|
51 | protected ParameterizedPort(ParameterizedPort original, Cloner cloner)
|
---|
52 | : base(original, cloner) {
|
---|
53 | parameters = cloner.Clone(original.parameters);
|
---|
54 | foreach (var p in Parameters)
|
---|
55 | p.Parent = this;
|
---|
56 | readOnlyParameters = null;
|
---|
57 | RegisterParametersEvents();
|
---|
58 | }
|
---|
59 | protected ParameterizedPort()
|
---|
60 | : base("ParameterizedPort") {
|
---|
61 | parameters = new PortParameterCollection();
|
---|
62 | readOnlyParameters = null;
|
---|
63 | RegisterParametersEvents();
|
---|
64 | }
|
---|
65 | protected ParameterizedPort(string name)
|
---|
66 | : base(name) {
|
---|
67 | parameters = new PortParameterCollection();
|
---|
68 | readOnlyParameters = null;
|
---|
69 | RegisterParametersEvents();
|
---|
70 | }
|
---|
71 | protected ParameterizedPort(string name, string description)
|
---|
72 | : base(name, description) {
|
---|
73 | parameters = new PortParameterCollection();
|
---|
74 | readOnlyParameters = null;
|
---|
75 | RegisterParametersEvents();
|
---|
76 | }
|
---|
77 |
|
---|
78 | [StorableHook(HookType.AfterDeserialization)]
|
---|
79 | private void AfterDeserialization() {
|
---|
80 | foreach (var p in Parameters)
|
---|
81 | p.Parent = this;
|
---|
82 | RegisterParametersEvents();
|
---|
83 | }
|
---|
84 |
|
---|
85 | public virtual void CloneParametersFromPort(IParameterizedPort port) {
|
---|
86 | Parameters.Clear();
|
---|
87 | foreach (var p in port.Parameters) {
|
---|
88 | var param = (IPortParameter)p.Clone();
|
---|
89 | if (!(param.Type.HasFlag(PortParameterType.Input) && param.Type.HasFlag(PortParameterType.Output))) {
|
---|
90 | param.Type = ~param.Type; // bitwise negation: input -> output, output -> input
|
---|
91 | }
|
---|
92 | Parameters.Add(param);
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | public event EventHandler InterfaceChanged;
|
---|
97 | protected virtual void OnInterfaceChanged() {
|
---|
98 | var handler = InterfaceChanged;
|
---|
99 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
100 | }
|
---|
101 |
|
---|
102 | #region Parameters Events
|
---|
103 | protected virtual void RegisterParametersEvents() {
|
---|
104 | parameters.ItemsAdded += Parameters_ItemsAdded;
|
---|
105 | parameters.ItemsRemoved += Parameters_ItemsRemoved;
|
---|
106 | parameters.ItemsReplaced += Parameters_ItemsReplaced;
|
---|
107 | parameters.CollectionReset += Parameters_CollectionReset;
|
---|
108 | foreach (var p in parameters)
|
---|
109 | RegisterParameterEvents(p);
|
---|
110 | }
|
---|
111 | protected virtual void Parameters_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<IPortParameter> e) {
|
---|
112 | foreach (var p in e.Items) {
|
---|
113 | p.Parent = this;
|
---|
114 | RegisterParameterEvents(p);
|
---|
115 | }
|
---|
116 | OnInterfaceChanged();
|
---|
117 | }
|
---|
118 | protected virtual void Parameters_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<IPortParameter> e) {
|
---|
119 | foreach (var p in e.Items) {
|
---|
120 | p.Parent = null;
|
---|
121 | DeregisterParameterEvents(p);
|
---|
122 | }
|
---|
123 | OnInterfaceChanged();
|
---|
124 | }
|
---|
125 | protected virtual void Parameters_ItemsReplaced(object sender, Collections.CollectionItemsChangedEventArgs<IPortParameter> e) {
|
---|
126 | foreach (var p in e.OldItems) {
|
---|
127 | p.Parent = null;
|
---|
128 | DeregisterParameterEvents(p);
|
---|
129 | }
|
---|
130 | foreach (var p in e.Items) {
|
---|
131 | p.Parent = this;
|
---|
132 | RegisterParameterEvents(p);
|
---|
133 | }
|
---|
134 | OnInterfaceChanged();
|
---|
135 | }
|
---|
136 | protected virtual void Parameters_CollectionReset(object sender, Collections.CollectionItemsChangedEventArgs<IPortParameter> e) {
|
---|
137 | foreach (var p in e.OldItems) {
|
---|
138 | p.Parent = null;
|
---|
139 | DeregisterParameterEvents(p);
|
---|
140 | }
|
---|
141 | foreach (var p in e.Items) {
|
---|
142 | p.Parent = this;
|
---|
143 | RegisterParameterEvents(p);
|
---|
144 | }
|
---|
145 | OnInterfaceChanged();
|
---|
146 | }
|
---|
147 | #endregion
|
---|
148 |
|
---|
149 | #region Parameter Events
|
---|
150 | protected virtual void RegisterParameterEvents(IPortParameter parameter) {
|
---|
151 | parameter.NameChanged += Parameter_NameChanged;
|
---|
152 | parameter.TypeChanged += Parameter_TypeChanged;
|
---|
153 | }
|
---|
154 | protected virtual void DeregisterParameterEvents(IPortParameter parameter) {
|
---|
155 | parameter.NameChanged -= Parameter_NameChanged;
|
---|
156 | parameter.TypeChanged -= Parameter_TypeChanged;
|
---|
157 | }
|
---|
158 | protected virtual void Parameter_NameChanged(object sender, EventArgs e) {
|
---|
159 | OnInterfaceChanged();
|
---|
160 | }
|
---|
161 | protected virtual void Parameter_TypeChanged(object sender, EventArgs e) {
|
---|
162 | OnInterfaceChanged();
|
---|
163 | }
|
---|
164 | #endregion
|
---|
165 | }
|
---|
166 | }
|
---|