Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Core/3.3/Networks/Node.cs @ 11712

Last change on this file since 11712 was 11577, checked in by swagner, 10 years ago

#2205: Restructured solution and projects and switched all projects to .NET 4.5

File size: 4.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.Collections.Generic;
25using System.Drawing;
26using System.Linq;
27
28namespace HeuristicLab.Core.Networks {
29  [Item("Node", "Abstract base class for nodes of a network.")]
30  [StorableClass]
31  public abstract class Node : NetworkItem, INode {
32    public static new Image StaticItemImage {
33      get { return HeuristicLab.Common.Resources.VSImageLibrary.RadialChart; }
34    }
35
36    new public INetwork Parent {
37      get { return (INetwork)base.Parent; }
38      set {
39        // NOTE: never call setter directly as the Parent property is set by the network which contains the node
40        base.Parent = value;
41      }
42    }
43    public override IEnumerable<INetworkItem> Children {
44      get { return base.Children.Concat(Ports.AsEnumerable<INetworkItem>()); }
45    }
46
47    [Storable]
48    private PortCollection ports;
49    protected PortCollection Ports {
50      get { return ports; }
51    }
52    private ReadOnlyKeyedItemCollection<string, IPort> readOnlyPorts;
53    IKeyedItemCollection<string, IPort> INode.Ports {
54      get {
55        if (readOnlyPorts == null) readOnlyPorts = ports.AsReadOnly();
56        return readOnlyPorts;
57      }
58    }
59
60    [StorableConstructor]
61    protected Node(bool deserializing) : base(deserializing) { }
62    protected Node(Node original, Cloner cloner)
63      : base(original, cloner) {
64      ports = cloner.Clone(original.ports);
65      foreach (var p in Ports)
66        p.Parent = this;
67      readOnlyPorts = null;
68      RegisterPortsEvents();
69    }
70    protected Node()
71      : base("Node") {
72      ports = new PortCollection();
73      readOnlyPorts = null;
74      RegisterPortsEvents();
75    }
76    protected Node(string name)
77      : base(name) {
78      ports = new PortCollection();
79      readOnlyPorts = null;
80      RegisterPortsEvents();
81    }
82    protected Node(string name, string description)
83      : base(name, description) {
84      ports = new PortCollection();
85      readOnlyPorts = null;
86      RegisterPortsEvents();
87    }
88
89    [StorableHook(HookType.AfterDeserialization)]
90    private void AfterDeserialization() {
91      foreach (var p in Ports)
92        p.Parent = this;
93      RegisterPortsEvents();
94    }
95
96    #region Ports Events
97    protected virtual void RegisterPortsEvents() {
98      ports.ItemsAdded += Ports_ItemsAdded;
99      ports.ItemsRemoved += Ports_ItemsRemoved;
100      ports.ItemsReplaced += Ports_ItemsReplaced;
101      ports.CollectionReset += Ports_CollectionReset;
102    }
103    protected virtual void Ports_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
104      foreach (var p in e.Items)
105        p.Parent = this;
106    }
107    protected virtual void Ports_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
108      foreach (var p in e.Items)
109        p.Parent = null;
110    }
111    protected virtual void Ports_ItemsReplaced(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
112      foreach (var p in e.OldItems)
113        p.Parent = null;
114      foreach (var p in e.Items)
115        p.Parent = this;
116    }
117    protected virtual void Ports_CollectionReset(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
118      foreach (var p in e.OldItems)
119        p.Parent = null;
120      foreach (var p in e.Items)
121        p.Parent = this;
122    }
123    #endregion
124  }
125}
Note: See TracBrowser for help on using the repository browser.