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

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

#2205: Implemented review comments

  • adapted formatting of multi-line LINQ queries
  • renamed Entity to NetworkItem
File size: 3.8 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", "An optimization network.")]
30  [Creatable("Optimization Networks")]
31  [StorableClass]
32  public class Network : Node, INetwork, IStorableContent {
33    public string Filename { get; set; }
34
35    public static new Image StaticItemImage {
36      get { return HeuristicLab.Common.Resources.VSImageLibrary.Module; }
37    }
38
39    public override IEnumerable<INetworkItem> Children {
40      get { return base.Children.Concat(Nodes.AsEnumerable<INetworkItem>()); }
41    }
42
43    [Storable]
44    private NodeCollection nodes;
45    public NodeCollection Nodes {
46      get { return nodes; }
47    }
48
49    [StorableConstructor]
50    protected Network(bool deserializing) : base(deserializing) { }
51    protected Network(Network original, Cloner cloner)
52      : base(original, cloner) {
53      nodes = cloner.Clone(original.nodes);
54      foreach (var n in Nodes)
55        n.Parent = this;
56      RegisterNodesEvents();
57    }
58    public Network()
59      : base("Network") {
60      nodes = new NodeCollection();
61      RegisterNodesEvents();
62    }
63    public Network(string name)
64      : base(name) {
65      nodes = new NodeCollection();
66      RegisterNodesEvents();
67    }
68    public Network(string name, string description)
69      : base(name, description) {
70      nodes = new NodeCollection();
71      RegisterNodesEvents();
72    }
73
74    [StorableHook(HookType.AfterDeserialization)]
75    private void AfterDeserialization() {
76      foreach (var n in Nodes)
77        n.Parent = this;
78      RegisterNodesEvents();
79    }
80
81    public override IDeepCloneable Clone(Cloner cloner) {
82      return new Network(this, cloner);
83    }
84
85    #region Nodes Events
86    protected virtual void RegisterNodesEvents() {
87      nodes.ItemsAdded += Nodes_ItemsAdded;
88      nodes.ItemsRemoved += Nodes_ItemsRemoved;
89      nodes.ItemsReplaced += Nodes_ItemsReplaced;
90      nodes.CollectionReset += Nodes_CollectionReset;
91    }
92    protected virtual void Nodes_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<INode> e) {
93      foreach (var n in e.Items)
94        n.Parent = this;
95    }
96    protected virtual void Nodes_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<INode> e) {
97      foreach (var n in e.Items)
98        n.Parent = null;
99    }
100    protected virtual void Nodes_ItemsReplaced(object sender, Collections.CollectionItemsChangedEventArgs<INode> e) {
101      foreach (var n in e.OldItems)
102        n.Parent = null;
103      foreach (var n in e.Items)
104        n.Parent = this;
105    }
106    protected virtual void Nodes_CollectionReset(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    #endregion
113  }
114}
Note: See TracBrowser for help on using the repository browser.