Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Nodes/Node.cs @ 11500

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

#2205: Worked on optimization networks

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