source: branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Core.Networks/ParameterizedPort.cs @ 11529

Last change on this file since 11529 was 11529, checked in by swagner, 8 years ago

#2205: Implemented review comments

  • made Node and Network abstract
  • renamed GenericNode to UserDefinedNode and sealed it
  • added sealed UserDefinedNetwork
  • made Nodes in Network not changeable by default
  • made Parameters in ParameterizedPort not changeable by default
  • removed value None in PortParameterType
  • refactored call algorithm.PrepareMessage() in AlgorithmNode
File size: 5.6 KB
Line 
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
22using HeuristicLab.Common;
23using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
24using System;
25using System.Collections.Generic;
26using System.Linq;
27
28namespace HeuristicLab.Core.Networks {
29  [Item("ParameterizedPort", "An abstract base class for parameterized ports of optimization 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 event EventHandler InterfaceChanged;
86    protected virtual void OnInterfaceChanged() {
87      var handler = InterfaceChanged;
88      if (handler != null) handler(this, EventArgs.Empty);
89    }
90
91    #region Parameters Events
92    protected virtual void RegisterParametersEvents() {
93      parameters.ItemsAdded += Parameters_ItemsAdded;
94      parameters.ItemsRemoved += Parameters_ItemsRemoved;
95      parameters.ItemsReplaced += Parameters_ItemsReplaced;
96      parameters.CollectionReset += Parameters_CollectionReset;
97      foreach (var p in parameters)
98        RegisterParameterEvents(p);
99    }
100    protected virtual void Parameters_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<IPortParameter> e) {
101      foreach (var p in e.Items) {
102        p.Parent = this;
103        RegisterParameterEvents(p);
104      }
105      OnInterfaceChanged();
106    }
107    protected virtual void Parameters_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<IPortParameter> e) {
108      foreach (var p in e.Items) {
109        p.Parent = null;
110        DeregisterParameterEvents(p);
111      }
112      OnInterfaceChanged();
113    }
114    protected virtual void Parameters_ItemsReplaced(object sender, Collections.CollectionItemsChangedEventArgs<IPortParameter> e) {
115      foreach (var p in e.OldItems) {
116        p.Parent = null;
117        DeregisterParameterEvents(p);
118      }
119      foreach (var p in e.Items) {
120        p.Parent = this;
121        RegisterParameterEvents(p);
122      }
123      OnInterfaceChanged();
124    }
125    protected virtual void Parameters_CollectionReset(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    #endregion
137
138    #region Parameter Events
139    protected virtual void RegisterParameterEvents(IPortParameter parameter) {
140      parameter.NameChanged += Parameter_NameChanged;
141      parameter.TypeChanged += Parameter_TypeChanged;
142    }
143    protected virtual void DeregisterParameterEvents(IPortParameter parameter) {
144      parameter.NameChanged -= Parameter_NameChanged;
145      parameter.TypeChanged -= Parameter_TypeChanged;
146    }
147    protected virtual void Parameter_NameChanged(object sender, EventArgs e) {
148      OnInterfaceChanged();
149    }
150    protected virtual void Parameter_TypeChanged(object sender, EventArgs e) {
151      OnInterfaceChanged();
152    }
153    #endregion
154  }
155}
Note: See TracBrowser for help on using the repository browser.