Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Networks/3.3/Programmable/ProgrammableNode.cs @ 13077

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

#2205: worked on optimization networks

  • added first version of network visualization
  • updated frame files
File size: 8.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 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, IVisualizableNode {
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
61    public string VisualName { get { return Name; } }
62
63    [Storable]
64    private INodeVisualProperties visualProperties;
65    public INodeVisualProperties VisualProperties {
66      get {
67        if (visualProperties == null)
68          visualProperties = new NodeVisualProperties();
69        return visualProperties;
70      }
71    }
72    #endregion
73
74    protected override string CodeTemplate {
75      get { return CodeResources.ProgrammableNodeCode; }
76    }
77
78    new protected CompiledProgrammableNode CompiledNetworkItem {
79      get { return (CompiledProgrammableNode)base.CompiledNetworkItem; }
80    }
81
82    [StorableConstructor]
83    protected ProgrammableNode(bool deserializing) : base(deserializing) { }
84    protected ProgrammableNode(ProgrammableNode original, Cloner cloner)
85      : base(original, cloner) {
86      // ports are cloned in CompiledProgrammableNode
87      readOnlyPorts = null;
88    }
89    protected ProgrammableNode()
90      : base("ProgrammableNode") {
91      ports = new PortCollection();
92      readOnlyPorts = null;
93    }
94    protected ProgrammableNode(string name)
95      : base(name) {
96      ports = new PortCollection();
97      readOnlyPorts = null;
98    }
99    protected ProgrammableNode(string name, string description)
100      : base(name, description) {
101      ports = new PortCollection();
102      readOnlyPorts = null;
103    }
104
105    #region CompiledProgrammableNode
106    [Item("CompiledProgrammableNode", "Abstract base class for compiled programmable nodes of a network.")]
107    public abstract class CompiledProgrammableNode : CompiledProgrammableNetworkItem, INode {
108      public static new Image StaticItemImage {
109        get { return HeuristicLab.Common.Resources.VSImageLibrary.RadialChart; }
110      }
111
112      #region Node Members
113      new public INetwork Parent {
114        get { return (INetwork)base.Parent; }
115        set {
116          // NOTE: never call setter directly as the Parent property is set by the network which contains the node
117          base.Parent = value;
118        }
119      }
120      public override IEnumerable<INetworkItem> Children {
121        get { return base.Children.Concat(Ports.AsEnumerable<INetworkItem>()); }
122      }
123
124      public PortCollection Ports {
125        get { return Context.ports; }
126      }
127      IKeyedItemCollection<string, IPort> INode.Ports {
128        get { return Ports; }
129      }
130
131      public string VisualName { get { return Context.VisualName; } }
132
133      public INodeVisualProperties VisualProperties {
134        get { return Context.VisualProperties; }
135      }
136      #endregion
137
138      new protected ProgrammableNode Context {
139        get { return (ProgrammableNode)base.Context; }
140      }
141
142      protected CompiledProgrammableNode(CompiledProgrammableNode original, Cloner cloner)
143        : base(original, cloner) {
144        Context.ports = cloner.Clone(original.Context.ports);
145      }
146      protected CompiledProgrammableNode(ProgrammableNode context)
147        : base(context) {
148      }
149
150      public override void Initialize() {
151        base.Initialize();
152        Ports.Clear();
153      }
154
155      private void MessagePort_MessageReceived(object sender, EventArgs<IMessage, CancellationToken> e) {
156        ProcessMessage(e.Value, (IMessagePort)sender, e.Value2);
157      }
158      protected virtual void ProcessMessage(IMessage message, IMessagePort port, CancellationToken token) { }
159
160      #region Events
161      public override void RegisterEvents() {
162        base.RegisterEvents();
163        Ports.ItemsAdded += Ports_ItemsAdded;
164        Ports.ItemsRemoved += Ports_ItemsRemoved;
165        Ports.ItemsReplaced += Ports_ItemsReplaced;
166        Ports.CollectionReset += Ports_CollectionReset;
167        foreach (var p in Ports) {
168          p.Parent = this;
169          RegisterPortEvents(p);
170        }
171      }
172      public override void DeregisterEvents() {
173        foreach (var p in Ports) {
174          DeregisterPortEvents(p);
175          p.Parent = null;
176        }
177        Ports.ItemsAdded -= Ports_ItemsAdded;
178        Ports.ItemsRemoved -= Ports_ItemsRemoved;
179        Ports.ItemsReplaced -= Ports_ItemsReplaced;
180        Ports.CollectionReset -= Ports_CollectionReset;
181        base.DeregisterEvents();
182      }
183      protected virtual void Ports_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
184        foreach (var p in e.Items) {
185          p.Parent = this;
186          RegisterPortEvents(p);
187        }
188      }
189      protected virtual void Ports_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
190        foreach (var p in e.Items) {
191          p.Parent = null;
192          DeregisterPortEvents(p);
193        }
194      }
195      protected virtual void Ports_ItemsReplaced(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
196        foreach (var p in e.OldItems) {
197          p.Parent = null;
198          DeregisterPortEvents(p);
199        }
200        foreach (var p in e.Items) {
201          p.Parent = this;
202          RegisterPortEvents(p);
203        }
204      }
205      protected virtual void Ports_CollectionReset(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
206        foreach (var p in e.OldItems) {
207          p.Parent = null;
208          DeregisterPortEvents(p);
209        }
210        foreach (var p in e.Items) {
211          p.Parent = this;
212          RegisterPortEvents(p);
213        }
214      }
215      #endregion
216
217      #region Port Events
218      protected virtual void RegisterPortEvents(IPort port) {
219        var mp = port as IMessagePort;
220        if (mp != null)
221          mp.MessageReceived += MessagePort_MessageReceived;
222      }
223      protected virtual void DeregisterPortEvents(IPort port) {
224        var mp = port as IMessagePort;
225        if (mp != null)
226          mp.MessageReceived -= MessagePort_MessageReceived;
227      }
228      #endregion
229    }
230    #endregion
231  }
232}
Note: See TracBrowser for help on using the repository browser.