Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2205_OptimizationNetworks/HeuristicLab.Networks/3.3/Programmable/ProgrammableNode.cs @ 16147

Last change on this file since 16147 was 13135, checked in by jkarder, 9 years ago

#2205: worked on optimization networks

  • refactored network visualization
File size: 7.5 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.Collections.Generic;
23using System.Drawing;
24using System.Linq;
25using System.Threading;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Core.Networks;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Networks.Programmable {
32  [Item("ProgrammableNode", "Abstract base class for programmable nodes of a network.")]
33  [StorableClass]
34  public abstract class ProgrammableNode : ProgrammableNetworkItem, IProgrammableNode {
35    public static new Image StaticItemImage {
36      get { return HeuristicLab.Common.Resources.VSImageLibrary.RadialChart; }
37    }
38
39    #region Node Members
40    new public INetwork Parent {
41      get { return CompiledNetworkItem.Parent; }
42      set {
43        // NOTE: never call setter directly as the Parent property is set by the network which contains the node
44        CompiledNetworkItem.Parent = value;
45      }
46    }
47
48    [Storable]
49    private PortCollection ports;
50    protected PortCollection Ports {
51      get { return CompiledNetworkItem.Ports; }
52    }
53    private ReadOnlyKeyedItemCollection<string, IPort> readOnlyPorts;
54    IKeyedItemCollection<string, IPort> INode.Ports {
55      get {
56        if (readOnlyPorts == null) readOnlyPorts = Ports.AsReadOnly();
57        return readOnlyPorts;
58      }
59    }
60    #endregion
61
62    protected override string CodeTemplate {
63      get { return CodeResources.ProgrammableNodeCode; }
64    }
65
66    new protected CompiledProgrammableNode CompiledNetworkItem {
67      get { return (CompiledProgrammableNode)base.CompiledNetworkItem; }
68    }
69
70    [StorableConstructor]
71    protected ProgrammableNode(bool deserializing) : base(deserializing) { }
72    protected ProgrammableNode(ProgrammableNode original, Cloner cloner)
73      : base(original, cloner) {
74      // ports are cloned in CompiledProgrammableNode
75      readOnlyPorts = null;
76    }
77    protected ProgrammableNode()
78      : base("ProgrammableNode") {
79      ports = new PortCollection();
80      readOnlyPorts = null;
81    }
82    protected ProgrammableNode(string name)
83      : base(name) {
84      ports = new PortCollection();
85      readOnlyPorts = null;
86    }
87    protected ProgrammableNode(string name, string description)
88      : base(name, description) {
89      ports = new PortCollection();
90      readOnlyPorts = null;
91    }
92
93    #region CompiledProgrammableNode
94    [Item("CompiledProgrammableNode", "Abstract base class for compiled programmable nodes of a network.")]
95    public abstract class CompiledProgrammableNode : CompiledProgrammableNetworkItem, INode {
96      public static new Image StaticItemImage {
97        get { return HeuristicLab.Common.Resources.VSImageLibrary.RadialChart; }
98      }
99
100      #region Node Members
101      new public INetwork Parent {
102        get { return (INetwork)base.Parent; }
103        set {
104          // NOTE: never call setter directly as the Parent property is set by the network which contains the node
105          base.Parent = value;
106        }
107      }
108      public override IEnumerable<INetworkItem> Children {
109        get { return base.Children.Concat(Ports.AsEnumerable<INetworkItem>()); }
110      }
111
112      public PortCollection Ports {
113        get { return Context.ports; }
114      }
115      IKeyedItemCollection<string, IPort> INode.Ports {
116        get { return Ports; }
117      }
118      #endregion
119
120      new protected ProgrammableNode Context {
121        get { return (ProgrammableNode)base.Context; }
122      }
123
124      protected CompiledProgrammableNode(CompiledProgrammableNode original, Cloner cloner)
125        : base(original, cloner) {
126        Context.ports = cloner.Clone(original.Context.ports);
127      }
128      protected CompiledProgrammableNode(ProgrammableNode context)
129        : base(context) {
130      }
131
132      public override void Initialize() {
133        base.Initialize();
134        Ports.Clear();
135      }
136
137      private void MessagePort_MessageReceived(object sender, EventArgs<IMessage, CancellationToken> e) {
138        ProcessMessage(e.Value, (IMessagePort)sender, e.Value2);
139      }
140      protected virtual void ProcessMessage(IMessage message, IMessagePort port, CancellationToken token) { }
141
142      #region Events
143      public override void RegisterEvents() {
144        base.RegisterEvents();
145        Ports.ItemsAdded += Ports_ItemsAdded;
146        Ports.ItemsRemoved += Ports_ItemsRemoved;
147        Ports.ItemsReplaced += Ports_ItemsReplaced;
148        Ports.CollectionReset += Ports_CollectionReset;
149        foreach (var p in Ports) {
150          p.Parent = this;
151          RegisterPortEvents(p);
152        }
153      }
154      public override void DeregisterEvents() {
155        foreach (var p in Ports) {
156          DeregisterPortEvents(p);
157          p.Parent = null;
158        }
159        Ports.ItemsAdded -= Ports_ItemsAdded;
160        Ports.ItemsRemoved -= Ports_ItemsRemoved;
161        Ports.ItemsReplaced -= Ports_ItemsReplaced;
162        Ports.CollectionReset -= Ports_CollectionReset;
163        base.DeregisterEvents();
164      }
165      protected virtual void Ports_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
166        foreach (var p in e.Items) {
167          p.Parent = this;
168          RegisterPortEvents(p);
169        }
170      }
171      protected virtual void Ports_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
172        foreach (var p in e.Items) {
173          p.Parent = null;
174          DeregisterPortEvents(p);
175        }
176      }
177      protected virtual void Ports_ItemsReplaced(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
178        foreach (var p in e.OldItems) {
179          p.Parent = null;
180          DeregisterPortEvents(p);
181        }
182        foreach (var p in e.Items) {
183          p.Parent = this;
184          RegisterPortEvents(p);
185        }
186      }
187      protected virtual void Ports_CollectionReset(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
188        foreach (var p in e.OldItems) {
189          p.Parent = null;
190          DeregisterPortEvents(p);
191        }
192        foreach (var p in e.Items) {
193          p.Parent = this;
194          RegisterPortEvents(p);
195        }
196      }
197      #endregion
198
199      #region Port Events
200      protected virtual void RegisterPortEvents(IPort port) {
201        var mp = port as IMessagePort;
202        if (mp != null)
203          mp.MessageReceived += MessagePort_MessageReceived;
204      }
205      protected virtual void DeregisterPortEvents(IPort port) {
206        var mp = port as IMessagePort;
207        if (mp != null)
208          mp.MessageReceived -= MessagePort_MessageReceived;
209      }
210      #endregion
211    }
212    #endregion
213  }
214}
Note: See TracBrowser for help on using the repository browser.