Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11526 was 11526, checked in by swagner, 9 years ago

#2205: Worked on optimization networks

File size: 4.3 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", "A node of an optimization network.")]
30  [StorableClass]
31  public class Node : Entity, 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<IEntity> Children {
44      get { return base.Children.Concat(Ports.AsEnumerable<IEntity>()); }
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    public Node()
71      : base("Node") {
72      ports = new PortCollection();
73      readOnlyPorts = null;
74      RegisterPortsEvents();
75    }
76    public Node(string name)
77      : base(name) {
78      ports = new PortCollection();
79      readOnlyPorts = null;
80      RegisterPortsEvents();
81    }
82    public 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    public override IDeepCloneable Clone(Cloner cloner) {
97      return new Node(this, cloner);
98    }
99
100    #region Ports Events
101    protected virtual void RegisterPortsEvents() {
102      ports.ItemsAdded += Ports_ItemsAdded;
103      ports.ItemsRemoved += Ports_ItemsRemoved;
104      ports.ItemsReplaced += Ports_ItemsReplaced;
105      ports.CollectionReset += Ports_CollectionReset;
106    }
107    protected virtual void Ports_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
108      foreach (var p in e.Items)
109        p.Parent = this;
110    }
111    protected virtual void Ports_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
112      foreach (var p in e.Items)
113        p.Parent = null;
114    }
115    protected virtual void Ports_ItemsReplaced(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
116      foreach (var p in e.OldItems)
117        p.Parent = null;
118      foreach (var p in e.Items)
119        p.Parent = this;
120    }
121    protected virtual void Ports_CollectionReset(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
122      foreach (var p in e.OldItems)
123        p.Parent = null;
124      foreach (var p in e.Items)
125        p.Parent = this;
126    }
127    #endregion
128  }
129}
Note: See TracBrowser for help on using the repository browser.