Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2205_OptimizationNetworks/HeuristicLab.Networks/3.3/Programmable/ProgrammableNetwork.cs @ 17712

Last change on this file since 17712 was 12944, checked in by jkarder, 9 years ago

#2205: added code resources

File size: 6.0 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.Core.Networks;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26using System.Collections.Generic;
27using System.Drawing;
28using System.Linq;
29
30namespace HeuristicLab.Networks.Programmable {
31  [Item("ProgrammableNetwork", "Abstract base class for programmable networks.")]
32  [StorableClass]
33  public abstract class ProgrammableNetwork : ProgrammableNode, IProgrammableNetwork, IStorableContent {
34    public static new Image StaticItemImage {
35      get { return HeuristicLab.Common.Resources.VSImageLibrary.Module; }
36    }
37
38    #region IStorableContent Members
39    public string Filename { get; set; }
40    #endregion
41
42    #region Network Members
43    [Storable]
44    private NodeCollection nodes;
45    protected NodeCollection Nodes {
46      get { return CompiledNetworkItem.Nodes; }
47    }
48    private ReadOnlyKeyedItemCollection<string, INode> readOnlyNodes;
49    IKeyedItemCollection<string, INode> INetwork.Nodes {
50      get {
51        if (readOnlyNodes == null) readOnlyNodes = Nodes.AsReadOnly();
52        return readOnlyNodes;
53      }
54    }
55    #endregion
56
57    protected override string CodeTemplate {
58      get { return CodeResources.ProgrammableNetworkCode; }
59    }
60
61    new protected CompiledProgrammableNetwork CompiledNetworkItem {
62      get { return (CompiledProgrammableNetwork)base.CompiledNetworkItem; }
63    }
64
65    [StorableConstructor]
66    protected ProgrammableNetwork(bool deserializing) : base(deserializing) { }
67    protected ProgrammableNetwork(ProgrammableNetwork original, Cloner cloner)
68      : base(original, cloner) {
69      // nodes are cloned in CompiledProgrammableNetwork
70      readOnlyNodes = null;
71    }
72    protected ProgrammableNetwork()
73      : base("ProgrammableNetwork") {
74      nodes = new NodeCollection();
75      readOnlyNodes = null;
76    }
77    protected ProgrammableNetwork(string name)
78      : base(name) {
79      nodes = new NodeCollection();
80      readOnlyNodes = null;
81    }
82    protected ProgrammableNetwork(string name, string description)
83      : base(name, description) {
84      nodes = new NodeCollection();
85      readOnlyNodes = null;
86    }
87
88    #region CompiledProgrammableNetwork
89    [Item("CompiledProgrammableNetwork", "Abstract base class for compiled programmable networks.")]
90    public abstract class CompiledProgrammableNetwork : CompiledProgrammableNode, INetwork {
91      public static new Image StaticItemImage {
92        get { return HeuristicLab.Common.Resources.VSImageLibrary.Module; }
93      }
94
95      #region Network Members
96      public override IEnumerable<INetworkItem> Children {
97        get { return base.Children.Concat(Nodes.AsEnumerable<INetworkItem>()); }
98      }
99
100      public NodeCollection Nodes {
101        get { return Context.nodes; }
102      }
103      IKeyedItemCollection<string, INode> INetwork.Nodes {
104        get { return Nodes; }
105      }
106      #endregion
107
108      new protected ProgrammableNetwork Context {
109        get { return (ProgrammableNetwork)base.Context; }
110      }
111
112      protected CompiledProgrammableNetwork(CompiledProgrammableNetwork original, Cloner cloner)
113        : base(original, cloner) {
114        Context.nodes = cloner.Clone(original.Context.nodes);
115      }
116      protected CompiledProgrammableNetwork(ProgrammableNetwork context)
117        : base(context) {
118      }
119
120      public override void Initialize() {
121        base.Initialize();
122        Nodes.Clear();
123      }
124
125      #region Events
126      public override void RegisterEvents() {
127        base.RegisterEvents();
128        Nodes.ItemsAdded += Nodes_ItemsAdded;
129        Nodes.ItemsRemoved += Nodes_ItemsRemoved;
130        Nodes.ItemsReplaced += Nodes_ItemsReplaced;
131        Nodes.CollectionReset += Nodes_CollectionReset;
132        foreach (var n in Nodes.ToList())
133          n.Parent = this;
134      }
135      public override void DeregisterEvents() {
136        foreach (var n in Nodes)
137          n.Parent = null;
138        Nodes.ItemsAdded -= Nodes_ItemsAdded;
139        Nodes.ItemsRemoved -= Nodes_ItemsRemoved;
140        Nodes.ItemsReplaced -= Nodes_ItemsReplaced;
141        Nodes.CollectionReset -= Nodes_CollectionReset;
142        base.DeregisterEvents();
143      }
144      protected virtual void Nodes_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<INode> e) {
145        foreach (var n in e.Items)
146          n.Parent = this;
147      }
148      protected virtual void Nodes_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<INode> e) {
149        foreach (var n in e.Items)
150          n.Parent = null;
151      }
152      protected virtual void Nodes_ItemsReplaced(object sender, Collections.CollectionItemsChangedEventArgs<INode> e) {
153        foreach (var n in e.OldItems)
154          n.Parent = null;
155        foreach (var n in e.Items)
156          n.Parent = this;
157      }
158      protected virtual void Nodes_CollectionReset(object sender, Collections.CollectionItemsChangedEventArgs<INode> e) {
159        foreach (var n in e.OldItems)
160          n.Parent = null;
161        foreach (var n in e.Items)
162          n.Parent = this;
163      }
164      #endregion
165    }
166    #endregion
167  }
168}
Note: See TracBrowser for help on using the repository browser.