Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2205: Worked on optimization networks

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