source: branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Core.Networks/Network.cs @ 11529

Last change on this file since 11529 was 11529, checked in by swagner, 5 years ago

#2205: Implemented review comments

  • made Node and Network abstract
  • renamed GenericNode to UserDefinedNode and sealed it
  • added sealed UserDefinedNetwork
  • made Nodes in Network not changeable by default
  • made Parameters in ParameterizedPort not changeable by default
  • removed value None in PortParameterType
  • refactored call algorithm.PrepareMessage() in AlgorithmNode
File size: 4.1 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("Network", "Abstract base class for networks.")]
30  [StorableClass]
31  public abstract class Network : Node, INetwork, IStorableContent {
32    public string Filename { get; set; }
33
34    public static new Image StaticItemImage {
35      get { return HeuristicLab.Common.Resources.VSImageLibrary.Module; }
36    }
37
38    public override IEnumerable<INetworkItem> Children {
39      get { return base.Children.Concat(Nodes.AsEnumerable<INetworkItem>()); }
40    }
41
42    [Storable]
43    private NodeCollection nodes;
44    protected NodeCollection Nodes {
45      get { return nodes; }
46    }
47    private ReadOnlyKeyedItemCollection<string, INode> readOnlyNodes;
48    IKeyedItemCollection<string, INode> INetwork.Nodes {
49      get {
50        if (readOnlyNodes == null) readOnlyNodes = nodes.AsReadOnly();
51        return readOnlyNodes;
52      }
53    }
54
55    [StorableConstructor]
56    protected Network(bool deserializing) : base(deserializing) { }
57    protected Network(Network original, Cloner cloner)
58      : base(original, cloner) {
59      nodes = cloner.Clone(original.nodes);
60      foreach (var n in Nodes)
61        n.Parent = this;
62      readOnlyNodes = null;
63      RegisterNodesEvents();
64    }
65    protected Network()
66      : base("Network") {
67      nodes = new NodeCollection();
68      readOnlyNodes = null;
69      RegisterNodesEvents();
70    }
71    protected Network(string name)
72      : base(name) {
73      nodes = new NodeCollection();
74      readOnlyNodes = null;
75      RegisterNodesEvents();
76    }
77    protected Network(string name, string description)
78      : base(name, description) {
79      nodes = new NodeCollection();
80      readOnlyNodes = null;
81      RegisterNodesEvents();
82    }
83
84    [StorableHook(HookType.AfterDeserialization)]
85    private void AfterDeserialization() {
86      foreach (var n in Nodes)
87        n.Parent = this;
88      RegisterNodesEvents();
89    }
90
91    #region Nodes Events
92    protected virtual void RegisterNodesEvents() {
93      nodes.ItemsAdded += Nodes_ItemsAdded;
94      nodes.ItemsRemoved += Nodes_ItemsRemoved;
95      nodes.ItemsReplaced += Nodes_ItemsReplaced;
96      nodes.CollectionReset += Nodes_CollectionReset;
97    }
98    protected virtual void Nodes_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<INode> e) {
99      foreach (var n in e.Items)
100        n.Parent = this;
101    }
102    protected virtual void Nodes_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<INode> e) {
103      foreach (var n in e.Items)
104        n.Parent = null;
105    }
106    protected virtual void Nodes_ItemsReplaced(object sender, Collections.CollectionItemsChangedEventArgs<INode> e) {
107      foreach (var n in e.OldItems)
108        n.Parent = null;
109      foreach (var n in e.Items)
110        n.Parent = this;
111    }
112    protected virtual void Nodes_CollectionReset(object sender, Collections.CollectionItemsChangedEventArgs<INode> e) {
113      foreach (var n in e.OldItems)
114        n.Parent = null;
115      foreach (var n in e.Items)
116        n.Parent = this;
117    }
118    #endregion
119  }
120}
Note: See TracBrowser for help on using the repository browser.