[11401] | 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;
|
---|
[11449] | 24 | using System.Collections.Generic;
|
---|
[11526] | 25 | using System.Drawing;
|
---|
[11449] | 26 | using System.Linq;
|
---|
[11401] | 27 |
|
---|
[11526] | 28 | namespace HeuristicLab.Core.Networks {
|
---|
[11529] | 29 | [Item("Node", "Abstract base class for nodes of a network.")]
|
---|
[11401] | 30 | [StorableClass]
|
---|
[11529] | 31 | public abstract class Node : NetworkItem, INode {
|
---|
[11401] | 32 | public static new Image StaticItemImage {
|
---|
| 33 | get { return HeuristicLab.Common.Resources.VSImageLibrary.RadialChart; }
|
---|
| 34 | }
|
---|
| 35 |
|
---|
[11449] | 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 | }
|
---|
[11528] | 43 | public override IEnumerable<INetworkItem> Children {
|
---|
| 44 | get { return base.Children.Concat(Ports.AsEnumerable<INetworkItem>()); }
|
---|
[11449] | 45 | }
|
---|
| 46 |
|
---|
[11401] | 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);
|
---|
[11423] | 65 | foreach (var p in Ports)
|
---|
| 66 | p.Parent = this;
|
---|
[11401] | 67 | readOnlyPorts = null;
|
---|
[11423] | 68 | RegisterPortsEvents();
|
---|
[11401] | 69 | }
|
---|
[11529] | 70 | protected Node()
|
---|
[11401] | 71 | : base("Node") {
|
---|
[11423] | 72 | ports = new PortCollection();
|
---|
[11401] | 73 | readOnlyPorts = null;
|
---|
[11423] | 74 | RegisterPortsEvents();
|
---|
[11401] | 75 | }
|
---|
[11529] | 76 | protected Node(string name)
|
---|
[11401] | 77 | : base(name) {
|
---|
[11423] | 78 | ports = new PortCollection();
|
---|
[11401] | 79 | readOnlyPorts = null;
|
---|
[11423] | 80 | RegisterPortsEvents();
|
---|
[11401] | 81 | }
|
---|
[11529] | 82 | protected Node(string name, string description)
|
---|
[11401] | 83 | : base(name, description) {
|
---|
[11423] | 84 | ports = new PortCollection();
|
---|
[11401] | 85 | readOnlyPorts = null;
|
---|
[11423] | 86 | RegisterPortsEvents();
|
---|
[11401] | 87 | }
|
---|
| 88 |
|
---|
[11423] | 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
|
---|
[11401] | 124 | }
|
---|
| 125 | }
|
---|