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

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

#2205: Implemented review comments

  • adapted formatting of multi-line LINQ queries
  • renamed Entity to NetworkItem
File size: 5.2 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    protected PortParameterCollection parameters;
38    public PortParameterCollection Parameters {
39      get { return parameters; }
40    }
41
42    [StorableConstructor]
43    protected ParameterizedPort(bool deserializing) : base(deserializing) { }
44    protected ParameterizedPort(ParameterizedPort original, Cloner cloner)
45      : base(original, cloner) {
46      parameters = cloner.Clone(original.parameters);
47      foreach (var p in Parameters)
48        p.Parent = this;
49      RegisterParametersEvents();
50    }
51    protected ParameterizedPort()
52      : base("ParameterizedPort") {
53      parameters = new PortParameterCollection();
54      RegisterParametersEvents();
55    }
56    protected ParameterizedPort(string name)
57      : base(name) {
58      parameters = new PortParameterCollection();
59      RegisterParametersEvents();
60    }
61    protected ParameterizedPort(string name, string description)
62      : base(name, description) {
63      parameters = new PortParameterCollection();
64      RegisterParametersEvents();
65    }
66
67    [StorableHook(HookType.AfterDeserialization)]
68    private void AfterDeserialization() {
69      foreach (var p in Parameters)
70        p.Parent = this;
71      RegisterParametersEvents();
72    }
73
74    public event EventHandler InterfaceChanged;
75    protected virtual void OnInterfaceChanged() {
76      var handler = InterfaceChanged;
77      if (handler != null) handler(this, EventArgs.Empty);
78    }
79
80    #region Parameters Events
81    protected virtual void RegisterParametersEvents() {
82      parameters.ItemsAdded += Parameters_ItemsAdded;
83      parameters.ItemsRemoved += Parameters_ItemsRemoved;
84      parameters.ItemsReplaced += Parameters_ItemsReplaced;
85      parameters.CollectionReset += Parameters_CollectionReset;
86      foreach (var p in parameters)
87        RegisterParameterEvents(p);
88    }
89    protected virtual void Parameters_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<IPortParameter> e) {
90      foreach (var p in e.Items) {
91        p.Parent = this;
92        RegisterParameterEvents(p);
93      }
94      OnInterfaceChanged();
95    }
96    protected virtual void Parameters_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<IPortParameter> e) {
97      foreach (var p in e.Items) {
98        p.Parent = null;
99        DeregisterParameterEvents(p);
100      }
101      OnInterfaceChanged();
102    }
103    protected virtual void Parameters_ItemsReplaced(object sender, Collections.CollectionItemsChangedEventArgs<IPortParameter> e) {
104      foreach (var p in e.OldItems) {
105        p.Parent = null;
106        DeregisterParameterEvents(p);
107      }
108      foreach (var p in e.Items) {
109        p.Parent = this;
110        RegisterParameterEvents(p);
111      }
112      OnInterfaceChanged();
113    }
114    protected virtual void Parameters_CollectionReset(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    #endregion
126
127    #region Parameter Events
128    protected virtual void RegisterParameterEvents(IPortParameter parameter) {
129      parameter.NameChanged += Parameter_NameChanged;
130      parameter.TypeChanged += Parameter_TypeChanged;
131    }
132    protected virtual void DeregisterParameterEvents(IPortParameter parameter) {
133      parameter.NameChanged -= Parameter_NameChanged;
134      parameter.TypeChanged -= Parameter_TypeChanged;
135    }
136    protected virtual void Parameter_NameChanged(object sender, EventArgs e) {
137      OnInterfaceChanged();
138    }
139    protected virtual void Parameter_TypeChanged(object sender, EventArgs e) {
140      OnInterfaceChanged();
141    }
142    #endregion
143  }
144}
Note: See TracBrowser for help on using the repository browser.